monitoring.py/monitors/network.py
2019-12-30 16:18:42 +01:00

50 lines
2.9 KiB
Python

import psutil
class Network:
interval = 1;
loopCount = 0
def __init__(self, exporter):
self.exporter = exporter
self.last = {}
def run(self):
data = psutil.net_io_counters(True, False)
for device in data:
if device in self.last:
bytes_sent = data[device].bytes_sent - self.last[device]["bytes_sent"]
bytes_recv = data[device].bytes_sent - self.last[device]["bytes_recv"]
packets_sent = data[device].packets_sent - self.last[device]["packets_sent"]
packets_recv = data[device].packets_recv - self.last[device]["packets_recv"]
errin = data[device].errin - self.last[device]["errin"]
errout = data[device].errout - self.last[device]["errout"]
dropin = data[device].dropin - self.last[device]["dropin"]
dropout = data[device].dropout - self.last[device]["dropout"]
self.exporter.incr("net."+device+".bytes_sent", bytes_sent)
self.exporter.incr("net."+device+".bytes_recv", bytes_recv)
self.exporter.incr("net."+device+".packets_sent", packets_sent)
self.exporter.incr("net."+device+".packets_recv", packets_recv)
self.exporter.incr("net."+device+".errin", errin)
self.exporter.incr("net."+device+".errout", errout)
self.exporter.incr("net."+device+".dropin", dropin)
self.exporter.incr("net."+device+".dropout", dropout)
else:
self.last[device] = {}
self.last[device]["bytes_sent"] = data[device].bytes_sent
self.last[device]["bytes_recv"] = data[device].bytes_recv
self.last[device]["packets_sent"] = data[device].packets_sent
self.last[device]["packets_recv"] = data[device].packets_recv
self.last[device]["errin"] = data[device].errin
self.last[device]["errout"] = data[device].errout
self.last[device]["dropin"] = data[device].dropin
self.last[device]["dropout"] = data[device].dropout
self.exporter.gauge("net."+device+".bytes_sent", data[device].bytes_sent)
self.exporter.gauge("net."+device+".bytes_recv", data[device].bytes_recv)
self.exporter.gauge("net."+device+".packets_sent", data[device].packets_sent)
self.exporter.gauge("net."+device+".packets_recv", data[device].packets_recv)
self.exporter.gauge("net."+device+".errin", data[device].errin)
self.exporter.gauge("net."+device+".errout", data[device].errout)
self.exporter.gauge("net."+device+".dropin", data[device].dropin)
self.exporter.gauge("net."+device+".dropout", data[device].dropout)
#print(device)
#print(data[device].bytes_sent)
#print(data)
#self.exporter.gauge("users", len(psutil.users()))