Test Failed
Push — master ( 7e7379...128504 )
by Nicolas
03:31
created

Plugin.__init__()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
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
"""Process count plugin."""
21
22
from glances.processes import glances_processes
23
from glances.plugins.glances_plugin import GlancesPlugin
24
25
# Define the history items list
26
items_history_list = [
27
    {'name': 'total', 'description': 'Total number of processes', 'y_unit': ''},
28
    {'name': 'running', 'description': 'Total number of running processes', 'y_unit': ''},
29
    {'name': 'sleeping', 'description': 'Total number of sleeping processes', 'y_unit': ''},
30
    {'name': 'thread', 'description': 'Total number of threads', 'y_unit': ''},
31
]
32
33
34
class Plugin(GlancesPlugin):
35
    """Glances process count plugin.
36
37
    stats is a list
38
    """
39
40
    sort_for_human = {
41
        'io_counters': 'disk IO',
42
        'cpu_percent': 'CPU consumption',
43
        'memory_percent': 'memory consumption',
44
        'cpu_times': 'process time',
45
        'username': 'user name',
46
        'name': 'process name',
47
        None: 'None',
48
    }
49
50
    def __init__(self, args=None, config=None):
51
        """Init the plugin."""
52
        super(Plugin, self).__init__(args=args, config=config, items_history_list=items_history_list)
53
54
        # We want to display the stat in the curse interface
55
        self.display_curse = True
56
57
        # Note: 'glances_processes' is already init in the glances_processes.py script
58
59
    def enable_extended(self):
60
        """Enable extended stats."""
61
        glances_processes.enable_extended()
62
63
    def disable_extended(self):
64
        """Disable extended stats."""
65
        glances_processes.disable_extended()
66
67
    @GlancesPlugin._check_decorator
68
    @GlancesPlugin._log_result_decorator
69
    def update(self):
70
        """Update processes stats using the input method."""
71
        # Init new stats
72
        stats = self.get_init_value()
73
74
        if self.input_method == 'local':
75
            # Update stats using the standard system lib
76
            # Here, update is call for processcount AND processlist
77
            glances_processes.update()
78
79
            # Return the processes count
80
            stats = glances_processes.get_count()
81
        elif self.input_method == 'snmp':
82
            # Update stats using SNMP
83
            # Not availaible
84
            pass
85
86
        # Update the stats
87
        self.stats = stats
88
89
        return self.stats
90
91
    def msg_curse(self, args=None, max_width=None):
92
        """Return the dict to display in the curse interface."""
93
        # Init the return message
94
        ret = []
95
96
        # Only process if stats exist and display plugin enable...
97
        if args.disable_process:
98
            msg = "PROCESSES DISABLED (press 'z' to display)"
99
            ret.append(self.curse_add_line(msg))
100
            return ret
101
102
        if not self.stats:
103
            return ret
104
105
        # Display the filter (if it exists)
106
        if glances_processes.process_filter is not None:
107
            msg = 'Processes filter:'
108
            ret.append(self.curse_add_line(msg, "TITLE"))
109
            msg = ' {} '.format(glances_processes.process_filter)
110
            if glances_processes.process_filter_key is not None:
111
                msg += 'on column {} '.format(glances_processes.process_filter_key)
112
            ret.append(self.curse_add_line(msg, "FILTER"))
113
            msg = '(\'ENTER\' to edit, \'E\' to reset)'
114
            ret.append(self.curse_add_line(msg))
115
            ret.append(self.curse_new_line())
116
117
        # Build the string message
118
        # Header
119
        msg = 'TASKS'
120
        ret.append(self.curse_add_line(msg, "TITLE"))
121
        # Compute processes
122
        other = self.stats['total']
123
        msg = '{:>4}'.format(self.stats['total'])
124
        ret.append(self.curse_add_line(msg))
125
126
        if 'thread' in self.stats:
127
            msg = ' ({} thr),'.format(self.stats['thread'])
128
            ret.append(self.curse_add_line(msg))
129
130
        if 'running' in self.stats:
131
            other -= self.stats['running']
132
            msg = ' {} run,'.format(self.stats['running'])
133
            ret.append(self.curse_add_line(msg))
134
135
        if 'sleeping' in self.stats:
136
            other -= self.stats['sleeping']
137
            msg = ' {} slp,'.format(self.stats['sleeping'])
138
            ret.append(self.curse_add_line(msg))
139
140
        msg = ' {} oth '.format(other)
141
        ret.append(self.curse_add_line(msg))
142
143
        # Display sort information
144
        msg = 'Programs' if self.args.programs else 'Threads'
145
        try:
146
            sort_human = self.sort_for_human[glances_processes.sort_key]
147
        except KeyError:
148
            sort_human = glances_processes.sort_key
149
        if glances_processes.auto_sort:
150
            msg += ' sorted automatically'
151
            ret.append(self.curse_add_line(msg))
152
            msg = ' by {}'.format(sort_human)
153
        else:
154
            msg += ' sorted by {}'.format(sort_human)
155
        ret.append(self.curse_add_line(msg))
156
157
        # Return the message with decoration
158
        return ret
159