summaryrefslogtreecommitdiffstats
path: root/glances/main.py
blob: 61a1273ff67a3d94c210fb33a9584656b10e2c61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
# -*- coding: utf-8 -*-
#
# This file is part of Glances.
#
# SPDX-FileCopyrightText: 2023 Nicolas Hennion <nicolas@nicolargo.com>
#
# SPDX-License-Identifier: LGPL-3.0-only
#

"""Glances main class."""

import argparse
import sys
import tempfile
from logging import DEBUG
from warnings import simplefilter

from glances import __version__, psutil_version, __apiversion__
from glances.globals import WINDOWS, disable, enable
from glances.config import Config
from glances.processes import sort_processes_key_list
from glances.logger import logger, LOG_FILENAME


class GlancesMain(object):
    """Main class to manage Glances instance."""

    # Default stats' minimum refresh time is 2 seconds
    DEFAULT_REFRESH_TIME = 2
    # Set the default cache lifetime to 1 second (only for server)
    cached_time = 1
    # By default, Glances is ran in standalone mode (no client/server)
    client_tag = False
    # Server TCP port number (default is 61209)
    server_port = 61209
    # Web Server TCP port number (default is 61208)
    web_server_port = 61208
    # Default username/password for client/server mode
    username = "glances"
    password = ""

    # Examples of use
    example_of_use = """
Examples of use:
  Monitor local machine (standalone mode):
    $ glances

  Display all Glances modules (plugins and exporters) and exit:
    $ glances --module-list

  Monitor local machine with the Web interface and start RESTful server:
    $ glances -w
    Glances web server started on http://0.0.0.0:61208/

  Only start RESTful API (without the WebUI):
    $ glances -w --disable-webui
    Glances API available on http://0.0.0.0:61208/api/

  Monitor local machine and export stats to a CSV file (standalone mode):
    $ glances --export csv --export-csv-file /tmp/glances.csv

  Monitor local machine and export stats to a InfluxDB server with 5s refresh rate (standalone mode):
    $ glances -t 5 --export influxdb

  Start a Glances XML-RPC server (server mode):
    $ glances -s

  Connect Glances to a Glances XML-RPC server (client mode):
    $ glances -c <ip_server>

  Connect Glances to a Glances server and export stats to a StatsD server (client mode):
    $ glances -c <ip_server> --export statsd

  Start the client browser (browser mode):
    $ glances --browser

  Display stats to stdout (one stat per line, possible to go inside stats using plugin.attribute):
    $ glances --stdout now,cpu.user,mem.used,load

  Display JSON stats to stdout (one stats per line):
    $ glances --stdout-json now,cpu,mem,load

  Display CSV stats to stdout (all stats in one line):
    $ glances --stdout-csv now,cpu.user,mem.used,load

  Enable some plugins disabled by default (comma-separated list):
    $ glances --enable-plugin sensors

  Disable some plugins (comma-separated list):
    $ glances --disable-plugin network,ports

  Disable all plugins except some (comma-separated list):
    $ glances --disable-plugin all --enable-plugin cpu,mem,load

"""

    def __init__(self):
        """Manage the command line arguments."""
        # Read the command line arguments
        self.args = self.parse_args()

    def version_msg(self):
        """Return the version message."""
        version = 'Glances version:\t{}\n'.format(__version__)
        version += 'Glances API version:\t{}\n'.format(__apiversion__)
        version += 'PsUtil version:\t\t{}\n'.format(psutil_version)
        version += 'Log file:\t\t{}\n'.format(LOG_FILENAME)
        return version

    def init_args(self):
        """Init all the command line arguments."""
        parser = argparse.ArgumentParser(
            prog='glances',
            conflict_handler='resolve',
            formatter_class=argparse.RawDescriptionHelpFormatter,
            epilog=self.example_of_use,
        )
        parser.add_argument('-V', '--version', action='version', version=self.version_msg())
        parser.add_argument('-d', '--debug', action='store_true', default=False, dest='debug', help='enable debug mode')
        parser.add_argument('-C', '--config', dest='conf_file', help='path to the configuration file')
        parser.add_argument('-P', '--plugins', dest='plugin_dir', help='path to additional plugin directory')
        # Disable plugin
        parser.add_argument(
            '--modules-list',
            '--module-list',
            action='store_true',
            default=False,
            dest='modules_list',
            help='display modules (plugins & exports) list and exit',
        )
        parser.add_argument(
            '--disable-plugin',
            '--disable-plugins',
            '--disable',
            dest='disable_plugin',
            help='disable plugin (comma-separated list or all). If all is used, \
                then you need to configure --enable-plugin.',
        )
        parser.add_argument(
            '--enable-plugin',
            '--enable-plugins',
            '--enable',
            dest='enable_plugin',
            help='enable plugin (comma-separated list)',
        )
        parser.add_argument(
            '--disable-process',
            action='store_true',
            default=False,
            dest='disable_process',
            help='disable process module',
        )
        # Enable or disable option
        parser.add_argument(
            '--disable-webui',
            action='store_true',
            default=False,
            dest='disable_webui',
            help='disable the Web Interface',
        )
        parser.add_argument(
            '--light',
            '--enable-light',
            action='store_true',
            default=False,
            dest='enable_light',
            help='light mode for Curses UI (disable all but the top menu)',
        )
        parser.add_argument(
            '-0',
            '--disable-irix',
            action='store_true',
            default=False,
            dest='disable_irix',
            help='task\'s cpu usage will be divided by the total number of CPUs',
        )
        parser.add_argument(
            '-1', '--percpu', action='store_true', default=False, dest='percpu', help='start Glances in per CPU mode'
        )
        parser.add_argument(
            '-2',
            '--disable-left-sidebar',
            action='store_true',
            default=False,
            dest='disable_left_sidebar',
            help='disable network, disk I/O, FS and sensors modules',
        )
        parser.add_argument(
            '-3',
            '--disable-quicklook',
            action='store_true',
            default=False,
            dest='disable_quicklook',
            help='disable quick look module',
        )
        parser.add_argument(
            '-4',
            '--full-quicklook',
            action='store_true',
            default=False,
            dest='full_quicklook',
            help='disable all but quick look and load',
        )
        parser.add_argument(
            '-5',
            '--disable-top',
            action='store_true',
            default=False,
            dest='disable_top',
            help='disable top menu (QL, CPU, MEM, SWAP and LOAD)',
        )
        parser.add_argument(
            '-6', '--meangpu', action='store_true', default=False, dest='meangpu', help='start Glances in mean GPU mode'
        )
        parser.add_argument(
            '--disable-history',
            action='store_true',
            default=False,
            dest='disable_history',
            help='disable stats history',
        )
        parser.add_argument(
            '--disable-bold',
            action='store_true',
            default=False,
            dest='disable_bold',
            help='disable bold mode in the terminal',
        )
        parser.add_argument(
            '--disable-bg',
            action='store_true',
            default=False,
            dest='disable_bg',
            help='disable background colors in the terminal',
        )
        parser.add_argument(
            '--enable-irq', action='store_true', default=False, dest='e