glances.main.GlancesMain.get_config()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
#
2
# This file is part of Glances.
3
#
4
# SPDX-FileCopyrightText: 2024 Nicolas Hennion <[email protected]>
5
#
6
# SPDX-License-Identifier: LGPL-3.0-only
7
#
8
9
"""Glances main class."""
10
11
import argparse
12
import sys
13
import tempfile
14
from logging import DEBUG
15
from warnings import simplefilter
16
17
from glances import __apiversion__, __version__, psutil_version
18
from glances.config import Config
19
from glances.globals import WINDOWS, disable, enable
20
from glances.logger import LOG_FILENAME, logger
21
from glances.processes import sort_processes_key_list
22
23
24
class GlancesMain:
25
    """Main class to manage Glances instance."""
26
27
    # Default stats' minimum refresh time is 2 seconds
28
    DEFAULT_REFRESH_TIME = 2
29
    # Set the default cache lifetime to 1 second (only for server)
30
    cached_time = 1
31
    # By default, Glances is ran in standalone mode (no client/server)
32
    client_tag = False
33
    # Server TCP port number (default is 61209)
34
    server_port = 61209
35
    # Web Server TCP port number (default is 61208)
36
    web_server_port = 61208
37
    # Default username/password for client/server mode
38
    username = "glances"
39
    password = ""
40
41
    # Examples of use
42
    example_of_use = """
43
Examples of use:
44
  Monitor local machine (standalone mode):
45
    $ glances
46
47
  Display all Glances modules (plugins and exporters) and exit:
48
    $ glances --module-list
49
50
  Monitor local machine with the Web interface and start RESTful server:
51
    $ glances -w
52
    Glances web server started on http://0.0.0.0:61208/
53
54
  Only start RESTful API (without the WebUI):
55
    $ glances -w --disable-webui
56
    Glances API available on http://0.0.0.0:61208/api/
57
58
  Monitor local machine and export stats to a CSV file (standalone mode):
59
    $ glances --export csv --export-csv-file /tmp/glances.csv
60
61
  Monitor local machine and export stats to a InfluxDB server with 5s refresh rate (standalone mode):
62
    $ glances -t 5 --export influxdb
63
64
  Start a Glances XML-RPC server (server mode):
65
    $ glances -s
66
67
  Connect Glances to a Glances XML-RPC server (client mode):
68
    $ glances -c <ip_server>
69
70
  Connect Glances to a Glances server and export stats to a StatsD server (client mode):
71
    $ glances -c <ip_server> --export statsd
72
73
  Start the client browser (browser mode):
74
    $ glances --browser
75
76
  Display stats to stdout (one stat per line, possible to go inside stats using plugin.attribute):
77
    $ glances --stdout now,cpu.user,mem.used,load
78
79
  Display JSON stats to stdout (one stats per line):
80
    $ glances --stdout-json now,cpu,mem,load
81
82
  Display CSV stats to stdout (all stats in one line):
83
    $ glances --stdout-csv now,cpu.user,mem.used,load
84
85
  Enable some plugins disabled by default (comma-separated list):
86
    $ glances --enable-plugin sensors
87
88
  Disable some plugins (comma-separated list):
89
    $ glances --disable-plugin network,ports
90
91
  Disable all plugins except some (comma-separated list):
92
    $ glances --disable-plugin all --enable-plugin cpu,mem,load
93
94
"""
95
96
    def __init__(self):
97
        """Manage the command line arguments."""
98
        # Read the command line arguments
99
        self.args = self.parse_args()
100
101
    def version_msg(self):
102
        """Return the version message."""
103
        version = f'Glances version:\t{__version__}\n'
104
        version += f'Glances API version:\t{__apiversion__}\n'
105
        version += f'PsUtil version:\t\t{psutil_version}\n'
106
        version += f'Log file:\t\t{LOG_FILENAME}\n'
107
        return version
108
109
    def init_args(self):
110
        """Init all the command line arguments."""
111
        parser = argparse.ArgumentParser(
112
            prog='glances',
113
            conflict_handler='resolve',
114
            formatter_class=argparse.RawDescriptionHelpFormatter,
115
            epilog=self.example_of_use,
116
        )
117
        parser.add_argument('-V', '--version', action='version', version=self.version_msg())
118
        parser.add_argument('-d', '--debug', action='store_true', default=False, dest='debug', help='enable debug mode')
119
        parser.add_argument('-C', '--config', dest='conf_file', help='path to the configuration file')
120
        parser.add_argument('-P', '--plugins', dest='plugin_dir', help='path to additional plugin directory')
121
        # Disable plugin
122
        parser.add_argument(
123
            '--modules-list',
124
            '--module-list',
125
            action='store_true',
126
            default=False,
127
            dest='modules_list',
128
            help='display modules (plugins & exports) list and exit',
129
        )
130
        parser.add_argument(
131
            '--disable-plugin',
132
            '--disable-plugins',
133
            '--disable',
134
            dest='disable_plugin',
135
            help='disable plugin (comma-separated list or all). If all is used, \
136
                then you need to configure --enable-plugin.',
137
        )
138
        parser.add_argument(
139
            '--enable-plugin',
140
            '--enable-plugins',
141
            '--enable',
142
            dest='enable_plugin',
143
            help='enable plugin (comma-separated list)',
144
        )
145
        parser.add_argument(
146
            '--disable-process',
147
            action='store_true',
148
            default=False,
149
            dest='disable_process',
150
            help='disable process module',
151
        )
152
        # Enable or disable option
153
        parser.add_argument(
154
            '--disable-webui',
155
            action='store_true',
156
            default=False,
157
            dest='disable_webui',
158
            help='disable the Web Interface',
159
        )
160
        parser.add_argument(
161
            '--light',
162
            '--enable-light',
163
            action='store_true',
164
            default=False,
165
            dest='enable_light',
166
            help='light mode for Curses UI (disable all but the top menu)',
167
        )
168
        parser.add_argument(
169
            '-0',
170
            '--disable-irix',
171
            action='store_true',
172
            default=False,
173
            dest='disable_irix',
174
            help='task\'s cpu usage will be divided by the total number of CPUs',
175
        )
176
        parser.add_argument(
177
            '-1',
178
            '--percpu',
179
            '--per-cpu',
180
            action='store_true',
181
            default=False,
182
            dest='percpu',
183
            help='start Glances in per CPU mode',
184
        )
185
        parser.add_argument(
186
            '-2',
187
            '--disable-left-sidebar',
188
            action='store_true',
189
            default=False,
190
            dest='disable_left_sidebar',
191
            help='disable network, disk I/O, FS and sensors modules',
192
        )
193
        parser.add_argument(
194
            '-3',
195
            '--disable-quicklook',
196
            action='store_true',
197
            default=False,
198
            dest='disable_quicklook',
199
            help='disable quick look module',
200
        )
201
        parser.add_argument(
202
            '-4',
203
            '--full-quicklook',
204
            action='store_true',
205
            default=False,
206
            dest='full_quicklook',
207
            help='disable all but quick look and load',
208
        )
209
        parser.add_argument(
210
            '-5',
211
            '--disable-top',
212
            action='store_true',
213
            default=False,
214
            dest='disable_top',
215
            help='disable top menu (QL, CPU, MEM, SWAP and LOAD)',
216
        )
217
        parser.add_argument(
218
            '-6', '--meangpu', action='store_true', default=False, dest='meangpu', help='start Glances in mean GPU mode'
219
        )
220
        parser.add_argument(
221
            '--disable-history',
222
            action='store_true',
223
            default=False,
224
            dest='disable_history',
225
            help='disable stats history',
226
        )
227
        parser.add_argument(
228
            '--disable-bold',
229
            action='store_true',
230
            default=False,
231
            dest='disable_bold',
232
            help='disable bold mode in the terminal',
233
        )
234
        parser.add_argument(
235
            '--disable-bg',
236
            action='store_true',
237
            default=False,
238
            dest='disable_bg',
239
            help='disable background colors in the terminal',
240
        )
241
        parser.add_argument(
242
            '--enable-irq', action='store_true', default=False, dest='enable_irq', help='enable IRQ module'
243
        )
244
        parser.add_argument(
245
            '--enable-process-extended',
246
            action='store_true',
247
            default=False,
248
            dest='enable_process_extended',
249
            help='enable extended stats on top process',
250
        )
251
        parser.add_argument(
252
            '--disable-separator',
253
            action='store_false',
254
            default=True,
255
            dest='enable_separator',
256
            help='disable separator in the UI (between top and others modules)',
257
        )
258
        parser.add_argument(
259
            '--disable-cursor',
260
            action='store_true',
261
            default=False,
262
            dest='disable_cursor',
263
            help='disable cursor (process selection) in the UI',
264
        )
265
        # Sort processes list
266
        parser.add_argument(
267
            '--sort-processes',
268
            dest='sort_processes_key',
269
            choices=sort_processes_key_list,
270
            help='Sort processes by: {}'.format(', '.join(sort_processes_key_list)),
271
        )
272
        # Display processes list by program name and not by thread
273
        parser.add_argument(
274
            '--programs',
275
            '--program',
276
            action='store_true',
277
            default=False,
278
            dest='programs',
279
            help='Accumulate processes by program',
280
        )
281
        # Export modules feature
282
        parser.add_argument('--export', dest='export', help='enable export module (comma-separated list)')
283
        parser.add_argument(
284
            '--export-csv-file', default='./glances.csv', dest='export_csv_file', help='file path for CSV exporter'
285
        )
286
        parser.add_argument(
287
            '--export-csv-overwrite',
288
            action='store_true',
289
            default=False,
290
            dest='export_csv_overwrite',
291
            help='overwrite existing CSV file',
292
        )
293
        parser.add_argument(
294
            '--export-json-file', default='./glances.json', dest='export_json_file', help='file path for JSON exporter'
295
        )
296
        parser.add_argument(
297
            '--export-graph-path',
298
            default=tempfile.gettempdir(),
299
            dest='export_graph_path',
300
            help='Folder for Graph exporter',
301
        )
302
        parser.add_argument(
303
            '--export-process-filter',
304
            default=None,
305
            type=str,
306
            dest='export_process_filter',
307
            help='set the export process filter (comman separated list of regular expression)',
308
        )
309
        # Client/Server option
310
        parser.add_argument(
311
            '-c', '--client', dest='client', help='connect to a Glances server by IPv4/IPv6 address or hostname'
312
        )
313
        parser.add_argument(
314
            '-s', '--server', action='store_true', default=False, dest='server', help='run Glances in server mode'
315
        )
316
        parser.add_argument(
317
            '--browser',
318
            action='store_true',
319
            default=False,
320
            dest='browser',
321
            help='start the client browser (list of servers)',
322
        )
323
        parser.add_argument(
324
            '--disable-autodiscover',
325
            action='store_true',
326
            default=False,
327
            dest='disable_autodiscover',
328
            help='disable autodiscover feature',
329
        )
330
        parser.add_argument(
331
            '-p',
332
            '--port',
333
            default=None,
334
            type=int,
335
            dest='port',
336
            help=f'define the client/server TCP port [default: {self.server_port}]',
337
        )
338
        parser.add_argument(
339
            '-B',
340
            '--bind',
341
            default='0.0.0.0',
342
            dest='bind_address',
343
            help='bind server to the given IPv4/IPv6 address or hostname',
344
        )
345
        parser.add_argument(
346
            '--username',
347
            action='store_true',
348
            default=False,
349
            dest='username_prompt',
350
            help='define a client/server username',
351
        )
352
        parser.add_argument(
353
            '--password',
354
            action='store_true',
355
            default=False,
356
            dest='password_prompt',
357
            help='define a client/server password',
358
        )
359
        parser.add_argument('-u', dest='username_used', help='use the given client/server username')
360
        parser.add_argument('--snmp-community', default='public', dest='snmp_community', help='SNMP community')
361
        parser.add_argument('--snmp-port', default=161, type=int, dest='snmp_port', help='SNMP port')
362
        parser.add_argument('--snmp-version', default='2c', dest='snmp_version', help='SNMP version (1, 2c or 3)')
363
        parser.add_argument('--snmp-user', default='private', dest='snmp_user', help='SNMP username (only for SNMPv3)')
364
        parser.add_argument(
365
            '--snmp-auth', default='password', dest='snmp_auth', help='SNMP authentication key (only for SNMPv3)'
366
        )
367
        parser.add_argument(
368
            '--snmp-force', action='store_true', default=False, dest='snmp_force', help='force SNMP mode'
369
        )
370
        parser.add_argument(
371
            '-t',
372
            '--time',
373
            default=self.DEFAULT_REFRESH_TIME,
374
            type=float,
375
            dest='time',
376
            help=f'set minimum refresh rate in seconds [default: {self.DEFAULT_REFRESH_TIME} sec]',
377
        )
378
        parser.add_argument(
379
            '-w',
380
            '--webserver',
381
            action='store_true',
382
            default=False,
383
            dest='webserver',
384
            help='run Glances in web server mode (FastAPI, Uvicorn, Jinja2 libs needed)',
385
        )
386
        parser.add_argument(
387
            '--cached-time',
388
            default=self.cached_time,
389
            type=int,
390
            dest='cached_time',
391
            help=f'set the server cache time [default: {self.cached_time} sec]',
392
        )
393
        parser.add_argument(
394
            '--stop-after',
395
            default=None,
396
            type=int,
397
            dest='stop_after',
398
            help='stop Glances after n refresh',
399
        )
400
        parser.add_argument(
401
            '--open-web-browser',
402
            action='store_true',
403
            default=False,
404
            dest='open_web_browser',
405
            help='try to open the Web UI in the default Web browser',
406
        )
407
        # Display options
408
        parser.add_argument(
409
            '-q',
410
            '--quiet',
411
            default=False,
412
            action='store_true',
413
            dest='quiet',
414
            help='do not display the curses interface',
415
        )
416
        parser.add_argument(
417
            '-f',
418
            '--process-filter',
419
            default=None,
420
            type=str,
421
            dest='process_filter',
422
            help='set the process filter pattern (regular expression)',
423
        )
424
        parser.add_argument(
425
            '--process-short-name',
426
            action='store_true',
427
            default=True,
428
            dest='process_short_name',
429
            help='force short name for processes name',
430
        )
431
        parser.add_argument(
432
            '--process-long-name',
433
            action='store_false',
434
            default=False,
435
            dest='process_short_name',
436
            help='force long name for processes name',
437
        )
438
        parser.add_argument(
439
            '--stdout',
440
            default=None,
441
            dest='stdout',
442
            help='display stats to stdout, one stat per line (comma-separated list of plugins/plugins.attribute)',
443
        )
444
        parser.add_argument(
445
            '--stdout-json',
446
            default=None,
447
            dest='stdout_json',
448
            help='display stats to stdout, JSON format (comma-separated list of plugins/plugins.attribute)',
449
        )
450
        parser.add_argument(
451
            '--stdout-csv',
452
            default=None,
453
            dest='stdout_csv',
454
            help='display stats to stdout, CSV format (comma-separated list of plugins/plugins.attribute)',
455
        )
456
        parser.add_argument(
457
            '--issue',
458
            default=None,
459
            action='store_true',
460
            dest='stdout_issue',
461
            help='test all plugins and exit (please copy/paste the output if you open an issue)',
462
        )
463
        parser.add_argument(
464
            '--trace-malloc',
465
            default=False,
466
            action='store_true',
467
            dest='trace_malloc',
468
            help='trace memory allocation and display it at the end of the process (python 3.4 or higher needed)',
469
        )
470
        parser.add_argument(
471
            '--memory-leak',
472
            default=False,
473
            action='store_true',
474
            dest='memory_leak',
475
            help='test memory leak (python 3.4 or higher needed)',
476
        )
477
        parser.add_argument(
478
            '--api-doc', default=None, action='store_true', dest='stdout_apidoc', help='display fields descriptions'
479
        )
480
        if not WINDOWS:
481
            parser.add_argument(
482
                '--hide-kernel-threads',
483
                action='store_true',
484
                default=False,
485
                dest='no_kernel_threads',
486
                help='hide kernel threads in the process list (not available on Windows)',
487
            )
488
        parser.add_argument(
489
            '-b',
490
            '--byte',
491
            action='store_true',
492
            default=False,
493
            dest='byte',
494
            help='display network rate in bytes per second',
495
        )
496
        parser.add_argument(
497
            '--diskio-show-ramfs',
498
            action='store_true',
499
            default=False,
500
            dest='diskio_show_ramfs',
501
            help='show RAM Fs in the DiskIO plugin',
502
        )
503
        parser.add_argument(
504
            '--diskio-iops',
505
            action='store_true',
506
            default=False,
507
            dest='diskio_iops',
508
            help='show IO per second in the DiskIO plugin',
509
        )
510
        parser.add_argument(
511
            '--fahrenheit',
512
            action='store_true',
513
            default=False,
514
            dest='fahrenheit',
515
            help='display temperature in Fahrenheit (default is Celsius)',
516
        )
517
        parser.add_argument(
518
            '--fs-free-space',
519
            action='store_true',
520
            default=False,
521
            dest='fs_free_space',
522
            help='display FS free space instead of used',
523
        )
524
        parser.add_argument(
525
            '--sparkline',
526
            action='store_true',
527
            default=False,
528
            dest='sparkline',
529
            help='display sparklines instead of bar in the curses interface',
530
        )
531
        parser.add_argument(
532
            '--disable-unicode',
533
            action='store_true',
534
            default=False,
535
            dest='disable_unicode',
536
            help='disable unicode characters in the curses interface',
537
        )
538
        parser.add_argument(
539
            '--hide-public-info',
540
            action='store_true',
541
            default=False,
542
            help='hide public information (like public IP)',
543
        )
544
        # Globals options
545
        parser.add_argument(
546
            '--disable-check-update',
547
            action='store_true',
548
            default=False,
549
            dest='disable_check_update',
550
            help='disable online Glances version ckeck',
551
        )
552
        parser.add_argument(
553
            '--strftime',
554
            dest='strftime_format',
555
            default='',
556
            help='strftime format string for displaying current date in standalone mode',
557
        )
558
559
        return parser
560
561
    def init_debug(self, args):
562
        """Init Glances debug mode."""
563
        if args.debug:
564
            logger.setLevel(DEBUG)
565
        else:
566
            simplefilter("ignore")
567
568
    def init_refresh_rate(self, args):
569
        """Init Glances refresh rate"""
570
        if self.config.has_section('global'):
571
            global_refresh = self.config.get_float_value('global', 'refresh', default=self.DEFAULT_REFRESH_TIME)
572
        else:
573
            global_refresh = self.DEFAULT_REFRESH_TIME
574
575
        # The configuration key can be overwrite from the command line (-t <time>)
576
        if args.time == self.DEFAULT_REFRESH_TIME:
577
            args.time = global_refresh
578
579
        logger.debug(f'Global refresh rate is set to {args.time} seconds')
580
581
    def init_plugins(self, args):
582
        """Init Glances plugins"""
583
        # Allow users to disable plugins from the glances.conf (issue #1378)
584
        for s in self.config.sections():
585
            if self.config.has_section(s) and (self.config.get_bool_value(s, 'disable', False)):
586
                disable(args, s)
587
                logger.debug(f'{s} disabled by the configuration file')
588
        # The configuration key can be overwrite from the command line
589
        if args and args.disable_plugin and 'all' in args.disable_plugin.split(','):
590
            if not args.enable_plugin:
591
                logger.critical("'all' key in --disable-plugin needs to be used with --enable-plugin")
592
                sys.exit(2)
593
            else:
594
                logger.info(
595
                    "'all' key in --disable-plugin, only plugins defined with --enable-plugin will be available"
596
                )
597
        if args.disable_plugin is not None:
598
            for p in args.disable_plugin.split(','):
599
                disable(args, p)
600
        if args.enable_plugin is not None:
601
            for p in args.enable_plugin.split(','):
602
                enable(args, p)
603
604
        # Exporters activation
605
        if args.export is not None:
606
            for p in args.export.split(','):
607
                setattr(args, 'export_' + p, True)
608
609
        # By default help is hidden
610
        args.help_tag = False
611
612
        # Display Rx and Tx, not the sum for the network
613
        args.network_sum = False
614
        args.network_cumul = False
615
616
        # Processlist id updated in processcount
617
        if getattr(args, 'enable_processlist', False):
618
            enable(args, 'processcount')
619
620
        # Set a default export_process_filter (with all process) when using the stdout mode
621
        if getattr(args, 'stdout', True) and args.process_filter is None:
622
            setattr(args, 'export_process_filter', '.*')
623
624
    def init_client_server(self, args):
625
        """Init Glances client/server mode."""
626
627
        # Client/server Port
628
        if args.port is None:
629
            if args.webserver:
630
                args.port = self.web_server_port
631
            else:
632
                args.port = self.server_port
633
        # Port in the -c URI #996
634
        if args.client is not None:
635
            args.client, args.port = (
636
                x if x else y for (x, y) in zip(args.client.partition(':')[::2], (args.client, args.port))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable y does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable x does not seem to be defined.
Loading history...
637
            )
638
639
        # Client autodiscover mode
640
        if args.disable_autodiscover:
641
            logger.info("Auto discover mode is disabled")
642
643
        # Server or client login/password
644
        if args.username_prompt:
645
            # Every username needs a password
646
            args.password_prompt = True
647
            # Prompt username
648
            if args.server:
649
                args.username = self.__get_username(description='Define the Glances server username: ')
650
            elif args.webserver:
651
                args.username = self.__get_username(description='Define the Glances webserver username: ')
652
            elif args.client:
653
                args.username = self.__get_username(description='Enter the Glances server username: ')
654
        else:
655
            if args.username_used:
656
                # A username has been set using the -u option ?
657
                args.username = args.username_used
658
            else:
659
                # Default user name is 'glances'
660
                args.username = self.username
661
662
        if args.password_prompt or args.username_used:
663
            # Interactive or file password
664
            if args.server:
665
                args.password = self.__get_password(
666
                    description=f'Define the Glances server password ({args.username} username): ',
667
                    confirm=True,
668
                    username=args.username,
669
                )
670
            elif args.webserver:
671
                args.password = self.__get_password(
672
                    description=f'Define the Glances webserver password ({args.username} username): ',
673
                    confirm=True,
674
                    username=args.username,
675
                )
676
            elif args.client:
677
                args.password = self.__get_password(
678
                    description=f'Enter the Glances server password ({args.username} username): ',
679
                    clear=True,
680
                    username=args.username,
681
                )
682
        else:
683
            # Default is no password
684
            args.password = self.password
685
686
    def init_ui_mode(self, args):
687
        # Manage light mode
688
        if getattr(args, 'enable_light', False):
689
            logger.info("Light mode is on")
690
            args.disable_left_sidebar = True
691
            disable(args, 'process')
692
            disable(args, 'alert')
693
            disable(args, 'amps')
694
            disable(args, 'containers')
695
696
        # Manage full quicklook option
697
        if getattr(args, 'full_quicklook', False):
698
            logger.info("Full quicklook mode")
699
            enable(args, 'quicklook')
700
            disable(args, 'cpu')
701
            disable(args, 'mem')
702
            disable(args, 'memswap')
703
            enable(args, 'load')
704
705
        # Manage disable_top option
706
        if getattr(args, 'disable_top', False):
707
            logger.info("Disable top menu")
708
            disable(args, 'quicklook')
709
            disable(args, 'cpu')
710
            disable(args, 'mem')
711
            disable(args, 'memswap')
712
            disable(args, 'load')
713
714
        # Memory leak
715
        if getattr(args, 'memory_leak', False):
716
            logger.info('Memory leak detection enabled')
717
            args.quiet = True
718
            if not args.stop_after:
719
                args.stop_after = 60
720
            args.time = 1
721
            args.disable_history = True
722
723
    def parse_args(self):
724
        """Parse command line arguments."""
725
        args = self.init_args().parse_args()
726
727
        # Load the configuration file, if it exists
728
        # This function should be called after the parse_args
729
        # because the configuration file path can be defined
730
        self.config = Config(args.conf_file)
731
732
        # Init Glances debug mode
733
        self.init_debug(args)
734
735
        # Plugins Glances refresh rate
736
        self.init_refresh_rate(args)
737
738
        # Manage Plugins disable/enable option
739
        self.init_plugins(args)
740
741
        # Init Glances client/server mode
742
        self.init_client_server(args)
743
744
        # Init UI mode
745
        self.init_ui_mode(args)
746
747
        # Init the generate_graph tag
748
        # Should be set to True to generate graphs
749
        args.generate_graph = False
750
751
        # Control parameter and exit if it is not OK
752
        self.args = args
753
754
        # Export is only available in standalone or client mode (issue #614)
755
        export_tag = self.args.export is not None and any(self.args.export)
756
        if WINDOWS and export_tag:
757
            # On Windows, export is possible but only in quiet mode
758
            # See issue #1038
759
            logger.info("On Windows OS, export disable the Web interface")
760
            self.args.quiet = True
761
            self.args.webserver = False
762
        elif not (self.is_standalone() or self.is_client()) and export_tag:
763
            logger.critical("Export is only available in standalone or client mode")
764
            sys.exit(2)
765
766
        # Filter is only available in standalone mode
767
        if not args.process_filter and not self.is_standalone():
768
            logger.debug("Process filter is only available in standalone mode")
769
770
        # Cursor option is only available in standalone mode
771
        if not args.disable_cursor and not self.is_standalone():
772
            logger.debug("Cursor is only available in standalone mode")
773
774
        # Let the plugins known the Glances mode
775
        self.args.is_standalone = self.is_standalone()
776
        self.args.is_client = self.is_client()
777
        self.args.is_client_browser = self.is_client_browser()
778
        self.args.is_server = self.is_server()
779
        self.args.is_webserver = self.is_webserver()
780
781
        # Check mode compatibility
782
        self.check_mode_compatibility()
783
784
        return args
785
786
    def check_mode_compatibility(self):
787
        """Check mode compatibility"""
788
        # Server and Web server are not compatible
789
        if self.args.is_server and self.args.is_webserver:
790
            logger.critical("Server and Web server modes should not be used together")
791
            sys.exit(2)
792
793
        # Trace malloc option
794
        if getattr(self.args, 'trace_malloc', True) and not self.is_standalone():
795
            logger.critical("Option --trace-malloc is only available in the terminal mode")
796
            sys.exit(2)
797
798
        # Memory leak option
799
        if getattr(self.args, 'memory_leak', True) and not self.is_standalone():
800
            logger.critical("Option --memory-leak is only available in the terminal mode")
801
            sys.exit(2)
802
803
    def is_standalone(self):
804
        """Return True if Glances is running in standalone mode."""
805
        return not self.args.client and not self.args.browser and not self.args.server and not self.args.webserver
806
807
    def is_client(self):
808
        """Return True if Glances is running in client mode."""
809
        return (self.args.client or self.args.browser) and not self.args.server
810
811
    def is_client_browser(self):
812
        """Return True if Glances is running in client browser mode."""
813
        return self.args.browser and not self.args.server
814
815
    def is_server(self):
816
        """Return True if Glances is running in server mode."""
817
        return not self.args.client and self.args.server
818
819
    def is_webserver(self):
820
        """Return True if Glances is running in Web server mode."""
821
        return not self.args.client and self.args.webserver
822
823
    def get_config(self):
824
        """Return configuration file object."""
825
        return self.config
826
827
    def get_args(self):
828
        """Return the arguments."""
829
        return self.args
830
831
    def get_mode(self):
832
        """Return the mode."""
833
        return self.mode
834
835
    def __get_username(self, description=''):
836
        """Read an username from the command line."""
837
        return input(description)
838
839
    def __get_password(self, description='', confirm=False, clear=False, username='glances'):
840
        """Read a password from the command line.
841
842
        - if confirm = True, with confirmation
843
        - if clear = True, plain (clear password)
844
        """
845
        from glances.password import GlancesPassword
846
847
        password = GlancesPassword(username=username, config=self.get_config())
848
        return password.get_password(description, confirm, clear)
849