rrdtool - Python (PyRRD) and RRD with while loop? -
solved, added solution further down i've been trying create graph pyrrd output of thermal sensor have connected raspberry pi, no luck getting actual data show in graph (but png-file created). i'm not sure if right way it, code spits out temperature every second, while loop works @ least.
import os import glob import time import subprocess # rdd-imports pyrrd.graph import def, cdef, vdef pyrrd.graph import line, area, gprint pyrrd.graph import colorattributes, graph pyrrd.rrd import datasource, rra, rrd # sensor-stuff os.system('modprobe w1-gpio') os.system('modprobe w1-therm') base_dir = '/sys/bus/w1/devices/' device_folder = glob.glob(base_dir + '28*')[0] device_file = device_folder + '/w1_slave' # rrd-stuff starttime = int(time.time()) filename = 'temptest.rrd' datasources = [] rras = [] datasource = datasource(dsname='temp', dstype='derive', heartbeat=5) datasources.append(datasource) rra1 = rra(cf='average', xff=0.5, steps=1, rows=5) rra2 = rra(cf='average', xff=0.5, steps=6, rows=10) rras.extend([rra1, rra2]) myrrd = rrd(filename, ds=datasources, rra=rras, start=starttime) myrrd.create() # graph-making graphfile = 'tempgraf.png' def1 = def(rrdfile=myrrd.filename, vname='mytemp', dsname=datasource.name) # data going green field cdef1 = cdef(vname='temp', rpn='%s,3600,*' % def1.vname) # line max value line1 = line(value=30, color='#990000', legend='max temp allowed') # green area area1 = area(defobj=cdef1, color='#006600', legend='temp') def read_temp_raw(): catdata = subprocess.popen(['cat',device_file], stdout=subprocess.pipe, stderr=subprocess.pipe) out,err = catdata.communicate() out_decode = out.decode('utf-8') lines = out_decode.split('\n') return lines def read_temp(): lines = read_temp_raw() 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 = float(temp_string) / 1000.0 myrrd.buffervalue(int(time.time()), int(temp_c)) myrrd.update() return temp_c while true: print(read_temp()) g = graph(graphfile, start=starttime, end=int(time.time()), vertical_label='temp(c)') g.data.extend([def1, cdef1, line1, area1]) g.write() time.sleep(1)
i have been experimenting around, reading alot in rrd manual , tutorial beginners can't right. i'm unsure rpn-stuff in #graph-making part. please me :) if there better way this, please tell me!
solution (to problem): dropped pyrrd , tried out rrdtools own python implementation. http://oss.oetiker.ch/rrdtool/prog/rrdpython.en.html
i created database outside of program , set step correctly in terminal (linux):
rrdtool create dailytemp.rrd --step 5 \ ds:temp:gauge:10:-100:200 \ rra:average:0.5:1:2880 rra:max:0.9:1:2880 \
i dropped code connected pyrrd , added import lines , 1 row rrdtool update. cleaner , can create graphs :d here "final" code:
import os import glob import time import subprocess import sys sys.path.append('/usr/local/lib/python2.7/site-packages/') import rrdtool, tempfile # sensor-stuff os.system('modprobe w1-gpio') os.system('modprobe w1-therm') base_dir = '/sys/bus/w1/devices/' device_folder = glob.glob(base_dir + '28*')[0] device_file = device_folder + '/w1_slave' # rrd-stuff, not specific starttime = int(time.time()) def read_temp_raw(): catdata = subprocess.popen(['cat',device_file], stdout=subprocess.pipe, stderr=subprocess.pipe) out,err = catdata.communicate() out_decode = out.decode('utf-8') lines = out_decode.split('\n') return lines def read_temp(): lines = read_temp_raw() 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 = float(temp_string) / 1000.0 print(int(temp_c)) print(int(time.time())) rrdtool.update('dailytemp.rrd','n:' + `temp_c`) return temp_c while true: print(read_temp()) time.sleep(5)
i have not implemented graph creating code yet can print out readings last 2 hours with:
rrdtool graph temp120.png --end --start end-7200s --width 400 \ def:ds0a=dailytemp.rrd:temp:average \
graph result (work in progress): adding picture got needed reputation (10)
it looks getting confused rrdtool. advice use rrdtool first generate graph manually. once have understood functioning can move on pyrrd.
these urls contain pretty documentation on rrd
http://oss.oetiker.ch/rrdtool/doc/index.en.html
if have rrd generated use following url , try generate graph manually.
http://oss.oetiker.ch/rrdtool/doc/rrdgraph.en.html
since mentioned empty png file getting generated please make sure data getting updated rrd properly. use rrdtool fetch figure out whether real data got updated in rrd.
Comments
Post a Comment