Advertisement
flipje

check_nagios_kvm_hosts

May 15th, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.86 KB | None | 0 0
  1. #!/bin/bash
  2. # +-----------------------------------------------------------------------------------------+
  3. # |  Dit script checked het aantal draaiende KVM hosts op Ubuntu/Debian                     |
  4. # |                                                                                         |
  5. # | Mei 2012 flip hess flip@lachendemensen.com                                              |
  6. # +-----------------------------------------------------------------------------------------+
  7.  
  8. # Global variables:
  9.  
  10. PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
  11. SCRIPT_PATH="${0}"
  12. ARGS="${#}"
  13.  
  14. VIRSH="/usr/bin/virsh list --all"
  15. STATE="/var/tmp/KVM-stalefile"
  16.  
  17. STATE_OK="0"
  18. STATE_WARNING="1"
  19. STATE_CRITICAL="2"
  20. STATE_UNKNOWN="3"
  21.  
  22. # Functions:
  23.  
  24.   # exit function
  25.   function die()
  26.   {
  27.     # output
  28.     echo -e "Error in ${SCRIPT_PATH}: ${1}"
  29.  
  30.     # return state unknown
  31.     exit ${STATE_UNKNOWN}
  32.   }
  33.  
  34.   # Shows usage function.
  35.   function fShowUsage()
  36.   {
  37.     echo -e "Usage: ${SCRIPT_PATH}
  38.    Check KVM state\n - flip@mhasmo.nl\n\nVersie 1.0"
  39.  
  40.     exit "${STATE_UNKNOWN}"
  41.   }
  42.  
  43.   # resets
  44.   function fCheckReset()
  45.   {
  46.     # Reset state old file
  47.     if [ ! -f "${STATE}" ] ; then
  48.        touch "${STATE}"
  49.        { ${VIRSH} | grep -Ev '(Id Name                 State|-------------------|^$)' | awk '{ print $2" "$3}'  > ${STATE}; } || die "Failed to get Virsh output"
  50.     else
  51.        echo "State file exist! Please remove state file ${STATE}" ; exit "${STATE_UNKNOWN}"
  52.     fi
  53.  
  54.     return 0
  55.   }
  56.  
  57.   # The main function.
  58.   function fMain()
  59.   {
  60.     # check for arguments:
  61.     [ "${ARGS}" = 0 ] || fShowUsage
  62.  
  63.     # this script depends on virsh
  64.     [  -x /usr/bin/virsh ] || { echo "This scripts depends on virsh, install it by using \"sudo apt-get install virsh\"" ; exit "${STATE_UNKNOWN}"; }
  65.  
  66.    #check state file
  67.    [ -f "${STATE}" ] || \
  68.         { fCheckReset && { echo "WARNING - check has been reset - a new ${STATE} error state file is created" ; exit "${STATE_WARNING}"; };}
  69.  
  70.    # get info
  71.    local TEMP="$(mktemp)"
  72.     # get output
  73.     ${VIRSH} | grep -Ev '(Id Name                 State|-------------------|^$)' | awk '{ print $2" "$3}' > ${TEMP} || die "Failed to get Virsh output"
  74.  
  75.    # diff
  76.    DIFFS="$( diff "${TEMP}" "${STATE}" |grep -E '(<|>)'  |sed -e 's/^>/missing virsh output:/g;s/^</new virsh output:/g' )"
  77.  
  78.    local DELTA="$( echo "${DIFFS}" | wc -l )"
  79.    local TOTAL="$( cat "${TEMP}" | wc -l )"
  80.  
  81.    # make results
  82.    if [ -z "${DIFFS}" ] ; then
  83.      rm ${TEMP}
  84.      echo -e "ALL OK: ${TOTAL} KVM images present in config. Virsh KVM output did not change"
  85.      exit ${STATE_OK}
  86.   else
  87.      rm ${TEMP}
  88.      echo -e "CRITICAL: ${DELTA} new Virsh KVM output lines"
  89.      echo "${DIFFS}"
  90.      exit ${STATE_CRITICAL}
  91.    fi
  92.   }
  93.  
  94.  
  95.  
  96.  # Start the program:
  97.   fMain
  98.  
  99.  # Exit with previous return code:
  100.   exit "${?}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement