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

14
.vscode/launch.json vendored
View File

@@ -9,10 +9,9 @@
"console": "integratedTerminal", "console": "integratedTerminal",
"env": { "env": {
"INTERVAL" : "5", "INTERVAL" : "5",
"INFLUXDB_TOKEN":"b46xoZRVsQvyGnv8TVMmTDmxT8LZZsi7O_u776MHIxiE9CPh7yHi0iHMOerUT5o1y65MMxeKKA7S-ijQ3elK-g==", "INFLUXDB_TOKEN":"apiv3_tbEpA8JmIRTfr8Wbw9npD79BcMlFsnV4_jhdt-CdUP53Mos61KBODGaggl2g5oKZZvZrZu3e6mpob6zorhEdbg",
"INFLUXDB_URL" : "http://localhost:8085", "INFLUXDB_URL" : "http://edelweiss-srv:8181",
"INFLUXDB_ORG" : "edelweiss", "INFLUXDB_DATABASE" : "edelweiss",
"INFLUXDB_BUCKET" : "theatre",
"MIKROTIK_IP": "192.168.31.1", "MIKROTIK_IP": "192.168.31.1",
"MIKROTIK_USER": "service", "MIKROTIK_USER": "service",
"MIKROTIK_PASSWORD": "dataservice", "MIKROTIK_PASSWORD": "dataservice",
@@ -29,10 +28,9 @@
"console": "integratedTerminal", "console": "integratedTerminal",
"env": { "env": {
"INTERVAL" : "5", "INTERVAL" : "5",
"INFLUXDB_TOKEN":"rg5dZrBHQxJTH4Etq2jmDduggCC28QaWcua0VVvW4hjsEVhy_JUpVhcyg-aLAbM-TXv92pTB7IGJlyAPvi7Kvw==", "INFLUXDB_TOKEN":"apiv3_tbEpA8JmIRTfr8Wbw9npD79BcMlFsnV4_jhdt-CdUP53Mos61KBODGaggl2g5oKZZvZrZu3e6mpob6zorhEdbg",
"INFLUXDB_URL" : "http://edelweiss-srv:4567", "INFLUXDB_URL" : "http://edelweiss-srv:8181",
"INFLUXDB_ORG" : "edelweiss", "INFLUXDB_DATABASE" : "edelweiss",
"INFLUXDB_BUCKET" : "ups",
"PORT": "/dev/ttyUSB1", "PORT": "/dev/ttyUSB1",
"BAUD": "2400", "BAUD": "2400",
"LOG_FILE": "/tmp/upsmon.log", "LOG_FILE": "/tmp/upsmon.log",

View File

@@ -1,17 +1,20 @@
services: services:
influxdb2: influxdb3:
image: influxdb:2-alpine image: influxdb:3-core
container_name: edx-influxdb container_name: edx-influxdb3
restart: unless-stopped restart: unless-stopped
ports: ports:
- 8086:8086 - 8181:8181
command:
- influxdb3
- serve
- --node-id=influxb-node0
- --object-store=file
- --data-dir=/var/lib/influxdb3
volumes: volumes:
- type: volume - type: volume
source: influxdb2-data source: influxdb3-data
target: /var/lib/influxdb2 target: /var/lib/influxdb3
- type: volume
source: influxdb2-config
target: /etc/influxdb2
grafana: grafana:
image: grafana/grafana:latest image: grafana/grafana:latest
@@ -22,22 +25,21 @@ services:
volumes: volumes:
- grafana-data:/var/lib/grafana - grafana-data:/var/lib/grafana
depends_on: depends_on:
- influxdb2 - influxdb3
upsmon: upsmon:
image: upsmon:latest image: upsmon:influx3
container_name: edx-upsmon container_name: edx-upsmon
restart: unless-stopped restart: unless-stopped
depends_on: depends_on:
- influxdb2 - influxdb3
devices: devices:
- /dev/ttyUSB1:/dev/ttyUSB1 - /dev/ttyUSB1:/dev/ttyUSB1
environment: environment:
- INTERVAL=5 - INTERVAL=5
- INFLUXDB_TOKEN=b46xoZRVsQvyGnv8TVMmTDmxT8LZZsi7O_u776MHIxiE9CPh7yHi0iHMOerUT5o1y65MMxeKKA7S-ijQ3elK-g== - INFLUXDB_TOKEN=apiv3_tbEpA8JmIRTfr8Wbw9npD79BcMlFsnV4_jhdt-CdUP53Mos61KBODGaggl2g5oKZZvZrZu3e6mpob6zorhEdbg
- INFLUXDB_URL=http://influxdb2:8086 - INFLUXDB_URL=http://influxdb3:8181
- INFLUXDB_ORG=edelweiss - INFLUXDB_DATABASE=edelweiss
- INFLUXDB_BUCKET=theatre
- PORT=/dev/ttyUSB1 - PORT=/dev/ttyUSB1
- BAUD=2400 - BAUD=2400
- LOG_FILE=/tmp/upsmon.log - LOG_FILE=/tmp/upsmon.log
@@ -45,17 +47,16 @@ services:
- LOG_CLI_LVL=INFO - LOG_CLI_LVL=INFO
routermon: routermon:
image: routermon:latest image: routermon:influx3
container_name: edx-routermon container_name: edx-routermon
restart: unless-stopped restart: unless-stopped
depends_on: depends_on:
- influxdb2 - influxdb3
environment: environment:
- INTERVAL=5 - INTERVAL=5
- INFLUXDB_TOKEN=b46xoZRVsQvyGnv8TVMmTDmxT8LZZsi7O_u776MHIxiE9CPh7yHi0iHMOerUT5o1y65MMxeKKA7S-ijQ3elK-g== - INFLUXDB_TOKEN=apiv3_tbEpA8JmIRTfr8Wbw9npD79BcMlFsnV4_jhdt-CdUP53Mos61KBODGaggl2g5oKZZvZrZu3e6mpob6zorhEdbg
- INFLUXDB_URL=http://influxdb2:8086 - INFLUXDB_URL=http://influxdb3:8181
- INFLUXDB_ORG=edelweiss - INFLUXDB_DATABASE=edelweiss
- INFLUXDB_BUCKET=theatre
- MIKROTIK_IP=192.168.31.1 - MIKROTIK_IP=192.168.31.1
- MIKROTIK_USER=service - MIKROTIK_USER=service
- MIKROTIK_PASSWORD=dataservice - MIKROTIK_PASSWORD=dataservice
@@ -68,3 +69,4 @@ volumes:
influxdb2-data: influxdb2-data:
influxdb2-config: influxdb2-config:
grafana-data: grafana-data:
influxdb3-data:

View File

@@ -2,7 +2,7 @@ FROM python:3.12-alpine
RUN apk update && apk upgrade --no-cache 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 COPY ./routermon.py /home/routermon.py

View File

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

View File

@@ -7,9 +7,7 @@ import logging
import signal import signal
import json import json
from influxdb_client.client.write.point import Point from influxdb_client_3 import InfluxDBClient3, Point
from influxdb_client.client.influxdb_client import InfluxDBClient
from influxdb_client.client.write_api import ASYNCHRONOUS, SYNCHRONOUS
# Get environment variables # Get environment variables
env = dict(os.environ) env = dict(os.environ)
@@ -56,10 +54,9 @@ def main():
LOGGER.debug(json.dumps(env, indent=2)) LOGGER.debug(json.dumps(env, indent=2))
run: SignalHandler = SignalHandler() run: SignalHandler = SignalHandler()
port = serial.Serial(port=env['PORT'], baudrate=int(env['BAUD']), bytesize=8, parity='N', stopbits=1) port = serial.Serial(port=env['PORT'], baudrate=int(env['BAUD']), bytesize=8, parity='N', stopbits=1)
write_client = InfluxDBClient(url=env['INFLUXDB_URL'], write_client = InfluxDBClient3(host=env['INFLUXDB_URL'],
token=env['INFLUXDB_TOKEN'], token=env['INFLUXDB_TOKEN'],
org=env['INFLUXDB_ORG']) database=env['INFLUXDB_DATABASE'])
write_api = write_client.write_api(write_options=ASYNCHRONOUS)
while run.running: while run.running:
try: try:
send(port, "Q1") send(port, "Q1")
@@ -82,7 +79,7 @@ def main():
p = Point('ups') p = Point('ups')
for k,v in values.items(): for k,v in values.items():
p.field(k,v) p.field(k,v)
write_api.write(bucket=env['INFLUXDB_BUCKET'], org=env['INFLUXDB_ORG'], record=p) write_client.write(record=p)
time.sleep(INTERVAL) time.sleep(INTERVAL)
except Exception as e: except Exception as e:
print(f"Unexpected exception: [{e}]") print(f"Unexpected exception: [{e}]")

View File

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