glances_core.py 2.4 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 19
#
# 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
"""CPU core plugin."""

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

A
PEP 257  
Alessio Sergi 已提交
29
    """Glances CPU core plugin.
A
PEP 257  
Alessio Sergi 已提交
30 31

    Get stats about CPU core number.
A
Alessio Sergi 已提交
32 33 34 35

    stats is integer (number of core)
    """

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

        # We dot not want to display the stat in the curse interface
        # The core number is displayed by the load plugin
        self.display_curse = False

44 45
        # Init the stat
        self.reset()
N
Nicolas Hennion 已提交
46

47
    def reset(self):
A
PEP 257  
Alessio Sergi 已提交
48
        """Reset/init the stat using the input method."""
49 50
        self.stats = {}

51
    def update(self):
A
PEP 257  
Alessio Sergi 已提交
52
        """Update core stats.
A
Alessio Sergi 已提交
53

A
PEP 257  
Alessio Sergi 已提交
54 55
        Stats is a dict (with both physical and log cpu number) instead of a integer.
        """
56 57 58
        # Reset the stats
        self.reset()

59
        if self.input_method == 'local':
60 61 62 63 64 65 66 67 68 69 70 71 72
            # Update stats using the standard system lib

            # The PSUtil 2.0 include psutil.cpu_count() and psutil.cpu_count(logical=False)
            # Return a dict with:
            # - phys: physical cores only (hyper thread CPUs are excluded)
            # - log: logical CPUs in the system
            # Return None if undefine
            try:
                self.stats["phys"] = psutil.cpu_count(logical=False)
                self.stats["log"] = psutil.cpu_count()
            except NameError:
                self.reset()

73
        elif self.input_method == 'snmp':
74 75 76
            # Update stats using SNMP
            # http://stackoverflow.com/questions/5662467/how-to-find-out-the-number-of-cpus-using-snmp
            pass
A
Alessio Sergi 已提交
77 78

        return self.stats