Skip to content

Instantly share code, notes, and snippets.

@Elwell
Created February 24, 2012 19:48
Show Gist options
  • Save Elwell/1903259 to your computer and use it in GitHub Desktop.
Save Elwell/1903259 to your computer and use it in GitHub Desktop.
apcaccess (APCupsd) to Pachube python script
#!/usr/bin/python
# Script to poll the UPS (via apcupsd) and publish interesting facts to
# pachube. You'll need to alter FEED_ID and insert your API key
# Published under GPL3+ by Andrew Elwell <Andrew.Elwell@gmail.com>
import subprocess # we scrape apcaccess output
import requests # CBA writing a pachube library
import json
interesting = ('linev', 'loadpct', 'battv', 'bcharge')
payload = []
# go and grab
res = subprocess.check_output("/sbin/apcaccess")
for line in res.split('\n'):
(key,spl,val) = line.partition(': ')
key = key.rstrip().lower()
if key in interesting: # just save what we want
val = val.strip()
val = val.split(' ',1)[0] # ignore anything after 1st space
payload.append({'id':key, 'current_value':val})
# set up pachube connection
API_KEY = "YOUR API KEY"
#jsonify it
stream = json.dumps({"version":"1.0.0","datastreams": payload})
r = requests.put("http://api.pachube.com/v2/feeds/FEED_ID", headers = {"X-PachubeApiKey": API_KEY}, data=stream)
@flyte
Copy link

flyte commented May 17, 2017

Just spotted this on a Google search and thought you might find a pure-Python version of apcaccess useful. https://github.com/flyte/apcaccess

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment