Replace py-cpuinfo by PsUtil and home made methods

pull/1872/head
Nicolas Hennion 2021-05-24 18:34:48 +02:00
parent 783e6e988b
commit 04c61576ad
4 changed files with 32 additions and 32 deletions

View File

@ -2,7 +2,7 @@
#
# This file is part of Glances.
#
# Copyright (C) 2019 Nicolargo <nicolas@nicolargo.com>
# Copyright (C) 2021 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
@ -29,12 +29,14 @@ class CpuPercent(object):
"""Get and store the CPU percent."""
def __init__(self, cached_time=1):
self.cpu_info = {}
self.cpu_percent = 0
self.percpu_percent = []
# cached_time is the minimum time interval between stats updates
# since last update is passed (will retrieve old cached info instead)
self.cached_time = 0
self.timer_cpu_info = Timer(0)
self.timer_cpu = Timer(0)
self.timer_percpu = Timer(0)
@ -50,6 +52,23 @@ class CpuPercent(object):
else:
return self.__get_cpu()
def get_info(self):
"""Get additional informations about the CPU"""
# Never update more than 1 time per cached_time
if self.timer_cpu_info.finished():
# Get the CPU name from the /proc/cpuinfo file
# @TODO: Multisystem...
try:
self.cpu_info['cpu_name'] = open('/proc/cpuinfo', 'r').readlines()[4].split(':')[1][1:-2]
except:
self.cpu_info['cpu_name'] = 'CPU'
# Get the CPU freq current/max
self.cpu_info['cpu_hz_current'] = psutil.cpu_freq().current
self.cpu_info['cpu_hz'] = psutil.cpu_freq().max
# Reset timer for cache
self.timer_cpu_info = Timer(self.cached_time)
return self.cpu_info
def __get_cpu(self):
"""Update and/or return the CPU using the psutil library."""
# Never update more than 1 time per cached_time

View File

@ -27,15 +27,6 @@ from glances.plugins.glances_plugin import GlancesPlugin
import psutil
# Import plugin specific dependency
try:
from cpuinfo import cpuinfo
except ImportError as e:
cpuinfo_tag = False
logger.warning("Missing Python Lib ({}), Quicklook plugin will not display CPU info".format(e))
else:
cpuinfo_tag = True
# Define the history items list
# All items in this list will be historised if the --enable-history tag is set
@ -79,6 +70,7 @@ class Plugin(GlancesPlugin):
# Get the latest CPU percent value
stats['cpu'] = cpu_percent.get()
stats['percpu'] = cpu_percent.get(percpu=True)
# Use the psutil lib for the memory (virtual and swap)
stats['mem'] = psutil.virtual_memory().percent
try:
@ -86,30 +78,17 @@ class Plugin(GlancesPlugin):
except RuntimeError:
# Correct issue in Illumos OS (see #1767)
stats['swap'] = None
# Get additional information
logger.info(cpu_percent.get_info())
stats['cpu_name'] = cpu_percent.get_info()['cpu_name']
stats['cpu_hz_current'] = self._mhz_to_hz(cpu_percent.get_info()['cpu_hz_current'])
stats['cpu_hz'] = self._mhz_to_hz(cpu_percent.get_info()['cpu_hz'])
elif self.input_method == 'snmp':
# Not available
pass
# Optionnaly, get the CPU name/frequency
# thanks to the cpuinfo lib: https://github.com/workhorsy/py-cpuinfo
if cpuinfo_tag:
cpu_info = cpuinfo.get_cpu_info()
# Check cpu_info (issue #881)
if cpu_info is not None:
# Use brand_raw if the key exist (issue #1685)
if cpu_info.get('brand_raw') is not None:
stats['cpu_name'] = cpu_info.get('brand_raw', 'CPU')
else:
stats['cpu_name'] = cpu_info.get('brand', 'CPU')
if 'hz_actual_raw' in cpu_info:
stats['cpu_hz_current'] = cpu_info['hz_actual_raw'][0]
elif 'hz_actual' in cpu_info:
stats['cpu_hz_current'] = cpu_info['hz_actual'][0]
if 'hz_advertised_raw' in cpu_info:
stats['cpu_hz'] = cpu_info['hz_advertised_raw'][0]
elif 'hz_advertised' in cpu_info:
stats['cpu_hz'] = cpu_info['hz_advertised'][0]
# Update the stats
self.stats = stats
@ -208,3 +187,7 @@ class Plugin(GlancesPlugin):
def _hz_to_ghz(self, hz):
"""Convert Hz to Ghz."""
return hz / 1000000000.0
def _mhz_to_hz(self, hz):
"""Convert Mhz to Hz."""
return hz * 1000000.0

View File

@ -19,7 +19,6 @@ paho-mqtt
pika
potsdb
prometheus_client
py-cpuinfo<=3.3.0
pygal
pymdstat
pysnmp

View File

@ -54,7 +54,6 @@ def get_install_extras_require():
'action': ['chevron'],
'browser': ['zeroconf==0.19.1' if PY2 else 'zeroconf>=0.19.1'],
'cloud': ['requests'],
'cpuinfo': ['py-cpuinfo<=4.0.0'],
'docker': ['docker>=2.0.0'],
'export': ['bernhard', 'cassandra-driver', 'couchdb', 'elasticsearch',
'graphitesender', 'influxdb>=1.0.0', 'kafka-python', 'pika',