Monday, January 24, 2011

collectd-mod-exec Part 3

Part1
Part2

I'll continue with the tmpcollect.sh script from the second part of this How-To. First of all I want to optimize it a bit. Running sed two times is not very performant and is also not necessary. Updated script is shown below:
root@Alix:~# cat /mnt/sd/bin/tmpcollect.sh
#!/bin/sh

logger "*** tmpcollect script is started ***"
HOST=$COLLECTD_HOSTNAME
INTERVAL=$COLLECTD_INTERVAL
[ -z "$INTERVAL" ] && INTERVAL=10
INTERVAL=$(awk -v i=$INTERVAL 'BEGIN{print int(i)}')
while sleep $INTERVAL; do
  values=$(echo `sensors | sed -r -n 's/temp[0-9][^0-9]*([^ ]*).*/\1/p'`)
  echo "PUTVAL \"$HOST/exec-temperature_sensors/sensors\" interval=$INTERVAL N:${values#*\ }:${values%\ *}"
done
You may have noticed that I'm not using 'temperature' data type any longer. Instead I'm using 'sensors' data type I've added into the 'types' database. This new type is accepting two values per one time interval (or per raw) allowing both values to be displayed on a single graph.
First of all I need to add a new data type into the 'types' database located in the /usr/share/collectd/types.db file. As far as my data is temperature I can reuse an already defined entry for the temperature. Because I'm going to specify two values per raw I'm not allowed to use the anonymous 'value' tag but have to specify unique names for all values. I'm going to use 'cpu' and 'board' names with the 'sensors' data type.
Adding a new entry into the 'types' database is done with the following command
echo -e "sensors\t\tcpu:GAUGE:-273.15:U, board:GAUGE:-273.15:U" >> /usr/share/collectd/types.db

Now I need to update an additional file that contains graph definitions and located in the  /usr/lib/lua/luci/statistics/rrdtool/definitions.lua. This lua file was shipped as a binary file with luci 0.9 and to update it I needed to get an original file from the luci branch. [http://luci.subsignal.org/trac/browser/luci/branches/luci-0.9/applications/luci-statistics/luasrc/statistics/rrdtool/definitions.lua]. With integration of luci 0.10 into the Backfire branch it is not needed anymore because sources aren't compressed into binaries. Usually I store this file on an external partition creating a symink in place after each firmware upgrade. As before I'm reusing existing definition for the temperature so the following entry is to be added (Note: this is no longer applicable, check this post for updated instructions):
  sensors = {
          "-v", "Celsius",
          "DEF:tempcpu_avg={file}:cpu:AVERAGE",
          "DEF:tempcpu_min={file}:cpu:MIN",
          "DEF:tempcpu_max={file}:cpu:MAX",
          "DEF:tempboard_avg={file}:board:AVERAGE",
          "DEF:tempboard_min={file}:board:MIN",
          "DEF:tempboard_max={file}:board:MAX",
          "AREA:tempcpu_max#" .. self.palette.HalfRed,
          "AREA:tempboard_max#" .. self.palette.HalfYellow,
          "AREA:tempcpu_min#" .. self.palette.Canvas,
          "AREA:tempboard_min#" .. self.palette.Canvas,
          "LINE1:tempcpu_avg#" .. self.palette.FullRed .. ":CPU Temperature  ",
          "GPRINT:tempcpu_min:MIN:%4.1lf Min,",
          "GPRINT:tempcpu_avg:AVERAGE:%4.1lf Avg,",
          "GPRINT:tempcpu_max:MAX:%4.1lf Max,",
          "GPRINT:tempcpu_avg:LAST:%4.1lf Last\\l",
          "LINE1:tempboard_avg#" .. self.palette.FullYellow .. ":Board Temperature",
          "GPRINT:tempboard_min:MIN:%4.1lf Min,",
          "GPRINT:tempboard_avg:AVERAGE:%4.1lf Avg,",
          "GPRINT:tempboard_max:MAX:%4.1lf Max,",
          "GPRINT:tempboard_avg:LAST:%4.1lf Last\\l"
  },
The name of this entry should correspond to the name of the entry added into the 'types.db' and in my case it's 'sensors'. Notice also that names cpu and board in lines [3-8] do correspond to the names I specified in the 'sensors' entry above.You may refer to the RRDTool docu to understand the rest of this notation.
Now lets take a look at the resulting graph:

[Image]


Much better! Board and CPU graphs are easy to distinguish and they are properly labeled.
Next time I'll explain how to get notified if CPU temperature reaches dangerous values.

No comments: