glances_processlist.py 10.1 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/>.

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

N
Nicolas Hennion 已提交
24
# Import Glances libs
N
Nicolas Hennion 已提交
25
from glances.core.glances_globals import glances_processes
A
Alessio Sergi 已提交
26
from glances.plugins.glances_plugin import GlancesPlugin
A
Alessio Sergi 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47


class Plugin(GlancesPlugin):
    """
    Glances's processes Plugin

    stats is a list
    """

    def __init__(self):
        GlancesPlugin.__init__(self)

        # 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

48
        # Note: 'glances_processes' is already init in the glances_processes.py script
49 50 51 52 53 54 55

    def update(self):
        """
        Update processes stats
        """

        # Note: Update is done in the processcount plugin
56 57

        # Just return the processes list
58 59
        self.stats = glances_processes.getlist()

60
        return self.stats
61

A
Alessio Sergi 已提交
62 63 64 65 66 67 68 69
    def msg_curse(self, args=None):
        """
        Return the dict to display in the curse interface
        """

        # Init the return message
        ret = []

70
        # Only process if stats exist and display plugin enable...
71
        if self.stats == [] or args.disable_process:
72 73
            return ret

A
Alessio Sergi 已提交
74
        # Compute the sort key
75 76 77
        try:
            args.process_sorted_by
        except AttributeError:
A
Alessio Sergi 已提交
78
            args.process_sorted_by = glances_processes.getsortkey()
79
        if args.process_sorted_by == 'auto':
N
Nicolas Hennion 已提交
80
            process_sort_key = glances_processes.getsortkey()
A
Alessio Sergi 已提交
81 82
        else:
            process_sort_key = args.process_sorted_by
83
        sort_style = 'SORT'
A
Alessio Sergi 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

        # Header
        msg = "{0:>6}".format(_("CPU%"))
        ret.append(self.curse_add_line(msg, sort_style if process_sort_key == 'cpu_percent' else 'DEFAULT'))
        msg = "{0:>6}".format(_("MEM%"))
        ret.append(self.curse_add_line(msg, sort_style if process_sort_key == 'memory_percent' else 'DEFAULT'))
        msg = "{0:>6}".format(_("VIRT"))
        ret.append(self.curse_add_line(msg, optional=True))
        msg = "{0:>6}".format(_("RES"))
        ret.append(self.curse_add_line(msg, optional=True))
        msg = "{0:>6}".format(_("PID"))
        ret.append(self.curse_add_line(msg, optional=True))
        msg = " {0:10}".format(_("USER"))
        ret.append(self.curse_add_line(msg, optional=True))
        msg = "{0:>3}".format(_("NI"))
        ret.append(self.curse_add_line(msg, optional=True))
        msg = " {0:1}".format(_("S"))
        ret.append(self.curse_add_line(msg, optional=True))
        msg = "{0:>9}".format(_("TIME+"))
        ret.append(self.curse_add_line(msg, optional=True))
        msg = "{0:>6}".format(_("IOR/s"))
        ret.append(self.curse_add_line(msg, sort_style if process_sort_key == 'io_counters' else 'DEFAULT', optional=True))
        msg = "{0:>6}".format(_("IOW/s"))
        ret.append(self.curse_add_line(msg, sort_style if process_sort_key == 'io_counters' else 'DEFAULT', optional=True))
        msg = " {0:8}".format(_("Command"))
        ret.append(self.curse_add_line(msg, optional=True))

        # Trying to display proc time
        tag_proc_time = True

        # Loop over processes (sorted by the sort key previously compute)
115
        for p in self.sortlist(process_sort_key):
A
Alessio Sergi 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
            ret.append(self.curse_new_line())
            # CPU
            msg = "{0:>6}".format(format(p['cpu_percent'], '>5.1f'))
            ret.append(self.curse_add_line(msg,
                                           self.get_alert(p['cpu_percent'], header="cpu")))
            # MEM
            msg = "{0:>6}".format(format(p['memory_percent'], '>5.1f'))
            ret.append(self.curse_add_line(msg,
                                           self.get_alert(p['memory_percent'], header="mem")))
            # VMS
            msg = "{0:>6}".format(self.auto_unit(p['memory_info'][1], low_precision=False))
            ret.append(self.curse_add_line(msg, optional=True))
            # RSS
            msg = "{0:>6}".format(self.auto_unit(p['memory_info'][0], low_precision=False))
            ret.append(self.curse_add_line(msg, optional=True))
            # PID
            msg = "{0:>6}".format(p['pid'])
            ret.append(self.curse_add_line(msg, optional=True))
            # USER
            msg = " {0:9}".format(p['username'][:9])
            ret.append(self.curse_add_line(msg, optional=True))
            # NICE
138 139 140 141 142 143
            nice = p['nice']
            msg = " {0:>3}".format(nice)
            if nice != 0:
                ret.append(self.curse_add_line(msg, decoration='NICE', optional=True))
            else:
                ret.append(self.curse_add_line(msg, optional=True))
A
Alessio Sergi 已提交
144
            # STATUS
145 146 147
            status = p['status']
            msg = " {0:>1}".format(status)
            if status == 'R':
148
                ret.append(self.curse_add_line(msg, decoration='STATUS', optional=True))
149 150
            else:
                ret.append(self.curse_add_line(msg, optional=True))
A
Alessio Sergi 已提交
151
            # TIME+
152
            if tag_proc_time:
A
Alessio Sergi 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166
                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:
                    msg = "{0}:{1}.{2}".format(str(dtime.seconds // 60 % 60),
                                               str(dtime.seconds % 60).zfill(2),
                                               str(dtime.microseconds)[:2].zfill(2))
            else:
                msg = " "
            msg = "{0:>9}".format(msg)
            ret.append(self.curse_add_line(msg, optional=True))
N
Nicolas Hennion 已提交
167
            # IO read/write
168
            if 'io_counters' in p:
N
Nicolas Hennion 已提交
169 170
                # IO read
                io_rs = (p['io_counters'][0] - p['io_counters'][2]) / p['time_since_update']
171
                if io_rs == 0:
N
Nicolas Hennion 已提交
172 173 174 175 176 177
                    msg = "{0:>6}".format("0")
                else:
                    msg = "{0:>6}".format(self.auto_unit(io_rs, low_precision=False))
                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']
178
                if io_ws == 0:
N
Nicolas Hennion 已提交
179 180 181 182
                    msg = "{0:>6}".format("0")
                else:
                    msg = "{0:>6}".format(self.auto_unit(io_ws, low_precision=False))
                ret.append(self.curse_add_line(msg, optional=True))
A
Alessio Sergi 已提交
183
            else:
N
Nicolas Hennion 已提交
184 185 186
                msg = "{0:>6}".format("?")
                ret.append(self.curse_add_line(msg, optional=True))
                ret.append(self.curse_add_line(msg, optional=True))
A
Alessio Sergi 已提交
187
            # Command line
188 189 190 191 192 193 194 195 196 197 198 199 200 201
            # If no command line for the process is available, fallback to
            # the bare process name instead
            cmdline = p['cmdline']
            if cmdline == "":
                msg = " {0}".format(p['name'])
                ret.append(self.curse_add_line(msg, optional=True, splittable=True))
            else:
                try:
                    cmd = cmdline.split()[0]
                    args = ' '.join(cmdline.split()[1:])
                    path, basename = os.path.split(cmd)
                    if os.path.isdir(path):
                        msg = " {0}".format(path) + os.sep
                        ret.append(self.curse_add_line(msg, optional=True, splittable=True))
202
                        ret.append(self.curse_add_line(basename, decoration='PROCESS', optional=True, splittable=True))
203 204
                    else:
                        msg = " {0}".format(basename)
205 206 207
                        ret.append(self.curse_add_line(msg, decoration='PROCESS', optional=True, splittable=True))
                    msg = " {0}".format(args)
                    ret.append(self.curse_add_line(msg, optional=True, splittable=True))
208 209
                except UnicodeEncodeError:
                    ret.append(self.curse_add_line("", optional=True, splittable=True))
A
Alessio Sergi 已提交
210 211 212

        # Return the message with decoration
        return ret
213 214 215 216 217

    def sortlist(self, sortedby=None):
        """
        Return the self.stats sorted by sortedby
        """
218
        if sortedby is None:
219 220 221 222
            # No need to sort...
            return self.stats

        sortedreverse = True
223
        if sortedby == 'name':
224 225
            sortedreverse = False

226
        if sortedby == 'io_counters':
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
            # 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 已提交
248
        return self.stats