Update network_load segment to work with psutil

This commit is contained in:
Kim Silkebækken 2013-01-20 19:19:16 +01:00
parent 0586bd059a
commit b4fc8ebe91

View File

@ -145,15 +145,25 @@ def network_load(interface='eth0', measure_interval=1, suffix='B/s', binary_pref
def get_bytes(): def get_bytes():
try: try:
with open('/sys/class/net/{interface}/statistics/rx_bytes'.format(interface=interface), 'rb') as file_obj: import psutil
rx = int(file_obj.read()) io_counters = psutil.network_io_counters(pernic=True)
with open('/sys/class/net/{interface}/statistics/tx_bytes'.format(interface=interface), 'rb') as file_obj: if_io = io_counters.get(interface)
tx = int(file_obj.read()) if not if_io:
return (rx, tx) return None
except IOError: return (if_io.bytes_recv, if_io.bytes_sent)
return (0, 0) except ImportError:
try:
with open('/sys/class/net/{interface}/statistics/rx_bytes'.format(interface=interface), 'rb') as file_obj:
rx = int(file_obj.read())
with open('/sys/class/net/{interface}/statistics/tx_bytes'.format(interface=interface), 'rb') as file_obj:
tx = int(file_obj.read())
return (rx, tx)
except IOError:
return None
b1 = get_bytes() b1 = get_bytes()
if b1 is None:
return None
time.sleep(measure_interval) time.sleep(measure_interval)
b2 = get_bytes() b2 = get_bytes()
return u'{rx_diff}{tx_diff}'.format( return u'{rx_diff}{tx_diff}'.format(