Raspberry katilines monitoringas
Jump to navigation
Jump to search
Script
#!/usr/bin/python
# (c) 2019 justinas@eofnet.lt
import os
import time
import argparse as ap
from rrdtool import update as rrd_update
import rrdtool
sensorsdb = '/etc/mods/sensors.rrd'
sensors = [{"hwid":"28-03049779621c", "ds":"sensor1", "name":"1 sensorius", "color":"#ff0000"},
{"hwid":"28-03049779691d", "ds":"sensor2", "name":"2 sensorius", "color":"#0000ff"},
{"hwid":"28-03049779b25b", "ds":"sensor3", "name":"3 sensorius", "color":"#3cb371"},
{"hwid":"28-03049779d1ab", "ds":"sensor4", "name":"4 sensorius", "color":"#6a5acd"}]
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
def read_temp_raw(dev):
f = open("/sys/bus/w1/devices/"+dev+"/w1_slave",'r')
lines = f.readlines()
f.close()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = round((float(temp_string) / 1000.0),2)
return str(temp_c)
def GenDefs():
defs = []
for sensor in sensors:
defs.append("DEF:"+sensor["ds"]+"="+sensorsdb+":"+sensor["ds"]+":AVERAGE")
return defs
def GenLines():
lines = []
count = 1
for sensor in sensors:
lines.append("LINE"+str(count)+":"+sensor["ds"]+sensor["color"]+":"+sensor["name"])
lines.append("GPRINT:"+sensor["ds"]+":LAST:Last\:%8.2lf %s")
lines.append("GPRINT:"+sensor["ds"]+":MIN:Min\:%8.2lf %s")
lines.append("GPRINT:"+sensor["ds"]+":AVERAGE:Average\:%8.2lf %s")
lines.append("GPRINT:"+sensor["ds"]+":MAX:Max\:%8.2lf %s\\n")
count += 1
return lines
def ReadAllSensors():
for sensor in sensors:
temp = str(read_temp_raw(sensor["hwid"]))
print "Sensor: "+str(sensor["name"])+" temp: "+temp+"C"
def Update():
print "Updating..."
template = "N"
for sensor in sensors:
tempc = read_temp_raw(sensor["hwid"])
template = template + ":"+tempc
print "Sensor: "+sensor["name"]+" temp: "+tempc+"C"
ret = rrd_update(sensorsdb, template)
if ret:
print rrdtool.error()
def Graph():
print "Graphing..."
for sched in ['daily' , 'weekly', 'monthly', 'yearly']:
if sched == 'weekly':
period = 'w'
elif sched == 'daily':
period = 'd'
elif sched == 'monthly':
period = 'm'
elif sched == 'yearly':
period = 'y'
ret = rrdtool.graph( "/var/www/html/temp-%s.png" %(sched), "--start", "-1%s" %(period), "--vertical-label=Temperatura",
'--watermark=Temperatures of the heating system',
"-w 1000 -h 600",
GenDefs(), GenLines())
parser = ap.ArgumentParser(description="Show temperature sensors readings or make cool graphs :)")
parser.add_argument('--graph', help='Write cool graph',action='store_true')
parser.add_argument('--update', help='Update RRD database',action='store_true')
args = parser.parse_args()
if args.graph is True:
Graph()
elif args.update is True:
Update()
else:
ReadAllSensors()
Cron
* * * * * root /root/temp/read_multiple.py --update > /dev/null 2>&1 */5 * * * * root /root/temp/read_multiple.py --graph > /dev/null 2>&1