Completed
Push — master ( adb900...a77972 )
by Nicolas
01:06
created

glances.plugins.Plugin.update()   A

Complexity

Conditions 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 18
rs 9.4285
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of Glances.
4
#
5
# Copyright (C) 2015 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
# Note: history items list is not compliant with process count
26
#       if a filter is applyed, the graph will show the filtered processes count
27
28
29
class Plugin(GlancesPlugin):
30
31
    """Glances process count plugin.
32
33
    stats is a list
34
    """
35
36
    def __init__(self, args=None):
37
        """Init the plugin."""
38
        super(Plugin, self).__init__(args=args)
39
40
        # We want to display the stat in the curse interface
41
        self.display_curse = True
42
43
        # Note: 'glances_processes' is already init in the glances_processes.py script
44
45
    def reset(self):
46
        """Reset/init the stats."""
47
        self.stats = {}
48
49
    def update(self):
50
        """Update processes stats using the input method."""
51
        # Reset stats
52
        self.reset()
53
54
        if self.input_method == 'local':
55
            # Update stats using the standard system lib
56
            # Here, update is call for processcount AND processlist
57
            glances_processes.update()
58
59
            # Return the processes count
60
            self.stats = glances_processes.getcount()
61
        elif self.input_method == 'snmp':
62
            # Update stats using SNMP
63
            # !!! TODO
64
            pass
65
66
        return self.stats
67
68
    def msg_curse(self, args=None):
69
        """Return the dict to display in the curse interface."""
70
        # Init the return message
71
        ret = []
72
73
        # Only process if stats exist and display plugin enable...
74
        if args.disable_process:
75
            msg = "PROCESSES DISABLED (press 'z' to display)"
76
            ret.append(self.curse_add_line(msg))
77
            return ret
78
79
        if not self.stats:
80
            return ret
81
82
        # Display the filter (if it exists)
83
        if glances_processes.process_filter is not None:
84
            msg = 'Processes filter:'
85
            ret.append(self.curse_add_line(msg, "TITLE"))
86
            msg = ' {0} '.format(glances_processes.process_filter)
87
            ret.append(self.curse_add_line(msg, "FILTER"))
88
            msg = '(\'ENTER\' to edit, \'E\' to reset)'
89
            ret.append(self.curse_add_line(msg))
90
            ret.append(self.curse_new_line())
91
92
        # Build the string message
93
        # Header
94
        msg = 'TASKS'
95
        ret.append(self.curse_add_line(msg, "TITLE"))
96
        # Compute processes
97
        other = self.stats['total']
98
        msg = '{0:>4}'.format(self.stats['total'])
99
        ret.append(self.curse_add_line(msg))
100
101
        if 'thread' in self.stats:
102
            msg = ' ({0} thr),'.format(self.stats['thread'])
103
            ret.append(self.curse_add_line(msg))
104
105
        if 'running' in self.stats:
106
            other -= self.stats['running']
107
            msg = ' {0} run,'.format(self.stats['running'])
108
            ret.append(self.curse_add_line(msg))
109
110
        if 'sleeping' in self.stats:
111
            other -= self.stats['sleeping']
112
            msg = ' {0} slp,'.format(self.stats['sleeping'])
113
            ret.append(self.curse_add_line(msg))
114
115
        msg = ' {0} oth '.format(other)
116
        ret.append(self.curse_add_line(msg))
117
118
        # Display sort information
119
        if glances_processes.auto_sort:
120
            msg = 'sorted automatically'
121
            ret.append(self.curse_add_line(msg))
122
            msg = ' by {0}'.format(glances_processes.sort_key)
123
            ret.append(self.curse_add_line(msg))
124
        else:
125
            msg = 'sorted by {0}'.format(glances_processes.sort_key)
126
            ret.append(self.curse_add_line(msg))
127
        ret[-1]["msg"] += ", %s view" % ("tree" if glances_processes.is_tree_enabled() else "flat")
128
        # if args.disable_irix:
129
        #     ret[-1]["msg"] += " - IRIX off"
130
131
        # Return the message with decoration
132
        return ret
133