27
|
1 import rrdtool |
|
2 import os |
29
|
3 import os.path |
27
|
4 import sys |
29
|
5 import glob |
|
6 from colorsys import hls_to_rgb |
27
|
7 |
28
|
8 import config |
27
|
9 |
|
10 def sensor_rrd_path(s): |
28
|
11 return '%s/sensor_%s.rrd' % (config.DATA_PATH, s) |
27
|
12 |
29
|
13 # returns (path, sensor_name) tuples |
|
14 def all_sensors(): |
|
15 return [(r, os.path.basename(r[:-4])) |
|
16 for r in glob.glob('%s/*.rrd' % config.DATA_PATH)] |
|
17 |
27
|
18 def create_rrd(sensor_id): |
|
19 rrdtool.create(sensor_rrd_path(sensor_id), '-s', '300', |
|
20 'DS:temp:GAUGE:600:-10:100', |
|
21 'RRA:AVERAGE:0.5:1:1051200') |
|
22 |
29
|
23 |
|
24 # stolen from viewmtn, stolen from monotone-viz |
|
25 def colour_from_string(str): |
|
26 def f(off): |
|
27 return ord(hashval[off]) / 256.0 |
|
28 hashval = sha.new(str).digest() |
|
29 hue = f(5) |
|
30 li = f(1) * 0.15 + 0.55 |
|
31 sat = f(2) * 0.5 + .5 |
|
32 return ''.join(["%.2x" % int(x * 256) for x in hls_to_rgb(hue, li, sat)]) |
|
33 |
|
34 def graph_png(start, length): |
|
35 rrds = all_sensors() |
|
36 |
|
37 graph_args = [] |
|
38 for n, (rrdfile, sensor) in enumerate(rrds): |
|
39 vname = 'temp%d' % n |
|
40 graph_args.append('DEF:%(vname)s=%(rrdfile)s:temp:AVERAGE' % locals()) |
|
41 width = config.LINE_WIDTH |
|
42 legend = config.SENSOR_NAMES.get(sensor, sensor) |
|
43 colour = config.SENSOR_COLOURS.get(legend, colour_from_string(r)) |
|
44 graph_args.append('LINE%(width)f:%(vname)s#%(colour)s:%(legend)s' % locals()) |
|
45 |
|
46 tempf = tempfile.NamedTemporaryFile() |
|
47 args = [tempf.name, '-s', str(start), |
|
48 '-e', str(start+length), |
|
49 '-w', config.GRAPH_WIDTH, |
|
50 '--slope-mode', |
|
51 '--imgformat', 'PNG'] |
|
52 + graph_args |
|
53 rrdtool.graph(*args) |
|
54 return tempf.read() |
|
55 |
27
|
56 def sensor_update(sensor_id, measurements, first_real_time, time_step): |
|
57 try: |
|
58 open(sensor_rrd_path(sensor_id)) |
|
59 except IOError, e: |
|
60 create_rrd(sensor_id) |
|
61 |
|
62 value_text = ' '.join('%f:%f' % p for p in |
|
63 zip(measurements, |
|
64 (first_real_time + time_step*t for t in xrange(len(measurements))))) |
|
65 |
|
66 rrdtool.update(sensor_rrd_path(sensor_id), value_text) |
|
67 |
|
68 def parse(lines): |
|
69 entries = dict(l.split('=', 1) for l in lines) |
|
70 if len(entries) != len(lines); |
|
71 raise Exception("Keys are not unique") |
|
72 |
|
73 num_sensors = int(entries['sensors']) |
|
74 num_measurements = int(entries['sensors']) |
|
75 |
|
76 sensor_ids = [entries['sensor_id%d' % n] for n in xrange(num_sensors)] |
|
77 |
|
78 meas = [] |
|
79 for s in sensors: |
|
80 meas.append([]) |
|
81 |
28
|
82 def val_scale(v): |
|
83 # convert decidegrees to degrees |
|
84 return 0.1 * v |
|
85 |
27
|
86 for n in xrange(num_measurements): |
28
|
87 vals = [val_scale(int(entries["meas%d" % n].strip().split()))] |
27
|
88 if len(vals) != num_sensors: |
|
89 raise Exception("Wrong number of sensors for measurement %d" % n) |
|
90 # we make an array of values for each sensor |
|
91 for s in xrange(num_sensors): |
|
92 meas[s].append(vals[s]) |
|
93 |
|
94 avr_now = float(entries['now') |
|
95 avr_first_time = float(entries['first_time']) |
|
96 time_step = float(entries['time_step']) |
|
97 |
|
98 first_real_time = time.time() - (avr_now - avr_first_time) |
|
99 |
|
100 for sensor_id, measurements in zip(sensors, meas): |
|
101 sensor_update(sensor_id, measurements, first_real_time, time_step) |