Skip to content

Instantly share code, notes, and snippets.

@madflojo
Created November 26, 2014 11:24
Show Gist options
  • Save madflojo/53b221dcfe1b0289e5b4 to your computer and use it in GitHub Desktop.
Save madflojo/53b221dcfe1b0289e5b4 to your computer and use it in GitHub Desktop.
Runbook: nginx health checker
#!/bin/bash
## nginx health check
## -------------------------------------------------
## This script will check if nginx is up or down. It will exit with a 0 status if nginx is up,
## and exit with a 1 status if nginx is down.
## This script also integrates cr-webhook.sh which is a notification script that sends a webhook
## request to a specified monitor at runbook.io. To use this script without the runbook webhook
## simply leave RUNBOOK_NOTIFY set to False.
## Runbook.io settings
# You can find these in the Runbook API Details at the bottom of all monitor creation pages
CHECK_KEY=""
MONITOR_URL=""
RUNBOOK_NOTIFY="False"
notify () {
if [ $1 == "True" ]
then
echo "nginx is $4, notifying runbook"
wget -O /var/tmp/cr-webhook.sh https://raw.githubusercontent.com/madflojo/runbook-webhook/master/cr-webhook.sh 2> /dev/null > /dev/null
if [ $? -eq 0 ]
then
chmod 755 /var/tmp/cr-webhook.sh
/var/tmp/cr-webhook.sh -k $2 -u $3 -a $4
if [ $? -eq 0 ]
then
echo "Runbook notified"
if [ $4 == "healthy" ]
then
exit 0
else
exit 1
fi
else
echo "Error notifying Runbook"
exit 1
fi
else
echo "Could not download cr-webhook.sh"
exit 1
fi
else
echo "nginx is $4"
if [ $4 == "healthy" ]
then
exit 0
else
exit 1
fi
fi
}
## Defaulting with ubuntu location
PIDFILE=/var/run/nginx.pid
if [ -r $PIDFILE ]
then
PID=$(cat $PIDFILE)
CHK=$(ps -p $PID)
if [ $? -eq 0 ]
then
notify $RUNBOOK_NOTIFY $CHECK_KEY $MONITOR_URL "healthy"
else
notify $RUNBOOK_NOTIFY $CHECK_KEY $MONITOR_URL "failed"
fi
else
CHK=$(ps -eo pid,cmd | grep -v grep | grep -c "nginx: master")
if [ $CHK -ge 1 ]
then
notify $RUNBOOK_NOTIFY $CHECK_KEY $MONITOR_URL "healthy"
else
notify $RUNBOOK_NOTIFY $CHECK_KEY $MONITOR_URL "failed"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment