Glen Pitt-Pladdy :: Blog
TEMPer under Linux (perl) with Cacti
I was looking for a cheap way to log temperatures, I hunted for a USB thermometer that could be used and came across the cheap and widely available "TEMPer" USB thermometers. These can be bought on eBay for under a tenner inclusive of shipping and are available in a number of variants - in my case TEMPer1 which has the probe on a lead so it can be placed away from the (hot) machine which the USB connects to and head generated in the USB bit doesn't affect the readings.
These are ideal for so many things:
A bit of scripting and you could use these to email / SMS warnings very easily. In my case I plumb them into Cacti from where you can use any of it's monitoring / warning plugins to do whatever you want with it.
TEMPer with Linux / Perl
The TEMPer devices behave as HID devices and there are a load of simple userspace programs to use them. In my case I noticed libdevice-usb-pcsensor-hidtemper-perl which is a Perl module for accessing them and available under recent Ubuntu and Debian (from wheezy). If you are not running a Ubuntu or Debian with them then grab the source package and build your own. I've successfully built the packages for Lucid and Squeeze and only had to edit debian/compat and change it to "7" to build libdevice-usb-perl on the older distros which is also needed.
Other than that once all the build dependencies are in a simple fakeroot dpkg-buildpackage produces ready to install packages. I am successfully using this under Ubuntu Lucid and Debian Squeeze with packages build like this.
The example code in the man pages is about all you need. The module is trivial to use - a quick bit of code and I had a snmpd extension script to pick the temperatures up. One catch is that they do sometimes produce false readings on the first read but that is easily solved by doing a dummy read first and then the real one.
The simple Perl script I to read all the TEMPer1 devices I have connected: readTEMPer
Make this executable and make sure you have privilege to read devices when you run it.
The TEMPer doesn't seem to be particularly accurate so the script has the option to add basic (scale & offset) calibration in. This can be done by measuring 2 known temperatures.
In my case I got a jug of hot water and the TEMPer said 76.5 while an accurate thermometer said 79.7 - then in room temperature water TEMPer said 17 and the accurate thermometer said 18.5.
Then we do some maths to work out the scale:
Now we need to work out the offset:
Let's verify those at the cold temperature: 17 * 1.029 + 1 = 18.5
So that's the calibration that you can add into the script:
my @CAL = ( # calibration list for each sensor
Be aware that if you have multiple devices, these seem to be ordered as they are detected which depends both on the bus (hub) and order of connection (or detection at boot). There is no serial number or anything about each TEMPer to tell them apart so you will have to experient and check how things get ordered depending on their connection.
TEMPer via SNMP
I have previously described SNMP basics and this script can be used as an snmpd extension script. Add the lines to snmpd.conf:
extend temper /etc/snmp/readTEMPer
.... or wherever you put the script, and restart snmpd.
The script will be run by snmpd as it's user. On Debian based systems that's the snmp user and group which doesn't have access to the device by default. To give access you can add the following into a rules file in /etc/udev/rules.d - in my case I called it 000-local.rules and it contains all my local modifications:
# TEMPer1 USB thermometer
Re-plug the device and that should be sufficient for it to be readable via snmpd. If you have other compatible devices you can use lsusb to check the vendor and product codes and add additional config as needed.
Graphing with Cacti
First off, as this template use MIN and MAX you need to makes sure that the right Consolidation Functions are set - under "Data Sources" select RRAs and for each RRA ensure that the AVERAGE, MIN and MAX are selected - I just selected the lot. Otherwise you will need to remove the traces from the graph template that don't have a matching Consolidation Function selected.
Download the TEMPer Cacti template and add the graph(s) of your choice from template and it should end up looking something like this depending on which graph you choose:
The basic template gives graphs for single sensors as well as 2 and 3 sensors on a graph, but it's easy to duplicate and make whatever variants suits you from the templates provided. For the averaged graphs there are min/max lines which should show the range in the averaging period - this is why we need the MIN and MAX CFs above. Again, if you don't want them then you can change the template to suit.
Often things work perfectly for one or sometimes two sensors, but more than that and it's likely that you will start ending up with missing data in Cacti. When you check via SNMP everything is fine and the data is there.
The problem I have discovered is the device timeout setting for SNMP in Cacti. It is often set to something like 500ms which is plenty for most things. TEMPer can however take a second or more to read all the devices so if you have this problem then it's likely that you will need increase the SNMP timeout for the host in Cacti.
Copyright Glen Pitt-Pladdy 2008-2017