Skip to content

Instantly share code, notes, and snippets.

@cosimo
Created May 12, 2015 13:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosimo/db351fe4b4310da242e4 to your computer and use it in GitHub Desktop.
Save cosimo/db351fe4b4310da242e4 to your computer and use it in GitHub Desktop.
Check if powerdns is alive and well
#!/bin/bash
#
# Powerdns sometimes will stall and keep two pipe backend processes
# for no apparent reason, and fill the logs with "Unable to bind to UDP socket"
# events. When that happens, then DNS service becomes unresponsive and
# doesn't answer queries.
#
# This script tries to detect such condition and will exit with a
# high status code (> 90) or zero if everything looks good.
#
# In this way, it'll be easier to integrate this script into a Monit check.
# For now, the Monit version that Squeeze carries (5.1.1) doesn't allow
# "check program" or "check process matching".
pdns_pattern='pdns_server-instance'
pdns_pidfile='/var/run/pdns.pid'
log () {
local msg="$1"
logger -t pdns_check "$msg"
}
if [ ! -e "${pdns_pidfile}" -o ! -s "${pdns_pidfile}" ]; then
log "[CRITICAL] Powerdns pid file '${pdns_pidfile}' doesn't exist."
exit 99
fi
pdns_pid=$(cat ${pdns_pidfile})
log "Powerdns pid is ${pdns_pid}"
if [ -z "${pdns_pid}" ]; then
log "[CRITICAL] Powerdns pidfile '${pdns_pid}' was empty?"
exit 98
fi
kill -s 0 ${pdns_pid} >/dev/null
if [ $? -ne 0 ]; then
log "[CRITICAL] Powerdns pid '${pdns_pid} seems dead"
exit 97
fi
udp_bind_failed=0
tail -n20 /var/log/syslog | grep 'Unable to bind to UDP socket' >/dev/null
if [ $? -eq 0 ]; then
udp_bind_failed=1
log "[CRITICAL] Found UDP bind failed events in syslog. Pdns is likely stalled or dead."
fi
pdns_instance_pid=$(pgrep -f "${pdns_pattern}")
if [ $? -ne 0 -o -z "${pdns_instance_pid}" ]; then
log "[CRITICAL] Powerdns worker instance (pdns_server-instance) seems dead. Some pipe backends stuck?"
if [[ "$udp_bind_failed" -eq 1 ]]; then
exit 196
else
exit 96
fi
fi
log "Powerdns is working fine (instance pid is ${pdns_instance_pid})"
# Logger might fail one day. Avoid that surprise.
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment