Add CPU for WIndows OS

This commit is contained in:
Nicolargo 2014-06-26 23:15:18 +02:00
parent 5252eab9c0
commit 9dda132c5e
5 changed files with 101 additions and 48 deletions

View File

@ -379,7 +379,7 @@ class GlancesCurses(object):
if plugin_stats['line'] not in self.line_to_y:
self.line_to_y[plugin_stats['line']] = plugin_stats['line']
display_y = self.line_to_y[plugin_stats['line']]
# Display
x = display_x
y = display_y

View File

@ -29,7 +29,8 @@ import psutil
# percentages of idle CPU time: .1.3.6.1.4.1.2021.11.11.0
snmp_oid = {'default': {'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'}}
'idle': '1.3.6.1.4.1.2021.11.11.0'},
'windows': {'percent': '1.3.6.1.2.1.25.3.3.1.2'}}
class Plugin(GlancesPlugin):
@ -90,15 +91,33 @@ class Plugin(GlancesPlugin):
self.stats[cpu] = getattr(cputimespercent, cpu)
elif self.get_input() == 'snmp':
# Update stats using SNMP
try:
self.stats = self.set_stats_snmp(snmp_oid=snmp_oid[self.get_short_system_name()])
except KeyError:
self.stats = self.set_stats_snmp(snmp_oid=snmp_oid['default'])
if self.stats['user'] == '':
self.reset()
return self.stats
if self.get_short_system_name() == 'windows':
# Windows
# You can find the CPU utilization of windows system by querying the oid
# Give also the number of core (number of element in the table)
# print snmp_oid[self.get_short_system_name()]
try:
self.stats = self.set_stats_snmp(snmp_oid=snmp_oid[self.get_short_system_name()],
bulk=True)
except KeyError:
self.reset()
# TODO: iter through CPU... startswith('percent')
self.stats['idle'] = self.stats['percent.3']
else:
# Default behavor
try:
self.stats = self.set_stats_snmp(snmp_oid=snmp_oid[self.get_short_system_name()])
except KeyError:
self.stats = self.set_stats_snmp(snmp_oid=snmp_oid['default'])
if self.stats['idle'] == '':
self.reset()
return self.stats
# Convert SNMP stats to float
for key in self.stats.iterkeys():
self.stats[key] = float(self.stats[key])

View File

@ -115,8 +115,7 @@ class Plugin(GlancesPlugin):
elif self.get_input() == 'snmp':
# Update stats using SNMP
# SNMP bulk command to get all file system in one shot
# SNMP bulk command to get all file system in one shot
try:
fs_stat = self.set_stats_snmp(snmp_oid=snmp_oid[self.get_short_system_name()],
bulk=True)

View File

@ -30,13 +30,16 @@ import psutil
# Total RAM Shared: .1.3.6.1.4.1.2021.4.13.0
# Total RAM Buffered: .1.3.6.1.4.1.2021.4.14.0
# Total Cached Memory: .1.3.6.1.4.1.2021.4.15.0
snmp_oid = {'total': '1.3.6.1.4.1.2021.4.5.0',
# 'used': '1.3.6.1.4.1.2021.4.6.0',
'free': '1.3.6.1.4.1.2021.4.11.0',
'shared': '1.3.6.1.4.1.2021.4.13.0',
'buffers': '1.3.6.1.4.1.2021.4.14.0',
'cached': '1.3.6.1.4.1.2021.4.15.0'}
# Note: For Windows, stats are in the FS table
snmp_oid = {'default': {'total': '1.3.6.1.4.1.2021.4.5.0',
'free': '1.3.6.1.4.1.2021.4.11.0',
'shared': '1.3.6.1.4.1.2021.4.13.0',
'buffers': '1.3.6.1.4.1.2021.4.14.0',
'cached': '1.3.6.1.4.1.2021.4.15.0'},
'windows': {'mnt_point': '1.3.6.1.2.1.25.2.3.1.3',
'alloc_unit': '1.3.6.1.2.1.25.2.3.1.4',
'size': '1.3.6.1.2.1.25.2.3.1.5',
'used': '1.3.6.1.2.1.25.2.3.1.6'}}
class Plugin(GlancesPlugin):
@ -88,7 +91,7 @@ class Plugin(GlancesPlugin):
# cached: (Linux, BSD): cache for various things.
# wired: (BSD, OSX): memory that is marked to always stay in RAM. It is never moved to disk.
# shared: (BSD): memory that may be simultaneously accessed by multiple processes.
self.stats = {}
self.reset()
for mem in ['total', 'available', 'percent', 'used', 'free',
'active', 'inactive', 'buffers', 'cached',
'wired', 'shared']:
@ -106,24 +109,42 @@ class Plugin(GlancesPlugin):
self.stats['used'] = self.stats['total'] - self.stats['free']
elif self.get_input() == 'snmp':
# Update stats using SNMP
self.stats = self.set_stats_snmp(snmp_oid=snmp_oid)
if self.get_short_system_name() == 'windows':
# Mem stats for Windows OS are stored in the FS table
try:
fs_stat = self.set_stats_snmp(snmp_oid=snmp_oid[self.get_short_system_name()],
bulk=True)
except KeyError:
self.reset()
else:
for fs in fs_stat:
# Memory stats are grabed in the same OID table (ignore it)
if fs == 'Virtual Memory':
self.stats['total'] = int(fs_stat[fs]['size']) * int(fs_stat[fs]['alloc_unit'])
self.stats['used'] = int(fs_stat[fs]['used']) * int(fs_stat[fs]['alloc_unit'])
self.stats['percent'] = float(self.stats['used'] * 100 / self.stats['total'])
self.stats['free'] = self.stats['total'] - self.stats['used']
break
else:
# Default behavor for others OS
self.stats = self.set_stats_snmp(snmp_oid=snmp_oid['default'])
if self.stats['total'] == '':
self.reset()
return self.stats
if self.stats['total'] == '':
self.reset()
return self.stats
for key in self.stats.iterkeys():
if self.stats[key] != '':
self.stats[key] = float(self.stats[key]) * 1024
for key in self.stats.iterkeys():
if self.stats[key] != '':
self.stats[key] = float(self.stats[key]) * 1024
# Use the 'free'/htop calculation
self.stats['free'] = self.stats['free'] - self.stats['total'] + (self.stats['buffers'] + self.stats['cached'])
# Use the 'free'/htop calculation
self.stats['free'] = self.stats['free'] - self.stats['total'] + (self.stats['buffers'] + self.stats['cached'])
# used=total-free
self.stats['used'] = self.stats['total'] - self.stats['free']
# used=total-free
self.stats['used'] = self.stats['total'] - self.stats['free']
# percent: the percentage usage calculated as (total - available) / total * 100.
self.stats['percent'] = float((self.stats['total'] - self.stats['free']) / self.stats['total'] * 100)
# percent: the percentage usage calculated as (total - available) / total * 100.
self.stats['percent'] = float((self.stats['total'] - self.stats['free']) / self.stats['total'] * 100)
return self.stats

View File

@ -105,22 +105,36 @@ class GlancesPlugin(object):
# Bulk request
snmpresult = clientsnmp.getbulk_by_oid(0, 10, *snmp_oid.values())
# Build the internal dict with the SNMP result
# key is the first item in the snmp_oid
index = 1
for item in snmpresult:
item_stats = {}
item_key = None
for key in snmp_oid.iterkeys():
oid = snmp_oid[key] + '.' + str(index)
if oid in item:
if item_key is None:
item_key = item[oid]
else:
item_stats[key] = item[oid]
if item_stats != {}:
ret[item_key] = item_stats
index += 1
if len(snmp_oid) == 1:
# Bulk command for only one OID
# Note: key is the item indexed but the OID result
for item in snmpresult:
if item.keys()[0].startswith(snmp_oid.values()[0]):
ret[snmp_oid.keys()[0] + item.keys()[0].split(snmp_oid.values()[0])[1]] = item.values()[0]
else:
# Build the internal dict with the SNMP result
# Note: key is the first item in the snmp_oid
index = 1
for item in snmpresult:
item_stats = {}
item_key = None
for key in snmp_oid.iterkeys():
oid = snmp_oid[key] + '.' + str(index)
if oid in item:
if item_key is None:
item_key = item[oid]
else:
item_stats[key] = item[oid]
if item_stats != {}:
ret[item_key] = item_stats
index += 1
# if '1.3.6.1.2.1.25.3.3.1.2' in snmp_oid.values():
# # if '1.3.6.1.2.1.25.2.3.1.3' in snmp_oid.values():
# print snmp_oid
# print snmpresult
# print ret
else:
# Simple get request
snmpresult = clientsnmp.get_by_oid(*snmp_oid.values())