icinga-plugins/checks/check_systemd.sh

191 lines
4.1 KiB
Bash
Raw Normal View History

2019-10-21 16:15:47 +02:00
#!/bin/bash
SYSTEMCTL="$(type -P systemctl)"
PARAMETER="$*"
STATUS="is-active"
#
#status
# is-active
# is-enabled
#
#pid
#
#Laufzeit
# systemctl show bluetooth.service --property=ActiveEnterTimestampMonotonicq
# Umrechung: date -d @"$(echo "$(date -u +%s) - <TimeStamp>" | bc)"
# systemctl show bluetooth.service --property=ActiveEnterTimestamp,MainPID,ActiveState,UnitFileState
function hilfetext(){
cat <<- EOF
$(basename "$0") [OPTION...]
Options:
-s | --status
-t | --timestamp
-u | --unit
--perfdata
-p | --pid
-h | --help
EOF
}
function ParameterEvaluation(){
options=$(getopt -o hps:tu: --long help --long pid --long perfdata --long status: --long timestamp --long unit: -- "$@")
#Option nicht verfügbar
[ "$?" -eq "0" ] || {
echo "Incorrect option provided"
exit 1
}
eval set -- "$options"
while true; do
case "$1" in
"-p"|"--pid")
PID="1"
;;
"--perfdata")
PERFDATA="1"
;;
"-s"|"--status (default: is-active")
shift; # The arg is next in position args
STATUS="$1"
;;
"-u"|"--unit")
shift; # The arg is next in position args
UNIT="$1"
;;
"-t"|"--timstamp")
TIMESTAMP="1"
;;
"-h"|"--help")
hilfetext
exit 0
;;
"--")
shift
break
;;
esac
shift
done
}
#Prüfen, ob hddtemp installiert ist
if [ ! -x "$SYSTEMCTL" ]
then
echo "systemctl 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" "$@"
#declare $(systemctl show "$UNIT" --property=ActiveEnterTimestampMonotonic,MainPID,ActiveState,UnitFileState)
RESULT=$(systemctl show "$UNIT" --property=ActiveEnterTimestamp,MainPID,ActiveState,UnitFileState)
ActiveEnterTimestamp=$(echo "$RESULT" | grep ActiveEnterTimestamp | cut -d "=" -f 2)
MainPID=$(echo "$RESULT" | grep MainPID | cut -d "=" -f 2)
ActiveState=$(echo "$RESULT" | grep ActiveState | cut -d "=" -f 2)
UnitFileState=$(echo "$RESULT" | grep UnitFileState | cut -d "=" -f 2)
#Debug
#echo -e "ActiveEnterTimestamp:\t$ActiveEnterTimestamp"
#echo -e "MainPID:\t$MainPID"
#echo -e "ActiveState:\t$ActiveState"
#echo -e "UnitFileState:\t$UnitFileState\n"
#Wenn die Unit oder der Status nicht gesetz
if [ -z "$UNIT" ]
then
echo -e "Unit nicht gesetzt"
exit 1
fi
#Debug
#echo -e "STATUS:\t\"$STATUS\"\n"
if [ "$STATUS" != "is-enabled" ] && [ "$STATUS" != "is-active" ]
then
echo -e "Status must be \"is-enabled\" or \"is-active\" "
exit 1
fi
##############################################
# Zusatzstatus
#PID
if [ -n "$PID" ] && [ -n "$TIMESTAMP" ]
then
pid="PID: $MainPID,"
elif [ -n "$PID" ]
then
pid="PID: $MainPID"
fi
#timestamp
if [ -n "$TIMESTAMP" ]
then
timestamp=" running since $ActiveEnterTimestamp"
#secs="echo $ActiveEnterTimestampMonotonic \ 1000000000 | bc"
#timestamp=$( printf '%dd %02dh:%02dm:%02ds\n' $(($secs/86400)) $(($secs%86400/3600)) $(($secs%3600/60)) $(($secs%60)) )
##Umrechnung Timestamp in Zeit
#timestampnow="$(date -u +%s)"
#timediff="$(echo "$timestampnow - $ActiveEnterTimestampMonotonic" | bc)"
#newtimestamp="$(date -d @"$timediff")"
#timestamp=" Timestamp: $newtimestamp"
fi
#perfdata
if [ -n "$PERFDATA" ]
then
perfdata=" | alle perfdaten"
fi
additional="- $pid$timestamp$perfdata"
##############################################
# Auswertung des Status und aufbereitung für nrpe
if [ "$STATUS" = "is-active" ] && [ "$ActiveState" = "active" ]
then
echo "OK - $UNIT ist running $additional"
exit 0
elif [ "$STATUS" = "is-active" ] && [ "$ActiveState" = "inactive" ]
then
echo "CRITICAL - $UNIT is not running $additional"
exit 2
elif [ "$STATUS" = "is-active" ]
then
echo "UNKNOWN - is-active: Unbekannter Status: \"$ActiveState\""
exit 3
elif [ "$STATUS" = "is-enabled" ] && [ "$UnitFileState" = "enabled" ]
then
echo "OK - $UNIT is enabled $additional"
exit 0
elif [ "$STATUS" = "is-enabled" ] && [ "$UnitFileState" = "disabled" ]
then
echo "CRITICAL - $UNIT is disabled $additional"
exit 2
elif [ "$STATUS" = "is-enabled" ]
then
echo "UNKNOWN - is-enabled: Unbekannter status: \"$UnitFileState\""
exit 3
else
echo "UNKNOWN - Unbekannter Status"
exit 3
fi