glances_processlist.py 10.4 KB
Newer Older
A
Alessio Sergi 已提交
1 2
# -*- coding: utf-8 -*-
#
3
# This file is part of Glances.
A
Alessio Sergi 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#
# Copyright (C) 2014 Nicolargo <nicolas@nicolargo.com>
#
# Glances is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Glances is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

A
PEP 257  
Alessio Sergi 已提交
20 21
"""Process list plugin."""

N
Nicolas Hennion 已提交
22
# Import sys libs
23
import os
A
Alessio Sergi 已提交
24 25
from datetime import timedelta

N
Nicolas Hennion 已提交
26
# Import Glances libs
27
from glances.core.glances_globals import glances_processes, is_windows
A
Alessio Sergi 已提交
28
from glances.plugins.glances_plugin import GlancesPlugin
A
Alessio Sergi 已提交
29 30 31


class Plugin(GlancesPlugin):
A
PEP 257  
Alessio Sergi 已提交
32 33

    """Glances' processes plugin.
A
Alessio Sergi 已提交
34 35 36 37

    stats is a list
    """

38
    def __init__(self, args=None):
A
PEP 257  
Alessio Sergi 已提交
39
        """Init the plugin."""
40
        GlancesPlugin.__init__(self, args=args)
A
Alessio Sergi 已提交
41 42 43 44 45 46 47 48 49 50

        # We want to display the stat in the curse interface
        self.display_curse = True
        # Set the message position
        # It is NOT the curse position but the Glances column/line
        # Enter -1 to right align
        self.column_curse = 1
        # Enter -1 to diplay bottom
        self.line_curse = 4

51
        # Note: 'glances_processes' is already init in the glances_processes.py script
52

53
    def reset(self):
A
PEP 257  
Alessio Sergi 已提交
54
        """Reset/init the stats."""
55
        self.stats = []
56 57

    def update(self):
A
PEP 257  
Alessio Sergi 已提交
58
        """Update processes stats using the input method."""
59 60
        # Reset stats
        self.reset()
61

62
        if self.get_input() == 'local':
63 64 65 66
            # Update stats using the standard system lib
            # Note: Update is done in the processcount plugin
            # Just return the processes list
            self.stats = glances_processes.getlist()
67
        elif self.get_input() == 'snmp':
68 69 70
            # Update stats using SNMP
            # !!! TODO
            pass
71

72
        return self.stats
73

A
Alessio Sergi 已提交
74
    def msg_curse(self, args=None):
A
PEP 257  
Alessio Sergi 已提交
75
        """Return the dict to display in the curse interface."""
A
Alessio Sergi 已提交
76 77 78
        # Init the return message
        ret = []

79
        # Only process if stats exist and display plugin enable...
80
        if self.stats == [] or args.disable_process:
81 82
            return ret

A
Alessio Sergi 已提交
83
        # Compute the sort key
84 85 86
        try:
            args.process_sorted_by
        except AttributeError:
A
Alessio Sergi 已提交
87
            args.process_sorted_by = glances_processes.getsortkey()
88
        if args.process_sorted_by == 'auto':
N
Nicolas Hennion 已提交
89
            process_sort_key = glances_processes.getsortkey()
A
Alessio Sergi 已提交
90 91
        else:
            process_sort_key = args.process_sorted_by
92
        sort_style = 'SORT'
A
Alessio Sergi 已提交
93 94

        # Header
A
Alessio Sergi 已提交
95
        msg = '{0:>6}'.format(_("CPU%"))
A
Alessio Sergi 已提交
96
        ret.append(self.curse_add_line(msg, sort_style if process_sort_key == 'cpu_percent' else 'DEFAULT'))
A
Alessio Sergi 已提交
97
        msg = '{0:>6}'.format(_("MEM%"))
A
Alessio Sergi 已提交
98
        ret.append(self.curse_add_line(msg, sort_style if process_sort_key == 'memory_percent' else 'DEFAULT'))
A
Alessio Sergi 已提交
99
        msg = '{0:>6}'.format(_("VIRT"))
A
Alessio Sergi 已提交
100
        ret.append(self.curse_add_line(msg, optional=True))
A
Alessio Sergi 已提交
101
        msg = '{0:>6}'.format(_("RES"))
A
Alessio Sergi 已提交
102
        ret.append(self.curse_add_line(msg, optional=True))
A
Alessio Sergi 已提交
103
        msg = '{0:>6}'.format(_("PID"))
104
        ret.append(self.curse_add_line(msg))
A
Alessio Sergi 已提交
105
        msg = ' {0:10}'.format(_("USER"))
106
        ret.append(self.curse_add_line(msg))
A
Alessio Sergi 已提交
107
        msg = '{0:>4}'.format(_("NI"))
108
        ret.append(self.curse_add_line(msg))
A
Alessio Sergi 已提交
109
        msg = '{0:>2}'.format(_("S"))
110
        ret.append(self.curse_add_line(msg))
A
Alessio Sergi 已提交
111
        msg = '{0:>9}'.format(_("TIME+"))
A
Alessio Sergi 已提交
112
        ret.append(self.curse_add_line(msg, optional=True))
A
Alessio Sergi 已提交
113
        msg = '{0:>6}'.format(_("IOR/s"))
A
Alessio Sergi 已提交
114
        ret.append(self.curse_add_line(msg, sort_style if process_sort_key == 'io_counters' else 'DEFAULT', optional=True))
A
Alessio Sergi 已提交
115
        msg = '{0:>6}'.format(_("IOW/s"))
A
Alessio Sergi 已提交
116
        ret.append(self.curse_add_line(msg, sort_style if process_sort_key == 'io_counters' else 'DEFAULT', optional=True))
A
Alessio Sergi 已提交
117
        msg = ' {0:8}'.format(_("Command"))
118
        ret.append(self.curse_add_line(msg))
A
Alessio Sergi 已提交
119 120 121 122 123

        # Trying to display proc time
        tag_proc_time = True

        # Loop over processes (sorted by the sort key previously compute)
124
        for p in self.sortlist(process_sort_key):
A
Alessio Sergi 已提交
125 126
            ret.append(self.curse_new_line())
            # CPU
A
Alessio Sergi 已提交
127
            msg = '{0:>6.1f}'.format(p['cpu_percent'])
A
Alessio Sergi 已提交
128 129 130
            ret.append(self.curse_add_line(msg,
                                           self.get_alert(p['cpu_percent'], header="cpu")))
            # MEM
A
Alessio Sergi 已提交
131
            msg = '{0:>6.1f}'.format(p['memory_percent'])
A
Alessio Sergi 已提交
132 133 134
            ret.append(self.curse_add_line(msg,
                                           self.get_alert(p['memory_percent'], header="mem")))
            # VMS
A
Alessio Sergi 已提交
135
            msg = '{0:>6}'.format(self.auto_unit(p['memory_info'][1], low_precision=False))
A
Alessio Sergi 已提交
136 137
            ret.append(self.curse_add_line(msg, optional=True))
            # RSS
A
Alessio Sergi 已提交
138
            msg = '{0:>6}'.format(self.auto_unit(p['memory_info'][0], low_precision=False))
A
Alessio Sergi 已提交
139 140
            ret.append(self.curse_add_line(msg, optional=True))
            # PID
A
Alessio Sergi 已提交
141
            msg = '{0:>6}'.format(p['pid'])
142
            ret.append(self.curse_add_line(msg))
A
Alessio Sergi 已提交
143
            # USER
144 145
            # docker internal users are displayed as ints only, therefore str()
            msg = ' {0:9}'.format(str(p['username'])[:9])
146
            ret.append(self.curse_add_line(msg))
A
Alessio Sergi 已提交
147
            # NICE
148
            nice = p['nice']
149 150
            if nice is None:
                nice = '?'
A
Alessio Sergi 已提交
151
            msg = '{0:>5}'.format(nice)
152 153
            if isinstance(nice, int) and ((is_windows and nice != 32) or
                                          (not is_windows and nice != 0)):
154
                ret.append(self.curse_add_line(msg, decoration='NICE'))
155
            else:
156
                ret.append(self.curse_add_line(msg))
A
Alessio Sergi 已提交
157
            # STATUS
158
            status = p['status']
A
Alessio Sergi 已提交
159
            msg = '{0:>2}'.format(status)
160
            if status == 'R':
161
                ret.append(self.curse_add_line(msg, decoration='STATUS'))
162
            else:
163
                ret.append(self.curse_add_line(msg))
A
Alessio Sergi 已提交
164
            # TIME+
165
            if tag_proc_time:
A
Alessio Sergi 已提交
166 167 168 169 170 171 172
                try:
                    dtime = timedelta(seconds=sum(p['cpu_times']))
                except Exception:
                    # Catched on some Amazon EC2 server
                    # See https://github.com/nicolargo/glances/issues/87
                    tag_proc_time = False
                else:
A
Alessio Sergi 已提交
173
                    msg = '{0}:{1}.{2}'.format(str(dtime.seconds // 60 % 60),
A
Alessio Sergi 已提交
174 175 176
                                               str(dtime.seconds % 60).zfill(2),
                                               str(dtime.microseconds)[:2].zfill(2))
            else:
A
Alessio Sergi 已提交
177 178
                msg = ' '
            msg = '{0:>9}'.format(msg)
A
Alessio Sergi 已提交
179
            ret.append(self.curse_add_line(msg, optional=True))
N
Nicolas Hennion 已提交
180
            # IO read/write
181
            if 'io_counters' in p:
N
Nicolas Hennion 已提交
182 183
                # IO read
                io_rs = (p['io_counters'][0] - p['io_counters'][2]) / p['time_since_update']
184
                if io_rs == 0:
A
Alessio Sergi 已提交
185
                    msg = '{0:>6}'.format("0")
N
Nicolas Hennion 已提交
186
                else:
A
Alessio Sergi 已提交
187
                    msg = '{0:>6}'.format(self.auto_unit(io_rs, low_precision=False))
N
Nicolas Hennion 已提交
188 189 190
                ret.append(self.curse_add_line(msg, optional=True))
                # IO write
                io_ws = (p['io_counters'][1] - p['io_counters'][3]) / p['time_since_update']
191
                if io_ws == 0:
A
Alessio Sergi 已提交
192
                    msg = '{0:>6}'.format("0")
N
Nicolas Hennion 已提交
193
                else:
A
Alessio Sergi 已提交
194
                    msg = '{0:>6}'.format(self.auto_unit(io_ws, low_precision=False))
N
Nicolas Hennion 已提交
195
                ret.append(self.curse_add_line(msg, optional=True))
A
Alessio Sergi 已提交
196
            else:
A
Alessio Sergi 已提交
197
                msg = '{0:>6}'.format("?")
N
Nicolas Hennion 已提交
198 199
                ret.append(self.curse_add_line(msg, optional=True))
                ret.append(self.curse_add_line(msg, optional=True))
A
Alessio Sergi 已提交
200
            # Command line
201 202 203 204
            # If no command line for the process is available, fallback to
            # the bare process name instead
            cmdline = p['cmdline']
            if cmdline == "":
A
Alessio Sergi 已提交
205
                msg = ' {0}'.format(p['name'])
206
                ret.append(self.curse_add_line(msg, splittable=True))
207 208 209 210 211 212
            else:
                try:
                    cmd = cmdline.split()[0]
                    args = ' '.join(cmdline.split()[1:])
                    path, basename = os.path.split(cmd)
                    if os.path.isdir(path):
A
Alessio Sergi 已提交
213
                        msg = ' {0}'.format(path) + os.sep
214 215
                        ret.append(self.curse_add_line(msg, splittable=True))
                        ret.append(self.curse_add_line(basename, decoration='PROCESS', splittable=True))
216
                    else:
A
Alessio Sergi 已提交
217
                        msg = ' {0}'.format(basename)
218
                        ret.append(self.curse_add_line(msg, decoration='PROCESS', splittable=True))
219
                    msg = " {0}".format(args)
220
                    ret.append(self.curse_add_line(msg, splittable=True))
221
                except UnicodeEncodeError:
222
                    ret.append(self.curse_add_line("", splittable=True))
A
Alessio Sergi 已提交
223 224 225

        # Return the message with decoration
        return ret
226 227

    def sortlist(self, sortedby=None):
A
PEP 257  
Alessio Sergi 已提交
228
        """Return the stats sorted by sortedby variable."""
229
        if sortedby is None:
230 231 232 233
            # No need to sort...
            return self.stats

        sortedreverse = True
234
        if sortedby == 'name':
235 236
            sortedreverse = False

237
        if sortedby == 'io_counters':
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
            # Specific case for io_counters
            # Sum of io_r + io_w
            try:
                # Sort process by IO rate (sum IO read + IO write)
                listsorted = sorted(self.stats,
                                    key=lambda process: process[sortedby][0] -
                                    process[sortedby][2] + process[sortedby][1] -
                                    process[sortedby][3],
                                    reverse=sortedreverse)
            except Exception:
                listsorted = sorted(self.stats,
                                    key=lambda process: process['cpu_percent'],
                                    reverse=sortedreverse)
        else:
            # Others sorts
            listsorted = sorted(self.stats,
                                key=lambda process: process[sortedby],
                                reverse=sortedreverse)

        self.stats = listsorted

A
Alessio Sergi 已提交
259
        return self.stats