Listing 5 Failsafe updates to the Nagios Config Files
are provided by this script, which gets the changes from CVS, checks
for errors, and creates new mailing lists if necessary.
#!/bin/sh
# Get changes to the configs
# Check them for errors
# HUP Nagios
# Create mailing lists as required.
HOME='/usr/local/nagios'
BIN="${HOME}/bin"
CFG='/etc/nagios'
STATUS="${HOME}/var/status"
LISTHOME='/usr/local/nagios/lists'
LISTMAKER="${BIN}/listmaker.sh" # <--the script from Listing 4
############ Get changes to the configs
echo "refreshing the conf files from CVS"
cd /etc/nagios/ && cvs update -dAC
chown nagios.nagios /etc/nagios/*
############# Check them for errors
echo "checking for configuration errors"
ERRORS=`${BIN}/nagios \
-v ${CFG}/nagios.cfg | grep 'Total Errors' | awk '{print $3}'`
if [ -z $ERRORS ] || [ "$ERRORS" -gt 0 ]
then
echo "ERRORS DETECTED"
${BIN}/nagios -v ${CFG}/nagios.cfg
exit 1
fi
echo "no errors found =-] "
############### HUP Nagios
echo "stopping nagios"
/etc/init.d/nagios stop
echo "waiting for children to die..."
sleep 1
CHILDREN=`ps -A | grep nagios | wc -l`
COUNTER=0
while [ ${CHILDREN} -gt 0 ]
do
echo "still ${CHILDREN} children alive, waiting..."
sleep 3
CHILDREN=`ps -A | grep nagios | wc -l`
COUNTER=$(($COUNTER+1))
if [ "$COUNTER" -gt 4 ]
then
echo "Killing impolitely, Weve been waiting too long"
killall -9 nagios
fi
done
echo "children dead, restarting parent"
/etc/init.d/nagios start
################### Create mailing lists as required.
echo -n "Did you add any new hosts or commands? (y/n) > "
read
if echo ${REPLY} | egrep -q '^[yY]'
then
find ${STATUS} -type f | sed -e \
's/.*\/\([^\/]\+\)\/\([^\/]\+\)$/\1 \2/' | while read i
do
HOST=`echo $i | cut -d\ -f1 | tr '[:upper:]' '[:lower:]'`
SERVICE=`echo $i | cut -d\ -f '2-' | tr '[:upper:]' \
'[:lower:]' | tr ' ' '_'`
if [ "${HOST}" != "${OLDHOST}" ] #must be a new host
then
if [ ! -e "${LISTHOME}/${HOST}-host" ]
then
echo "${HOST} host" | $LISTMAKER
fi
OLDHOST=${HOST}
fi
if [ ! -e "${LISTHOME}/${HOST}-${SERVICE}" ]
then
echo ${HOST} ${SERVICE} | $LISTMAKER
fi
echo -n '.'
done
fi
exit 0
|