Mercurial > templog
annotate web/log.py @ 44:96c336896201
mostly works for testing
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 23 Jun 2012 23:37:29 +0800 |
parents | 9b5b202129c3 |
children | 425af63d584d |
rev | line source |
---|---|
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
1 #:vim:et:ts=4:sts=4:sw=4: |
27 | 2 import rrdtool |
3 import os | |
29 | 4 import os.path |
27 | 5 import sys |
29 | 6 import glob |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
7 import hashlib |
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
8 import tempfile |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
9 import time |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
10 import syslog |
44 | 11 import sqlite3 |
29 | 12 from colorsys import hls_to_rgb |
27 | 13 |
28 | 14 import config |
27 | 15 |
16 def sensor_rrd_path(s): | |
28 | 17 return '%s/sensor_%s.rrd' % (config.DATA_PATH, s) |
27 | 18 |
29 | 19 # returns (path, sensor_name) tuples |
20 def all_sensors(): | |
21 return [(r, os.path.basename(r[:-4])) | |
22 for r in glob.glob('%s/*.rrd' % config.DATA_PATH)] | |
23 | |
27 | 24 def create_rrd(sensor_id): |
25 rrdtool.create(sensor_rrd_path(sensor_id), '-s', '300', | |
26 'DS:temp:GAUGE:600:-10:100', | |
27 'RRA:AVERAGE:0.5:1:1051200') | |
28 | |
29 | 29 |
30 # stolen from viewmtn, stolen from monotone-viz | |
31 def colour_from_string(str): | |
32 def f(off): | |
33 return ord(hashval[off]) / 256.0 | |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
34 hashval = hashlib.sha1(str).digest() |
29 | 35 hue = f(5) |
36 li = f(1) * 0.15 + 0.55 | |
37 sat = f(2) * 0.5 + .5 | |
38 return ''.join(["%.2x" % int(x * 256) for x in hls_to_rgb(hue, li, sat)]) | |
39 | |
40 def graph_png(start, length): | |
41 rrds = all_sensors() | |
42 | |
43 graph_args = [] | |
44 for n, (rrdfile, sensor) in enumerate(rrds): | |
45 vname = 'temp%d' % n | |
46 graph_args.append('DEF:%(vname)s=%(rrdfile)s:temp:AVERAGE' % locals()) | |
47 width = config.LINE_WIDTH | |
48 legend = config.SENSOR_NAMES.get(sensor, sensor) | |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
49 colour = config.SENSOR_COLOURS.get(legend, colour_from_string(sensor)) |
29 | 50 graph_args.append('LINE%(width)f:%(vname)s#%(colour)s:%(legend)s' % locals()) |
51 | |
52 tempf = tempfile.NamedTemporaryFile() | |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
53 args = [tempf.name, '-s', str(int(start)), |
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
54 '-e', str(int(start+length)), |
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
55 '-w', str(config.GRAPH_WIDTH), |
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
56 '-h', str(config.GRAPH_HEIGHT), |
29 | 57 '--slope-mode', |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
58 '--border', '0', |
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
59 '--color', 'BACK#ffffff', |
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
60 '--imgformat', 'PNG'] \ |
29 | 61 + graph_args |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
62 if config.GRAPH_FONT: |
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
63 args += ['--font', 'DEFAULT:0:%s' % config.GRAPH_FONT] |
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
64 print>>sys.stderr, args |
29 | 65 rrdtool.graph(*args) |
66 return tempf.read() | |
67 | |
27 | 68 def sensor_update(sensor_id, measurements, first_real_time, time_step): |
69 try: | |
70 open(sensor_rrd_path(sensor_id)) | |
71 except IOError, e: | |
72 create_rrd(sensor_id) | |
73 | |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
74 print>>sys.stderr, sensor_id, measurements, first_real_time, time_step |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
75 |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
76 if measurements: |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
77 values = ['%d:%f' % p for p in |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
78 zip((first_real_time + time_step*t for t in xrange(len(measurements))), |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
79 measurements)] |
27 | 80 |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
81 rrdfile = sensor_rrd_path(sensor_id) |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
82 print>>sys.stderr, values |
40 | 83 # XXX what to do here when it fails... |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
84 rrdtool.update(rrdfile, *values) |
33 | 85 |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
86 # be paranoid |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
87 f = file(rrdfile) |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
88 os.fsync(f.fileno()) |
27 | 89 |
33 | 90 def record_debug(lines): |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
91 f = open('%s/debug.log' % config.DATA_PATH, 'a+') |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
92 f.write('===== %s =====\n' % time.strftime('%a, %d %b %Y %H:%M:%S')) |
33 | 93 f.writelines(('%s\n' % s for s in lines)) |
94 f.flush() | |
95 return f | |
96 | |
27 | 97 def parse(lines): |
33 | 98 |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
99 debugf = record_debug(lines) |
33 | 100 |
27 | 101 entries = dict(l.split('=', 1) for l in lines) |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
102 if len(entries) != len(lines): |
27 | 103 raise Exception("Keys are not unique") |
104 | |
105 num_sensors = int(entries['sensors']) | |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
106 num_measurements = int(entries['measurements']) |
27 | 107 |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
108 sensors = [entries['sensor_id%d' % n] for n in xrange(num_sensors)] |
27 | 109 |
110 meas = [] | |
111 for s in sensors: | |
112 meas.append([]) | |
113 | |
28 | 114 def val_scale(v): |
115 # convert decidegrees to degrees | |
116 return 0.1 * v | |
117 | |
27 | 118 for n in xrange(num_measurements): |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
119 vals = [val_scale(int(x)) for x in entries["meas%d" % n].strip().split()] |
27 | 120 if len(vals) != num_sensors: |
121 raise Exception("Wrong number of sensors for measurement %d" % n) | |
122 # we make an array of values for each sensor | |
123 for s in xrange(num_sensors): | |
124 meas[s].append(vals[s]) | |
125 | |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
126 avr_now = float(entries['now']) |
27 | 127 avr_first_time = float(entries['first_time']) |
128 time_step = float(entries['time_step']) | |
129 | |
40 | 130 #sqlite |
131 # - time | |
132 # - voltage | |
133 # - boot time | |
134 | |
27 | 135 first_real_time = time.time() - (avr_now - avr_first_time) |
136 | |
137 for sensor_id, measurements in zip(sensors, meas): | |
40 | 138 # XXX sqlite add |
27 | 139 sensor_update(sensor_id, measurements, first_real_time, time_step) |
33 | 140 |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
141 debugf.write("Updated %d sensors\n" % len(sensors)) |
33 | 142 debugf.flush() |