Added stocks plugin

This commit is contained in:
2020-04-17 19:16:58 +03:00
parent b7c1a3f6f4
commit daa8e5316c
13 changed files with 180 additions and 64 deletions

View File

@ -27,6 +27,9 @@ class Cpu(BaseModel):
class CpuPlugin(Plugin):
models = [Cpu]
def get_interval(self):
return config.CPU_INTERVAL
def store(self, cpu, times, freq):
entry = Cpu()
entry.cpu = cpu

View File

@ -28,46 +28,52 @@ class DiskIO(BaseModel):
write_speed = FloatField(null=False)
class DiskPlugin(Plugin):
models = [DiskUsage, DiskIO]
class DiskUsagePlugin(Plugin):
models = [DiskUsage]
def get_interval(self):
return config.DISK_USAGE_INTERVAL
def execute(self):
for partition in psutil.disk_partitions():
usage = psutil.disk_usage(partition.mountpoint)
entry = DiskUsage()
entry.partition = partition.device
entry.mountpoint = partition.mountpoint
entry.total = usage.total
entry.used = usage.used
entry.free = usage.free
entry.save()
class DiskIOPlugin(Plugin):
models = [DiskIO]
def __init__(self):
self.__i = 0
self.__previous_io = {}
def get_interval(self):
return config.DISK_IO_INTERVAL
def store_io(self, disk, current):
previous = self.__previous_io.get(disk, current)
entry = DiskIO()
entry.disk = disk
entry.read_count = (current.read_count - previous.read_count) / config.INTERVAL
entry.write_count = (current.write_count - previous.write_count) / config.INTERVAL
entry.read_speed = (current.read_bytes - previous.read_bytes) / config.INTERVAL
entry.write_speed = (current.write_bytes - previous.write_bytes) / config.INTERVAL
entry.read_count = (current.read_count - previous.read_count) / self.get_interval()
entry.write_count = (current.write_count - previous.write_count) / self.get_interval()
entry.read_speed = (current.read_bytes - previous.read_bytes) / self.get_interval()
entry.write_speed = (current.write_bytes - previous.write_bytes) / self.get_interval()
entry.save()
self.__previous_io[disk] = current
def execute(self):
# Collect disk usage
if (self.__i % config.DISK_SPACE_FREQUENCY) == 0:
for partition in psutil.disk_partitions():
usage = psutil.disk_usage(partition.mountpoint)
entry = DiskUsage()
entry.partition = partition.device
entry.mountpoint = partition.mountpoint
entry.total = usage.total
entry.used = usage.used
entry.free = usage.free
entry.save()
# Collect IO
self.store_io(None, psutil.disk_io_counters(perdisk=False))
io_reads = psutil.disk_io_counters(perdisk=True)
for disk, current in io_reads.items():
self.store_io(disk, current)
self.__i += 1

View File

@ -26,6 +26,9 @@ class Memory(BaseModel):
class MemoryPlugin(Plugin):
models = [Memory]
def get_interval(self):
return config.MEMORY_INTERVAL
def execute(self):
vmem = psutil.virtual_memory()
swap = psutil.swap_memory()

View File

@ -25,15 +25,18 @@ class NetworkPlugin(Plugin):
def __init__(self):
self.__previous_io = {}
def get_interval(self):
return config.NETWORK_INTERVAL
def store_io(self, nic, current):
previous = self.__previous_io.get(nic, current)
entry = NetworkIO()
entry.nic = nic
entry.packets_sent = (current.packets_sent - previous.packets_sent) / config.INTERVAL
entry.packets_recv = (current.packets_recv - previous.packets_recv) / config.INTERVAL
entry.bytes_sent = (current.bytes_sent - previous.bytes_sent) / config.INTERVAL
entry.bytes_recv = (current.bytes_recv - previous.bytes_recv) / config.INTERVAL
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()
entry.save()
self.__previous_io[nic] = current

View File

@ -22,8 +22,10 @@ class PingPlugin(Plugin):
models = [Ping]
def __init__(self):
self.__timeout = config.INTERVAL // 3
self.__i = 0
self.__timeout = config.PING_INTERVAL // 3
def get_interval(self):
return config.PING_INTERVAL
async def do_ping(self, host):
command = ['ping', '-c', '1', '-W', str(self.__timeout), host]
@ -47,13 +49,12 @@ class PingPlugin(Plugin):
await asyncio.gather(*[self.do_ping(host) for host in config.PING_HOSTS])
def execute(self):
if (self.__i % config.PING_FREQUENCY) == 0:
if getattr(asyncio, 'run', None) is not None:
# Python 3.7+
asyncio.run(self.execute_internal())
else:
loop = asyncio.get_event_loop()
loop.run_until_complete(self.execute_internal())
loop.close()
self.__i += 1
if getattr(asyncio, 'run', None) is not None:
# Python 3.7+
asyncio.run(self.execute_internal())
else:
loop = asyncio.get_event_loop()
loop.run_until_complete(self.execute_internal())
loop.close()

View File

@ -22,6 +22,9 @@ class Temperatures(BaseModel):
class TemperaturesPlugin(Plugin):
models = [Temperatures]
def get_interval(self):
return config.TEMPERATURE_INTERVAL
def execute(self):
for sensor, temps in psutil.sensors_temperatures(config.TEMPERATURE_USE_FAHRENHEIT).items():
for temp in temps: