glances_percpu.py 4.6 KB
Newer Older
A
Alessio Sergi 已提交
1 2
# -*- coding: utf-8 -*-
#
3
# This file is part of Glances.
A
Alessio Sergi 已提交
4
#
5
# Copyright (C) 2015 Nicolargo <nicolas@nicolargo.com>
A
Alessio Sergi 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18
#
# 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 已提交
19 20

"""Per-CPU plugin."""
A
Alessio Sergi 已提交
21

A
Alessio Sergi 已提交
22 23
import psutil

24 25
from glances.plugins.glances_plugin import GlancesPlugin

A
Alessio Sergi 已提交
26 27

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

29
    """Glances per-CPU plugin.
A
Alessio Sergi 已提交
30

31 32
    'stats' is a list of dictionaries that contain the utilization percentages
    for each CPU.
A
Alessio Sergi 已提交
33 34
    """

35
    def __init__(self, args=None):
A
PEP 257  
Alessio Sergi 已提交
36
        """Init the plugin."""
37
        GlancesPlugin.__init__(self, args=args)
A
Alessio Sergi 已提交
38 39 40 41

        # We want to display the stat in the curse interface
        self.display_curse = True

N
Nicolas Hennion 已提交
42
        # Init stats
43
        self.reset()
N
Nicolas Hennion 已提交
44

45
    def reset(self):
A
PEP 257  
Alessio Sergi 已提交
46
        """Reset/init the stats."""
47 48
        self.stats = []

A
Alessio Sergi 已提交
49
    def update(self):
A
PEP 257  
Alessio Sergi 已提交
50
        """Update per-CPU stats using the input method."""
51 52 53
        # Reset stats
        self.reset()

54 55
        # Grab per-CPU stats using psutil's cpu_percent(percpu=True) and
        # cpu_times_percent(percpu=True) methods
56
        if self.get_input() == 'local':
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
            percpu_percent = psutil.cpu_percent(interval=0.0, percpu=True)
            percpu_times_percent = psutil.cpu_times_percent(interval=0.0, percpu=True)
            for cputimes in percpu_times_percent:
                for cpupercent in percpu_percent:
                    cpu = {'total': cpupercent,
                           'user': cputimes.user,
                           'system': cputimes.system,
                           'idle': cputimes.idle}
                    # The following stats are for API purposes only
                    if hasattr(cputimes, 'nice'):
                        cpu['nice'] = cputimes.nice
                    if hasattr(cputimes, 'iowait'):
                        cpu['iowait'] = cputimes.iowait
                    if hasattr(cputimes, 'irq'):
                        cpu['irq'] = cputimes.irq
                    if hasattr(cputimes, 'softirq'):
                        cpu['softirq'] = cputimes.softirq
                    if hasattr(cputimes, 'steal'):
                        cpu['steal'] = cputimes.steal
                    if hasattr(cputimes, 'guest'):
                        cpu['guest'] = cputimes.guest
                    if hasattr(cputimes, 'guest_nice'):
                        cpu['guest_nice'] = cputimes.guest_nice
                self.stats.append(cpu)
A
Alessio Sergi 已提交
81
        else:
82 83 84 85
            # Update stats using SNMP
            pass

        return self.stats
A
Alessio Sergi 已提交
86 87

    def msg_curse(self, args=None):
A
PEP 257  
Alessio Sergi 已提交
88
        """Return the dict to display in the curse interface."""
A
Alessio Sergi 已提交
89 90 91
        # Init the return message
        ret = []

92
        # No per CPU stat ? Exit...
D
desbma 已提交
93
        if not self.stats:
A
Alessio Sergi 已提交
94
            msg = _("PER CPU not available")
95 96 97
            ret.append(self.curse_add_line(msg, "TITLE"))
            return ret

A
Alessio Sergi 已提交
98 99
        # Build the string message
        # Header
A
Alessio Sergi 已提交
100
        msg = '{0:8}'.format(_("PER CPU"))
A
Alessio Sergi 已提交
101 102
        ret.append(self.curse_add_line(msg, "TITLE"))

103
        # Total per-CPU usage
A
Alessio Sergi 已提交
104
        for cpu in self.stats:
105
            msg = '{0:>6}%'.format(cpu['total'])
A
Alessio Sergi 已提交
106 107
            ret.append(self.curse_add_line(msg))

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        # User per-CPU
        ret.append(self.curse_new_line())
        msg = '{0:8}'.format(_("user:"))
        ret.append(self.curse_add_line(msg))
        for cpu in self.stats:
            msg = '{0:>6}%'.format(cpu['user'])
            ret.append(self.curse_add_line(msg, self.get_alert(cpu['user'], header="user")))

        # System per-CPU
        ret.append(self.curse_new_line())
        msg = '{0:8}'.format(_("system:"))
        ret.append(self.curse_add_line(msg))
        for cpu in self.stats:
            msg = '{0:>6}%'.format(cpu['system'])
            ret.append(self.curse_add_line(msg, self.get_alert(cpu['system'], header="system")))

        # Idle per-CPU
        ret.append(self.curse_new_line())
        msg = '{0:8}'.format(_("idle:"))
        ret.append(self.curse_add_line(msg))
        for cpu in self.stats:
            msg = '{0:>6}%'.format(cpu['idle'])
A
Alessio Sergi 已提交
130 131 132 133
            ret.append(self.curse_add_line(msg))

        # Return the message with decoration
        return ret