glances_percpu.py 4.9 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

22 23
from glances.plugins.glances_plugin import GlancesPlugin

A
flake8  
Alessio Sergi 已提交
24 25
import psutil

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

N
nicolargo 已提交
45
    def get_key(self):
A
PEP 257  
Alessio Sergi 已提交
46
        """Return the key of the list."""
N
nicolargo 已提交
47 48
        return 'cpu_number'

49
    def reset(self):
A
PEP 257  
Alessio Sergi 已提交
50
        """Reset/init the stats."""
51 52
        self.stats = []

A
Alessio Sergi 已提交
53
    def update(self):
A
PEP 257  
Alessio Sergi 已提交
54
        """Update per-CPU stats using the input method."""
55 56 57
        # Reset stats
        self.reset()

58 59
        # Grab per-CPU stats using psutil's cpu_percent(percpu=True) and
        # cpu_times_percent(percpu=True) methods
60
        if self.input_method == 'local':
61 62
            percpu_percent = psutil.cpu_percent(interval=0.0, percpu=True)
            percpu_times_percent = psutil.cpu_times_percent(interval=0.0, percpu=True)
N
nicolargo 已提交
63
            cpu_number = 0
64 65
            for cputimes in percpu_times_percent:
                for cpupercent in percpu_percent:
N
nicolargo 已提交
66 67 68
                    cpu = {'key': self.get_key(),
                           'cpu_number': str(cpu_number),
                           'total': cpupercent,
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
                           '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)
N
nicolargo 已提交
88
                cpu_number += 1
A
Alessio Sergi 已提交
89
        else:
90 91 92 93
            # Update stats using SNMP
            pass

        return self.stats
A
Alessio Sergi 已提交
94 95

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

100
        # No per CPU stat ? Exit...
D
desbma 已提交
101
        if not self.stats:
A
Alessio Sergi 已提交
102
            msg = 'PER CPU not available'
103 104 105
            ret.append(self.curse_add_line(msg, "TITLE"))
            return ret

A
Alessio Sergi 已提交
106 107
        # Build the string message
        # Header
A
Alessio Sergi 已提交
108
        msg = '{0:8}'.format('PER CPU')
A
Alessio Sergi 已提交
109 110
        ret.append(self.curse_add_line(msg, "TITLE"))

111
        # Total per-CPU usage
A
Alessio Sergi 已提交
112
        for cpu in self.stats:
113
            msg = '{0:>6}%'.format(cpu['total'])
A
Alessio Sergi 已提交
114 115
            ret.append(self.curse_add_line(msg))

116 117
        # User per-CPU
        ret.append(self.curse_new_line())
A
Alessio Sergi 已提交
118
        msg = '{0:8}'.format('user:')
119 120 121 122 123 124 125
        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())
A
Alessio Sergi 已提交
126
        msg = '{0:8}'.format('system:')
127 128 129 130 131 132 133
        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())
A
Alessio Sergi 已提交
134
        msg = '{0:8}'.format('idle:')
135 136 137
        ret.append(self.curse_add_line(msg))
        for cpu in self.stats:
            msg = '{0:>6}%'.format(cpu['idle'])
A
Alessio Sergi 已提交
138 139 140 141
            ret.append(self.curse_add_line(msg))

        # Return the message with decoration
        return ret