Deploy to docker PLS

This commit is contained in:
Sean Corrigan 2021-09-14 16:35:58 -04:00
parent bde886da72
commit 9e93b732ec

58
main.py
View File

@ -2,6 +2,51 @@
## Requires NUT2 from PIP ## Requires NUT2 from PIP
from nut2 import PyNUTClient from nut2 import PyNUTClient
import os import os
import requests
import json
from datetime import datetime
from influxdb import InfluxDBClient
from time import sleep
### telegram logging
# API Key
apikey = '1857016076:AAGVag6tEtEpJHNpPtLyHYU-JsnN7arwDRk'
# Key of your chat.
chatid = '840699197'
def logTelegram(message):
os.system("curl --silent -X POST https://api.telegram.org/bot" + apikey + "/sendMessage -d chat_id=" + chatid +" -d text=\""+ message +"\"")
### end telegram logging
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
# try: # try:
# hostList = os.environ["hosts"] # hostList = os.environ["hosts"]
@ -21,13 +66,20 @@ for host in hostList:
for UPS in client.list_ups(): for UPS in client.list_ups():
upslist[host].append(UPS) upslist[host].append(UPS)
print(upslist) # print(upslist)
data = {}
## Pull whatever data we would like into a dict of each ups
for host in upslist: for host in upslist:
client = PyNUTClient(host, '3493') client = PyNUTClient(host, '3493')
for UPS in upslist[host]: for UPS in upslist[host]:
print() data[UPS] = client.list_vars(UPS)
print("{} Battery Percent: {}%".format(UPS, client.list_vars(UPS)['battery.charge']))
uploadInflux(data)
# client = PyNUTClient('10.0.5.181', '3493') # client = PyNUTClient('10.0.5.181', '3493')