Correct issue with SNMP fallback mode in Python 3

This commit is contained in:
Nicolargo 2014-07-05 17:17:23 +02:00
parent 9405479f13
commit f9778982ce
9 changed files with 35 additions and 11 deletions

View File

@ -49,7 +49,7 @@ LOGGING_CFG = {
'filename': os.path.join(tempfile.gettempdir(), 'glances.log') 'filename': os.path.join(tempfile.gettempdir(), 'glances.log')
}, },
'console':{ 'console':{
'level':'ERROR', 'level':'CRITICAL',
'class':'logging.StreamHandler', 'class':'logging.StreamHandler',
'formatter': 'short' 'formatter': 'short'
} }

View File

@ -59,6 +59,9 @@ class GlancesSNMPClient(object):
ret[name.prettyPrint()] = '' ret[name.prettyPrint()] = ''
else: else:
ret[name.prettyPrint()] = val.prettyPrint() ret[name.prettyPrint()] = val.prettyPrint()
# In Python 3, prettyPrint() return 'b'linux'' instead of 'linux'
if ret[name.prettyPrint()].startswith('b\''):
ret[name.prettyPrint()] = ret[name.prettyPrint()][2:-1]
return ret return ret
def get_by_oid(self, *oid): def get_by_oid(self, *oid):
@ -93,6 +96,9 @@ class GlancesSNMPClient(object):
item[name.prettyPrint()] = '' item[name.prettyPrint()] = ''
else: else:
item[name.prettyPrint()] = val.prettyPrint() item[name.prettyPrint()] = val.prettyPrint()
# In Python 3, prettyPrint() return 'b'linux'' instead of 'linux'
if item[name.prettyPrint()].startswith('b\''):
item[name.prettyPrint()] = item[name.prettyPrint()][2:-1]
ret.append(item) ret.append(item)
return ret return ret

View File

@ -240,8 +240,10 @@ class GlancesStatsClientSNMP(GlancesStats):
oid_os_name = clientsnmp.get_by_oid("1.3.6.1.2.1.1.1.0") oid_os_name = clientsnmp.get_by_oid("1.3.6.1.2.1.1.1.0")
try: try:
self.system_name = self.get_system_name(oid_os_name['1.3.6.1.2.1.1.1.0']) self.system_name = self.get_system_name(oid_os_name['1.3.6.1.2.1.1.1.0'])
logger.info(_('SNMP system name detected: {}').format(self.system_name))
except KeyError: except KeyError:
self.system_name = None self.system_name = None
logger.warning(_('Can not detect SNMP system name'))
return ret return ret
@ -253,7 +255,12 @@ class GlancesStatsClientSNMP(GlancesStats):
return short_system_name return short_system_name
# Find the short name in the oid_to_short_os_name dict # Find the short name in the oid_to_short_os_name dict
for r,v in oid_to_short_system_name.iteritems(): try:
iteritems = oid_to_short_system_name.iteritems()
except AttributeError:
# Correct issue #386
iteritems = oid_to_short_system_name.items()
for r,v in iteritems:
if re.search(r, oid_system_name): if re.search(r, oid_system_name):
short_system_name = v short_system_name = v
break break

View File

@ -120,7 +120,7 @@ class Plugin(GlancesPlugin):
return self.stats return self.stats
# Convert SNMP stats to float # Convert SNMP stats to float
for key in self.stats.iterkeys(): for key in list(self.stats.keys()):
self.stats[key] = float(self.stats[key]) self.stats[key] = float(self.stats[key])
return self.stats return self.stats

View File

@ -19,10 +19,14 @@
"""File system plugin.""" """File system plugin."""
# System libs
import base64 import base64
# Glances libs
from glances.core.glances_globals import version, logger
from glances.plugins.glances_plugin import GlancesPlugin from glances.plugins.glances_plugin import GlancesPlugin
# PSutil lib for local grab
import psutil import psutil
# SNMP OID # SNMP OID

View File

@ -23,6 +23,7 @@
import os import os
# Import Glances libs # Import Glances libs
from glances.core.glances_globals import logger
from glances.plugins.glances_core import Plugin as CorePlugin from glances.plugins.glances_core import Plugin as CorePlugin
from glances.plugins.glances_plugin import GlancesPlugin from glances.plugins.glances_plugin import GlancesPlugin
@ -84,14 +85,20 @@ class Plugin(GlancesPlugin):
# Update stats using SNMP # Update stats using SNMP
self.stats = self.set_stats_snmp(snmp_oid=snmp_oid) self.stats = self.set_stats_snmp(snmp_oid=snmp_oid)
self.stats['cpucore'] = nb_log_core
if self.stats['min1'] == '': if self.stats['min1'] == '':
self.reset() self.reset()
return self.stats return self.stats
for key in self.stats.iterkeys(): # Python 3 return a dict like:
self.stats[key] = float(self.stats[key]) # {'min1': "b'0.08'", 'min5': "b'0.12'", 'min15': "b'0.15'"}
try:
iteritems = self.stats.iteritems()
except AttributeError:
iteritems = self.stats.items()
for k,v in iteritems:
self.stats[k] = float(v)
self.stats['cpucore'] = nb_log_core
return self.stats return self.stats

View File

@ -132,7 +132,7 @@ class Plugin(GlancesPlugin):
self.reset() self.reset()
return self.stats return self.stats
for key in self.stats.iterkeys(): for key in list(self.stats.keys()):
if self.stats[key] != '': if self.stats[key] != '':
self.stats[key] = float(self.stats[key]) * 1024 self.stats[key] = float(self.stats[key]) * 1024

View File

@ -102,7 +102,7 @@ class Plugin(GlancesPlugin):
self.reset() self.reset()
return self.stats return self.stats
for key in self.stats.iterkeys(): for key in list(self.stats.keys()):
if self.stats[key] != '': if self.stats[key] != '':
self.stats[key] = float(self.stats[key]) * 1024 self.stats[key] = float(self.stats[key]) * 1024

View File

@ -121,7 +121,7 @@ class GlancesPlugin(object):
for item in snmpresult: for item in snmpresult:
item_stats = {} item_stats = {}
item_key = None item_key = None
for key in snmp_oid.iterkeys(): for key in list(snmp_oid.keys()):
oid = snmp_oid[key] + '.' + str(index) oid = snmp_oid[key] + '.' + str(index)
if oid in item: if oid in item:
if item_key is None: if item_key is None:
@ -136,7 +136,7 @@ class GlancesPlugin(object):
snmpresult = clientsnmp.get_by_oid(*snmp_oid.values()) snmpresult = clientsnmp.get_by_oid(*snmp_oid.values())
# Build the internal dict with the SNMP result # Build the internal dict with the SNMP result
for key in snmp_oid.iterkeys(): for key in list(snmp_oid.keys()):
ret[key] = snmpresult[snmp_oid[key]] ret[key] = snmpresult[snmp_oid[key]]
return ret return ret