Mercurial > templog
annotate web/log.py @ 77:959e88c0bdfa
ui tweaks
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 04 Jul 2012 22:36:52 +0800 |
parents | ca08442635ca |
children | c7b6da1adb47 |
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 |
49
206294a354a7
update values one at a time, ignore failure
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
12 import traceback |
67 | 13 import datetime |
75
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
73
diff
changeset
|
14 import struct |
77 | 15 import binascii |
29 | 16 from colorsys import hls_to_rgb |
27 | 17 |
28 | 18 import config |
27 | 19 |
20 def sensor_rrd_path(s): | |
28 | 21 return '%s/sensor_%s.rrd' % (config.DATA_PATH, s) |
27 | 22 |
29 | 23 # returns (path, sensor_name) tuples |
24 def all_sensors(): | |
25 return [(r, os.path.basename(r[:-4])) | |
26 for r in glob.glob('%s/*.rrd' % config.DATA_PATH)] | |
27 | |
27 | 28 def create_rrd(sensor_id): |
43
ea99aae87884
Create with a start date so that it works
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
29 # start date of 10 seconds into 1970 is used so that we can |
ea99aae87884
Create with a start date so that it works
Matt Johnston <matt@ucc.asn.au>
parents:
40
diff
changeset
|
30 # update with prior values straight away. |
61 | 31 if 'voltage' in sensor_id: |
63 | 32 args = [ |
33 '--step', '3600', | |
34 'DS:temp:GAUGE:7200:1:10', | |
35 'RRA:AVERAGE:0.5:1:87600'] | |
61 | 36 else: |
63 | 37 args = [ |
38 '--step', '300', | |
39 'DS:temp:GAUGE:600:-10:100', | |
61 | 40 'RRA:AVERAGE:0.5:1:1051200'] |
27 | 41 |
61 | 42 rrdtool.create(sensor_rrd_path(sensor_id), |
63 | 43 '--start', 'now-60d', |
61 | 44 *args) |
29 | 45 |
46 # stolen from viewmtn, stolen from monotone-viz | |
47 def colour_from_string(str): | |
48 def f(off): | |
49 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
|
50 hashval = hashlib.sha1(str).digest() |
29 | 51 hue = f(5) |
52 li = f(1) * 0.15 + 0.55 | |
53 sat = f(2) * 0.5 + .5 | |
54 return ''.join(["%.2x" % int(x * 256) for x in hls_to_rgb(hue, li, sat)]) | |
55 | |
56 def graph_png(start, length): | |
57 rrds = all_sensors() | |
58 | |
59 graph_args = [] | |
61 | 60 have_volts = False |
29 | 61 for n, (rrdfile, sensor) in enumerate(rrds): |
61 | 62 if 'avrtemp' in sensor: |
63 continue | |
64 if 'voltage' in sensor: | |
65 have_volts = True | |
66 vname = 'scalevolts' | |
71
7d243ba2dd39
swap the left and right scales, kind of clunky
Matt Johnston <matt@ucc.asn.au>
parents:
69
diff
changeset
|
67 graph_args.append('DEF:%(vname)s=%(rrdfile)s:temp:AVERAGE:step=3600' % locals()) |
61 | 68 else: |
69 vname = 'temp%d' % n | |
71
7d243ba2dd39
swap the left and right scales, kind of clunky
Matt Johnston <matt@ucc.asn.au>
parents:
69
diff
changeset
|
70 graph_args.append('DEF:raw%(vname)s=%(rrdfile)s:temp:AVERAGE' % locals()) |
7d243ba2dd39
swap the left and right scales, kind of clunky
Matt Johnston <matt@ucc.asn.au>
parents:
69
diff
changeset
|
71 graph_args.append('CDEF:%(vname)s=raw%(vname)s,0.1,*,2,+' % locals()) |
29 | 72 width = config.LINE_WIDTH |
73 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
|
74 colour = config.SENSOR_COLOURS.get(legend, colour_from_string(sensor)) |
29 | 75 graph_args.append('LINE%(width)f:%(vname)s#%(colour)s:%(legend)s' % locals()) |
76 | |
67 | 77 end = int(start+length) |
78 start = int(start) | |
79 | |
29 | 80 tempf = tempfile.NamedTemporaryFile() |
67 | 81 dateformat = '%H:%M:%S %Y-%m-%d' |
82 watermark = ("Now %s\t" | |
83 "Start %s\t" | |
84 "End %s" % ( | |
85 datetime.datetime.now().strftime(dateformat), | |
86 datetime.datetime.fromtimestamp(start).strftime(dateformat), | |
87 datetime.datetime.fromtimestamp(end).strftime(dateformat) )) | |
88 | |
89 args = [tempf.name, '-s', str(start), | |
90 '-e', str(end), | |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
91 '-w', str(config.GRAPH_WIDTH), |
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
92 '-h', str(config.GRAPH_HEIGHT), |
29 | 93 '--slope-mode', |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
94 '--border', '0', |
73
0a8639039453
Get rid of axes labels, tidy html
Matt Johnston <matt@ucc.asn.au>
parents:
71
diff
changeset
|
95 # '--vertical-label', 'Voltage', |
71
7d243ba2dd39
swap the left and right scales, kind of clunky
Matt Johnston <matt@ucc.asn.au>
parents:
69
diff
changeset
|
96 '--y-grid', '0.1:1', |
7d243ba2dd39
swap the left and right scales, kind of clunky
Matt Johnston <matt@ucc.asn.au>
parents:
69
diff
changeset
|
97 '--dynamic-labels', |
56 | 98 '--grid-dash', '1:0', |
77 | 99 '--zoom', str(config.ZOOM), |
56 | 100 '--color', 'GRID#00000000', |
101 '--color', 'MGRID#aaaaaa', | |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
102 '--color', 'BACK#ffffff', |
67 | 103 '--disable-rrdtool-tag', |
104 '--watermark', watermark, | |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
105 '--imgformat', 'PNG'] \ |
29 | 106 + graph_args |
67 | 107 args += ['--font', 'DEFAULT:12:%s' % config.GRAPH_FONT] |
108 args += ['--font', 'WATERMARK:10:%s' % config.GRAPH_FONT] | |
61 | 109 if have_volts: |
71
7d243ba2dd39
swap the left and right scales, kind of clunky
Matt Johnston <matt@ucc.asn.au>
parents:
69
diff
changeset
|
110 args += ['--right-axis', '10:-20', # matches the scalevolts CDEF above |
73
0a8639039453
Get rid of axes labels, tidy html
Matt Johnston <matt@ucc.asn.au>
parents:
71
diff
changeset
|
111 '--right-axis-format', '%.0lf', |
0a8639039453
Get rid of axes labels, tidy html
Matt Johnston <matt@ucc.asn.au>
parents:
71
diff
changeset
|
112 # '--right-axis-label', 'Temperature' |
0a8639039453
Get rid of axes labels, tidy html
Matt Johnston <matt@ucc.asn.au>
parents:
71
diff
changeset
|
113 ] |
61 | 114 |
29 | 115 rrdtool.graph(*args) |
116 return tempf.read() | |
117 | |
27 | 118 def sensor_update(sensor_id, measurements, first_real_time, time_step): |
119 try: | |
120 open(sensor_rrd_path(sensor_id)) | |
121 except IOError, e: | |
122 create_rrd(sensor_id) | |
123 | |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
124 if measurements: |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
125 values = ['%d:%f' % p for p in |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
126 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
|
127 measurements)] |
27 | 128 |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
129 rrdfile = sensor_rrd_path(sensor_id) |
40 | 130 # XXX what to do here when it fails... |
49
206294a354a7
update values one at a time, ignore failure
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
131 for v in values: |
206294a354a7
update values one at a time, ignore failure
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
132 try: |
206294a354a7
update values one at a time, ignore failure
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
133 rrdtool.update(rrdfile, v) |
63 | 134 except rrdtool.error, e: |
49
206294a354a7
update values one at a time, ignore failure
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
135 print>>sys.stderr, "Bad rrdtool update '%s'" % v |
206294a354a7
update values one at a time, ignore failure
Matt Johnston <matt@ucc.asn.au>
parents:
45
diff
changeset
|
136 traceback.print_exc(file=sys.stderr) |
33 | 137 |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
138 # be paranoid |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
139 f = file(rrdfile) |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
140 os.fsync(f.fileno()) |
27 | 141 |
33 | 142 def record_debug(lines): |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
143 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
|
144 f.write('===== %s =====\n' % time.strftime('%a, %d %b %Y %H:%M:%S')) |
33 | 145 f.writelines(('%s\n' % s for s in lines)) |
146 f.flush() | |
147 return f | |
148 | |
75
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
73
diff
changeset
|
149 def convert_ds18b20_12bit(reading): |
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
73
diff
changeset
|
150 value = struct.unpack('>h', binascii.unhexlify(reading))[0] |
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
73
diff
changeset
|
151 return value * 0.0625 |
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
73
diff
changeset
|
152 |
27 | 153 def parse(lines): |
33 | 154 |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
155 debugf = record_debug(lines) |
33 | 156 |
27 | 157 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
|
158 if len(entries) != len(lines): |
27 | 159 raise Exception("Keys are not unique") |
160 | |
161 num_sensors = int(entries['sensors']) | |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
162 num_measurements = int(entries['measurements']) |
27 | 163 |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
164 sensors = [entries['sensor_id%d' % n] for n in xrange(num_sensors)] |
27 | 165 |
166 meas = [] | |
167 for s in sensors: | |
168 meas.append([]) | |
169 | |
170 for n in xrange(num_measurements): | |
77 | 171 vals = [convert_ds18b20_12bit(x) for x in entries["meas%d" % n].strip().split()] |
27 | 172 if len(vals) != num_sensors: |
173 raise Exception("Wrong number of sensors for measurement %d" % n) | |
174 # we make an array of values for each sensor | |
175 for s in xrange(num_sensors): | |
176 meas[s].append(vals[s]) | |
177 | |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
178 avr_now = float(entries['now']) |
27 | 179 avr_first_time = float(entries['first_time']) |
180 time_step = float(entries['time_step']) | |
181 | |
61 | 182 if 'avrtemp' in entries: |
183 avrtemp = val_scale(int(entries['avrtemp'])) | |
184 sensor_update('avrtemp', [avrtemp], time.time(), 1) | |
185 | |
186 if 'voltage' in entries: | |
187 voltage = 0.001 * int(entries['voltage']) | |
188 sensor_update('voltage', [voltage], time.time(), 1) | |
189 | |
40 | 190 #sqlite |
191 # - time | |
192 # - voltage | |
193 # - boot time | |
194 | |
27 | 195 first_real_time = time.time() - (avr_now - avr_first_time) |
196 | |
197 for sensor_id, measurements in zip(sensors, meas): | |
40 | 198 # XXX sqlite add |
27 | 199 sensor_update(sensor_id, measurements, first_real_time, time_step) |
33 | 200 |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
33
diff
changeset
|
201 debugf.write("Updated %d sensors\n" % len(sensors)) |
33 | 202 debugf.flush() |