Scaling metrics in collectd

cmh
2 min readSep 4, 2016

--

I’m a bit of a metrics nerd — I gather metrics using collectd on just about every system I run. This includes a bunch of Raspberry Pi systems which I use for monitoring. One of the things I like to monitor is temperature, as it’s very easy to wire up a DS18B20 temp sensor with the Raspberry Pi. Monitoring the output of a DS18B20 and the CPU temp can be done from within collectd using the curl plugin:

<Plugin curl>
<Page "temp">
URL "file:///sys/class/thermal/thermal_zone0/temp"
<Match>
Regex "([0-9]*)"
DSType "GaugeLast"
Type "temperature"
Instance "cpu"
</Match>
</Page>
<Page "temp">
URL "file:///sys/bus/w1/devices/28-0004330ce1ff/w1_slave"
<Match>
Regex "(-?[0-9][0-9][0-9]+)"
DSType "GaugeLast"
Type "temperature"
Instance "firstfloor"
</Match>
</Page>
</Plugin>

The ability to query a file for the temps makes integration with collectd very, very easy. The biggest problem, however, is that the temperatures are reported in millidegrees Celcius — 21.7 degrees would be 21700. While this is easy enough to deal with when viewing the metrics in Graphite, (using the scale and offset functions) it would be nice to log the metrics directly as the value we want.

I’ve seen that collectd has “target.scale” in the documentation, but it’s very poorly explained. I know it’s part of the chains, but again, the doc leaves a bunch to be desired. Examples are rare and don’t directly address this use case, and it requires a bit of trial-and-error to get something that works.

So, in the interest of saving someone else the time, here’s what I figured out which scales the above metrics to what we want:

# You need to load these plugins for the below code
LoadPlugin match_regex
LoadPlugin target_scale
# PreCache changes the values before they hit the cache
<Chain "PreCache">
<Rule>
<Match "regex">
Plugin "^curl$"
PluginInstance "^temp$"
Invert false
</Match>
<Target "scale">
Factor 0.001
</Target>
</Rule>
</Chain>

This adjusts the values so the metrics are reported as degrees Celcius, like we want.

--

--

cmh

IT nerd and bike mechanic. Constantly making 3D printed stuff for bikes or bike mechanics.