collector/plugins/system/network_plugin.py

51 lines
1.5 KiB
Python
Raw Normal View History

2020-04-14 22:49:22 +00:00
from collections import namedtuple
from datetime import datetime
import psutil
from peewee import *
from playhouse.shortcuts import model_to_dict
import config
from database import BaseModel
from plugins.plugin import Plugin
2020-04-14 22:49:22 +00:00
class NetworkIO(BaseModel):
2020-04-18 20:52:27 +00:00
time = DateTimeField(index=True, default=datetime.utcnow)
2020-04-14 22:49:22 +00:00
nic = TextField(null=True)
packets_sent = FloatField(null=False) # all values are per second
packets_recv = FloatField(null=False)
bytes_sent = FloatField(null=False)
bytes_recv = FloatField(null=False)
class NetworkPlugin(Plugin):
models = [NetworkIO]
def __init__(self):
self.__previous_io = {}
2020-04-17 16:16:58 +00:00
def get_interval(self):
return config.NETWORK_INTERVAL
2020-04-14 22:49:22 +00:00
def store_io(self, nic, current):
previous = self.__previous_io.get(nic, current)
entry = NetworkIO()
entry.nic = nic
2020-04-17 16:16:58 +00:00
entry.packets_sent = (current.packets_sent - previous.packets_sent) / self.get_interval()
entry.packets_recv = (current.packets_recv - previous.packets_recv) / self.get_interval()
entry.bytes_sent = (current.bytes_sent - previous.bytes_sent) / self.get_interval()
entry.bytes_recv = (current.bytes_recv - previous.bytes_recv) / self.get_interval()
2020-04-14 22:49:22 +00:00
entry.save()
self.__previous_io[nic] = current
def execute(self):
self.store_io(None, psutil.net_io_counters(pernic=False))
io_reads = psutil.net_io_counters(pernic=True)
for nic, current in io_reads.items():
self.store_io(nic, current)