Check active server in DNS failover pair
root@com6-prod-nagios01:/etc/nagios3/conf.d# cat /root/nagios/check_primary_server/check_primary_server.sh
#!/bin/bash
#Written Andrew Stringer 18/08/2016.
#Purpose is to show which is the master server from a xxxx
#Retcode 0 = OK
#Retcode 1 = Warning
#Retcode 2 = Critical
#Retcode 3 = Unknown
#Nagios Config
#Check which service is Live!
#define command {
# command_name check_primary_server
# command_line /usr/lib/nagios/plugins/check_primary_server.sh '$ARG1$'
# }
## Dummy host to pin services to
#define host{
# use generic-host
# host_name ActiveServers
# alias ActiveServers
# address 127.0.0.1
# hostgroups ActiveServers
# icon_image base/icon.png
# #contact_groups unixadmin
# notes_url https://wiki.int/page.php?id=troubleshooting:active-servers
# parents sw1, sw2
# }
#define hostgroup{
# hostgroup_name ActiveServers
# alias Active Server
# }
#define service {
# use generic-service
# host_name ActiveServers
# service_description Check active STAG Memcache
# check_command check_primary_server!stag-memcache.int.zone.
# contact_groups unixadmin
# }
#Start of code
error() { echo "DNS name is required. Usage:- check_primary_server.sh DNS-name" 1>&2; exit 3; }
#Check we have some arguments passed
if [ -z "$1" ] ; then
error
exit 3
else
LIVEHOST_TMP=`host ${1}`
RETCODE=$?
#echo "${LIVEHOST_TMP}"
CNAME=`echo ${LIVEHOST_TMP} | grep alias` 2>&1 > /dev/null
CNAMERC=$?
#echo "cnamerc is $CNAMERC"
#check if we get an A record returned, if so, the address we are being fed with is wrong. Exit with error
if [[ $RETCODE == 0 ]] && [[ $CNAMERC == 1 ]] ; then
echo "CNAME required, you have supplied an A record!"
exit 3
fi
if [[ $RETCODE == 0 ]] ; then
LIVEHOST=`echo ${LIVEHOST_TMP} | grep alias | cut -d' ' -f 6 | cut -d. -f1`
TEXT="Active server is ${LIVEHOST}"
STATUS=0
elif [[ $RETCODE == 2 ]]; then
LIVEHOST="invalid"
TEXT="DNS name is unresolvable."
STATUS=2
else
TEXT="Unknown Error"
STATUS=3
fi
#echo "ret $STATUS -- $LIVEHOST"
fi
#exit from check.
if [[ $STATUS -eq 0 ]]; then
echo "${TEXT}"
exit 0
elif [[ $STATUS -eq 1 ]]; then
echo "${TEXT}"
exit
elif [[ $STATUS -eq 2 ]]; then
echo "${TEXT}"
exit 2
else
echo "${TEXT}"
exit 3
fi
exit 0