提交 d68c991a 编写于 作者: N Nicolargo

SNMP input: Uptime and CPU

上级 66525c6e
......@@ -122,18 +122,16 @@ class GlancesClient():
# Test if SNMP is available on the server side
clientsnmp = GlancesSNMPClient()
try:
# !!! Simple request with system name
# !!! Had to have a standard method to check SNMP server
clientsnmp.get_by_oid("1.3.6.1.2.1.1.5.0")
except:
# !!! Simple request with system name
# !!! Had to have a standard method to check SNMP server
print(_("Trying {0}...").format(self.get_mode()))
if (clientsnmp.get_by_oid("1.3.6.1.2.1.1.5.0") == {}):
print(_("Error: Connection to {0} server failed").format(self.get_mode()))
sys.exit(2)
from glances.core.glances_stats import GlancesStatsClientSNMP
print(_("Fallback to {0}").format(self.get_mode()))
# Init stats
self.stats = GlancesStatsClientSNMP()
......
......@@ -24,6 +24,18 @@ import psutil
from glances.plugins.glances_plugin import GlancesPlugin
# SNMP OID
# percentage of user CPU time: .1.3.6.1.4.1.2021.11.9.0
# raw user cpu time: .1.3.6.1.4.1.2021.11.50.0
# percentages of system CPU time: .1.3.6.1.4.1.2021.11.10.0
# raw system cpu time: .1.3.6.1.4.1.2021.11.52.0
# percentages of idle CPU time: .1.3.6.1.4.1.2021.11.11.0
# raw idle cpu time: .1.3.6.1.4.1.2021.11.53.0
# raw nice cpu time: .1.3.6.1.4.1.2021.11.51.0
snmp_oid = { 'user': '1.3.6.1.4.1.2021.11.9.0',
'system': '1.3.6.1.4.1.2021.11.10.0',
'idle': '1.3.6.1.4.1.2021.11.11.0',
'user_raw': '1.3.6.1.4.1.2021.11.50.0' }
class Plugin(GlancesPlugin):
"""
......@@ -46,37 +58,51 @@ class Plugin(GlancesPlugin):
# Init stats
self.first_call = True
self.reset()
def reset(self):
"""
Reset/init the stats
"""
self.stats = {}
def update(self):
def update(self, input='local'):
"""
Update CPU stats
Update CPU stats using the input method
Input method could be: local (mandatory) or snmp (optionnal)
"""
# Grab CPU using the PSUtil cpu_times_percent method
cputimespercent = psutil.cpu_times_percent(interval=0.0, percpu=False)
# Get all possible value for CPU stats
# user
# system
# idle
# nice (UNIX)
# iowait (Linux)
# irq (Linux, FreeBSD)
# softirq (Linux)
# steal (Linux >= 2.6.11)
# The following stats are returned by the API but not displayed in the UI:
# guest (Linux >= 2.6.24)
# guest_nice (Linux >= 3.2.0)
cpu_stats = {}
for cpu in ['user', 'system', 'idle', 'nice',
'iowait', 'irq', 'softirq', 'steal',
'guest', 'guest_nice']:
if hasattr(cputimespercent, cpu):
cpu_stats[cpu] = getattr(cputimespercent, cpu)
# Set the global variable to the new stats
self.stats = cpu_stats
# Reset stats
self.reset()
if input == 'local':
# Update stats using the standard system lib
# Grab CPU using the PSUtil cpu_times_percent method
cputimespercent = psutil.cpu_times_percent(interval=0.0, percpu=False)
# Get all possible value for CPU stats
# user
# system
# idle
# nice (UNIX)
# iowait (Linux)
# irq (Linux, FreeBSD)
# softirq (Linux)
# steal (Linux >= 2.6.11)
# The following stats are returned by the API but not displayed in the UI:
# guest (Linux >= 2.6.24)
# guest_nice (Linux >= 3.2.0)
for cpu in ['user', 'system', 'idle', 'nice',
'iowait', 'irq', 'softirq', 'steal',
'guest', 'guest_nice']:
if hasattr(cputimespercent, cpu):
self.stats[cpu] = getattr(cputimespercent, cpu)
elif input == 'snmp':
# Update stats using SNMP
self.stats = self.set_stats_snmp(snmp_oid=snmp_oid)
for key in self.stats.iterkeys():
self.stats[key] = float(self.stats[key])
return self.stats
......@@ -147,7 +173,10 @@ class Plugin(GlancesPlugin):
# Idles CPU
# ret.append(self.curse_add_line(" ", optional=True))
if 'idle' in self.stats:
msg = " {0:8}".format(_("idle:"))
msg = ""
if 'nice' in self.stats:
msg = " "
msg += "{0:8}".format(_("idle:"))
ret.append(self.curse_add_line(msg, optional=True))
msg = "{0}".format(format(self.stats['idle'] / 100, '>6.1%'))
ret.append(self.curse_add_line(msg, optional=True))
......
......@@ -92,6 +92,7 @@ class Plugin(GlancesPlugin):
else:
self.stats['os_version'] = ""
elif input == 'snmp':
# Update stats using SNMP
self.stats = self.set_stats_snmp(snmp_oid=snmp_oid)
return self.stats
......
......@@ -18,7 +18,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import system libs
from datetime import datetime
from datetime import datetime, timedelta
# Check for psutil already done in the glances_core script
import psutil
......@@ -26,6 +26,8 @@ import psutil
# Import Glances libs
from glances.plugins.glances_plugin import GlancesPlugin
# SNMP OID
snmp_oid = { '_uptime': '1.3.6.1.2.1.1.3.0' }
class Plugin(GlancesPlugin):
"""
......@@ -46,15 +48,41 @@ class Plugin(GlancesPlugin):
self.column_curse = -1
# Enter -1 to diplay bottom
self.line_curse = 0
# Init the stats
self.reset()
def update(self):
def reset(self):
"""
Update uptime stat
Reset/init the stats
"""
uptime = datetime.now() - datetime.fromtimestamp(psutil.boot_time())
self.stats = {}
# Convert uptime to string (because datetime is not JSONifi)
self.stats = str(uptime).split('.')[0]
def update(self, input='local'):
"""
Update uptime stat using the input method
Input method could be: local (mandatory) or snmp (optionnal)
"""
# Reset stats
self.reset()
if input == 'local':
# Update stats using the standard system lib
uptime = datetime.now() - datetime.fromtimestamp(psutil.boot_time())
# Convert uptime to string (because datetime is not JSONifi)
self.stats = str(uptime).split('.')[0]
elif input == 'snmp':
# Update stats using SNMP
uptime = self.set_stats_snmp(snmp_oid=snmp_oid)['_uptime']
try:
# In hundredths of seconds
self.stats = str(timedelta(seconds=int(uptime) / 100))
except:
pass
# Return the result
return self.stats
def msg_curse(self, args=None):
"""
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册