So - one trivial script (reusing much of the code for my MQTT variant) you get https://gist.github.com/1903259. The only minor niggle was working out how to prepend the minimum extra json content (version and datastreams) as I'd not used the json libs before.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
1 comment:
You could also use httplib2. We do Puts in the audrey agent (github.com/aeolusproject/audrey/). It uses the oauth library which uses httplib2 as the underlying http lib.
Post a Comment