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

View File

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

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)}")

View File

@@ -7,9 +7,7 @@ import logging
import signal
import json
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)
@@ -56,10 +54,9 @@ def main():
LOGGER.debug(json.dumps(env, indent=2))
run: SignalHandler = SignalHandler()
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'],
org=env['INFLUXDB_ORG'])
write_api = write_client.write_api(write_options=ASYNCHRONOUS)
database=env['INFLUXDB_DATABASE'])
while run.running:
try:
send(port, "Q1")
@@ -82,7 +79,7 @@ def main():
p = Point('ups')
for k,v in values.items():
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)
except Exception as 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 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