Test Failed
Push — develop ( 66c9ff...e21229 )
by Nicolas
05:06
created

glances/amps/glances_systemd.py (13 issues)

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
"""
0 ignored issues
show
A suspicious escape sequence \/ was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R 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.

Loading history...
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
0 ignored issues
show
This line is too long as per the coding-style (81/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
30
unit has started successfully or not.
31
loaded: Number of loaded units (unit's configuration has been parsed by systemd).
0 ignored issues
show
This line is too long as per the coding-style (81/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
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
0 ignored issues
show
This line is too long as per the coding-style (124/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
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
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
49
50
from glances.logger import logger
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
51
from glances.compat import iteritems, to_ascii
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
52
from glances.amps.glances_amp import GlancesAmp
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
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
The argument process_list seems to be unused.
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:
0 ignored issues
show
Coding Style Naming introduced by
The name e does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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]:
0 ignored issues
show
Coding Style Naming introduced by
The name r does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
80
                # Split per space .*
81
                column = r.split()
82
                if len(column) > 3:
83
                    # load column
84
                    for c in range(1, 3):
0 ignored issues
show
Coding Style Naming introduced by
The name c does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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):
0 ignored issues
show
Coding Style Naming introduced by
The name v does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
92
                output += '{}: {}\n'.format(k, v)
93
            self.set_result(output, separator=' ')
94
95
        return self.result()
96