1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# |
3
|
|
|
# This file is part of Glances. |
4
|
|
|
# |
5
|
|
|
# Copyright (C) 2019 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
|
|
|
Default AMP |
22
|
|
|
========= |
23
|
|
|
|
24
|
|
|
Monitor a process by executing a command line. This is the default AMP's behavor |
25
|
|
|
if no AMP script is found. |
26
|
|
|
|
27
|
|
|
Configuration file example |
28
|
|
|
-------------------------- |
29
|
|
|
|
30
|
|
|
[amp_foo] |
31
|
|
|
enable=true |
32
|
|
|
regex=\/usr\/bin\/foo |
33
|
|
|
refresh=10 |
34
|
|
|
one_line=false |
35
|
|
|
command=foo status |
36
|
|
|
""" |
37
|
|
|
|
38
|
|
|
from subprocess import check_output, STDOUT, CalledProcessError |
|
|
|
|
39
|
|
|
|
40
|
|
|
from glances.compat import u, to_ascii |
|
|
|
|
41
|
|
|
from glances.logger import logger |
|
|
|
|
42
|
|
|
from glances.amps.glances_amp import GlancesAmp |
|
|
|
|
43
|
|
|
|
44
|
|
|
|
45
|
|
|
class Amp(GlancesAmp): |
46
|
|
|
"""Glances' Default AMP.""" |
47
|
|
|
|
48
|
|
|
NAME = '' |
49
|
|
|
VERSION = '1.1' |
50
|
|
|
DESCRIPTION = '' |
51
|
|
|
AUTHOR = 'Nicolargo' |
52
|
|
|
EMAIL = '[email protected]' |
53
|
|
|
|
54
|
|
|
def __init__(self, name=None, args=None): |
55
|
|
|
"""Init the AMP.""" |
56
|
|
|
self.NAME = name.capitalize() |
|
|
|
|
57
|
|
|
super(Amp, self).__init__(name=name, args=args) |
58
|
|
|
|
59
|
|
|
def update(self, process_list): |
60
|
|
|
"""Update the AMP""" |
61
|
|
|
# Get the systemctl status |
62
|
|
|
logger.debug('{}: Update AMP stats using command {}'.format(self.NAME, self.get('service_cmd'))) |
|
|
|
|
63
|
|
|
# Get command to execute |
64
|
|
|
try: |
65
|
|
|
res = self.get('command') |
66
|
|
|
except OSError as e: |
|
|
|
|
67
|
|
|
logger.debug('{}: Error while executing command ({})'.format(self.NAME, e)) |
|
|
|
|
68
|
|
|
return self.result() |
69
|
|
|
# No command found, use default message |
70
|
|
|
if res is None: |
71
|
|
|
# Set the default message if command return None |
72
|
|
|
# Default sum of CPU and MEM for the matching regex |
73
|
|
|
self.set_result('CPU: {:.1f}% | MEM: {:.1f}%'.format( |
74
|
|
|
sum([p['cpu_percent'] for p in process_list]), |
75
|
|
|
sum([p['memory_percent'] for p in process_list]))) |
76
|
|
|
return self.result() |
77
|
|
|
# Run command(s) |
78
|
|
|
# Comman separated commands can be executed |
79
|
|
|
try: |
80
|
|
|
msg = '' |
81
|
|
|
for cmd in res.split(';'): |
82
|
|
|
msg += u(check_output(cmd.split(), stderr=STDOUT)) |
83
|
|
|
self.set_result(to_ascii(msg.rstrip())) |
84
|
|
|
except CalledProcessError as e: |
|
|
|
|
85
|
|
|
self.set_result(e.output) |
86
|
|
|
return self.result() |
87
|
|
|
|
Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with
r
orR
are they interpreted as regular expressions.The escape sequence that was used indicates that you might have intended to write a regular expression.
Learn more about the available escape sequences. in the Python documentation.