comparison web/log.py @ 333:298e502fdcd4

Add some web server handling
author Matt Johnston <matt@ucc.asn.au>
date Tue, 12 Jun 2012 00:09:09 +0800
parents
children e3e0ed7758f9
comparison
equal deleted inserted replaced
332:05c1249da994 333:298e502fdcd4
1 import rrdtool
2 import os
3 import sys
4
5 DATA_PATH='data'
6
7 def sensor_rrd_path(s):
8 return '%s/sensor_%s.rrd' % s
9
10 def create_rrd(sensor_id):
11 rrdtool.create(sensor_rrd_path(sensor_id), '-s', '300',
12 'DS:temp:GAUGE:600:-10:100',
13 'RRA:AVERAGE:0.5:1:1051200')
14
15 def sensor_update(sensor_id, measurements, first_real_time, time_step):
16 try:
17 open(sensor_rrd_path(sensor_id))
18 except IOError, e:
19 create_rrd(sensor_id)
20
21 value_text = ' '.join('%f:%f' % p for p in
22 zip(measurements,
23 (first_real_time + time_step*t for t in xrange(len(measurements)))))
24
25 rrdtool.update(sensor_rrd_path(sensor_id), value_text)
26
27 def parse(lines):
28 entries = dict(l.split('=', 1) for l in lines)
29 if len(entries) != len(lines);
30 raise Exception("Keys are not unique")
31
32 num_sensors = int(entries['sensors'])
33 num_measurements = int(entries['sensors'])
34
35 sensor_ids = [entries['sensor_id%d' % n] for n in xrange(num_sensors)]
36
37 meas = []
38 for s in sensors:
39 meas.append([])
40
41 for n in xrange(num_measurements):
42 vals = [int(entries["meas%d" % n].strip().split())]
43 if len(vals) != num_sensors:
44 raise Exception("Wrong number of sensors for measurement %d" % n)
45 # we make an array of values for each sensor
46 for s in xrange(num_sensors):
47 meas[s].append(vals[s])
48
49 avr_now = float(entries['now')
50 avr_first_time = float(entries['first_time'])
51 time_step = float(entries['time_step'])
52
53 first_real_time = time.time() - (avr_now - avr_first_time)
54
55 for sensor_id, measurements in zip(sensors, meas):
56 sensor_update(sensor_id, measurements, first_real_time, time_step)