File:Check kvm.sh

From RARForge
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Check_kvm.sh(file size: 3 KB, MIME type: application/x-sh)

Warning: This file type may contain malicious code. By executing it, your system may be compromised.

<source lang=bash>

  1. !/bin/sh
  2. check_kvm - Check that a virtual machine is running.
  3. Written by Karl Rink <krink@csun.edu>
  4. ----------------------------------------------------------------------
  5. COPYRIGHT  : 12.2010 California State University, Northridge
  6. AUTHOR  : Karl Rink
  7. BELONGS TO  : Qemu/Kvm Nagios Integration
  8. DESCRIPTION  : Runs "virsh list" and returns the available vms
  9. $Revision: 1.0 $
  10. Permission to use, copy, modify, distribute, and sell this software
  11. and its documentation for any purpose is hereby granted without fee,
  12. provided that the above copyright notice appear in all copies and that
  13. both that copyright notice and this permission notice appear in
  14. supporting documentation.
  15. The above copyright notice and this permission notice shall be
  16. included in all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. IN NO EVENT SHALL THE AUTHOR OR SUSE BE LIABLE FOR ANY CLAIM, DAMAGES
  21. OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
  23. THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. ----------------------------------------------------------------------
  1. Install: Put this script in nagios client directory libexec/check_kvm
  2. As a nagios nrpe plugin, add the following line to your client nrpe.cfg
  3. command[virt_check_kvm]=/opt/nagios/libexec/check_kvm
  4. No sudo is necessary if you simply add nagios user to the libvirt group
  5. libvirtd:x:118:nagios

PATH=/bin:/usr/bin:/sbin:/usr/sbin export PATH

spec=0 if [ "d$1" != "d" ]; then

spec=1
LIST=$(virsh list --all |  sed '1,2d' | sed '/^$/d'| grep " $1 "  |  awk '{print $2":"$3}') 

else

LIST=$(virsh list --all | sed '1,2d' | sed '/^$/d'| awk '{print $2":"$3}') 

fi

if [ ! "$LIST" ]; then

 EXITVAL=3 #Status 3 = UNKNOWN (orange) 
 echo "Unknown guests"
 exit $EXITVAL

fi

OK=0 WARN=0 CRIT=0 NUM=0

hostname=`hostname`

for host in $(echo $LIST) do

 name=$(echo $host | awk -F: '{print $1}')
 state=$(echo $host | awk -F: '{print $2}')
 NUM=$(expr $NUM + 1)
 case "$state" in
   running|blocked) OK=$(expr $OK + 1) ;;
   paused) WARN=$(expr $WARN + 1) ;;
   shutdown|shut*|crashed) CRIT=$(expr $CRIT + 1) ;;
   *) CRIT=$(expr $CRIT + 1) ;;
 esac

done

if [ "$NUM" -eq "$OK" ]; then

 EXITVAL=0 #Status 0 = OK (green)

fi

if [ "$WARN" -gt 0 ]; then

 EXITVAL=1 #Status 1 = WARNING (yellow)

fi

if [ "$CRIT" -gt 0 ]; then

 EXITVAL=2 #Status 2 = CRITICAL (red)

fi

echo ON HOST $hostname: hosts:$NUM OK:$OK WARN:$WARN CRIT:$CRIT - $LIST

exit $EXITVAL </source>

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeDimensionsUserComment
current00:08, 2 March 2013 (3 KB)Robertr (talk | contribs)

The following page uses this file: