#!/bin/sh
#
# ## Plugin for Nagios to monitor temperature with a 1 wire temperature sensor 
# ## Written by Guenther Orth (http://www.enbiz.de/)
# ##
# ## - 20070801 coded and tested for Linux
# ## - 20120310 change some code 
#
#
# ## You are free to use this script under the terms of the Gnu Public License.
# ## No guarantee - use at your own risc.
#
# ## Installation guidlines:
# ## needed software: digitemp (e.g. apt-get install digitemp)
# ## meeded hardware: 1wire temperature sensor described in
# ## http://www.linuxnetmag.com/de/issue8/m8temperature1.html (german)
# ## http://www.linuxnetmag.com/en/issue8/m8temperature1.html (english)
# ## First start digitemp with this parameters
# ## digitemp_DS9097 -i -s /dev/ttyS0
# ## Then copy the file .digitemprc to /usr/local/nagios/ 
# ## and the script check_digitemp.sh to the 
# ## check-plugins-directory e.g. /usr/local/nagios/libexec
# ## Install sudo and edit the file /etc/sudoers. Insert
# ## nagios ALL=(ALL) NOPASSWD: /usr/bin/digitemp_DS9097
# ## Copy the file .digitemprc also to the /(root)-directory. This is
# ## needet, because the nrpe-daemon starts the programms with the PWD=/
#
#
# ## Todo:
# ## - errorhandling
# ## - check range and value of parameters
#
# Usage: ./check_digitemp -ts <C|F> -w <warn> -c <crit> -f
#
# ## Description:
#
# This plugin reads the 1 wire sensor with digitemp and compare it
# with the supplied thresholds.
#
# ## Output:
# 
# The plugin prints the temperature in centigrade or in fahrenheit followed by "ok"
# or either "warning" or "critical" if the corresponding threshold is reached.
#
# Exit Codes
# 0 OK       Temperature checked and everything is ok
# 1 Warning  Temperature  above "warning" threshold
# 2 Critical Temperature  above "critical" threshold
# 3 Unknown  Invalid command line arguments or could not read the sensor
#
# Example: check_digitemp -ts C -w 22 -c 25 -f
#
# 21.81 C - ok		(exit code 0)
# 23.00 C - warning	(exit code 1)
# 26.23 C - critical	(exit code 2)

# Paths to commands used in this script. These
# may have to be modified to match your system setup.

DIGITEMP="sudo /usr/bin/digitemp_DS9097"
DIGIPARM="-a -q -c /usr/local/nagios/.digitemprc -o %.2C"
CUT="/usr/bin/cut"


PROGNAME=`/usr/bin/basename $0`
PROGPATH=`echo $0 | /bin/sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION="Revision 1.1"
AUTHOR="(c) 2008 Guenther Orth (http://www.enbiz.de/)"

# Exit codes
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4

print_revision() {
    echo "$REVISION $AUTHOR"
}

print_usage() {
    echo "Usage: $PROGNAME [-ts|--tempscale <C|F>] [-w|--warning <warn>] [-c|--critical <crit>] [-f|--perfdata]"
    echo "Usage: $PROGNAME -h|--help"
    echo "Usage: $PROGNAME -V|--version"
    echo ""
}

print_help() {
    print_revision $PROGNAME $REVISION
    echo ""
    echo "Temperature monitor plugin with digitemp for Nagios"
    echo "Description of the parameters:"
    echo " -ts|--tempscale: Temperaturscale Celsius (C) or Fahrenheit (F)"
    echo " -w|--warning: threshold for warning temperature (integer)"
    echo " -c|--critical: threshold for critical temperature (integer)"
    echo " -f|--perfdata: send perfdata to nagios"
    echo ""
    print_usage
    echo ""
}

# Make sure the correct number of command line
# arguments have been supplied

if [ $# -lt 1 ]; then
    print_usage
    exit $STATE_UNKNOWN
fi

# Grab the command line arguments

thresh_warn=""
thresh_crit=""
perfdata=0
exitstatus=$STATE_WARNING #default
while test -n "$1"; do
    case "$1" in
        --help)
            print_help
            exit $STATE_OK
            ;;
        -h)
            print_help
            exit $STATE_OK
            ;;
        --version)
            print_revision $PROGNAME $VERSION
            exit $STATE_OK
            ;;
        -V)
            print_revision $PROGNAME $VERSION
            exit $STATE_OK
           ;;
        -ts)
            tempscale=$2
            shift
            ;;
        --tempscale)
            tempscale=$2
            shift
            ;;
        --warning)
            thresh_warn=$2
            shift
            ;;
        -w)
            thresh_warn=$2
            shift
            ;;
        --critical)
            thresh_crit=$2
            shift
            ;;
        -c)
           thresh_crit=$2
            shift
            ;;
        -f)
            perfdata=1
            ;;
        -perfdata)
            perfdata=1
            ;;
        *)
            echo "Unknown argument: $1"
            print_usage
            exit $STATE_UNKNOWN
            ;;
    esac
    shift
done

##### Get temperature with digitemp
# Temperature with 2 digits after .
TEMPERATURE=`$DIGITEMP $DIGIPARM`
# Temperature without decimall places
TEMPCUT=`echo $TEMPERATURE|$CUT -d . -f 1`
result="ok"
exitstatus=$STATE_OK

### Check, if TEMPCUT is empty (error detect the sensor)
if [ "$TEMPCUT" = "%" ]; then
	echo "warning, error detecting the sensor"
	exit $STATE_WARNING
fi

##### Compare with thresholds

if [ "$thresh_warn" != "" ]; then
    if [ $TEMPCUT -ge $thresh_warn ]; then
        result="warning"
        exitstatus=$STATE_WARNING
    fi
fi
if [ "$thresh_crit" != "" ]; then
    if [ $TEMPCUT -ge $thresh_crit ]; then
        result="critical"
        exitstatus=$STATE_CRITICAL
    fi
fi
if [ $perfdata -eq 1 ]; then
    result="$result|'temp'=${TEMPERATURE};${thresh_warn};${thresh_crit}"
fi

echo "$TEMPERATURE $tempscale - $result"
exit $exitstatus
