#!/usr/bin/python import sys import time import glob import os import commands import MySQLdb path = '/opt/tempberrypi' def getVarFromFile(filename): import imp f = open(filename) global data data = imp.load_source('data', '', f) f.close() getVarFromFile(path + '/etc/TBp1.conf') # find the path of a sensor directory that starts with 28 db = MySQLdb.connect(data.servername,data.username,data.password,data.dbname) cursor = db.cursor() i = 1 textdata = '' rrdtool_data = "/usr/bin/rrdtool create /opt/tempberrypi/rrds/sensortemp.rrd --start now --step 60 " devicelist = glob.glob('/sys/bus/w1/devices/*/w1_slave') date = commands.getoutput("/bin/date +%s") targetfile = path + "/etc/rrd_sensors.txt" os.rename(os.path.realpath(targetfile), os.path.realpath(targetfile)+"."+date) f = open(os.path.realpath(targetfile), "w") for sensor in devicelist: sensor_id = sensor.split('/') # Select SQL with id=4. cursor.execute("SELECT * FROM sensors_info WHERE sensor_id = '"+ str(sensor_id[5]) +"' ORDER BY sid ASC") #data = cursor.fetchall() #print "data = "+str(data[1]) row = cursor.fetchone() if row: #if (row[3] == "on" and row[4] == "yes" and row[6] == "on"): if (row[4] == "on" and row[5] == "yes"): rrdtool_data = rrdtool_data + "DS:temp"+str(i)+":GAUGE:120:-200:2000 " i = i + 1 print "==========================================================================" print "= " print "= The sensor with ID "+ str(sensor_id[5]) + "" print "= " print "= Plots are \""+ row[7]+"\" for this sensor" print "= " print "==========================================================================" print "" continue cursor.close () db.close() rrdtool_data = rrdtool_data + " \ RRA:AVERAGE:0.5:1:12 \ RRA:AVERAGE:0.5:1:288 \ RRA:AVERAGE:0.5:12:168 \ RRA:AVERAGE:0.5:12:720 \ RRA:AVERAGE:0.5:288:365 \ RRA:MIN:0.5:1:600 \ RRA:MIN:0.5:6:700 \ RRA:MIN:0.5:24:775 \ RRA:MIN:0.5:144:1500 \ RRA:MIN:0.5:288:2000 \ RRA:MAX:0.5:1:60 \ RRA:MAX:0.5:6:700 \ RRA:MAX:0.5:24:775 \ RRA:MAX:0.5:144:1500 \ RRA:MAX:0.5:288:2000" f.write(rrdtool_data) f.close() date = commands.getoutput(rrdtool_data) commands.getoutput("/bin/rm ./c") sys.exit()