Last active
August 29, 2015 14:18
-
-
Save artschwagerb/4c8408b4897c6bed1192 to your computer and use it in GitHub Desktop.
Server Room Temperature Probe
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 sys | |
import rrdtool | |
import json | |
MIN_TEMP = 0 | |
ERROR_TEMP = -999.99 | |
TEMP_SENSORS = { | |
"E" : {"sensor":"28-00043a4518ff","location":"Server Room Rack B/C (Back)","temperature":{"celsius":None,"fahrenheit":None}}, | |
"A" : {"sensor":"28-00043e827cff","location":"Server Room Rack A (Ambient)","temperature":{"celsius":None,"fahrenheit":None}}, | |
"B" : {"sensor":"28-00043b61b4ff","location":"Server Room Rack E (Ambient)","temperature":{"celsius":None,"fahrenheit":None}}, | |
"D" : {"sensor":"28-00043a52a1ff","location":"Server Room Rack D/E (Back)","temperature":{"celsius":None,"fahrenheit":None}}, | |
"C" : {"sensor":"28-00043b6803ff","location":"Server Room Rack C/D (Back)","temperature":{"celsius":None,"fahrenheit":None}}, | |
} | |
def create_rrd(): | |
ret = rrdtool.create("temperatures.rrd", "--step", "300", "--start", '0', | |
"DS:A:GAUGE:600:U:U", | |
"DS:B:GAUGE:600:U:U", | |
"DS:C:GAUGE:600:U:U", | |
"DS:D:GAUGE:600:U:U", | |
"DS:E:GAUGE:600:U:U", | |
"RRA:AVERAGE:0.5:1:600", | |
"RRA:AVERAGE:0.5:6:700", | |
"RRA:AVERAGE:0.5:24:775", | |
"RRA:AVERAGE:0.5:288:797", | |
"RRA:MAX:0.5:1:600", | |
"RRA:MAX:0.5:6:700", | |
"RRA:MAX:0.5:24:775", | |
"RRA:MAX:0.5:444:797") | |
if ret: | |
print rrdtool.error() | |
def read_temperature(sensor): | |
tfile = open("/sys/bus/w1/devices/{0}/w1_slave".format(sensor)) | |
text = tfile.read() | |
tfile.close() | |
lines = text.split("\n") | |
if lines[0].find("YES") > 0: | |
temp = float((lines[1].split(" ")[9])[2:])/1000 | |
return temp | |
return ERROR_TEMP | |
def create_json(): | |
# Open a file for writing | |
sorted(TEMP_SENSORS) | |
out_file = open("/usr/share/nginx/www/temperatures.json","w") | |
json.dump(TEMP_SENSORS,out_file, indent=4) | |
out_file.close() | |
def update_rrd(): | |
for rrd in TEMP_SENSORS: | |
celsius = read_temperature(TEMP_SENSORS[rrd]['sensor']) | |
TEMP_SENSORS[rrd]['temperature']['celsius'] = celsius | |
TEMP_SENSORS[rrd]['temperature']['fahrenheit'] = 9.0/5.0 * celsius + 32 | |
update_str = "N:{0}:{1}:{2}:{3}:{4}".format(TEMP_SENSORS['A']['temperature']['fahrenheit'],TEMP_SENSORS['B']['temperature']['fahrenheit'],TEMP_SENSORS['C']['temperature']['fahrenheit'],TEMP_SENSORS['D']['temperature']['fahrenheit'],TEMP_SENSORS['E']['temperature']['fahrenheit']) | |
ret = rrdtool.update('temperatures.rrd',update_str); | |
if ret: | |
print rrdtool.error() | |
def create_graph(): | |
ret = rrdtool.graph("/usr/share/nginx/www/temperatures.png", "-w 760", "-h 400","--start", "-3d", "--vertical-label=Temperature (F)", '--title', "Server Room Temperature", | |
"DEF:A=temperatures.rrd:A:AVERAGE", | |
"DEF:B=temperatures.rrd:B:AVERAGE", | |
"DEF:C=temperatures.rrd:C:AVERAGE", | |
"DEF:D=temperatures.rrd:D:AVERAGE", | |
"DEF:E=temperatures.rrd:E:AVERAGE", | |
"CDEF:Ambient=A,B,+,2,/", | |
"CDEF:ATrend=Ambient,43200,TREND", | |
"CDEF:Back=C,D,+,E,+,3,/", | |
"CDEF:BTrend=Back,43200,TREND", | |
"LINE2:ATrend#FF0000:Ambient Trend", | |
"LINE2:BTrend#0000FF:Back of Rack Trend"+"\\r", | |
"LINE1:A#FF8000:"+TEMP_SENSORS['A']['location'], | |
"LINE1:B#A599FF:"+TEMP_SENSORS['B']['location']+"\\r", | |
"LINE1:C#557755:"+TEMP_SENSORS['C']['location'], | |
"LINE1:D#FF00FF:"+TEMP_SENSORS['D']['location'], | |
"LINE1:E#00FF00:"+TEMP_SENSORS['E']['location']) | |
update_rrd() | |
create_graph() | |
create_json() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment