1
|
|
|
# |
2
|
|
|
# This file is part of Glances. |
3
|
|
|
# |
4
|
|
|
# SPDX-FileCopyrightText: 2022 Nicolas Hennion <[email protected]> |
5
|
|
|
# |
6
|
|
|
# SPDX-License-Identifier: LGPL-3.0-only |
7
|
|
|
# |
8
|
|
|
|
9
|
|
|
r""" |
10
|
|
|
Default AMP |
11
|
|
|
========= |
12
|
|
|
|
13
|
|
|
Monitor a process by executing a command line. This is the default AMP's behavior |
14
|
|
|
if no AMP script is found. |
15
|
|
|
|
16
|
|
|
Configuration file example |
17
|
|
|
-------------------------- |
18
|
|
|
|
19
|
|
|
[amp_foo] |
20
|
|
|
enable=true |
21
|
|
|
regex=\/usr\/bin\/foo |
22
|
|
|
refresh=10 |
23
|
|
|
one_line=false |
24
|
|
|
command=foo status |
25
|
|
|
""" |
26
|
|
|
|
27
|
|
|
from glances.amps.amp import GlancesAmp |
28
|
|
|
from glances.logger import logger |
29
|
|
|
from glances.secure import secure_popen |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
class Amp(GlancesAmp): |
33
|
|
|
"""Glances' Default AMP.""" |
34
|
|
|
|
35
|
|
|
NAME = '' |
36
|
|
|
VERSION = '1.1' |
37
|
|
|
DESCRIPTION = '' |
38
|
|
|
AUTHOR = 'Nicolargo' |
39
|
|
|
EMAIL = '[email protected]' |
40
|
|
|
|
41
|
|
|
def __init__(self, name=None, args=None): |
42
|
|
|
"""Init the AMP.""" |
43
|
|
|
self.NAME = name.capitalize() |
44
|
|
|
super().__init__(name=name, args=args) |
45
|
|
|
|
46
|
|
|
def update(self, process_list): |
47
|
|
|
"""Update the AMP""" |
48
|
|
|
# Get the systemctl status |
49
|
|
|
logger.debug('{}: Update AMP stats using command {}'.format(self.NAME, self.get('service_cmd'))) |
50
|
|
|
# Get command to execute |
51
|
|
|
try: |
52
|
|
|
res = self.get('command') |
53
|
|
|
except OSError as e: |
54
|
|
|
logger.debug(f'{self.NAME}: Error while executing command ({e})') |
55
|
|
|
return self.result() |
56
|
|
|
# No command found, use default message |
57
|
|
|
if res is None: |
58
|
|
|
# Set the default message if command return None |
59
|
|
|
# Default sum of CPU and MEM for the matching regex |
60
|
|
|
self.set_result( |
61
|
|
|
'CPU: {:.1f}% | MEM: {:.1f}%'.format( |
62
|
|
|
sum([p['cpu_percent'] for p in process_list]), sum([p['memory_percent'] for p in process_list]) |
63
|
|
|
) |
64
|
|
|
) |
65
|
|
|
return self.result() |
66
|
|
|
# Run command(s) |
67
|
|
|
# Comma separated commands can be executed |
68
|
|
|
try: |
69
|
|
|
self.set_result(secure_popen(res).rstrip()) |
70
|
|
|
except Exception as e: |
71
|
|
|
self.set_result(e.output) |
72
|
|
|
return self.result() |
73
|
|
|
|