minor refactoring
This commit is contained in:
18
routermon.py
18
routermon.py
@@ -8,7 +8,7 @@ import routeros_api
|
||||
|
||||
import influxdb_client
|
||||
from influxdb_client import Point
|
||||
from influxdb_client.client.write_api import ASYNCHRONOUS
|
||||
from influxdb_client.client.write_api import ASYNCHRONOUS, SYNCHRONOUS
|
||||
|
||||
# Get environment variables
|
||||
env = dict(os.environ)
|
||||
@@ -40,33 +40,33 @@ def main():
|
||||
|
||||
run = Runner.running
|
||||
last = 0
|
||||
points = []
|
||||
if_stats_old = None
|
||||
if_points = []
|
||||
if_stats_old = api.get_resource('/interface/ethernet').call('print', {'proplist': 'name,rx-bytes,tx-bytes'})
|
||||
### MAIN LOOP ###
|
||||
while run:
|
||||
try:
|
||||
now = time.time()
|
||||
if_stats = api.get_resource('/interface/ethernet').call('print', {'proplist': 'name,rx-bytes,tx-bytes'})
|
||||
hw_stats = api.get_resource('/system/resource').call('print', {'proplist':'uptime,cpu-load'})[0]
|
||||
# calcolo della velocita' interfaccia a ogni ciclo
|
||||
now = time.time()
|
||||
for n,d in enumerate(if_stats):
|
||||
for k,v in d.items():
|
||||
if str.isdecimal(v):
|
||||
if_stats[n][k] = int(v)
|
||||
if if_stats_old is not None:
|
||||
if_stats[n]['rx-rate'] = int((d['rx-bytes']-if_stats_old[n]['rx-bytes'])/(now-last))
|
||||
if_stats[n]['tx-rate'] = int((d['tx-bytes']-if_stats_old[n]['tx-bytes'])/(now-last))
|
||||
points.append(
|
||||
if_points.append(
|
||||
Point("interfaces")
|
||||
.tag("interface", d['name'])
|
||||
.field('rx-rate', if_stats[n]['rx-rate'])
|
||||
.field('tx-rate', if_stats[n]['tx-rate'])
|
||||
)
|
||||
rs1 = write_api.write(bucket=env['INFLUXDB_BUCKET'], org=env['INFLUXDB_ORG'], record=points)
|
||||
rs1 = write_api.write(bucket=env['INFLUXDB_BUCKET'], org=env['INFLUXDB_ORG'],
|
||||
record=if_points)
|
||||
rs2 = write_api.write(bucket=env['INFLUXDB_BUCKET'], org=env['INFLUXDB_ORG'],
|
||||
record=Point('resources')
|
||||
.field('cpu',int(hw_stats['cpu-load']))
|
||||
record=Point('resources').field('cpu',int(hw_stats['cpu-load']))
|
||||
)
|
||||
LOGGER.debug(f"InfluxWrite: W1:{rs1}, W2:{rs2}")
|
||||
if_stats_old = if_stats
|
||||
last = time.time()
|
||||
LOGGER.debug(f"\nInterfaces: {json.dumps(if_stats, indent = 2)}")
|
||||
|
||||
Reference in New Issue
Block a user