skeleton_check

letzte Änderung: 16. Jul 2016, 14:55 Veröffentlicht: 9. Sep 2008, 21:47

Das Grundgerüst für neue Nagios-Plugins auf Basis der Bash. Über Jahre gewachsen.

Jetzt auch mit funktionierendem Timeout. Diesen habe ich dem Buch „Nagios – Das Praxisbuch“, S. 88 von Gerhard Laußer entnommen.


#!/bin/bash
#
# Author: Markus Gruenwald
# Url: www.mg-it.net
# Date: 2005-03-24
#
#
#
 
 
PROGNAME="${0##*/}"
PROGPATH="${0%/*}"
AUTHOR="Markus Gruenwald"
URL="www.mg-it.net"
CREATED="1970-01-01"
LAST_CHANGE="1993-12-04"
VERSION=$(tr -s "-" <<<  $LAST_CHANGE)
 
 
RUNNING_AS=$(whoami)
 
TIMEOUT=10
KILL_OPTION="-s ALRM"
 
# for debug
DEBUG=0
NAGIOS_PATH="/usr/local/nagios"
LOG_PATH="${NAGIOS_PATH}/log"
LOG_FILE="${LOG_PATH}/${PROGNAME}.log"
 
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4
 
 
print_usage() {
 
cat << EOU
 
For testing:
  $PROGNAME -w 1 -c 2 -t 4 -r 5
  "-w 1 -c1" = Dummy-Values, "-t 4" = Timeout 4 sec., "-r 5" = simulate runtime of 5 sec.
 
Usage:
    $PROGNAME -w <warning> -c <critical> 
              [-a|-z] -t [timeout]
 
    -w warning # (bytes or percentage)
    -c critical # (bytes or percentage)
    -a enable Switch 1
    -z enable Switch 2
 
    -t plugin timeout (default 10 seconds)
 
    -h Help
    -v Version
 
EOU
 
}
 
 
print_description () {
 
cat << EOD
 
What is this good for? Absolutely nothin! # Description
 
EOD
 
}
 
 
print_version () {
 
cat << EOV
 
$PROGNAME v$VERSION
EOV
 
} 
 
 
print_license () {
 
cat << EOL
 
Created at $CREATED by $AUTHOR ($URL)
Last change: $LAST_CHANGE
 
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
EOL
 
}
 
 
print_help () {
 
print_description
print_usage
print_version
print_license
 
}
 
log (){
	echo "$(date +"%d.%m.%y %H:%M:%S") *PID: $$ * $*" >> $LOG_FILE
}
 
 
timeout_handler (){
  echo -ne "\bPlugin timed out after $TIMEOUT seconds!\n"
  exit $STATE_UNKNOWN
}
 
 
timer (){
  sleep $1
#  disown -ar
  $KILL $KILL_OPTION $2 2>&1 > /dev/null
}
 
 
is_arg_integer () {
  if ! [ "$1" -eq "$1" ] 2>/dev/null
    then 
      echo -e "Value \"$1\" is invalid for option \"$2\". Must be integer\n\n"
      print_help
      exit $STATE_UNKNOWN
  fi
}
 
is_integer () {
  if ! [ "$1" -eq "$1" ] 2>/dev/null
    then 
      return 1
    else
      return 0
  fi
}
 
 
set_state () {
 
        if (( $1 >= $CRIT_TH ))
          then
            RETVAL=$STATE_CRITICAL
            STATUS=CRITICAL
        elif (( $1 >= $WARN_TH ))
          then
            [ "$RETVAL" != "$STATE_CRITICAL" ] && RETVAL=$STATE_WARNING
            STATUS=WARNING
        else
          STATUS=OK
        fi
 
} 
 
# Initial check of dependencies and pathes
# grep (example)
! GREP=$(type -p grep) && echo "ERROR: MISSING grep" && exit $STATE_UNKNOWN
 
# for kill "type" problem with shell builtins 
! KILL=$(which kill) && echo "ERROR: MISSING command kill" && exit $STATE_UNKNOWN
#! SUDO=$(type -p sudo) && echo "ERROR: MISSING sudo" && exit $STATE_UNKNOWN
#! DU="sudo $(type -p du)" && echo "ERROR: MISSING du (GNU coreutils)" && exit $STATE_UNKNOWN
#! STAT=$(type -p stat) && echo "ERROR: MISSING stat (GNU coreutils)" && exit $STATE_UNKNOWN
#! BC=$(type -p bc) && echo "ERROR: MISSING bc (basic calculator)" && exit $STATE_UNKNOWN
 
 
# sudoers (example)
#[[ "$RUNNING_AS" != root ]] && ! sudo -l|grep "du"|grep "NOPASSWD"|grep "root"  > /dev/null && echo "missing sudoers entry for $RUNNING_AS and du" && exit $STATE_UNKNOWN
 
 
 
# Grab the command line arguments
 
while getopts "w:c:t:r:azhv" OPT; do
        case $OPT in
                w)
                    is_arg_integer "$OPTARG" "-w"
                    WARN_TH=$OPTARG
                    ;;
 
                c)
                    is_arg_integer "$OPTARG" "-c"
                    CRIT_TH=$OPTARG
                    ;;
 
                t)
                    is_arg_integer "$OPTARG" "-t"
                    TIMEOUT=$OPTARG
                    ;;
 
                r)
                    is_arg_integer "$OPTARG" "-r"
                    MAX_TURN=$OPTARG
                    ;;
 
                a)
                    SWITCH=1
                    ;;
 
                z)
                    SWITCH=2
                    ;;
 
                h)
                    print_help
                    exit $STATE_OK
                    ;;
 
                v)
                    print_version
                    print_license
                    exit $STATE_OK
                    ;;
 
                *)
                    print_usage
                    exit $STATE_UNKNOWN
                    ;;
        esac
done
 
# Verify warning/critical values for none percentage thresholds  >>>>>>>>>>>>>> 
if [ -z "$WARN_TH" ]
  then
    echo -e "\n  Option -w not set!" && print_usage && exit $STATE_UNKNOWN
 
elif [ -z "$CRIT_TH" ]
  then
    echo -e "\n  Option -c not set!" && print_usage && exit $STATE_UNKNOWN
 
elif (( $WARN_TH > $CRIT_TH ))
  then
    echo -e "\n  Option -w must be less than -c." && print_usage && exit $STATE_UNKNOWN
 
fi
# <<<<<<<<<<<<<<< Verify warning/critical values for none percentage thresholds
 
 
# Verify warning/critical values for percentage thresholds >>>>>>>>>>>>>> 
# if [ -z "$WARN_TH" ] || (( $WARN_TH > 100 ))
#   then
#     echo "Option -w must be set and max. 100" && print_help && exit $STATE_UNKNOWN
#  
# elif [ -z "$CRIT_TH" ] || (( $CRIT_TH > 100 ))
#   then
#     echo "Option -c must be set and max. 100" && print_help && exit $STATE_UNKNOWN
#  
# elif (( $WARN_TH >= $CRIT_TH ))
#   then
#     echo "Option -w must be less than -c." && print_help && exit $STATE_UNKNOWN
#  
# fi
# <<<<<<<<<<<<<<< Verify warning/critical values for percentage thresholds
 
 
# Start timeout handling
trap 'timeout_handler' ALRM
timer $TIMEOUT $$ &
TIMER_PID=$!
 
 
# Debug if enabled 
if [ "$DEBUG" == "1" ]
  then
    log "START-----------------"
fi
 
 
 
# 8<---------------------- CHECK
#                          Example code
SPIN=(- \\ \| /)
Y=0
while [ $Y -le $MAX_TURN ]
  do
    for a in $(seq 4)
      do
        for X in "${SPIN[@]}"
          do
            echo -ne "\b\b\b\b"
            echo -ne "\b\b\b\b $Y $X"
            sleep 0.05
          done
         sleep 0.03
      done
    Y=$((Y+1))
    sleep 0.01
  done
 
RETSTR="$(echo -ne '\b\b\b\b\b\bSkeleton Plugin OK')"
RETVAL=$STATE_OK
 
# CHECK ---------------------->8
 
 
 
sleep 0.001
kill $TIMER_PID
 
echo "${RETSTR}${PERF_DATA}"
exit $RETVAL