2009-10-11

Feeding lm_sensors Data to a Remote Database

A more correct way to do this would be to use libsensors(3) but I couldn't find Python bindings for it, so I just read and parse the output of the sensors(1) program. The temperature and fanspeed readings come from a spare Asus Eee PC and the raw data can be seen here (may be offline). I wish I had a more varying and interesting data source but this will do for now for the purpose of playing with Google Chart API.


#!/usr/bin/env python

import os, scanf

temperature = 0
fanspeed = 0

for line in os.popen('/usr/bin/sensors'):
    if len(line) < 2:
        continue

    fields = line.split()

# Python scanf from http://hkn.eecs.berkeley.edu/~dyoo/python/scanf/

    key = fields[0]
    if key == 'temp1:':
        temperature = scanf.sscanf(fields[1], '+%d.0')
    if key == 'fan1:':
        fanspeed = scanf.sscanf(fields[1], '%d')

import urllib2

# Authentication code ripped from python.org docs

password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
top_level_url = "http://jonikahara.com/lab/thermometer/update"
password_mgr.add_password(None, top_level_url, 'USERNAME', 'PASSWORD')
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
opener.open('http://jonikahara.com/lab/thermometer/update?' + 'temperature=' + str(temperature[0]) + '&fanspeed=' + str(fanspeed[0]))

Sunday, Kauppakatu (Photo)

Sunday, Kauppakatu