Cambio versione a influxdb3 per usare InfluxQL per le query

This commit is contained in:
2025-05-31 15:48:46 +02:00
parent 5f34f3169a
commit 25536e0842
6 changed files with 51 additions and 56 deletions

View File

@@ -2,7 +2,7 @@ FROM python:3.12-alpine
RUN apk update && apk upgrade --no-cache
RUN pip install --no-cache-dir RouterOS-API influxdb-client
RUN pip install --no-cache-dir RouterOS-API influxdb3-python
COPY ./routermon.py /home/routermon.py

View File

@@ -6,9 +6,7 @@ import signal
import json
import routeros_api
from influxdb_client.client.write.point import Point
from influxdb_client.client.influxdb_client import InfluxDBClient
from influxdb_client.client.write_api import ASYNCHRONOUS, SYNCHRONOUS
from influxdb_client_3 import InfluxDBClient3, Point
# Get environment variables
env = dict(os.environ)
@@ -29,10 +27,9 @@ class SignalHandler:
def main():
INTERVAL = int(env['INTERVAL'])
# Init InfluxDB
write_client = InfluxDBClient(url=env['INFLUXDB_URL'],
token=env['INFLUXDB_TOKEN'],
org=env['INFLUXDB_ORG'])
write_api = write_client.write_api(write_options=ASYNCHRONOUS)
write_client = InfluxDBClient3(host=env['INFLUXDB_URL'],
token=env['INFLUXDB_TOKEN'],
database=env['INFLUXDB_DATABASE'])
# Init routerOS API
connection = routeros_api.RouterOsApiPool(env['MIKROTIK_IP'],
username=env['MIKROTIK_USER'],
@@ -52,8 +49,8 @@ def main():
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]
if_stats: list[dict] = api.get_resource('/interface/ethernet').call('print', {'proplist': 'name,rx-bytes,tx-bytes'})
hw_stats: dict[str,str] = api.get_resource('/system/resource').call('print', {'proplist':'uptime,cpu-load,total-memory,free-memory'})[0]
# calcolo della velocita' interfaccia a ogni ciclo
for n,d in enumerate(if_stats):
for k,v in d.items():
@@ -62,17 +59,18 @@ def main():
if_stats[n]['rx-rate'] = int((if_stats[n]['rx-bytes']-if_stats_old[n]['rx-bytes'])/(now-last))
if_stats[n]['tx-rate'] = int((if_stats[n]['tx-bytes']-if_stats_old[n]['tx-bytes'])/(now-last))
if_points.append(
Point("interfaces")
.tag("interface", d['name'])
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=if_points)
rs2 = write_api.write(bucket=env['INFLUXDB_BUCKET'], org=env['INFLUXDB_ORG'],
record=Point('resources').field('cpu',int(hw_stats['cpu-load']))
)
LOGGER.debug(f"InfluxWrite: W1:{rs1}, W2:{rs2}")
write_client.write(record=if_points)
hw_point = Point('resources')
for k,v in hw_stats.items():
hw_point.field(k,int(v) if v.isdecimal() else v)
write_client.write(record=hw_point)
if_stats_old = if_stats
last = time.time()
LOGGER.debug(f"\nInterfaces: {json.dumps(if_stats, indent = 2)}")