Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # +-----------------------------------------------------------------------------------------+
- # | Dit script checked het aantal draaiende KVM hosts op Ubuntu/Debian |
- # | |
- # | Mei 2012 flip hess flip@lachendemensen.com |
- # +-----------------------------------------------------------------------------------------+
- # Global variables:
- PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
- SCRIPT_PATH="${0}"
- ARGS="${#}"
- VIRSH="/usr/bin/virsh list --all"
- STATE="/var/tmp/KVM-stalefile"
- STATE_OK="0"
- STATE_WARNING="1"
- STATE_CRITICAL="2"
- STATE_UNKNOWN="3"
- # Functions:
- # exit function
- function die()
- {
- # output
- echo -e "Error in ${SCRIPT_PATH}: ${1}"
- # return state unknown
- exit ${STATE_UNKNOWN}
- }
- # Shows usage function.
- function fShowUsage()
- {
- echo -e "Usage: ${SCRIPT_PATH}
- Check KVM state\n - flip@mhasmo.nl\n\nVersie 1.0"
- exit "${STATE_UNKNOWN}"
- }
- # resets
- function fCheckReset()
- {
- # Reset state old file
- if [ ! -f "${STATE}" ] ; then
- touch "${STATE}"
- { ${VIRSH} | grep -Ev '(Id Name State|-------------------|^$)' | awk '{ print $2" "$3}' > ${STATE}; } || die "Failed to get Virsh output"
- else
- echo "State file exist! Please remove state file ${STATE}" ; exit "${STATE_UNKNOWN}"
- fi
- return 0
- }
- # The main function.
- function fMain()
- {
- # check for arguments:
- [ "${ARGS}" = 0 ] || fShowUsage
- # this script depends on virsh
- [ -x /usr/bin/virsh ] || { echo "This scripts depends on virsh, install it by using \"sudo apt-get install virsh\"" ; exit "${STATE_UNKNOWN}"; }
- #check state file
- [ -f "${STATE}" ] || \
- { fCheckReset && { echo "WARNING - check has been reset - a new ${STATE} error state file is created" ; exit "${STATE_WARNING}"; };}
- # get info
- local TEMP="$(mktemp)"
- # get output
- ${VIRSH} | grep -Ev '(Id Name State|-------------------|^$)' | awk '{ print $2" "$3}' > ${TEMP} || die "Failed to get Virsh output"
- # diff
- DIFFS="$( diff "${TEMP}" "${STATE}" |grep -E '(<|>)' |sed -e 's/^>/missing virsh output:/g;s/^</new virsh output:/g' )"
- local DELTA="$( echo "${DIFFS}" | wc -l )"
- local TOTAL="$( cat "${TEMP}" | wc -l )"
- # make results
- if [ -z "${DIFFS}" ] ; then
- rm ${TEMP}
- echo -e "ALL OK: ${TOTAL} KVM images present in config. Virsh KVM output did not change"
- exit ${STATE_OK}
- else
- rm ${TEMP}
- echo -e "CRITICAL: ${DELTA} new Virsh KVM output lines"
- echo "${DIFFS}"
- exit ${STATE_CRITICAL}
- fi
- }
- # Start the program:
- fMain
- # Exit with previous return code:
- exit "${?}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement