1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# |
3
|
|
|
# This file is part of Glances. |
4
|
|
|
# |
5
|
|
|
# Copyright (C) 2017 Nicolargo <[email protected]> |
6
|
|
|
# |
7
|
|
|
# Glances is free software; you can redistribute it and/or modify |
8
|
|
|
# it under the terms of the GNU Lesser General Public License as published by |
9
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
# (at your option) any later version. |
11
|
|
|
# |
12
|
|
|
# Glances is distributed in the hope that it will be useful, |
13
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
# GNU Lesser General Public License for more details. |
16
|
|
|
# |
17
|
|
|
# You should have received a copy of the GNU Lesser General Public License |
18
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
# |
20
|
|
|
|
21
|
|
|
"""Init the Glances software.""" |
22
|
|
|
|
23
|
|
|
# Import system libs |
24
|
|
|
import locale |
25
|
|
|
import platform |
26
|
|
|
import signal |
27
|
|
|
import sys |
28
|
|
|
|
29
|
|
|
# Global name |
30
|
|
|
__version__ = '2.8' |
31
|
|
|
__author__ = 'Nicolas Hennion <[email protected]>' |
32
|
|
|
__license__ = 'LGPL' |
33
|
|
|
|
34
|
|
|
# Import psutil |
35
|
|
|
try: |
36
|
|
|
from psutil import __version__ as psutil_version |
37
|
|
|
except ImportError: |
38
|
|
|
print('PSutil library not found. Glances cannot start.') |
39
|
|
|
sys.exit(1) |
40
|
|
|
|
41
|
|
|
# Import Glances libs |
42
|
|
|
# Note: others Glances libs will be imported optionally |
43
|
|
|
from glances.logger import logger |
44
|
|
|
from glances.main import GlancesMain |
45
|
|
|
from glances.globals import WINDOWS |
46
|
|
|
|
47
|
|
|
# Check locale |
48
|
|
|
try: |
49
|
|
|
locale.setlocale(locale.LC_ALL, '') |
50
|
|
|
except locale.Error: |
51
|
|
|
print("Warning: Unable to set locale. Expect encoding problems.") |
52
|
|
|
|
53
|
|
|
# Check Python version |
54
|
|
|
if sys.version_info < (2, 7) or (3, 0) <= sys.version_info < (3, 3): |
55
|
|
|
print('Glances requires at least Python 2.7 or 3.3 to run.') |
56
|
|
|
sys.exit(1) |
57
|
|
|
|
58
|
|
|
# Check PSutil version |
59
|
|
|
psutil_min_version = (2, 0, 0) |
60
|
|
|
psutil_version_info = tuple([int(num) for num in psutil_version.split('.')]) |
61
|
|
|
if psutil_version_info < psutil_min_version: |
62
|
|
|
print('PSutil 2.0 or higher is needed. Glances cannot start.') |
63
|
|
|
sys.exit(1) |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
def __signal_handler(signal, frame): |
67
|
|
|
"""Callback for CTRL-C.""" |
68
|
|
|
end() |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
def end(): |
72
|
|
|
"""Stop Glances.""" |
73
|
|
|
if core.is_standalone(): |
74
|
|
|
# Stop the standalone (CLI) |
75
|
|
|
standalone.end() |
76
|
|
|
logger.info("Stop Glances (with CTRL-C)") |
77
|
|
|
elif core.is_client(): |
78
|
|
|
# Stop the client |
79
|
|
|
client.end() |
80
|
|
|
logger.info("Stop Glances client (with CTRL-C)") |
81
|
|
|
elif core.is_server(): |
82
|
|
|
# Stop the server |
83
|
|
|
server.end() |
84
|
|
|
logger.info("Stop Glances server (with CTRL-C)") |
85
|
|
|
elif core.is_webserver(): |
86
|
|
|
# Stop the Web server |
87
|
|
|
webserver.end() |
88
|
|
|
logger.info("Stop Glances web server(with CTRL-C)") |
89
|
|
|
|
90
|
|
|
# The end... |
91
|
|
|
sys.exit(0) |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
def start_standalone(config, args): |
95
|
|
|
"""Start the standalone mode""" |
96
|
|
|
logger.info("Start standalone mode") |
97
|
|
|
|
98
|
|
|
# Share global var |
99
|
|
|
global standalone |
100
|
|
|
|
101
|
|
|
# Import the Glances standalone module |
102
|
|
|
from glances.standalone import GlancesStandalone |
103
|
|
|
|
104
|
|
|
# Init the standalone mode |
105
|
|
|
standalone = GlancesStandalone(config=config, args=args) |
106
|
|
|
|
107
|
|
|
# Start the standalone (CLI) loop |
108
|
|
|
standalone.serve_forever() |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
def start_clientbrowser(config, args): |
112
|
|
|
"""Start the browser client mode""" |
113
|
|
|
logger.info("Start client mode (browser)") |
114
|
|
|
|
115
|
|
|
# Share global var |
116
|
|
|
global client |
117
|
|
|
|
118
|
|
|
# Import the Glances client browser module |
119
|
|
|
from glances.client_browser import GlancesClientBrowser |
120
|
|
|
|
121
|
|
|
# Init the client |
122
|
|
|
client = GlancesClientBrowser(config=config, args=args) |
123
|
|
|
|
124
|
|
|
# Start the client loop |
125
|
|
|
client.serve_forever() |
126
|
|
|
|
127
|
|
|
# Shutdown the client |
128
|
|
|
client.end() |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
def start_client(config, args): |
132
|
|
|
"""Start the client mode""" |
133
|
|
|
logger.info("Start client mode") |
134
|
|
|
|
135
|
|
|
# Share global var |
136
|
|
|
global client |
137
|
|
|
|
138
|
|
|
# Import the Glances client browser module |
139
|
|
|
from glances.client import GlancesClient |
140
|
|
|
|
141
|
|
|
# Init the client |
142
|
|
|
client = GlancesClient(config=config, args=args) |
143
|
|
|
|
144
|
|
|
# Test if client and server are in the same major version |
145
|
|
|
if not client.login(): |
146
|
|
|
logger.critical("The server version is not compatible with the client") |
147
|
|
|
sys.exit(2) |
148
|
|
|
|
149
|
|
|
# Start the client loop |
150
|
|
|
client.serve_forever() |
151
|
|
|
|
152
|
|
|
# Shutdown the client |
153
|
|
|
client.end() |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
def start_server(config, args): |
157
|
|
|
"""Start the server mode""" |
158
|
|
|
logger.info("Start server mode") |
159
|
|
|
|
160
|
|
|
# Share global var |
161
|
|
|
global server |
162
|
|
|
|
163
|
|
|
# Import the Glances server module |
164
|
|
|
from glances.server import GlancesServer |
165
|
|
|
|
166
|
|
|
server = GlancesServer(cached_time=args.cached_time, |
167
|
|
|
config=config, |
168
|
|
|
args=args) |
169
|
|
|
print('Glances server is running on {}:{}'.format(args.bind_address, args.port)) |
170
|
|
|
|
171
|
|
|
# Set the server login/password (if -P/--password tag) |
172
|
|
|
if args.password != "": |
173
|
|
|
server.add_user(args.username, args.password) |
174
|
|
|
|
175
|
|
|
# Start the server loop |
176
|
|
|
server.serve_forever() |
177
|
|
|
|
178
|
|
|
# Shutdown the server? |
179
|
|
|
server.server_close() |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
def start_webserver(config, args): |
183
|
|
|
"""Start the Web server mode""" |
184
|
|
|
logger.info("Start web server mode") |
185
|
|
|
|
186
|
|
|
# Share global var |
187
|
|
|
global webserver |
188
|
|
|
|
189
|
|
|
# Import the Glances web server module |
190
|
|
|
from glances.webserver import GlancesWebServer |
191
|
|
|
|
192
|
|
|
# Init the web server mode |
193
|
|
|
webserver = GlancesWebServer(config=config, args=args) |
194
|
|
|
|
195
|
|
|
# Start the web server loop |
196
|
|
|
webserver.serve_forever() |
197
|
|
|
|
198
|
|
|
|
199
|
|
|
def main(): |
200
|
|
|
"""Main entry point for Glances. |
201
|
|
|
|
202
|
|
|
Select the mode (standalone, client or server) |
203
|
|
|
Run it... |
204
|
|
|
""" |
205
|
|
|
# Log Glances and PSutil version |
206
|
|
|
logger.info('Start Glances {}'.format(__version__)) |
207
|
|
|
logger.info('{} {} and PSutil {} detected'.format( |
208
|
|
|
platform.python_implementation(), |
209
|
|
|
platform.python_version(), |
210
|
|
|
psutil_version)) |
211
|
|
|
|
212
|
|
|
# Share global var |
213
|
|
|
global core |
214
|
|
|
|
215
|
|
|
# Create the Glances main instance |
216
|
|
|
core = GlancesMain() |
217
|
|
|
config = core.get_config() |
218
|
|
|
args = core.get_args() |
219
|
|
|
|
220
|
|
|
# Catch the CTRL-C signal |
221
|
|
|
signal.signal(signal.SIGINT, __signal_handler) |
222
|
|
|
|
223
|
|
|
# Glances can be ran in standalone, client or server mode |
224
|
|
|
if core.is_standalone() and not WINDOWS: |
225
|
|
|
start_standalone(config=config, args=args) |
226
|
|
|
elif core.is_client() and not WINDOWS: |
227
|
|
|
if core.is_client_browser(): |
228
|
|
|
start_clientbrowser(config=config, args=args) |
229
|
|
|
else: |
230
|
|
|
start_client(config=config, args=args) |
231
|
|
|
elif core.is_server(): |
232
|
|
|
start_server(config=config, args=args) |
233
|
|
|
elif core.is_webserver() or (core.is_standalone() and WINDOWS): |
234
|
|
|
# Web server mode replace the standalone mode on Windows OS |
235
|
|
|
# In this case, try to start the web browser mode automaticaly |
236
|
|
|
if core.is_standalone() and WINDOWS: |
237
|
|
|
args.open_web_browser = True |
238
|
|
|
start_webserver(config=config, args=args) |
239
|
|
|
|