danielhilst

nagios service dependence handler 0.2

Nov 1st, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.51 KB | None | 0 0
  1. #!/bin/bash -xu
  2.  
  3. SERVICESTATE=""
  4. LASTSERVICESTATE=""
  5. HOSTNAME=""
  6. SERVICEDESC=""
  7. DEPENDENTS=""
  8. CRITICALSERVICE=""
  9. DEPINDX=0
  10.  
  11. OK=0
  12. WARNING=1
  13. CRITICAL=2
  14. UNKNOWN=3
  15.  
  16. COMMANDFILE="/var/nagios/rw/nagios.cmd"
  17. LIVE="/var/nagios/rw/live"
  18.  
  19. usage() {
  20.     echo $0 ' -c $SERVICESTATE$ -l $LASTSERVICESTATE$ -d DEPENDENTS -H $HOSTNAME$ -s $SERVICEDESC$ -o SOMEOUTPUT'
  21. }
  22.  
  23. while getopts "c:l:d:H:S:o:Ch" OPT; do
  24.     case $OPT in
  25.     c)
  26.         SERVICESTATE=$OPTARG
  27.         ;;
  28.     l)
  29.         LASTSERVICESTATE=$OPTARG
  30.         ;;
  31.     d)
  32.         DEPENDENTS=($(echo $OPTARG| tr -s ',' ' '))
  33.         ;;
  34.     H)
  35.         HOSTNAME=$OPTARG
  36.         ;;
  37.     S)
  38.         SERVICEDESC=$OPTARG
  39.         ;;
  40.     o)
  41.         OUTPUT=$OPTARG
  42.         ;;
  43.     C)
  44.         CRITICALSERVICE="TRUE"
  45.         ;;
  46.     h)
  47.         usage
  48.         exit 0
  49.         ;;
  50.     ?)
  51.         usage
  52.         exit 1
  53.         ;;
  54.     esac
  55. done
  56.  
  57. if test -z "$SERVICESTATE" -o -z "$LASTSERVICESTATE" -o -z "${DEPENDENTS[*]}" -o -z "$HOSTNAME" -o -z "$SERVICEDESC"; then
  58.     usage
  59.     exit 1
  60. fi
  61.  
  62. _get_service_plugin_output() {
  63.     echo "GET services
  64. Filter: description = $1
  65. Columns: plugin_output" | unixcat $LIVE
  66. }
  67.  
  68. _get_service_state() {
  69.     echo "GET services
  70. Filter: description = $1
  71. Columns: state" | unixcat $LIVE
  72. }
  73.  
  74. _get_service_error_counters() {
  75.     if test -z "$1"; then
  76.     echo "_get_service_error_counters called without arguments"
  77.     exit 1
  78.     fi
  79.  
  80.     local PLUGINOUTPUT=$(_get_service_plugin_output $1)
  81.     local CRIT_COUNTER=${PLUGINOUTPUT##*_FAIL_}
  82.     local WARN_COUNTER=${PLUGINOUTPUT%%_FAIL_*}
  83.  
  84.     # WARN_COUNTER is empty or is not a number
  85.     if [[ -z "$WARN_COUNTER" || ! "$WARN_COUNTER" =~ "^[0-9]+$" ]]; then
  86.     WARN_COUNTER=0
  87.     fi
  88.  
  89.     if [[ -z "$CRIT_COUNTER" || ! "$CRIT_COUNTER" =~ "^[0-9]+$" ]]; then
  90.     CRIT_COUNTER=0
  91.     fi
  92.  
  93.     echo "$WARN_COUNTER $CRIT_COUNTER"
  94. }
  95.  
  96. # $1 => The service to receive the unkown state
  97. # $2 => The plugin_output
  98. # $3 => Set the status to critical if $3 is true
  99. _change_state() {
  100.     local SERVICE=$1
  101.     local OUTPUT=$2
  102.     local STATUS=$3
  103.     echo "[$(date +%s)] PROCESS_SERVICE_CHECK_RESULT;$HOSTNAME;$SERVICE;$STATUS;$OUTPUT" > $COMMANDFILE
  104. }
  105.  
  106. # $1 => The service to enable active checks
  107. _enable_active_checks() {
  108.     echo "[$(date +%s)] ENABLE_SVC_CHECK;$HOSTNAME;$1" > $COMMANDFILE    
  109. }
  110.  
  111. # $1 => The service to diable active checks
  112. _disable_active_checks() {
  113.     echo "[$(date +%s)] DISABLE_SVC_CHECK;$HOSTNAME;$1" > $COMMANDFILE
  114. }
  115.    
  116.  
  117. ok_to_other() {
  118.     for DEP in ${DEPENDENTS[*]}; do
  119.  
  120.     # Get error counter
  121.     local ERRORCOUNTERS=($(_get_service_error_counters "$DEP"))
  122.     local WARN_COUNTER=${ERRORCOUNTERS[0]}
  123.     local CRIT_COUNTER=${ERRORCOUNTERS[1]}
  124.    
  125.         # Disble active checks on dependents, if is not already disabled
  126.     if test "$WARN_COUNTER" -eq "0" -a "$CRIT_COUNTER" -eq "0"; then
  127.         _disable_active_checks $DEP
  128.     fi
  129.  
  130.  
  131.         # Passive change the state
  132.     local DEPSTATE=$(_get_service_state $DEP)
  133.     if test -n "$CRITICALSERVICE" -o "$DEPSTATE" -eq "$CRITICAL"; then
  134.         let CRIT_COUNTER++
  135.         _change_state $DEP ${WARN_COUNTER}_FAIL_${CRIT_COUNTER} $CRITICAL # Change/Keep to CRITICAL
  136.     else
  137.         let WARN_COUNTER++
  138.         _change_state $DEP ${WARN_COUNTER}_FAIL_${CRIT_COUNTER} $WARNING # Change to WARNING
  139.     fi
  140.     done
  141. }
  142.  
  143. other_to_ok() {
  144.     for DEP in ${DEPENDENTS[*]}; do
  145.  
  146.     local ERRORCOUNTERS=($(_get_service_error_counters "$DEP"))
  147.     local WARN_COUNTER=${ERRORCOUNTERS[0]}
  148.     local CRIT_COUNTER=${ERRORCOUNTERS[1]}
  149.  
  150.     local DEPSTATE=$(_get_service_state $DEP)  
  151.  
  152.     if test -n "$CRITICALSERVICE"; then
  153.         let CRIT_COUNTER--
  154.         if test "$CRIT_COUNTER" -eq "0" -a "$WARN_COUNTER" -gt "0"; then
  155.         _change_state $DEP ${WARN_COUNTER}_FAIL_${CRIT_COUNTER} $WARNING
  156.         elif test "$CRIT_COUNTER" -gt "0"; then
  157.         _change_state $DEP ${WARN_COUNTER}_FAIL_${CRIT_COUNTER} $CRITICAL
  158.         elif test "$CRIT_COUNTER" -lt "0"; then
  159.         _change_state $DEP ERROR $UNKNOWN
  160.         fi
  161.     else
  162.         let WARN_COUNTER--
  163.         if test "$DEPSTATE" == "$CRITICAL"; then
  164.         _change_state $DEP ${WARN_COUNTER}_FAIL_${CRIT_COUNTER} $CRITICAL
  165.         elif test "$WARN_COUNTER" -ge "0"; then
  166.         _change_state $DEP ${WARN_COUNTER}_FAIL_${CRIT_COUNTER} $WARNING
  167.         else
  168.         _change_state $DEP ERROR $UNKNOWN
  169.         fi
  170.     fi
  171.  
  172.     # If error counter is 0 enable active checks
  173.     if test "$WARN_COUNTER" -eq "0" -a "$CRIT_COUNTER" -eq "0"; then
  174.         _enable_active_checks $DEP    
  175.     fi
  176.     done
  177. }
  178.  
  179. if test "$LASTSERVICESTATE" == "OK" -a "$SERVICESTATE" != "OK"; then
  180.     ok_to_other
  181. elif test "$LASTSERVICESTATE" != "OK" -a "$SERVICESTATE" == "OK"; then
  182.     other_to_ok
  183. fi
Advertisement
Add Comment
Please, Sign In to add comment