Skip to content

Commit

Permalink
Updated
Browse files Browse the repository at this point in the history
  • Loading branch information
dbsqp authored Apr 9, 2021
1 parent e45fee8 commit 72a6fd5
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions nut-influxdbv2-exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
from nut2 import PyNUTClient
from influxdb_client import InfluxDBClient, Point, WriteOptions
from influxdb_client.client.write_api import SYNCHRONOUS
import json
import json, socket


# NUT related variables
nut_port = os.getenv('NUT_PORT', '3493')
nut_password = os.getenv('NUT_PASSWORD', 'secret')
nut_username = os.getenv('NUT_USERNAME', 'monuser')
nut_upsname = os.getenv('NUT_UPSNAME', 'ups')

nut_ip_list_str = os.getenv('NUT_IP_LIST', '')
nut_host_list_str = os.getenv('NUT_HOST_LIST', '')
nut_ip_list_str = os.getenv('NUT_IP_LIST', '[]')
nut_host_list_str = os.getenv('NUT_HOST_LIST', '[]')
nut_ip_list=eval(nut_ip_list_str)
nut_host_list=eval(nut_host_list_str)

Expand All @@ -28,7 +27,7 @@
# InfluxDBv2 variables
influxdb2_host=os.getenv('INFLUXDB2_HOST', "localhost")
influxdb2_port=int(os.getenv('INFLUXDB2_PORT', "8086"))
influxdb2_org=os.getenv('INFLUXDB2_ORG', "org")
influxdb2_org=os.getenv('INFLUXDB2_ORG', "Home")
influxdb2_token=os.getenv('INFLUXDB2_TOKEN', "token")
influxdb2_bucket=os.getenv('INFLUXDB2_BUCKET', "DEV")

Expand All @@ -55,7 +54,7 @@
hostname = socket.gethostname()
host_ip = socket.gethostbyname(hostname)
if debug:
print ( " docker: "+host_ip )
print ( "docker: "+host_ip )


# setup InfluxDB
Expand All @@ -66,7 +65,7 @@

client = InfluxDBClient(url=influxdb2_url, token=influxdb2_token, org=influxdb2_org)
if client and debug:
print("Influx: OK")
print("influx: online")

write_api = client.write_api(write_options=SYNCHRONOUS)

Expand Down Expand Up @@ -113,29 +112,31 @@ def construct_object(data, remove_keys, host):


if debug:
print("NUT_USER: ", nut_username)
print("NUT_PASS: ", nut_password)
print("N user: "+nut_username)
print("N pass: "+nut_password)
print("IP list:")
print (json.dumps(nut_ip_list,indent=4))
print("Host list:")
print (json.dumps(nut_host_list,indent=4))


for ipaddress in nut_ip_list:
# setup NUT
position = nut_ip_list.index(ipaddress)
host=nut_host_list[position]
if debug:
print(" IP: ", ipaddress)
print("host: ", host)
# loop over unique names (allows non unique ip for test)
for host in nut_host_list:
position = nut_host_list.index(host)
ipaddress = nut_ip_list[position]
print("\nDO NUT: "+nut_upsname+"@"+ipaddress+" > "+host)

# setup NUT
ups_client = PyNUTClient(host=ipaddress, port=nut_port, login=nut_username, password=nut_password, debug=nut_debug)
if ups_client and debug:
print("NUT: OK")


print(" NUT: online")

# push to Influx
while True:
try:
ups_data = ups_client.list_vars(nut_upsname)
if debug:
print ("UPS: "+nut_upsname"@"+host)
print ("RAW: "+nut_upsname+" @ "+ipaddress)
print (json.dumps(ups_data,indent=4))
except:
tb = traceback.format_exc()
Expand All @@ -148,7 +149,7 @@ def construct_object(data, remove_keys, host):

try:
if debug:
print ("INFLUX: "+influxdb2_bucket)
print ("INFLUX: "+influxdb2_bucket+" @ "+host)
print (json.dumps(json_body,indent=4))
write_api.write(bucket=influxdb2_bucket, org=influxdb2_org, record=[json_body])
break
Expand All @@ -160,4 +161,3 @@ def construct_object(data, remove_keys, host):
exit(2)

time.sleep( 5 )

0 comments on commit 72a6fd5

Please sign in to comment.