<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7650.28">
<TITLE>check_sensors rework</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/plain format -->

<P><FONT SIZE=2>The official check_sensors plugin didn't report any info other than a regexp 'ALARM' from lm_sensors. I rewrote the plugin to send data, and have been making nice plots with nagios-grapher a few days now.<BR>
<BR>
This version reports cpu, motherboard and hard drive temperatures, fan speeds and voltages (in separate instances). Warning and critical level setting only affects cpu temperature and all fans.<BR>
<BR>
By default this plugin should work like the earlier version, the switches modify the operation mode. See --help for more information.<BR>
<BR>
<BR>
--<BR>
<BR>
#!/usr/bin/env bash<BR>
# gets temperature, fan speed and voltages from lm_sensors<BR>
# and smartctl for HDD temp<BR>
# Copyright (C) 2006 Mikael Lammentausta<BR>
<BR>
<BR>
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/nagios/sbin:/usr/local/nagios/sbin<BR>
<BR>
PROGNAME=`basename $0`<BR>
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`<BR>
REVISION=`echo '$Revision: 2.0 $' | sed -e 's/[^0-9.]//g'`<BR>
<BR>
#. $PROGPATH/utils.sh<BR>
<BR>
<BR>
print_usage() {<BR>
        echo "Usage: $PROGNAME [options]"<BR>
        echo "  e.g. $PROGNAME -T temperature -w 50 -c 60 -d /dev/hda,/dev/hdb"<BR>
        echo<BR>
        echo "Options:"<BR>
        echo -e "\t --help | -h          print help"<BR>
        echo -e "\t --version | -V       print version"<BR>
        echo -e "\t --verbose | -v       be verbose"<BR>
        echo -e "\t --type | -T [type]   "<BR>
        echo -e "\t    temperature       check and print temperature data"<BR>
        echo -e "\t    voltages          check and print voltage data"<BR>
        echo -e "\t    fan               check and print fan data"<BR>
        echo -e "\t -w [value]           set warning value"<BR>
        echo -e "\t -c [value]           set critical value"<BR>
        echo -e "\t -d [hdd drives]      set hard drive to scan, comma-separated"<BR>
        echo -e "\t -dd [driver type]    set hard drive type for smartctl (default ata)"<BR>
<BR>
        echo<BR>
        echo "If no options are given, $PROGRAME will print only status."<BR>
        echo "NOTE: you can only ask one data type at once, $PROGNAME will"<BR>
        echo "      not output temp, voltage and fan info at the same time."<BR>
        echo "      Critical/warning level only watches the CPU temperature."<BR>
        echo "      You cannot set levels for voltages."<BR>
        echo<BR>
}<BR>
<BR>
print_help() {<BR>
        print_revision $PROGNAME $REVISION<BR>
        echo<BR>
        echo "This plugin checks hardware status using the lm_sensors package."<BR>
        echo<BR>
        print_usage<BR>
        echo<BR>
        support<BR>
        exit 3<BR>
}<BR>
<BR>
# set defaults<BR>
check_temp=0<BR>
check_voltages=0<BR>
check_fan=0<BR>
check_smart=0<BR>
isverbose=0<BR>
hddtype="ata"<BR>
<BR>
<BR>
<BR>
# get the data<BR>
sensordata=$(sensors 2>&1)<BR>
status=$?<BR>
# test status of lm_sensors<BR>
if [ ! "$(type -p sensors)" ]; then<BR>
        echo "SENSORS UNKNOWN - command not found (did you install lmsensors?)"<BR>
        exit -1<BR>
elif [ ! "$(type -p smartctl)" ] && [ ${check_smart} -eq 1 ]; then<BR>
        echo "Smartctl binary not found - no data available from hard drives"<BR>
        echo<BR>
        check_smart=0<BR>
elif test ${status} -ne 0 ; then<BR>
        echo "WARNING - sensors returned state $status"<BR>
        exit -1<BR>
elif [ $check_smart -eq 1 ] && [ ! "$hdd_drive" ]; then<BR>
        echo "No HDD drive defined. Use the -d switch."<BR>
        exit -1<BR>
fi<BR>
<BR>
<BR>
# all is ok<BR>
if [ $isverbose -eq 1 ]; then<BR>
        echo -e "${sensordata}"<BR>
fi<BR>
<BR>
<BR>
# put the multiple items given at the prompt to an array<BR>
form_hdd_array() {<BR>
        IFS_bak="$IFS" <BR>
        IFS=","<BR>
<BR>
        local drive<BR>
        hdd_drives[0]="reserved"<BR>
<BR>
        for drive in ${1}; do<BR>
<BR>
                hdd_drives[${#hdd_drives[@]}]="$drive"<BR>
<BR>
        done<BR>
<BR>
        IFS="$IFS_bak"<BR>
}<BR>
<BR>
<BR>
# check functions<BR>
check_temp() {<BR>
        cpu_temp="$(grep -i 'cpu temp' <<< "${sensordata}" | \<BR>
                                grep -Eo '[0-9\.]+[[:punct:]]?[ ]?[CF]+' | head -n 1)"<BR>
<BR>
        mobo_temp="$(grep -i 'M/B temp' <<< "${sensordata}" | \<BR>
                                 grep -Eo '\+[0-9]*[[:punct:]]?[ ]?[CF]+' | head -n 1)"<BR>
<BR>
        local drive<BR>
        hdd_temp[0]="reserved"<BR>
<BR>
        for drive in $(seq 1 1 ${#hdd_drives[@]}); do<BR>
                [ -e "${hdd_drives[$drive]}" ] && \<BR>
                hdd_temp[${#hdd_temp[@]}]="$(smartctl -A ${hdd_drives[$drive]} -d $hddtype | \<BR>
                      grep -i temperature | \<BR>
                  awk '{for (i=10; i<=NF; i++) printf("%s ",$i);printf ("\n")}') C"<BR>
        done<BR>
        }<BR>
<BR>
# check fan speed<BR>
check_fan() {<BR>
        fans="$(grep -i 'fan' <<< "${sensordata}" | cut -d'(' -f 1 )"<BR>
        }<BR>
<BR>
# check voltages<BR>
check_voltages() {<BR>
<BR>
        # parse all voltages nicely and put into one string<BR>
        voltages="$(sensors | grep -E '[0-9]{2}\ V' | \<BR>
                    cut -d'(' -f 1 | sed 's/\ *//g ; s/:/\ =\ /' | \<BR>
                                tr '\n' '; ' )"<BR>
<BR>
        }<BR>
<BR>
<BR>
main() {<BR>
        # temperature<BR>
        if [ $check_temp -eq 1 ]; then<BR>
                check_temp<BR>
                local cpu_status=0<BR>
<BR>
                # if either temp exceeds critical..<BR>
                [ "$critical" ] && \<BR>
                        if [ $(grep -Eo '[0-9]+' <<< ${cpu_temp}) -ge $critical ]; then<BR>
#                          [ $(grep -Eo '[0-9]+' <<< ${mobo_temp}) -ge $critical ]; then<BR>
<BR>
                                echo -n "[CRITICAL]  "         <BR>
                                cpu_status=2<BR>
                        fi<BR>
<BR>
                # if either temp exceeds warning...<BR>
                [ "$warning" ] && \<BR>
                        if [ $(grep -Eo '[0-9]+' <<< ${cpu_temp}) -ge $warning ]; then<BR>
#                          [ $(grep -Eo '[0-9]+' <<< ${mobo_temp}) -ge $warning ]; then<BR>
                                echo "[WARNING]  "<BR>
                                cpu_status=1<BR>
                        fi<BR>
<BR>
<BR>
                # output data<BR>
                echo -n "CPU = ${cpu_temp}  MOTHERBOARD = ${mobo_temp}  "<BR>
<BR>
                for drive in $(seq 1 1 ${#hdd_drives[@]}); do<BR>
                        echo -n "${hdd_drives[$drive]} = ${hdd_temp[$drive]}  "<BR>
                done<BR>
<BR>
                exit $cpu_status<BR>
               <BR>
<BR>
        # fan<BR>
        elif [ $check_fan -eq 1 ]; then<BR>
                check_fan<BR>
                local fan_status=0<BR>
                local worst_state=0<BR>
                IFS_bak="$IFS"<BR>
                IFS=$'\n'<BR>
<BR>
                for fan in $fans; do<BR>
                        fan_status=0<BR>
                        fan_name="$(awk -F':' {'print $1'} <<< $fan )"<BR>
                        speed="$(awk /[0-9]+/{'print $2'} <<< $fan)"<BR>
<BR>
                        # if a fan exceeds critical..<BR>
                        [ "$critical" ] && \<BR>
                                if [ $speed -le $critical ]; then<BR>
                                        fan_status=2<BR>
                                fi<BR>
<BR>
                        # if a fan exceeds warning..<BR>
                        [ "$warning" ] && \<BR>
                                if [ $speed -le $warning ]; then<BR>
                                        fan_status=1<BR>
                                fi<BR>
                               <BR>
                        # normal status -eq 0<BR>
                        [ $fan_status -eq 2 ] && echo -n "[CRITICAL]: "<BR>
                        [ $fan_status -eq 1 ] && echo -n "[WARNING]: "<BR>
                        echo -n "${fan_name} = ${speed} RPM  "<BR>
<BR>
                        # remember worst state<BR>
                        [ $fan_status -gt $worst_state ] && $worst_state=$fan_status<BR>
<BR>
                done<BR>
<BR>
                echo<BR>
                exit $worst_state<BR>
<BR>
<BR>
        # voltages<BR>
        elif [ $check_voltages -eq 1 ]; then<BR>
                # no critical/warning for voltages!<BR>
                check_voltages<BR>
                echo "$voltages"<BR>
                exit 0<BR>
<BR>
<BR>
        # default operation<BR>
        else<BR>
                if echo ${sensordata} | egrep ALARM > /dev/null; then<BR>
                        echo SENSOR CRITICAL - Sensor alarm detected!<BR>
                        exit 2<BR>
                else<BR>
                        echo sensor ok<BR>
                        exit 0<BR>
                fi<BR>
        fi<BR>
}<BR>
<BR>
<BR>
# parse cmd arguments<BR>
if [ "$#" -gt 0 ]; then<BR>
        while [ "$#" -gt 0 ]; do<BR>
                case "$1" in<BR>
                        '--help'|'-h')<BR>
                                print_help<BR>
                                exit 3<BR>
                                ;;<BR>
                        '--version'|'-V')<BR>
                                print_revision $PROGNAME $REVISION<BR>
                                exit 3<BR>
                                ;;<BR>
                        '--verbose'|'-v')<BR>
                                isverbose=1<BR>
                                shift 1<BR>
                                ;;<BR>
                        '-T'|'--type')<BR>
                                case $2 in<BR>
                                        'temp'|'temperature')<BR>
                                                check_temp=1<BR>
                                                check_smart=1<BR>
                                                ;;<BR>
                                        'voltages')<BR>
                                                check_voltages=1<BR>
                                                ;;<BR>
                                        'fan')<BR>
                                                check_fan=1<BR>
                                                ;;<BR>
                                        *)<BR>
                                                echo "Unknown type!"<BR>
                                                exit 3<BR>
                                                ;;<BR>
                                esac<BR>
                                shift 2<BR>
                                ;;<BR>
                        '-c')<BR>
                                critical="$2"<BR>
                                shift 2<BR>
                                ;;<BR>
                        '-w')<BR>
                                warning="$2"<BR>
                                shift 2<BR>
                                ;;<BR>
                        '-d')<BR>
                                form_hdd_array "$2"<BR>
                                # drives are now in $hdd_drives[]<BR>
                                shift 2<BR>
                                ;;<BR>
                        '-dd')<BR>
                                hddtype="$2"<BR>
                                ;;<BR>
                        *)<BR>
                                echo "Unknown option!"<BR>
                                print_usage<BR>
                                exit 3<BR>
                                ;;<BR>
                esac<BR>
        done<BR>
fi<BR>
<BR>
main<BR>
</FONT>
</P>

</BODY>
</HTML>