Relatively easy, yes. For example, I display the temperature of my garage here. You can see all the other crap that I monitor here.

To do this I use a couple of shell scripts and mrtg.

N.B. anyone who is any good at Unix shell scripts, sed and the like, cover your eyes now. My Unix commandline knowledge is just barely enough to get by with stuff like this and I know it is nasty...

The first shell script is called "tempread" and looks like this:

Code:
#!/bin/bash

echo `head -n 3 /dev/ttyS0 | tail -n 1`



When you start reading data from the serial port (/dev/ttyS0) you get the following output:

Code:

R V1.0 2001-10-13 11:56:11 C

1 0012.68

1 0012.68

1 0012.75

... (it keeps outputing the temperature until you stop reading



So my shell script uses "head" to grab the first three lines of the output of the serial port. It then uses "tail" to grab the last line of the three.

The "tick" marks, i.e. the ` characters, tell bash to run the command inside and return the text generated.

So the output of tempread looks like:

Code:

1 0012.68



The next shell script is "tempmrtg":

Code:

#!/bin/bash
echo "`/usr/local/bin/tempread |cut -c 5-9` * 100" | bc | sed s/.00$//
echo "0"
echo "0"
echo "0"



This is the script that generates the output for mrtg. Because mrtg is really designed for monitoring router throughput usage it only deals with integers. It expects four values from any scripts it calls. The first is the throughtput in, second throughput out and the last two I forget.

Because I want better than whole number resolution I multiply my temperature by 100. So if the temperature is 12.68 I end up returning 1268 to mrtg.

The first line of "tempmrtg" calls "tempread", extras the temperature digits using cut, combines the temperature with "* 100" and passes that to bc (and command line calculator). It then uses a regex and sed to strip the ".00" off of the end of the resulting value.

The last thing is an entry in the mrtg config file that tell it to call "tempmrtg" to get the values:

Code:

Target[temp]: `/usr/local/bin/tempmrtg`
MaxBytes[temp]: 4000
YTicsFactor[temp]: 0.01
Factor[temp]: 0.01
Options[temp]: gauge, nopercent, noo, transparent
XSize[temp]: 600
YSize[temp]: 150
YLegend[temp]: Temp
ShortLegend[temp]: C
LegendO[temp]:  Nowt:
LegendI[temp]:  Temp C:
Title[temp]: Server room temp
PageTop[temp]: <H1>Server room Temperature
</H1>
<TABLE>
<TR><TD>System:</TD><TD>Server room temp</TD></TR>
</TABLE>



Amongst other things this tell mrtg that:
- it should run "tempmrtg" to get the values for this graph
- that it should divide all the values by 100
- that the temperature will never be more than 40 degrees
- that there is only one value to graph ("noo" tells it that)

Now that I have written all that down it seems a little more complex than I remembered

There are tools that I probably better than mrtg for this stuff now. I use mrtg because that is what I know. The most likely canditate would be rrdtool.


Edited by andy (15/04/2006 07:38)