rrd.py 5.55 KB
Newer Older
julric's avatar
julric committed
1 2 3
import output
import datetime
import rrdtool
4
from subprocess import check_output
julric's avatar
julric committed
5 6 7 8 9 10 11 12 13 14 15 16
from rrdtool import update as rrd_update

class rrd(output.Output):
        requiredData = ["rrd_file"]
        optionalData = []
        def __init__(self,data):
                self.rrd_file=data["rrd_file"]

        def outputData(self,dataPoints):

                rrd_value = "N"

17 18 19
# build the RRD-update-string in the following order:
# TemperatureBMP:TemperatureDHT:Pressure:Relative_Humidity:Light_Level:Hitrogen_Dioxide:Carbon_Monoxide:Volume:Smoke_Level

julric's avatar
julric committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
                if [i for i, x in enumerate(dataPoints) if x["name"] == "TemperatureBMP" and x["sensor"] == "BMP085"]:
                        array_index = [i for i, x in enumerate(dataPoints) if x["name"] == "TemperatureBMP" and x["sensor"] == "BMP085"][0]                
                        rrd_value = rrd_value + ":" + str(int(round(dataPoints[array_index]["value"])))
                else:
                        rrd_value = rrd_value + ":U"
                if [i for i, x in enumerate(dataPoints) if x["name"] == "TemperatureDHT" and x["sensor"] == "DHT22"]:
                        array_index = [i for i, x in enumerate(dataPoints) if x["name"] == "TemperatureDHT" and x["sensor"] == "DHT22"][0]
                        rrd_value = rrd_value + ":" + str(int(round(dataPoints[array_index]["value"])))
                else:
                        rrd_value = rrd_value + ":U"
                if [i for i, x in enumerate(dataPoints) if x["name"] == "Pressure" and x["sensor"] == "BMP085"]:
                        array_index = [i for i, x in enumerate(dataPoints) if x["name"] == "Pressure" and x["sensor"] == "BMP085"][0]                
                        rrd_value = rrd_value + ":" + str(int(round(dataPoints[array_index]["value"])))
                else:
                        rrd_value = rrd_value + ":U"
                if [i for i, x in enumerate(dataPoints) if x["name"] == "Relative_Humidity" and x["sensor"] == "DHT22"]:
                        array_index = [i for i, x in enumerate(dataPoints) if x["name"] == "Relative_Humidity" and x["sensor"] == "DHT22"][0]                
                        rrd_value = rrd_value + ":" + str(int(round(dataPoints[array_index]["value"])))
                else:
                        rrd_value = rrd_value + ":U"
                if [i for i, x in enumerate(dataPoints) if x["name"] == "Light_Level" and x["sensor"] == "LDR"]:
                        array_index = [i for i, x in enumerate(dataPoints) if x["name"] == "Light_Level" and x["sensor"] == "LDR"][0]                
                        rrd_value = rrd_value + ":" + str(int(round(dataPoints[array_index]["value"])))
                else:
                        rrd_value = rrd_value + ":U"
                if [i for i, x in enumerate(dataPoints) if x["name"] == "Nitrogen_Dioxide" and x["sensor"] == "MiCS-2710"]:
                        array_index = [i for i, x in enumerate(dataPoints) if x["name"] == "Nitrogen_Dioxide" and x["sensor"] == "MiCS-2710"][0]                
                        rrd_value = rrd_value + ":" + str(int(round(dataPoints[array_index]["value"])))
                else:
                        rrd_value = rrd_value + ":U"
                if [i for i, x in enumerate(dataPoints) if x["name"] == "Carbon_Monoxide" and x["sensor"] == "MiCS-5525"]:
                        array_index = [i for i, x in enumerate(dataPoints) if x["name"] == "Carbon_Monoxide" and x["sensor"] == "MiCS-5525"][0]                
                        rrd_value = rrd_value + ":" + str(int(round(dataPoints[array_index]["value"])))
                else:
                        rrd_value = rrd_value + ":U"
                if [i for i, x in enumerate(dataPoints) if x["name"] == "Volume" and x["sensor"] == "ABM_713_RC"]:
                        array_index = [i for i, x in enumerate(dataPoints) if x["name"] == "Volume" and x["sensor"] == "ABM_713_RC"][0]                
                        rrd_value = rrd_value + ":" + str(int(round(dataPoints[array_index]["value"])))
                else:
                        rrd_value = rrd_value + ":U"
60 61 62 63 64 65
                if [i for i, x in enumerate(dataPoints) if x["name"] == "Smoke_Level" and x["sensor"] == "TGS2600"]:
                        array_index = [i for i, x in enumerate(dataPoints) if x["name"] == "Smoke_Level" and x["sensor"] == "TGS2600"][0]
                        rrd_value = rrd_value + ":" + str(int(round(dataPoints[array_index]["value"])))
                else:
                        rrd_value = rrd_value + ":U"
                                                                                                        
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
#                print rrd_value 
# prints something like "N:20:21:999:44:14127:34286:641304:2758:17073"


# get the number of datasources in the RRD-file

                last = check_output(["rrdtool", "lastupdate", self.rrd_file])
                lastvalues = last.split( )
                datasources = (len(lastvalues)-1)/2

# array lastvalues consists of every sensor-name, then the time and the sensor-values:
# ['tempbmp', 'tempdht', 'press', 'humi', 'light', 'no2', 'co', 'vol', 'smoke', '1406917791:', '9969', '28', '30', '993', '40', '1840', '64672', '424615', '3081']

# check if number of sensor values matches number of items in the RRD-file
                if datasources == (len(rrd_value.split(':'))-1):
                        ret = rrd_update(self.rrd_file, rrd_value);
                        return True
                else:
                        print "ERROR: Your RRD-datasources missmatch the number of polled sensors!"
                        return False