Skip to content

Instantly share code, notes, and snippets.

@gretel
Created January 11, 2018 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gretel/485b32c359e3461056683a699b820b38 to your computer and use it in GitHub Desktop.
Save gretel/485b32c359e3461056683a699b820b38 to your computer and use it in GitHub Desktop.
send basic values gathered via 'upsc' from "network ups tools" to graphite in python3
#!/usr/bin/env python3
#
import graphyte
import subprocess
import time
UPS_ID='apc1500@kali'
TIME_INTERVAL=15
GRAPHITE_HOST='graphite.jitter.local'
GRAPHITE_PREFIX='ups.apc1500'
graphyte.init(GRAPHITE_HOST, prefix=GRAPHITE_PREFIX)
def gather_ups():
try:
# call cli
status = subprocess.check_output(['upsc', UPS_ID])
#print(status)
lines = status.splitlines()
# parse roughly
battery_charge = int(lines[0][16:19])
battery_runtime = int(lines[3][17:21])
battery_voltage = float(lines[6][17:21])
ups_status = lines[29][12:14]
# online
if ups_status == b"OL":
ups_status = 0
# on battery
elif ups_status == b"OB":
ups_status = 1
# battery low
elif ups_status == b"LB":
ups_status = -1
# unhandled
else:
raise ValueError("invalid ups_status", ups_status)
data = [('battery_charge', battery_charge),('battery_runtime', battery_runtime),('battery_voltage',battery_voltage),('ups_status', ups_status)]
except Exception as e:
# duh
print('exception', e)
# keep running
return []
else:
return data
while True:
data_ups = gather_ups()
print(data_ups)
for k, v in data_ups:
#print(k, v)
graphyte.send(k, v)
time.sleep(TIME_INTERVAL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment