deluan
3/8/2015 - 5:00 AM

Use this script for services you don't want BB to alert in red on the first failure. It will become yellow first, and then, after a configur

Use this script for services you don't want BB to alert in red on the first failure. It will become yellow first, and then, after a configurable number of polls, it will go red. Useful with network tests over a noisy link (except when you're responsible for the link ;). Tested only with Linux. http://goo.gl/WZFwNZ

#!/bin/sh
#
# $Id: bb-trigger.sh,v 1.2 2002/01/29 18:50:17 deluan Exp $
#
# by Deluan Cotts Quintao (bb@deluan.com.br)
#
# Use this script for services you don't want BB to alert in red on the first error.
# It will become yellow first, and then, after a configurable number of polls, it will
# go red. Useful with network tests over a noisy link (except when you're responsible
# for the link ;)
#
# Instalation:
#
#	$ cd $BBHOME
#	$ ./runbb.sh stop
#	$ cd bin
#	$ mv bb bb.ORIG
#
#	Save this script as $BBHOME/bin/bb
#	Edit variables in the CONFIGURATION section (bellow)
#
#	$ chmod +x bb
#	$ ./runbb.sh start
#	
# OBS: This script has only been tested with Red Hat Linux 7.1
#	
# *******************************************************
# Use at your own risks, I take no responsabilities
# if you use this script. You can redistribute at will.
# If you change this script, I just ask you leave my name
# in the script and the copyright must stay the same.
# *******************************************************
#
# $Log: bb-trigger.sh,v $
# Revision 1.2  2002/01/29 18:50:17  deluan
# First public release.
#
#

IPADDR=$1
MSG=$2

#BBHOME=/usr/local/bb
#BBTMP=/tmp

# echo "***** BBHOME IS SET TO $BBHOME IN bb-trigger.sh"
if test ! "$BBTMP"                      # GET DEFINITIONS IF NEEDED
then
        . $BBHOME/etc/bbdef.sh          # INCLUDE STANDARD DEFINITIONS
fi

###########################################
########### CONFIGURATION #################

TRIGGEREDSVCS="$BBNETSVCS http dns dig"		# Services that will be intercepted
TRIGGERCOUNT=1					# How many polls before becoming "red"

BASENAME=/bin/basename

########### END OF CONFIGURATION ##########
###########################################


# Read current counter value for host.svc and increment it by one
get_err_count()
{
        ERRFILE=${BBTMP}/$1.err.count
        if [ -f "$ERRFILE" ]
        then
                ERRCOUNT=`cat $ERRFILE`
                ERRCOUNT=`${EXPR} $ERRCOUNT + 1`
        else
                ERRCOUNT=1
        fi
        echo $ERRCOUNT > $ERRFILE
        return $ERRCOUNT;
}

# Clear (remove) counter value for host.svc
clear_err_count()
{
        ERRFILE=${BBTMP}/$1.err.count
        if [ -f "$ERRFILE" ]
        then
                ${RM} -f $ERRFILE
        fi
}

#set -x	# for debuging...

set $MSG
MSGTYPE=$1; shift

if [ $MSGTYPE = status ]; then
	HOSTSVC=$1; shift
	COLOR=$1
	SVC=`echo $HOSTSVC | ${CUT} -f 2 -d "."`
	if `echo $TRIGGEREDSVCS | ${GREP} $SVC > /dev/null`; then
		if [ $COLOR = red ]; then
			get_err_count $HOSTSVC
			ERRORCOUNT=$?
			if [ "$ERRORCOUNT" -le "$TRIGGERCOUNT" ]; then
				MSG=`echo "$MSG" | ${SED} "s/red/yellow/"`
				MSG="$MSG""

Failed on attempt #$ERRORCOUNT"
			fi
		elif [ $COLOR = green ]; then
			clear_err_count $HOSTSVC
		fi
	fi
elif [ $MSGTYPE = combo ]; then
	ERRLIST=`( cd $BBTMP; ls *.err.count  2> /dev/null )`
	for fn in $ERRLIST; do
		HOSTSVC=`${BASENAME} $fn .err.count`
		if `echo $MSG | ${GREP} $HOSTSVC > /dev/null`; then
			clear_err_count $HOSTSVC
		fi
	done
fi


${BB}.ORIG $IPADDR "$MSG"