Gathering server statistics: collectd

Custom scripts

by ross at 09:36:00 on October 29, 2011

Creating custom scripts

Create /root/bin/hdd-temperature:

#!/bin/sh

PATH=/root/bin:/usr/local/bin:/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin
export PATH

HDDS=`sysctl -n kern.disks | sed 's/cd0//'`
HOSTNAME="${COLLECTD_HOSTNAME:-`hostname -f`}"
INTERVAL="${COLLECTD_INTERVAL:-60}"

INTERVAL=`echo $INTERVAL | sed 's/^\(.*\)\..*$/\1/'`

while true; do
        CYCLE_START=`date +%s`

        for HDD in $HDDS; do
                TEMP=`(sudo smartctl -A $HDD | grep Temperature_Celsius | awk '{ print $10; }') 2>/dev/null`
                if [ $? -ne 0 -o -z "$TEMP" ]; then
                        TEMP="U"
                fi

                echo "PUTVAL $HOSTNAME/exec/temperature-$HDD interval=$INTERVAL N:$TEMP"
        done

        CYCLE_END=`date +%s`
        DIFF=`expr $CYCLE_END - $CYCLE_START`
        if [ $DIFF -lt $INTERVAL ]; then
                sleep `expr $INTERVAL - $DIFF`
        fi
done

In order for this example script to work you have to install sysutils/smartmontools, security/sudo and add the following to /usr/local/etc/sudoers:

collectd ALL = NOPASSWD: /usr/local/sbin/smartctl

Sudo will spam to /var/log/messages about each invocation of smartctl. You can lower priority of such messages (they will still be available in /var/log/all.log) by adding this to /usr/local/etc/sudoers:

Defaults syslog_goodpri=debug

 

Comments