martes, 16 de marzo de 2010

Monitorizar Xen con Nagios - CPUs





Un pequeño script que comprueba que las cpus de las maquinas virtuales no exceden del warning y critical.

#!/bin/bash

#" parametres entrada nivell de warning i de critical
if [ $# -ne 2 ] ; then
echo "Error es requereixen 2 parametres, el percentatge de warning i de critical"
exit 2
fi

PECRIT=$2
PEWARN=$1
RETORN=0
TEXTRETORN=""
TEMPFILE=`mktemp`

nombre_dominis=`sudo /usr/sbin/xm list | grep -wv "Name" | wc -l`
llistat=`sudo /usr/sbin/xentop -bi1 | awk '{ print $1}' | grep -wv "NAME"`

sudo /usr/sbin/xentop -bi2 >$TEMPFILE
for ite in $llistat ; do
nivell_cpu=`grep "$ite" $TEMPFILE | awk '{ print $4 }' | tail -1 | cut -d'.' -f1`

if [ $nivell_cpu -ge $PECRIT ] ; then
RETORN=2
TEXTRETORN="$TEXTRETORN Crit($ite),"
elif [ $nivell_cpu -ge $PEWARN ] ; then
if [ $RETORN -ne 2 ] ; then
RETORN=1
fi
TEXTRETORN="$TEXTRETORN Warn($ite),"
fi
done
if [ $RETORN -eq 0 ] ; then
TEXTRETORN="OK. Totes maquines ($nombre_dominis) per sota limits CPU."
fi
if [ $RETORN -eq 1 ] ; then
TEXTRETORN="WARNING. $TEXTRETORN"
fi
if [ $RETORN -eq 2 ] ; then
TEXTRETORN="ERROR. $TEXTRETORN"
fi

rm -f $TEMPFILE
echo $TEXTRETORN
exit $RETORN

La salida si todo esta Ok seria:
OK. Totes maquines (5) per sota limits CPU.

Luego hay que añadir el comando para hacer sudo en el usuario nagios para los comandos xentop y xm list

nagios ALL=(ALL) NOPASSWD:/usr/sbin/xm list
nagios ALL=(ALL) NOPASSWD:/usr/sbin/xentop

No hay comentarios:

Publicar un comentario