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 | r""" |
||
21 | Systemd AMP |
||
22 | =========== |
||
23 | |||
24 | Monitor the state of the systemd system and service (unit) manager. |
||
25 | |||
26 | How to read the stats |
||
27 | --------------------- |
||
28 | |||
29 | active: Number of active units. This is usually a fairly basic way to tell if the |
||
30 | unit has started successfully or not. |
||
31 | loaded: Number of loaded units (unit's configuration has been parsed by systemd). |
||
32 | failed: Number of units with an active failed status. |
||
33 | |||
34 | Source reference: https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units |
||
35 | |||
36 | Configuration file example |
||
37 | -------------------------- |
||
38 | |||
39 | [amp_systemd] |
||
40 | # Systemd |
||
41 | enable=true |
||
42 | regex=\/usr\/lib\/systemd\/systemd |
||
43 | refresh=60 |
||
44 | one_line=true |
||
45 | systemctl_cmd=/usr/bin/systemctl --plain |
||
46 | """ |
||
47 | |||
48 | from subprocess import check_output, CalledProcessError |
||
49 | |||
50 | from glances.logger import logger |
||
51 | from glances.compat import iteritems, to_ascii |
||
52 | from glances.amps.glances_amp import GlancesAmp |
||
53 | |||
54 | |||
55 | class Amp(GlancesAmp): |
||
56 | """Glances' Systemd AMP.""" |
||
57 | |||
58 | NAME = 'Systemd' |
||
59 | VERSION = '1.0' |
||
60 | DESCRIPTION = 'Get services list from systemctl (systemd)' |
||
61 | AUTHOR = 'Nicolargo' |
||
62 | EMAIL = '[email protected]' |
||
63 | |||
64 | # def __init__(self, args=None): |
||
65 | # """Init the AMP.""" |
||
66 | # super(Amp, self).__init__(args=args) |
||
67 | |||
68 | def update(self, process_list): |
||
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
69 | """Update the AMP""" |
||
70 | # Get the systemctl status |
||
71 | logger.debug('{}: Update stats using systemctl {}'.format(self.NAME, self.get('systemctl_cmd'))) |
||
72 | try: |
||
73 | res = check_output(self.get('systemctl_cmd').split()) |
||
74 | except (OSError, CalledProcessError) as e: |
||
75 | logger.debug('{}: Error while executing systemctl ({})'.format(self.NAME, e)) |
||
76 | else: |
||
77 | status = {} |
||
78 | # For each line |
||
79 | for r in to_ascii(res).split('\n')[1:-8]: |
||
80 | # Split per space .* |
||
81 | column = r.split() |
||
82 | if len(column) > 3: |
||
83 | # load column |
||
84 | for c in range(1, 3): |
||
85 | try: |
||
86 | status[column[c]] += 1 |
||
87 | except KeyError: |
||
88 | status[column[c]] = 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 |