SNMP client: Network interface is OK but high CPU consumption

pull/354/head
Nicolargo 2014-05-04 23:05:45 +02:00
parent 8bb9244bb9
commit 5cbe52aa39
1 changed files with 45 additions and 21 deletions

View File

@ -28,7 +28,11 @@ from glances.core.glances_timer import getTimeSinceLastUpdate
from glances.plugins.glances_plugin import GlancesPlugin from glances.plugins.glances_plugin import GlancesPlugin
# SNMP OID # SNMP OID
snmp_oid = { '_interface_name': '1.3.6.1.2.1.2.2.1.2' } # http://www.net-snmp.org/docs/mibs/interfaces.html
snmp_oid = { 'ifNumber': '1.3.6.1.2.1.2.1.0' }
netif_oid = { 'interface_name': '1.3.6.1.2.1.2.2.1.2',
'cumulative_rx': '1.3.6.1.2.1.2.2.1.10',
'cumulative_tx': '1.3.6.1.2.1.2.2.1.16' }
class Plugin(GlancesPlugin): class Plugin(GlancesPlugin):
@ -52,7 +56,6 @@ class Plugin(GlancesPlugin):
# Init the stats # Init the stats
self.reset() self.reset()
self.network_old = self.stats
def reset(self): def reset(self):
""" """
@ -80,7 +83,7 @@ class Plugin(GlancesPlugin):
return self.stats return self.stats
# Previous network interface stats are stored in the network_old variable # Previous network interface stats are stored in the network_old variable
if self.network_old == []: if not hasattr(self, 'network_old'):
# First call, we init the network_old var # First call, we init the network_old var
try: try:
self.network_old = netiocounters self.network_old = netiocounters
@ -98,8 +101,8 @@ class Plugin(GlancesPlugin):
try: try:
# Try necessary to manage dynamic network interface # Try necessary to manage dynamic network interface
netstat = {} netstat = {}
netstat['time_since_update'] = time_since_update
netstat['interface_name'] = net netstat['interface_name'] = net
netstat['time_since_update'] = time_since_update
netstat['cumulative_rx'] = network_new[net].bytes_recv netstat['cumulative_rx'] = network_new[net].bytes_recv
netstat['rx'] = (network_new[net].bytes_recv - netstat['rx'] = (network_new[net].bytes_recv -
self.network_old[net].bytes_recv) self.network_old[net].bytes_recv)
@ -113,28 +116,49 @@ class Plugin(GlancesPlugin):
continue continue
else: else:
self.stats.append(netstat) self.stats.append(netstat)
# Save stats to compute next bitrate
self.network_old = network_new self.network_old = network_new
elif input == 'snmp': elif input == 'snmp':
# Update stats using SNMP # Update stats using SNMP
# !!! High CPU consumption: use getbulk request # !!! High CPU consumption on the client side
# !!! http://stackoverflow.com/questions/23085205/pysnmp-query-a-select-list-of-interfaces
# !!!http://pysnmp.sourceforge.net/examples/current/v3arch/manager/cmdgen/getbulk-v2c.html
time_since_update = getTimeSinceLastUpdate('net') time_since_update = getTimeSinceLastUpdate('net')
for net in range(1, 10):
netstat = {} # Get number of network interfaces
net_oid = { 'interface_name': snmp_oid['_interface_name'] + '.' + str(net) } try:
ifNumber = int(self.set_stats_snmp(snmp_oid=snmp_oid)['ifNumber']) + 1
except:
return self.stats
# Loop over network interfaces
network_new = {}
ifIndex = 1
ifCpt = 1
while (ifCpt < ifNumber) or (ifIndex > 1024):
# Add interface index to netif OID
net_oid = dict((k, v + '.' + str(ifIndex)) for (k, v) in netif_oid.items())
netstat = self.set_stats_snmp(snmp_oid=net_oid) netstat = self.set_stats_snmp(snmp_oid=net_oid)
if (str(netstat['interface_name']) == ''): if str(netstat['interface_name']) == '':
ifIndex += 1
continue continue
else:
ifCpt += 1
network_new[ifIndex] = netstat
if hasattr(self, 'network_old'):
netstat['time_since_update'] = time_since_update netstat['time_since_update'] = time_since_update
netstat['cumulative_rx'] = 0 netstat['rx'] = (float(network_new[ifIndex]['cumulative_rx']) -
netstat['rx'] = 0 float(self.network_old[ifIndex]['cumulative_rx']))
netstat['cumulative_tx'] = 0 netstat['tx'] = (float(network_new[ifIndex]['cumulative_tx']) -
netstat['tx'] = 0 float(self.network_old[ifIndex]['cumulative_tx']))
netstat['cumulative_cx'] = (netstat['cumulative_rx'] + netstat['cumulative_cx'] = (float(netstat['cumulative_rx']) +
netstat['cumulative_tx']) float(netstat['cumulative_tx']))
netstat['cx'] = netstat['rx'] + netstat['tx'] netstat['cx'] = netstat['rx'] + netstat['tx']
self.stats.append(netstat) self.stats.append(netstat)
ifIndex += 1
# Save stats to compute next bitrate
self.network_old = network_new
return self.stats return self.stats