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 requests
import json import json
from datetime import datetime 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 from time import sleep
# nutserver settings
hostList = os.environ["hosts"] HOSTLIST = os.environ.get("INFLUXDB_URL", "10.0.5.5")
hostList = hostList.split(",") HOSTLIST = HOSTLIST.split(",")
influx = os.environ['influx']
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: for UPS in data:
# bind_params = {'host': str(UPS)}
json_body = [ json_body = [
{ {
"measurement": "data", "measurement": "data",
@ -25,16 +33,29 @@ def uploadInflux(data, host=influx, port=8086): # Main upload
}, },
"fields": { "fields": {
"battery": data[UPS]["battery.charge"], "battery": float(data[UPS]["battery.charge"]),
"runtime-s": data[UPS]["battery.runtime"], "runtime-s": float(data[UPS]["battery.runtime"]),
"load": data[UPS]["ups.load"], "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 = InfluxDBClient(host, port, database = "telegraf") # Init connection to Influx Server
client.write_points(json_body) # Write Speedtest results # 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 = {} upslist = {}
## pull all the UPSes froom each host into a nice dict ## pull all the UPSes froom each host into a nice dict
for host in hostList: for host in HOSTLIST:
try: try:
upslist[host] = [] upslist[host] = []
client = PyNUTClient(host, '3493') client = PyNUTClient(host, '3493')
@ -64,7 +85,7 @@ def main():
data[UPS] = client.list_vars(UPS) data[UPS] = client.list_vars(UPS)
except: except:
print('UPS {} is having issues'.format(host)) print('UPS {} is having issues'.format(host))
# print(data)
uploadInflux(data) uploadInflux(data)
while True: while True:

View File

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