summaryrefslogtreecommitdiff
path: root/envmon/onewire2graphite.py
blob: 4b7743e04a1c479097f8f59a306e1b9026be3cab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/python

import time
import sys

import onewire
import socket

CARBON_SERVER = '2a02:238:f02a:8e2f:1:67:7261:7068' # 'sonar.local.sublab.org'
CARBON_PORT = 2003

prefix = "sublab.beaglebone.env."

sock = socket.socket(socket.AF_INET6)
sock.connect((CARBON_SERVER, CARBON_PORT))

sensors = onewire.sensors()
sensobj = dict([(sensor, onewire.sensor(sensor)) for sensor in sensors])
vals = {}

while True:
    nextslot = time.time() + 65
    nextslot = nextslot - (nextslot % 60)

    while nextslot - time.time() > 5.0:
        for sensor in sensors:
            try:
                vals.setdefault(sensor, None)
                vals[sensor] = sensobj[sensor].temperature
            except onewire.SensorNotPresent, e:
                print >>sys.stderr, sensor, 'SensorNotPresent:', str(e)
            except onewire.OutOfRange, e:
                # print >>sys.stderr, sensor, 'OutOfRange:', str(e)
                pass
            except Exception:
                print >>sys.stderr, "On %s: Could not retrieve temperature for '%s':" % (
                    time.strftime('%a, %d %b %Y %T %z'), sensor)
                sys.excepthook(*sys.exc_info())
                print >>sys.stderr, '========================================'
        time.sleep(0.5)

    time.sleep(max(nextslot - time.time(), 0))
    goodvals = [sensor for sensor in sensors if vals.get(sensor, None) is not None]
    print '%d of %d sensors have data' % (len(goodvals), len(sensors))
    message = ''.join(["%s %f %d\n" % (prefix + sensor, vals[sensor], nextslot) for sensor in goodvals])
    if message != '':
        # print message
        sock.sendall(message)