Skip to content

Commit

Permalink
Merge pull request #1 from dbsqp/dev
Browse files Browse the repository at this point in the history
Push dev to master
  • Loading branch information
dbsqp authored Apr 12, 2021
2 parents c37b631 + 0a61d36 commit 1936e87
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 52 deletions.
34 changes: 20 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
# nut-influxdbv2

This is a docker container that pulls data from a NUT server and pushed to InfluxDB. Based on work of mihai-cindea [https://github.com/mihai-cindea/nut-influxdb-exporter)

## Changes
Updated for InfluxDBv2. Changed outputted values.
Updated for InfluxDBv2. Changed outputted values. Poll multiple NUT servers.

## Roadmap
Add poll of multiple NUT servers by env varable of server IP addresses

## How to run
## NUT Servers
- Get IP address of NUT servers
- Create list of IPs: ['IP1','IP2', ...]
- Create list of hostnames (mapped to hosts in Influx): ['Host1','Host2', ...]

## InfluxDBv2 Setup
Setup InfluxDBv2, create bucket and create a token with write permissions for bucket.

## Docker Setup
```
$ docker run -d \
-e NUT_HOST="<NUT Server IP address>" \
-e NUT_PORT="<NUT Port>" \
-e NUT_PASSWORD="<NUT password>" \
-e NUT_USERNAME="<NUT username>" \
-e NUT_HOSTNAME="<Influx Host Tag>" \
-e NUT_UPSNAME="<NUT UPS Name>" \
-e NUT_IP_LIST="['IP1','IP2',...]" \
-e NUT_HOST_LIST="['Host1','Host2',...]" \
-e NUT_PORT="3493" \
-e NUT_PASSWORD="secret" \
-e NUT_USERNAME="monuser" \
-e NUT_UPSNAME="ups" \
-e INFLUXDB2_HOST="<INFLUXDBv2 SERVER>" \
-e INFLUXDB2_PORT="8086" \
-e INFLUXDB2_ORG="" \
-e INFLUXDB2_ORG="Home" \
-e INFLUXDB2_TOKEN="" \
-e INFLUXDB2_BUCKET="" \
--name "nut-influxdbv2" \
dbsqp/nut-influxdbv2:latest
-e INFLUXDB2_BUCKET="DEV" \
--name "Nut-InfluxDBv2" \
dbsqp/nut-influxdbv2:dev
```

## Debug
Expand Down
90 changes: 52 additions & 38 deletions nut-influxdbv2-exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@


# NUT related variables
nut_host = os.getenv('NUT_HOST', '127.0.0.1')
nut_port = os.getenv('NUT_PORT', '3493')
nut_password = os.getenv('NUT_PASSWORD', 'secret')
nut_username = os.getenv('NUT_USERNAME', 'monuser')
nut_hostname = os.getenv('NUT_HOSTNAME', 'localhost')
nut_upsname = os.getenv('NUT_UPSNAME', 'ups')
nut_watts = os.getenv('NUT_WATTS', '700')
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)


# Other vars
debug_str = os.getenv('DEBUG', 'false')
Expand Down Expand Up @@ -52,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 @@ -63,18 +65,11 @@

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)


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

# define convert
def convert_to_type(s):
Expand All @@ -89,12 +84,12 @@ def convert_to_type(s):
return s

#define data object
def construct_object(data, remove_keys):
def construct_object(data, remove_keys, host):
tags = {}
fields = {}

tags['source']="NUT"
tags['host']=nut_hostname
tags['host']=host

for k, v in data.items():
if k == "device.model":
Expand All @@ -117,35 +112,54 @@ def construct_object(data, remove_keys):
return result


# Main
while True:
try:
if debug:
print ("UPS: "+nut_upsname)
ups_data = ups_client.list_vars(nut_upsname)
if debug:
print (json.dumps(ups_data,indent=4))
except:
tb = traceback.format_exc()
if debug:
print(tb)
print("Error getting data from NUT")
exit(1)
if debug:
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))


# 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: online")

# push to Influx
while True:
try:
ups_data = ups_client.list_vars(nut_upsname)
if debug:
print ("RAW: "+nut_upsname+" @ "+ipaddress)
print (json.dumps(ups_data,indent=4))
except:
tb = traceback.format_exc()
if debug:
print(tb)
print("Error getting data from NUT at "+ipaddress+" "+host)
exit(1)


json_body = construct_object(ups_data, remove_keys)
json_body = construct_object(ups_data, remove_keys, host)

try:
if debug:
print ("INFLUX: "+influxdb2_bucket)
print (json.dumps(json_body,indent=4))
write_api.write(bucket=influxdb2_bucket, org=influxdb2_org, record=[json_body])
break
except:
tb = traceback.format_exc()
try:
if debug:
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
except:
tb = traceback.format_exc()
if debug:
print(tb)
print("Error connecting to InfluxDBv2")
exit(2)

time.sleep( 5 )

0 comments on commit 1936e87

Please sign in to comment.