Home > BashScripts, Nagios, plugins, Varnish > check_backend.sh

check_backend.sh

March 25th, 2014 Leave a comment Go to comments

Another bash script to monitor if there were any failed connections to the backend’s.
It is a simple aproach using the command:
varnishstat -1 -f backend_fail
Then it puts the output on a tmp file and next time it runs it checks if it is still having erros on the backend.

when configuring on nagios do not check more then once, every 5min.

Here is the code:

#!/bin/bash
# bash simple script to get the varnish 3 hit_ratio
# by Felipe Ferreira Mar/2014

if [ $# -ne 2 ]; then
# set default hit_ratio thresholds
CRIT=5
WARN=1
else
CRIT=$1 
WARN=$2
fi

L="/tmp/backend_fail.log"  #log de antes
LN="/tmp/backend_fail_now.log" #log de agora

function sai(){
	echo $MSG
	/bin/cp -f $LN $L
	exit $E
}

if [ ! -f  $L ];then
	echo "0" > $L
	echo "0" > $LN
	echo "FILE $L NOT FOUND"
fi

varnishstat -1  -f backend_fail |awk '{ print $2}'>$LN

B=`tail -1 $L`
N=`tail -1 $LN`

if [ "$N" -eq "$B" ]; then
	MSG="OK - No erros found on backend|erros=0"
	E=0
	D=0
	sai
else
	#count diff
	D=`echo $N - $B |bc`
fi

MSG="$D erros on backend|erros=${D}"

if [ "$D" -ge "$CRIT" ]; then
	MSG="CRITICAL - $MSG"
	E=2
	sai
fi
if [ "$D" -ge "$WARN" ]; then
	MSG="WARNING - $MSG"
	E=1
	sai
else
	MSG="OK - $MSG"
	E=0
	sai
fi

Another way to do the same thing, is to have varnishlog running and analise the output

Here is the code:


#!/bin/bash
# Verifica se acounteceu algum erro com o backend do tipo 500 (Service Unavailable)
# A cada cheque zera a contagem
# ARGUMENTOS: WARNING CRITICAL TRUE for debuging

TMP="/tmp/logiis.tmp"
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKOWN=3
PID=0
WARN=50
CRIT=100
PID=`ps -ef |grep  "Unavailable -w" |grep -v grep |awk '{ print $2 }' |head -n 1`

if [ $# -gt 1 ]; then
        WARN=$1
        CRIT=$2
fi  
if [ "$3" == "true" ]; then
  set -x
fi


#Verifica se o Varnishlog esta executando
if [ -n "$PID"  ]; then
	cnterros=`strings /tmp/logiis.tmp | wc -l`
        #Zero o arquivo mas continua rodando o varnishlog
        `> $TMP`
else
        printf  "UNKOWN - Varnishlog"$PID" nao encontrado.Executando varnishlog command"
       `nohup varnishlog -I Unavailable -w "$TMP" >& /dev/null &`
        exit $STATE_UNKOWN
fi
if [ "$cnterros" -ge "$CRIT" ] ; then
        printf  "CRTICAL - Existem $cnterros erros no IIS|IIS_Erros=$cnterros,$WARN,$CRIT"
        exit $STATE_CRITICAL
elif [ "$cnterros" -ge "$WARN" ] ; then
        printf "WARNING - Existem $cnterros erros no IIS|IIS_Erros=$cnterros,$WARN,$CRIT"
        exit $STATE_WARNING
elif [ "$cnterros" -le "$WARN" ]; then
        printf "OK - Existem $cnterros erros no backend de IIS|IIS_Erros=$cnterros,$WARN,$CRIT"
        exit $STATE_OK
fi
  1. No comments yet.
  1. No trackbacks yet.

VAMOVE *

*