Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash -xu
- SERVICESTATE=""
- LASTSERVICESTATE=""
- HOSTNAME=""
- SERVICEDESC=""
- DEPENDENTS=""
- CRITICALSERVICE=""
- DEPINDX=0
- OK=0
- WARNING=1
- CRITICAL=2
- UNKNOWN=3
- COMMANDFILE="/var/nagios/rw/nagios.cmd"
- LIVE="/var/nagios/rw/live"
- usage() {
- echo $0 ' -c $SERVICESTATE$ -l $LASTSERVICESTATE$ -d DEPENDENTS -H $HOSTNAME$ -s $SERVICEDESC$ -o SOMEOUTPUT'
- }
- while getopts "c:l:d:H:S:o:Ch" OPT; do
- case $OPT in
- c)
- SERVICESTATE=$OPTARG
- ;;
- l)
- LASTSERVICESTATE=$OPTARG
- ;;
- d)
- DEPENDENTS=($(echo $OPTARG| tr -s ',' ' '))
- ;;
- H)
- HOSTNAME=$OPTARG
- ;;
- S)
- SERVICEDESC=$OPTARG
- ;;
- o)
- OUTPUT=$OPTARG
- ;;
- C)
- CRITICALSERVICE="TRUE"
- ;;
- h)
- usage
- exit 0
- ;;
- ?)
- usage
- exit 1
- ;;
- esac
- done
- if test -z "$SERVICESTATE" -o -z "$LASTSERVICESTATE" -o -z "${DEPENDENTS[*]}" -o -z "$HOSTNAME" -o -z "$SERVICEDESC"; then
- usage
- exit 1
- fi
- _get_service_plugin_output() {
- echo "GET services
- Filter: description = $1
- Columns: plugin_output" | unixcat $LIVE
- }
- _get_service_state() {
- echo "GET services
- Filter: description = $1
- Columns: state" | unixcat $LIVE
- }
- _get_service_error_counters() {
- if test -z "$1"; then
- echo "_get_service_error_counters called without arguments"
- exit 1
- fi
- local PLUGINOUTPUT=$(_get_service_plugin_output $1)
- local CRIT_COUNTER=${PLUGINOUTPUT##*_FAIL_}
- local WARN_COUNTER=${PLUGINOUTPUT%%_FAIL_*}
- # WARN_COUNTER is empty or is not a number
- if [[ -z "$WARN_COUNTER" || ! "$WARN_COUNTER" =~ "^[0-9]+$" ]]; then
- WARN_COUNTER=0
- fi
- if [[ -z "$CRIT_COUNTER" || ! "$CRIT_COUNTER" =~ "^[0-9]+$" ]]; then
- CRIT_COUNTER=0
- fi
- echo "$WARN_COUNTER $CRIT_COUNTER"
- }
- # $1 => The service to receive the unkown state
- # $2 => The plugin_output
- # $3 => Set the status to critical if $3 is true
- _change_state() {
- local SERVICE=$1
- local OUTPUT=$2
- local STATUS=$3
- echo "[$(date +%s)] PROCESS_SERVICE_CHECK_RESULT;$HOSTNAME;$SERVICE;$STATUS;$OUTPUT" > $COMMANDFILE
- }
- # $1 => The service to enable active checks
- _enable_active_checks() {
- echo "[$(date +%s)] ENABLE_SVC_CHECK;$HOSTNAME;$1" > $COMMANDFILE
- }
- # $1 => The service to diable active checks
- _disable_active_checks() {
- echo "[$(date +%s)] DISABLE_SVC_CHECK;$HOSTNAME;$1" > $COMMANDFILE
- }
- ok_to_other() {
- for DEP in ${DEPENDENTS[*]}; do
- # Get error counter
- local ERRORCOUNTERS=($(_get_service_error_counters "$DEP"))
- local WARN_COUNTER=${ERRORCOUNTERS[0]}
- local CRIT_COUNTER=${ERRORCOUNTERS[1]}
- # Disble active checks on dependents, if is not already disabled
- if test "$WARN_COUNTER" -eq "0" -a "$CRIT_COUNTER" -eq "0"; then
- _disable_active_checks $DEP
- fi
- # Passive change the state
- local DEPSTATE=$(_get_service_state $DEP)
- if test -n "$CRITICALSERVICE" -o "$DEPSTATE" -eq "$CRITICAL"; then
- let CRIT_COUNTER++
- _change_state $DEP ${WARN_COUNTER}_FAIL_${CRIT_COUNTER} $CRITICAL # Change/Keep to CRITICAL
- else
- let WARN_COUNTER++
- _change_state $DEP ${WARN_COUNTER}_FAIL_${CRIT_COUNTER} $WARNING # Change to WARNING
- fi
- done
- }
- other_to_ok() {
- for DEP in ${DEPENDENTS[*]}; do
- local ERRORCOUNTERS=($(_get_service_error_counters "$DEP"))
- local WARN_COUNTER=${ERRORCOUNTERS[0]}
- local CRIT_COUNTER=${ERRORCOUNTERS[1]}
- local DEPSTATE=$(_get_service_state $DEP)
- if test -n "$CRITICALSERVICE"; then
- let CRIT_COUNTER--
- if test "$CRIT_COUNTER" -eq "0" -a "$WARN_COUNTER" -gt "0"; then
- _change_state $DEP ${WARN_COUNTER}_FAIL_${CRIT_COUNTER} $WARNING
- elif test "$CRIT_COUNTER" -gt "0"; then
- _change_state $DEP ${WARN_COUNTER}_FAIL_${CRIT_COUNTER} $CRITICAL
- elif test "$CRIT_COUNTER" -lt "0"; then
- _change_state $DEP ERROR $UNKNOWN
- fi
- else
- let WARN_COUNTER--
- if test "$DEPSTATE" == "$CRITICAL"; then
- _change_state $DEP ${WARN_COUNTER}_FAIL_${CRIT_COUNTER} $CRITICAL
- elif test "$WARN_COUNTER" -ge "0"; then
- _change_state $DEP ${WARN_COUNTER}_FAIL_${CRIT_COUNTER} $WARNING
- else
- _change_state $DEP ERROR $UNKNOWN
- fi
- fi
- # If error counter is 0 enable active checks
- if test "$WARN_COUNTER" -eq "0" -a "$CRIT_COUNTER" -eq "0"; then
- _enable_active_checks $DEP
- fi
- done
- }
- if test "$LASTSERVICESTATE" == "OK" -a "$SERVICESTATE" != "OK"; then
- ok_to_other
- elif test "$LASTSERVICESTATE" != "OK" -a "$SERVICESTATE" == "OK"; then
- other_to_ok
- fi
Advertisement
Add Comment
Please, Sign In to add comment