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
|
|
|
SystemV AMP |
22
|
|
|
=========== |
23
|
|
|
|
24
|
|
|
Monitor the state of the Syste V init system and service. |
25
|
|
|
|
26
|
|
|
How to read the stats |
27
|
|
|
--------------------- |
28
|
|
|
|
29
|
|
|
Running: Number of running services. |
30
|
|
|
Stopped: Number of stopped services. |
31
|
|
|
Upstart: Number of service managed by Upstart. |
32
|
|
|
|
33
|
|
|
Source reference: http://askubuntu.com/questions/407075/how-to-read-service-status-all-results |
|
|
|
|
34
|
|
|
|
35
|
|
|
Configuration file example |
36
|
|
|
-------------------------- |
37
|
|
|
|
38
|
|
|
[amp_systemv] |
39
|
|
|
# Systemv |
40
|
|
|
enable=true |
41
|
|
|
regex=\/sbin\/init |
42
|
|
|
refresh=60 |
43
|
|
|
one_line=true |
44
|
|
|
service_cmd=/usr/bin/service --status-all |
45
|
|
|
""" |
46
|
|
|
|
47
|
|
|
from subprocess import check_output, STDOUT |
|
|
|
|
48
|
|
|
|
49
|
|
|
from glances.logger import logger |
|
|
|
|
50
|
|
|
from glances.compat import iteritems |
|
|
|
|
51
|
|
|
from glances.amps.glances_amp import GlancesAmp |
|
|
|
|
52
|
|
|
|
53
|
|
|
|
54
|
|
|
class Amp(GlancesAmp): |
55
|
|
|
"""Glances' Systemd AMP.""" |
56
|
|
|
|
57
|
|
|
NAME = 'SystemV' |
58
|
|
|
VERSION = '1.0' |
59
|
|
|
DESCRIPTION = 'Get services list from service (initd)' |
60
|
|
|
AUTHOR = 'Nicolargo' |
61
|
|
|
EMAIL = '[email protected]' |
62
|
|
|
|
63
|
|
|
# def __init__(self, args=None): |
64
|
|
|
# """Init the AMP.""" |
65
|
|
|
# super(Amp, self).__init__(args=args) |
66
|
|
|
|
67
|
|
|
def update(self, process_list): |
|
|
|
|
68
|
|
|
"""Update the AMP""" |
69
|
|
|
# Get the systemctl status |
70
|
|
|
logger.debug('{}: Update stats using service {}'.format(self.NAME, self.get('service_cmd'))) |
|
|
|
|
71
|
|
|
try: |
72
|
|
|
res = check_output(self.get('service_cmd').split(), stderr=STDOUT).decode('utf-8') |
|
|
|
|
73
|
|
|
except OSError as e: |
|
|
|
|
74
|
|
|
logger.debug('{}: Error while executing service ({})'.format(self.NAME, e)) |
|
|
|
|
75
|
|
|
else: |
76
|
|
|
status = {'running': 0, 'stopped': 0, 'upstart': 0} |
77
|
|
|
# For each line |
78
|
|
|
for r in res.split('\n'): |
|
|
|
|
79
|
|
|
# Split per space .* |
80
|
|
|
l = r.split() |
|
|
|
|
81
|
|
|
if len(l) < 4: |
82
|
|
|
continue |
83
|
|
|
if l[1] == '+': |
84
|
|
|
status['running'] += 1 |
85
|
|
|
elif l[1] == '-': |
86
|
|
|
status['stopped'] += 1 |
87
|
|
|
elif l[1] == '?': |
88
|
|
|
status['upstart'] += 1 |
89
|
|
|
# Build the output (string) message |
90
|
|
|
output = 'Services\n' |
91
|
|
|
for k, v in iteritems(status): |
|
|
|
|
92
|
|
|
output += '{}: {}\n'.format(k, v) |
93
|
|
|
self.set_result(output, separator=' ') |
94
|
|
|
|
95
|
|
|
return self.result() |
96
|
|
|
|
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.