editied to work with influxDB2

This commit is contained in:
Sean C 2023-07-01 18:23:27 -04:00
parent 2db51fe249
commit 28d6508f8a
2 changed files with 36 additions and 15 deletions

49
main.py
View File

@ -5,18 +5,26 @@ import os
import requests
import json
from datetime import datetime
from influxdb import InfluxDBClient
from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS
from time import sleep
hostList = os.environ["hosts"]
hostList = hostList.split(",")
influx = os.environ['influx']
# nutserver settings
HOSTLIST = os.environ.get("INFLUXDB_URL", "10.0.5.5")
HOSTLIST = HOSTLIST.split(",")
def uploadInflux(data, host=influx, port=8086): # Main upload
# Constants (replace with your values)
INFLUXDB_URL = os.environ.get("INFLUX_URL", 'http://10.0.5.61:8086')
INFLUXDB_TOKEN = os.environ.get("INFLUXDB_TOKEN", 'E2GVPIovlYDbAy3ZztRVyuMyILGGnLEI9NsTcyvVgt9LUW9Mhv5Hrzle5gLY7W4xxBVqFk3DYsfss4a3hcDlZQ==')
INFLUXDB_ORG = os.environ.get("INFLUXDB_ORG", 'carbonnet')
INFLUXDB_BUCKET = os.environ.get("INFLUXDB_BUCKET", 'testing')
def uploadInflux(data): # Main upload
for UPS in data:
# bind_params = {'host': str(UPS)}
json_body = [
{
"measurement": "data",
@ -25,16 +33,29 @@ def uploadInflux(data, host=influx, port=8086): # Main upload
},
"fields": {
"battery": data[UPS]["battery.charge"],
"runtime-s": data[UPS]["battery.runtime"],
"load": data[UPS]["ups.load"],
"battery": float(data[UPS]["battery.charge"]),
"runtime-s": float(data[UPS]["battery.runtime"]),
"load": float(data[UPS]["ups.load"]),
"input_voltage": float(data[UPS]["input.voltage"]),
"battery_voltage": float(data[UPS]["battery.voltage"]),
}
}
]
client = InfluxDBClient(host, port, database = "telegraf") # Init connection to Influx Server
client.write_points(json_body) # Write Speedtest results
# client = InfluxDBClient(host, port, database = "telegraf") # Init connection to Influx Server
# client.write_points(json_body) # Write Speedtest results
client = InfluxDBClient(url=INFLUXDB_URL, token=INFLUXDB_TOKEN, org=INFLUXDB_ORG)
# Get a write API.
write_api = client.write_api(write_options=SYNCHRONOUS)
# Write the data to the database.
write_api.write(INFLUXDB_BUCKET, INFLUXDB_ORG, json_body)
# Close the clients to free up resources
write_api.__del__()
client.__del__()
@ -43,7 +64,7 @@ def main():
upslist = {}
## pull all the UPSes froom each host into a nice dict
for host in hostList:
for host in HOSTLIST:
try:
upslist[host] = []
client = PyNUTClient(host, '3493')
@ -64,7 +85,7 @@ def main():
data[UPS] = client.list_vars(UPS)
except:
print('UPS {} is having issues'.format(host))
# print(data)
uploadInflux(data)
while True:

View File

@ -1,2 +1,2 @@
influxdb
influxdb_client
nut2