-
Notifications
You must be signed in to change notification settings - Fork 0
/
pymotdstats.py
655 lines (524 loc) · 19.5 KB
/
pymotdstats.py
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
#!/usr/bin/env python3
# pymotdstats, a dynamic message of the day written in python
# Copyright (C) 2021 Matthieu Petiot
# https://github.com/ardeidae/pymotdstats
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import configparser
import glob
import os
import re
import time
from enum import Enum, unique
from subprocess import check_output, CalledProcessError
VERSION = '1.1.0'
INI_FILE = '/etc/pymotdstats.ini'
UNKNOWN = 'Unknown'
UNKNOWN_ADDRESS = 'Unknown address'
UNKNOWN_UPTIME = 'Unknown uptime'
@unique
class Protocol(Enum):
""" This Enum stores tcp, udp, tcp6 or udp6 values. """
TCP = 'tcp'
TCP6 = 'tcp6'
UDP = 'udp'
UDP6 = 'udp6'
@classmethod
def from_value(cls, _value):
""" Get Protocol Enum from its value.
:type _value: str
:param _value: a value of Protocol
:rtype: Protocol
:return: Protocol Enum
"""
if type(_value) == str:
for p in cls:
if p.value == _value:
return p
return None
def __lt__(self, other):
return self.value < other.value
def __eq__(self, other):
return self.value == other.value
def __hash__(self):
return hash(self.value)
class TermColor:
""" Some Terminal Colors and styles. """
bold = '\033[1m'
reset = '\033[0m'
red = '\033[91m'
green = '\033[92m'
orange = '\033[93m'
blue = '\033[94m'
def get_config(_ini_file):
""" Get a setting dict from an ini file.
:type _ini_file: str
:param _ini_file: the path to ini file
:rtype: dict
:return: the settings dict
"""
config = dict()
if os.path.isfile(_ini_file):
parser = configparser.RawConfigParser()
try:
parser.read(_ini_file)
sections = parser.sections()
for s in ('display', 'threshold'):
if s in sections:
for k in parser[s]:
try:
config[k] = int(parser[s][k].strip())
except ValueError:
config[k] = None
for s in ('disk', 'services'):
if s in sections:
for k in parser[s]:
stripped = map(str.strip, parser[s][k].split(','))
config[k] = set(filter(lambda x: x, stripped))
s = 'ports'
if s in sections:
for k in parser[s]:
try:
stripped = map(str.strip, parser[s][k].split(','))
config[k] = set(map(int, filter(lambda x: x, stripped)))
except ValueError:
config[k] = set()
except configparser.Error:
# nothing to do
pass
return config
def get_hostname():
""" Get the hostname, or Unknown.
:rtype: str
:return: the hostname
"""
try:
return check_output('hostname', encoding='utf8').strip()
except FileNotFoundError:
return UNKNOWN
def get_uptime():
""" Get the uptime, or Unknown uptime.
:rtype: str
:return: the uptime
"""
try:
return check_output(['uptime', '-p'], encoding='utf8').strip()
except FileNotFoundError:
return UNKNOWN_UPTIME
def get_default_iface():
""" Return the default network interface. The one where our default route
is. If it cannot be found, return None.
:rtype: str
:return: the interface
"""
try:
with open('/proc/net/route') as file_:
for line in file_.readlines():
word = line.split()
# default destination must be '00000000'
if word[1] == '00000000':
return word[0]
except FileNotFoundError:
# nothing to do
pass
return None
def get_ip(_iface):
""" Find the local ip address for the given device.
:type _iface: str
:param _iface: the given interface
:rtype: str
:return: the IP address, or 'Unknown address'
"""
if _iface is None:
return UNKNOWN_ADDRESS
try:
cmd = ('ip', 'route', 'list', 'dev', _iface)
for line in check_output(cmd, encoding='utf8').strip().split('\n'):
last_word = ''
for word in line.split():
if last_word == 'src':
return word
last_word = word
except FileNotFoundError:
# nothing to do
pass
return UNKNOWN_ADDRESS
def get_load():
""" Get the load average for 1, 5, and 15 minutes as a dictionary. Values
can be 'Unknown'.
:rtype: str
:return: the load
"""
load = dict()
try:
with open('/proc/loadavg') as file_:
line = file_.read()
array = line.split()
load['1min'] = float(array[0])
load['5min'] = float(array[1])
load['15min'] = float(array[2])
except FileNotFoundError:
load['1min'] = UNKNOWN
load['5min'] = UNKNOWN
load['15min'] = UNKNOWN
return load
def get_processes():
""" Get the number of running processes.
:rtype: int
:return: the number of running processes
"""
return len(glob.glob('/proc/[0-9]*'))
def get_users():
""" Get the connected users on the system as a set.
:rtype: set
:return: set of connected users
"""
users = set()
try:
for line in check_output('who', encoding='utf8').strip().split('\n'):
array = line.split()
if len(array) > 0:
users.add(array[0])
except FileNotFoundError:
# nothing to do
pass
return users
def get_mount_points():
""" Get the file system mount points as a set.
:rtype: set
:return: the mount points
"""
ignore_words = ('debugfs', 'devpts', 'proc', 'sysfs', 'tmpfs')
mount_points = set()
pattern = re.compile(r'^\s*#')
try:
with open('/etc/fstab') as file_:
for line in file_:
if not pattern.match(line):
array = line.split()
if len(array) >= 2:
if array[0] not in ignore_words and array[2] != 'swap':
mount_points.add(array[1])
except FileNotFoundError:
# nothing to do
pass
return mount_points
def get_disk_space(_mount_points, _exclude):
""" Get disk space for each mount point.
:type _mount_points: set
:param _mount_points, a set of mount points for which we want disk space.
:type _exclude: set
:param _exclude, a set of mount points to exclude from results.
:rtype: dict
:return a dictionary whose key is the mount point, and the value is another
dictionary whose keys are used space in percent and available space.
"""
results = dict()
try:
if type(_mount_points) is set:
cmd = ('df', '-h')
for line in check_output(cmd, encoding='utf8').strip().split('\n'):
array = line.split()
if array[5] in _mount_points and array[5] not in _exclude:
result = dict()
result['use%'] = int(array[4][:-1])
result['available'] = array[3]
results[array[5]] = result
except FileNotFoundError:
# nothing to do
pass
return results
def get_meminfo():
""" Get memory information as a dictionary: free mem, total mem, free swap,
total swap, buffers, cached, reclaimable.
:rtype: dict
:return: the memory information
"""
meminfo = dict()
keys = ('MemFree', 'MemTotal', 'SwapFree', 'SwapTotal',
'Buffers', 'Cached', 'SReclaimable')
try:
with open('/proc/meminfo') as file_:
for line in file_.readlines():
key, size = map(str.strip, line.split(':'))
if key in keys:
meminfo[key] = int(size.split()[0])
except FileNotFoundError:
# nothing to do
pass
return meminfo
def get_listening_ports():
""" Get a set of pairs (listening port and protocol).
:rtype: set
:return: a set of pairs of listening ports and protocol
"""
listening_ports = set()
try:
with open(os.devnull, 'w') as devnull:
cmd = ('netstat', '-nlp')
for line in check_output(cmd, stderr=devnull,
encoding='utf8').strip().split('\n'):
array = line.split()
proto = array[0]
current_port = array[3].split(':')[-1]
if proto in ('tcp', 'tcp6') and array[5].startswith('LISTEN') \
or proto in ('udp', 'udp6'):
port = (int(current_port), Protocol.from_value(proto))
listening_ports.add(port)
except FileNotFoundError:
# nothing to do
pass
return listening_ports
def get_checked_ports(_ports_to_monitor):
""" Get a dict of pairs (listening port and protocol) and their listening
state.
:type _ports_to_monitor: set
:param _ports_to_monitor: a set of ports to monitor
:rtype: dict
:return: a dict of pairs (listening port and protocol) and state
"""
checked_ports = dict()
if type(_ports_to_monitor) is set:
listening_ports = get_listening_ports()
ports_on_error = _ports_to_monitor - listening_ports
ports_on_success = _ports_to_monitor - ports_on_error
for p in ports_on_error:
checked_ports[p] = False
for p in ports_on_success:
checked_ports[p] = True
return checked_ports
def get_checked_services(_services_to_monitor):
""" Get a dict of service names and their running state.
:type _services_to_monitor: set
:param _services_to_monitor: a set of services to monitor
:rtype: dict
:return: a dict of service names and state
"""
results = dict()
if type(_services_to_monitor) is set:
for s in services_to_monitor:
try:
processes = check_output(['pgrep', '--exact', s],
encoding='utf8').strip().split('\n')
results[s] = len(processes) > 0
except FileNotFoundError:
results[s] = False
except CalledProcessError:
results[s] = False
return results
def get_cpu_number():
""" Get number of processors, or 'Unknown'.
:rtype: int
:return: the number of processors or 'Unknown'
"""
try:
with open('/proc/cpuinfo') as file_:
cpuinfo = file_.read()
return len(re.findall(r'\bprocessor\b', cpuinfo))
except FileNotFoundError:
return UNKNOWN
def add_memory_row(_rows, _warn_threshold, _crit_threshold, _format_str,
_title, _value, _percent):
""" Add and format a memory row according to values and thresholds.
:type _rows: list
:param _rows: the rows as a list
:type _warn_threshold: int
:param _warn_threshold: the warning threshold
:type _crit_threshold: int
:param _crit_threshold: the critical threshold
:type _format_str: str
:param _format_str: the python format string with alignments
:type _title: str
:param _title: the row title
:type _value: int
:param _value: the value
:type _percent: int
:param _percent: the percent value
:rtype: None
:return: nothing
"""
if type(_rows) is list:
if _percent >= _crit_threshold:
text_color = TermColor.red
elif _percent >= _warn_threshold:
text_color = TermColor.orange
else:
text_color = TermColor.green
_rows.append(text_color + _format_str.format(_title, _value, _percent)
+ TermColor.reset)
config = get_config(INI_FILE)
fs_exclude = config.get('fs_exclude', set())
ports_to_monitor = \
{(p, Protocol.TCP) for p in config.get('tcp_ports_to_monitor', set())} \
| {(p, Protocol.TCP6) for p in config.get('tcp6_ports_to_monitor', set())} \
| {(p, Protocol.UDP) for p in config.get('udp_ports_to_monitor', set())} \
| {(p, Protocol.UDP6) for p in config.get('udp6_ports_to_monitor', set())}
services_to_monitor = set(config.get('services_to_monitor', list()))
DISK_WARNING = config.get('disk_warning', 80) or 80
DISK_CRITICAL = config.get('disk_critical', 90) or 90
MEM_WARNING = config.get('mem_warning', 80) or 80
MEM_CRITICAL = config.get('mem_critical', 90) or 90
SWAP_WARNING = config.get('swap_warning', 10) or 10
SWAP_CRITICAL = config.get('swap_critical', 20) or 20
MAX_ROWS = config.get('max_rows', 15) or 15
col_width = config.get('col_width', 32) or 32
try:
# shell mode
term_size = os.get_terminal_size()
COL_WIDTH = max(int((term_size.columns - 1 - 6) / 3), col_width)
except OSError:
# cron mode
COL_WIDTH = col_width
datetime = time.asctime()
hostname = get_hostname()
uptime = get_uptime()
iface = get_default_iface()
ip = get_ip(iface)
load = get_load()
processes = get_processes()
users = get_users()
mount_points = get_mount_points()
disk_space = get_disk_space(mount_points, fs_exclude)
meminfo = get_meminfo()
checked_ports = get_checked_ports(ports_to_monitor)
checked_services = get_checked_services(services_to_monitor)
columns = dict()
columns[0] = list()
columns[1] = list()
columns[2] = list()
disk_status_color = TermColor.green
for i in disk_space:
if disk_space[i]['use%'] >= DISK_CRITICAL:
disk_status_color = TermColor.red
elif disk_space[i]['use%'] >= DISK_WARNING \
and disk_status_color != TermColor.red:
disk_status_color = TermColor.orange
columns[0].append(disk_status_color + TermColor.bold
+ 'Disk status'.center(COL_WIDTH, '.') + TermColor.reset)
# noinspection PyStringFormat
columns[0].append(TermColor.bold
+ f'{{:{COL_WIDTH - 10}}} free use%'.format('Partition')
+ TermColor.reset)
if meminfo:
mem_perc = int(100 - 100 * meminfo['MemFree'] / meminfo['MemTotal'])
mem_used = int((meminfo['MemTotal'] - meminfo['MemFree']) / 1024)
if meminfo['SwapTotal'] == 0:
swap_perc = '---'
swap_used = '---'
else:
swap_perc = int(100 - 100 * meminfo['SwapFree'] / meminfo['SwapTotal'])
swap_used = int((meminfo['SwapTotal'] - meminfo['SwapFree']) / 1024)
buffers = int(meminfo['Buffers'] / 1024)
buffers_perc = int(100 * meminfo['Buffers'] / meminfo['MemTotal'])
cached = int(meminfo['Cached'] / 1024)
cached_perc = int(100 * meminfo['Cached'] / meminfo['MemTotal'])
reclaimable = int(meminfo['SReclaimable'] / 1024)
reclaimable_perc = int(100 * meminfo['SReclaimable'] / meminfo['MemTotal'])
if mem_perc >= MEM_CRITICAL or swap_perc >= SWAP_CRITICAL \
or buffers_perc >= MEM_CRITICAL or cached_perc >= MEM_CRITICAL:
memory_status_color = TermColor.red + TermColor.bold
elif mem_perc >= MEM_WARNING or swap_perc >= SWAP_WARNING \
or buffers_perc >= MEM_WARNING or cached_perc >= MEM_WARNING:
memory_status_color = TermColor.orange + TermColor.bold
else:
memory_status_color = TermColor.green + TermColor.bold
mem_format = f'{{:<{COL_WIDTH - 12}}} {{:>6}} {{:>4}}'
add_memory_row(columns[1], MEM_WARNING, MEM_CRITICAL, mem_format,
'Memory', mem_used, mem_perc)
add_memory_row(columns[1], SWAP_WARNING, SWAP_CRITICAL, mem_format,
'Swap', swap_used, swap_perc)
add_memory_row(columns[1], MEM_WARNING, MEM_CRITICAL, mem_format,
'Buffers', buffers, buffers_perc)
add_memory_row(columns[1], MEM_WARNING, MEM_CRITICAL, mem_format,
'Cached', cached, cached_perc)
add_memory_row(columns[1], MEM_WARNING, MEM_CRITICAL, mem_format,
'Reclaimable', reclaimable, reclaimable_perc)
else:
memory_status_color = TermColor.green + TermColor.bold
# noinspection PyStringFormat
columns[1].insert(0, TermColor.bold
+ f'{{:{COL_WIDTH - 8}}} MB %'.format('Memory used')
+ TermColor.reset)
columns[1].insert(0, memory_status_color
+ 'Memory status'.center(COL_WIDTH, '.')
+ TermColor.reset)
if all(checked_ports.values()) and all(checked_services.values()):
services_ports_status_color = TermColor.green + TermColor.bold
else:
services_ports_status_color = TermColor.red + TermColor.bold
columns[2].append(services_ports_status_color
+ 'Services status'.center(COL_WIDTH, '.')
+ TermColor.reset)
# noinspection PyStringFormat
columns[2].append(TermColor.bold
+ f'{{:{COL_WIDTH - 7}}} status'.format('Services/ports')
+ TermColor.reset)
for i in disk_space:
if disk_space[i]['use%'] >= DISK_CRITICAL:
partition_color = TermColor.red
elif disk_space[i]['use%'] >= DISK_WARNING:
partition_color = TermColor.orange
else:
partition_color = TermColor.green
# noinspection PyStringFormat
columns[0].append(partition_color
+ f'{{:<{COL_WIDTH - 10}}} {{:>4}} {{:>4}}'
.format(i, disk_space[i]['available'],
disk_space[i]['use%']) + TermColor.reset)
for i in sorted(checked_services.keys()):
if checked_services[i]:
service_color = TermColor.green
service_status = 'running'
else:
service_color = TermColor.red
service_status = 'KO'
# noinspection PyStringFormat
columns[2].append(service_color + f'{{:<{COL_WIDTH - 9}}} {{:>8}}'
.format(i, service_status) + TermColor.reset)
for i in sorted(checked_ports.keys()):
if checked_ports[i]:
port_color = TermColor.green
port_status = 'listening'
else:
port_color = TermColor.red
port_status = 'KO'
# noinspection PyStringFormat
columns[2].append(port_color + f'{{:<{COL_WIDTH - 11}}} {{:>10}}'
.format(str(i[0]) + '/' + i[1].value, port_status)
+ TermColor.reset)
print('\n' + TermColor.blue + TermColor.bold + 'System status for {} at {}'
.format(hostname, datetime)
.center(COL_WIDTH * 3 + 6, '.') + TermColor.reset)
print('\n\t\tIP: {}/{}'.format(ip,
iface if iface is not None else UNKNOWN))
print('\t\t{} user(s), {} processes'.format(len(users), processes))
print('\t\t{}'.format(uptime))
print('\t\tLoad: {} on {} CPU\n'.format(
' / '.join(['{} ({})'.format(load[k], k) for k in load.keys()]),
get_cpu_number()))
column_sizes = [len(columns[x]) for x in columns]
rows_to_print = min(MAX_ROWS, max(*column_sizes))
for i in range(rows_to_print):
row = ''
for j in columns:
if i < len(columns[j]):
row += columns[j][i]
else:
row += ' ' * COL_WIDTH
if j < len(columns) - 1:
row += ' | '
print(row)
print('\n' + TermColor.blue + TermColor.bold + 'pymotdstats {}'
.format(VERSION)
.rjust(COL_WIDTH * 3 + 6, '.') + TermColor.reset)