Skip to content

Instantly share code, notes, and snippets.

@jovimon
Last active July 26, 2020 15:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jovimon/758b02206bfffea46c4e407751846896 to your computer and use it in GitHub Desktop.
Save jovimon/758b02206bfffea46c4e407751846896 to your computer and use it in GitHub Desktop.
FreeNAS CollectD UPS metrics collector

FreeNAS CollectD UPS metrics collector

Quick and dirty script to get UPS stats into CollectD so you can forward them to Grafana. Tested with a Salicru UPS configured with the blazer_usb driver.

Update 2020-07-26:

At some point between the original publication of this howto and FreeNAS 13.1, default nut metrics appeeared on my InfluxDB server, so even though nut sends fewer metrics than the script on this Gist (see the default-nut-metrics.png file as opposed to the file listing at the end of the howto), I'm going to stick with the default metrics, so I don't have to mess around with installing scripts every time my FreeNAS box reboots.

Original post:

Steps:

  1. Configure the UPS Service on the FreeNAS GUI. Remember the identifier you selected. You can check in the console if everything is configured properly with the command upsc <identifier>.

  2. Create a file with the following script, changing the UPS_CONFIG_NAME variable to the identifier you configured on the previous step:

#!/bin/bash

HOSTNAME="${COLLECTD_HOSTNAME:-`hostname -f`}"
INTERVAL="${COLLECTD_INTERVAL:-60}"
UPS_CONFIG_NAME="salicru"

while sleep "$INTERVAL"
do

#/usr/local/bin/upsc $UPS_CONFIG_NAME | grep -v -e ups.type -e driver. -e device. | awk -v host=$HOSTNAME -v iv=$INTERVAL -v upsname=$UPS_CONFIG_NAME -F":" '{gsub(" ", "", $2);print "PUTVAL " host"/ups-" upsname"/" $1, "interval="iv " N:" $2 }' | tee /tmp/test_salicru

/usr/local/bin/upsc $UPS_CONFIG_NAME | while read line
do
parametro=$(printf '%s' "$line" | awk -F":" '{print $1}')
valor=$(printf '%s' "$line" | awk -F":" '{gsub(" ", "", $2);print $2}')

if [ $parametro = "ups.type" ] || [ $parametro = "ups.productid" ] || [ $parametro = "ups.vendorid" ] || [ $parametro = "device.type" ] || [ $parametro = "driver.name" ] || [ $parametro = "driver.parameter.pollinterval" ] || [ $parametro = "driver.parameter.port" ] || [ $parametro = "driver.parameter.synchronous" ] || [ $parametro = "driver.version" ] || [ $parametro = "driver.version.interval" ]
then
        continue
fi

if [ $parametro = "ups.beeper.status" ]
then
        if [ $valor = "enabled" ]
        then
                valor=1
        else
                valor=0
        fi
fi

if [ $parametro = "ups.status" ]
then
        if [ $valor = "OL" ]
        then
                valor=1
        elif [ $valor = "OB" ]
        then
                valor=0
        else
                valor=-1
        fi
fi

echo "PUTVAL $HOSTNAME/ups-$UPS_CONFIG_NAME/$parametro interval=$INTERVAL N:$valor"
done

done
  1. Add following line to /etc/local/collectd.conf in the "exec" plugin, with the filename of the script you created in the step before.

    Exec "nobody" "<your filename>"

2b. If you want your configuration to survive reboots, make the same change to /conf/base/etc/ix.rc.d/ix-collectd. Beware changing this file will make the config survive reboots, but apparently not FreeNAS version upgrades.

  1. Restart collectd service

    service collectd restart

  2. Check if the rrd files are created.

    ls -l /var/db/collectd/rrd/localhost/ups-*

# ls -l /var/db/collectd/rrd/localhost/ups-*
total 4761
-rw-r--r--  1 root  wheel  148648 Aug 17 21:07 battery.charge.rrd
-rw-r--r--  1 root  wheel  148648 Aug 17 21:07 battery.voltage.high.rrd
-rw-r--r--  1 root  wheel  148648 Aug 17 21:07 battery.voltage.low.rrd
-rw-r--r--  1 root  wheel  148648 Aug 17 21:07 battery.voltage.nominal.rrd
-rw-r--r--  1 root  wheel  148648 Aug 17 21:07 battery.voltage.rrd
-rw-r--r--  1 root  wheel  148648 Aug 17 21:07 input.current.nominal.rrd
-rw-r--r--  1 root  wheel  148648 Aug 17 21:07 input.frequency.nominal.rrd
-rw-r--r--  1 root  wheel  148648 Aug 17 21:07 input.frequency.rrd
-rw-r--r--  1 root  wheel  148648 Aug 17 21:07 input.voltage.fault.rrd
-rw-r--r--  1 root  wheel  148648 Aug 17 21:07 input.voltage.nominal.rrd
-rw-r--r--  1 root  wheel  148648 Aug 17 21:07 input.voltage.rrd
-rw-r--r--  1 root  wheel  148648 Aug 17 21:07 output.voltage.rrd
-rw-r--r--  1 root  wheel  148648 Aug 17 21:06 ups.beeper.status.rrd
-rw-r--r--  1 root  wheel  148648 Aug 17 21:07 ups.delay.shutdown.rrd
-rw-r--r--  1 root  wheel  148648 Aug 17 21:07 ups.delay.start.rrd
-rw-r--r--  1 root  wheel  148648 Aug 17 21:07 ups.load.rrd
-rw-r--r--  1 root  wheel  148648 Aug 17 21:06 ups.status.rrd
-rw-r--r--  1 root  wheel  148648 Aug 17 21:07 ups.temperature.rrd
  1. Go to wherever you send your metrics and create some beautiful panels.

If files are not created.

You can set debug logging in collectd (https://collectd.org/wiki/index.php/Plugin:LogFile) adding the following lines to /etc/local/collectd.conf:

LoadPlugin "logfile"
<Plugin "logfile">
  LogLevel "debug"
  File "/var/log/collectd.log"
  Timestamp true
</Plugin>

And then looking into /var/log/collectd.log file. If you see entries like "No such dataset registered: " you have to add the definition of all of the metrics to the /usr/local/share/collectd/types.db file:

battery.charge value:GAUGE:0:U
battery.voltage value:GAUGE:0:U
battery.voltage.high value:GAUGE:0:U
battery.voltage.low value:GAUGE:0:U
battery.voltage.nominal value:GAUGE:0:U
input.current.nominal value:GAUGE:0:U
input.frequency value:GAUGE:0:U
input.frequency.nominal value:GAUGE:0:U
input.voltage value:GAUGE:0:U
input.voltage.fault value:GAUGE:0:U
input.voltage.nominal value:GAUGE:0:U
output.voltage value:GAUGE:0:U
ups.beeper.status value:GAUGE:0:U
ups.delay.shutdown value:GAUGE:0:U
ups.delay.start value:GAUGE:0:U
ups.load value:GAUGE:0:U
ups.status value:GAUGE:0:U
ups.temperature value:GAUGE:0:U
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment