NUT_to_Influx/main.py
Sean Corrigan 633682b6bf ooopsie
2021-11-05 12:28:34 -04:00

71 lines
1.7 KiB
Python

## Requires NUT2 from PIP
from nut2 import PyNUTClient
import os
import requests
import json
from datetime import datetime
from influxdb import InfluxDBClient
from time import sleep
hostList = os.environ["hosts"]
hostList = hostList.split(",")
def uploadInflux(data, host='192.168.0.13', port=8086): # Main upload
for UPS in data:
# bind_params = {'host': str(UPS)}
json_body = [
{
"measurement": "data",
"tags": {
'ups': str(UPS),
},
"fields": {
"battery": data[UPS]["battery.charge"],
"runtime-s": data[UPS]["battery.runtime"],
"load": data[UPS]["ups.load"],
}
}
]
client = InfluxDBClient(host, port, database = "telegraf") # Init connection to Influx Server
client.write_points(json_body) # Write Speedtest results
def main():
upslist = {}
## pull all the UPSes froom each host into a nice dict
for host in hostList:
try:
upslist[host] = []
client = PyNUTClient(host, '3493')
for UPS in client.list_ups():
upslist[host].append(UPS)
except:
print('UPS {} is having issues'.format(host))
# print(upslist)
data = {}
## Pull whatever data we would like into a dict of each ups
for host in upslist:
client = PyNUTClient(host, '3493')
try:
for UPS in upslist[host]:
data[UPS] = client.list_vars(UPS)
except:
print('UPS {} is having issues'.format(host))
uploadInflux(data)
while True:
main()
sleep(15)