Last active
February 24, 2016 14:18
-
-
Save bassdread/d41161235cbea253b947 to your computer and use it in GitHub Desktop.
This file contains 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 | |
import collections | |
from datetime import datetime | |
import json | |
from pySMART import Device | |
from traceback import print_exc | |
try: | |
import redis | |
REDIS_CONNECTION = redis.StrictRedis(host='localhost', port=6379, db=0) | |
except ImportError: | |
pass | |
try: | |
from influxdb import InfluxDBClient | |
INFLUX_CLIENT = InfluxDBClient('localhost', 8086, 'USERNAME', 'PASSWORD', 'DATABASE') | |
except ImportError: | |
pass | |
# map devices to mount points | |
DISKS = { | |
'sda': 'root', | |
'sdb': 'films', | |
'sdc': 'backup', | |
'sdd': 'tv' | |
} | |
DATA_FILE_LOCATION = '/tmp/disk_data.json' | |
USE_INFLUX = True | |
USE_REDIS = True | |
def fetch_sensor_data(): | |
try: | |
data = {} | |
for device, name in DISKS.iteritems(): | |
disk = Device('/dev/{}'.format(device)) | |
for attribute in disk.attributes: | |
if not attribute: | |
continue | |
if attribute.name == "Temperature_Celsius": | |
data[name] = float(attribute.raw) | |
sorted_data = collections.OrderedDict(sorted(data.items())) | |
write_data_file(sorted_data) | |
if USE_REDIS: | |
write_data_redis(data) | |
if USE_INFLUX: | |
write_data_influx(data) | |
for name, reading in sorted_data.iteritems(): | |
print "{0}: {1}".format(name, reading) | |
except Exception as exception: | |
print_exc() | |
print "Failed to get sensor data: {0}".format(exception.message) | |
def write_data_influx(data): | |
json_payload = [] | |
time = datetime.now().strftime("%Y-%m-%dT%H:%M:%S") | |
try: | |
for name, value in data.iteritems(): | |
payload = { | |
"measurement": name, | |
"tags": { | |
"host": "jarvis", | |
"location": "garage" | |
}, | |
"time": time, | |
"fields": { | |
"value": value | |
} | |
} | |
json_payload.append(payload) | |
INFLUX_CLIENT.write_points(json_payload) | |
except Exception as exception: | |
print_exc() | |
print "Failed to write to Influx. Error {}".format(exception.message) | |
def write_data_redis(data): | |
try: | |
for name, value in data.iteritems(): | |
REDIS_CONNECTION.set(name, value) | |
except Exception as exception: | |
print_exc() | |
print "Failed to write to Redis. Error {}".format(exception.message) | |
def write_data_file(data): | |
try: | |
data['time'] = datetime.now().strftime("%Y-%m-%dT%H:%M:%S") | |
with open(DATA_FILE_LOCATION, 'w') as data_file: | |
data_file.write(json.dumps(data)) | |
data_file.close() | |
return True | |
except Exception as exception: | |
print_exc() | |
print "Failed to write to data file. Error {}".format( | |
exception.message) | |
if __name__ == "__main__": | |
fetch_sensor_data() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment