icinga-plugins/checks/check_hddtemp.sh

249 lines
5.0 KiB
Bash
Raw Normal View History

#!/bin/bash
2019-10-13 20:15:02 +02:00
################################################################################
#
# This plugin checks the temperature via hddtemp
#
# I wrote this check-Script, because all other tools didn't do what I wanted.
# The goal was:
# - hddtemp is to be used (small, fast)
# - a better output (with manufacturer and model)
# - deliver performance data
# - options in linux style
# - it should do its job perfectly
2019-10-22 10:18:37 +02:00
# - switchable from celsius to fahrenheit
2019-10-13 20:15:02 +02:00
#
2019-10-13 20:30:58 +02:00
# The warning and critical values are from a 2007 Google study (Failure Trends in a Large Disk Drive Population)
2019-10-13 20:15:02 +02:00
#
2019-10-22 10:18:37 +02:00
#
# USAGE:
# ./check_hddtemp.sh -d <device> -w <warn> -c <crit>
# Nagios script to get the temperatue of HDD from hddtemp
#
# If you may have to let nagios run this script as root
# This is how the sudoers file looks in my debian system:
# nagios ALL=(root) NOPASSWD:/<path>/<to>/check_hddtemp.sh
2019-10-13 20:15:02 +02:00
################################################################################
2019-10-22 08:12:38 +02:00
HDDTEMP="$(type -P hddtemp)"
PARAMETER="$*"
WARNING="40"
2019-10-13 20:30:58 +02:00
CRITICAL="45"
2019-10-22 08:13:08 +02:00
UNIT="c"
function hilfetext(){
cat <<- EOF
$(basename "$0") [OPTION...]
Options:
-d | --device
2019-10-22 08:13:08 +02:00
--fahrenheit
--celsius (default)
-w | --warning (default: 40)
-c | --critical (default: 50)
-p | --perfdata
-h | --help
2019-10-22 08:13:08 +02:00
EOF
}
function ParameterEvaluation(){
2019-10-22 08:13:08 +02:00
options=$(getopt -o c:d:hpw: --long fahrenheit --long celsius --long critical: --long device: --long help --long perfdata --long warning: -- "$@")
#Option nicht verfügbar
[ "$?" -eq "0" ] || {
echo "Incorrect option provided"
exit 1
}
eval set -- "$options"
while true; do
case "$1" in
"-d"|"--device")
shift; # The arg is next in position args
2019-10-22 11:12:16 +02:00
DEVICES+=("$1")
;;
2019-10-22 08:13:08 +02:00
"--celsius")
UNIT="c"
;;
"--fahrenheit")
UNIT="f"
;;
"-w"|"--warning")
shift; # The arg is next in position args
WARNING="$1"
;;
"-c"|"--critical")
shift; # The arg is next in position args
CRITICAL="$1"
;;
"-p"|"--perfdata")
PERFDATA="1"
;;
"-h"|"--help")
hilfetext
exit 0
;;
2019-10-22 08:13:08 +02:00
"--")
shift
break
;;
esac
shift
done
}
#Prüfen, ob hddtemp installiert ist
if [ ! -x "$HDDTEMP" ]
then
echo "hddtemp nicht gefunden"
exit 1
fi
#Wenn keine Option oder Argument angegeben wurde, wird die Hilfe ausgegeben
if [ -z "$PARAMETER" ]
then
hilfetext
exit 0
fi
ParameterEvaluation "$0" "$@"
#Debug
#echo -e "WARNING:\t\"$WARNING\""
#echo -e "CRITICAL:\t\"$CRITICAL\""
2019-10-22 11:12:16 +02:00
#for device in ${DEVICES[@]}
#do
# echo -e "device: \"$device\""
#done
#echo -e "HELP\t\t\"$HELP\""
#echo -e "PERFDATA\t\t\"$PERFDATA\""
#echo -e "Alle Args:\t\"$@\""
#Check, if WARNING under CRITICAL
if [ "$WARNING" -ge "$CRITICAL" ]
then
echo "WARNING must be lower then CRITICAL (WARNING=$WARNING;CRITICAL=$CRITICAL)"
exit 1
fi
#Check, if device is a block-device
2019-10-22 11:12:16 +02:00
for device in ${DEVICES[@]}
do
if [ ! -b "$device" ]
then
echo "Device $device does not exist or is not a block-Device"
exitvar=1
fi
done
if [ "$exitvar" = "1" ]
then
exit 1
fi
2019-10-22 11:12:16 +02:00
for device in ${DEVICES[@]}
do
if [ "$UNIT" = "c" ]
then
RESULT=$($HDDTEMP --unit=C $device)
elif [ "$UNIT" = "f" ]
then
RESULT=$($HDDTEMP --unit=F $device)
fi
2019-10-22 11:12:16 +02:00
OUTDEV=$(echo "$RESULT" | awk -F: '{print $1}')
2019-10-22 11:40:10 +02:00
OUTMODEL=$(echo "$RESULT" | awk -F: '{print $2}' | tr -cd '[:print:]' | tr -s ' ' ' ' )
2019-10-22 11:12:16 +02:00
OUTTEMP=$(echo "$RESULT" | awk -F: '{print $3}')
OUTTEMPCLEAN=$(echo "$OUTTEMP" | tr -dc '0-9')
#Debug
2019-10-22 11:12:16 +02:00
#echo -e "OUTDEV:\t$OUTDEV"
#echo -e "OUTDEVmodel:\t$OUTDEVmodel"
#echo -e "OUTTEMP:\t$OUTTEMP"
#echo -e "OUTTEMPCLEAN:\t$OUTTEMPCLEAN"
#Erstellen des Perfdata-Results
if [ -n "$PERFDATA" ]
then
#perfdata: temperatur=<temperatur>;<warn>;<crit>
PERFRESULT+=" $(echo "$OUTDEV" | awk -F "/" '{print $NF}')=$OUTTEMPCLEAN;$WARNING;$CRITICAL"
fi
#initialisierung der Varible
exitausgabe="0"
#Auswertung der Temperaturen
if [ "$OUTTEMPCLEAN" -lt "$WARNING" ]
then
# OK - Status
ausgabe+="$OUTMODEL on $OUTDEV is $OUTTEMP "
if [ "$exitausgabe" -gt "0" ]
then
echo do nothing
else
exitausgabe="0"
fi
elif [ "$OUTTEMPCLEAN" -ge "$WARNING" ] && [ "$OUTTEMPCLEAN" -lt "$CRITICAL" ]
then
# WARNING - Status
ausgabe+="$OUTMODEL on $OUTDEV is $OUTTEMP "
if [ "$exitausgabe" -gt "1" ]
then
echo do nothing
else
exitausgabe="1"
fi
elif [ "$OUTTEMPCLEAN" -ge "$CRITICAL" ]
then
#CRITICAL - Status
ausgabe+="$OUTMODEL on $OUTDEV is $OUTTEMP "
if [ "$exitausgabe" -gt "2" ]
then
echo do nothing
else
exitausgabe="2"
fi
else
#UNKNOWN - Status
ausgabe+="$OUTMODEL on $OUTDEV is $OUTTEMP "
if [ "$exitausgabe" -gt "3" ]
then
echo do nothing
else
exitausgabe="3"
fi
fi
done
2019-10-22 11:12:16 +02:00
#Debug
#echo -e "Ausgabe:\t$ausgabe"
#echo -e "PERFRESULT:\t$PERFRESULT"
#echo -e "exitausgbe:\t$exitausgabe"
case $exitausgabe in
"0")
echo -e "OK - $ausgabe |$PERFRESULT"
exit 0
;;
"1")
echo -e "WARNING - $ausgabe |$PERFRESULT"
exit 1
;;
"2")
echo -e "CRITICAL - $ausgabe |$PERFRESULT"
exit 2
;;
"3")
echo -e "UNKNOWN - $ausgabe |$PERFRESULT"
exit 3
;;
*)
echo -e "UNKNOWN - Error"
exit 3
esac