Skip to content

Instantly share code, notes, and snippets.

@clodio
Created February 25, 2017 08:52
Show Gist options
  • Select an option

  • Save clodio/defda6a67213b22a7df08a63bfa79ba7 to your computer and use it in GitHub Desktop.

Select an option

Save clodio/defda6a67213b22a7df08a63bfa79ba7 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# FROM Nagios script to check website is up and responding in a timely manner
# Written by Chris Freeman (cfree6223@gmail.com)
# Version 1.1
# (c) GPLv2 2011
#
# Special thanks to dkwiebe and Konstantine Vinogradov for suggestions and feedback
#
# MODIFIED BY Claude seguret to add graphite metrics
### Environment paths
NETCAT=/bin/netcat
DATE=/bin/date
WGET=/usr/bin/wget
ECHO=/bin/echo
AWK=/usr/bin/awk
CKSUM=/usr/bin/cksum
TR=/usr/bin/tr
GREP=/bin/grep
RM=/bin/rm
# Temp file dir
WGETOUT=/tmp/wgetoutput
# graphite
PREFIX="10min.bcaa.$ENV.$HOSTNAME.check_business"
GRAPHITE_HOST="monitoring.bcaa.local"
GRAPHITE_PORT="2003"
CHECK_NAME="NONAME"
# TimeDIFF : time to call web service
TIMEDIFF=1
### Functions
# Check dependencies and paths
checkpaths(){
for PATH in $NETCAT $DATE $WGET $ECHO $AWK $CKSUM $TR $GREP $RM; do
if [ ! -f "$PATH" ]; then
STATUS=UNKNOWN
OUTMSG="ERROR: $PATH does does not exist"
output
fi
done
}
# Check inputs and formats
checkinputs(){
if [ ! -n "$WARN" ]; then
ERROR="Warning not set"
usage
fi
case $WARN in
*[!0-9]*)
ERROR="Warning must be an integer in milliseconds"
usage
esac
if [ ! -n "$CRIT" ]; then
ERROR="Critical not set"
usage
fi
case $CRIT in
*[!0-9]*)
ERROR="Critical must be an integer in milliseconds"
usage
esac
if [ "$CRIT" -lt "$WARN" ]; then
ERROR="Critical must be greater than Warning"
usage
fi
if [ ! -n "$URL" ]; then
ERROR="URL not set"
usage
fi
}
# Make temp file unique for URL
mktmpfile(){
WGETOUTCKSUM=$WGETOUT`$ECHO $URL |$CKSUM |$AWK '{print $1}'`
echo "">$WGETOUTCKSUM
}
# delete temp file unique for URL
rmtmpfile(){
WGETOUTCKSUM=$WGETOUT`$ECHO $URL |$CKSUM |$AWK '{print $1}'`
$RM $WGETOUTCKSUM
}
# Print usage statement
usage(){
$ECHO "RESPONSE: UNKNOWN - Error: $ERROR"
$ECHO "Usage: check_http_graphite -w <warning milliseconds> -c <critical milliseconds> -u <url> [ -n <check name> -s <string to find> -N nocert ]"
exit 3
}
# Check if URL resolves, port is open and webpage contains data
checkopen(){
# Determine PORT from scheme
STARTTIME=$($DATE +%s%N)
SCHEME=`$ECHO $URL |$AWK -F: '{print $1}'| $TR [:upper:] [:lower:]`
# Strip scheme out of URL
case $URL in
*://*)
SHORTURL=`$ECHO $URL |$AWK -F"://" '{print $2}'`;;
*)
SHORTURL=$URL;;
esac
# Strip path out of URL
case $SHORTURL in
*/*)
SHORTURL=`$ECHO $SHORTURL |$AWK -F/ '{print $1}'`;;
esac
# if no scheme check for ports in SHORTURL or else default to 80
case $SHORTURL in
*:*@*:*)
if [ ! -n "$PORT" ]; then
PORT=`$ECHO $SHORTURL |$AWK -F: '{print $3}'`
fi
SHORTURL=`$ECHO $SHORTURL |$AWK -F@ '{print $2}'`
SHORTURL=`$ECHO $SHORTURL |$AWK -F: '{print $1}'`;;
*:*@*)
if [ ! -n "$PORT" ]; then
PORT=80
fi
SHORTURL=`$ECHO $SHORTURL |$AWK -F@ '{print $2}'`;;
*:*)
if [ ! -n "$PORT" ]; then
PORT=`$ECHO $SHORTURL |$AWK -F: '{print $2}'`
fi
SHORTURL=`$ECHO $SHORTURL |$AWK -F: '{print $1}'`;;
*)
if [ "$SCHEME" = "https" ]; then
PORT=443
fi
if [ ! -n "$PORT" ]; then
PORT=80
fi;;
esac
# Check if URL resolves and port is open
if ! $NETCAT -z $SHORTURL $PORT > /dev/null 2>&1; then
OUTMSG=" URL '$SHORTURL' can't resolve or port $PORT not open"
STATUS=CRITICAL
output
fi
# Check if page can be loaded and contains data
# calculate timeout
TIMEOUT=$(($CRIT+1000))
if [ -n "$NOCERT" ]; then
$WGET --no-check-certificate -q --no-proxy -T $TIMEOUT -O $WGETOUTCKSUM $URL
else
$WGET -q --no-proxy -T $TIMEOUT -O $WGETOUTCKSUM $URL
fi
ENDTIME=$($DATE +%s%N)
if [ ! -s "$WGETOUTCKSUM" ]; then
OUTMSG="$URL does not contain any data"
STATUS=CRITICAL
output
fi
if [ ! -s $STRING_TO_BE_CONTAINED ]; then
if ! $GREP -qe "$STRING_TO_BE_CONTAINED" "$WGETOUTCKSUM";
then
# String not found
OUTMSG="string '$STRING_TO_BE_CONTAINED' not found"
STATUS=CRITICAL
output
fi
fi
# Check page response time
TIMEDIFF=$((($ENDTIME-$STARTTIME)/1000000))
if [ "$TIMEDIFF" -lt "$WARN" ]; then
STATUS=OK
elif [ "$TIMEDIFF" -ge "$WARN" ] && [ "$TIMEDIFF" -lt "$CRIT" ]; then
OUTMSG="slow response time"
STATUS=WARNING
elif [ "$TIMEDIFF" -ge "$CRIT" ]; then
OUTMSG="slow response time"
STATUS=CRITICAL
fi
OUTMSG="$TIMEDIFF ms"
}
# Output statement and exit
output(){
rmtmpfile
$ECHO "RESPONSE: $STATUS - $OUTMSG""|Response="$TIMEDIFF"ms;"$WARN";"$CRIT";0"
if [ "$STATUS" = "OK" ]; then
$ECHO $PREFIX".""$CHECK_NAME"."OK" $TIMEDIFF $($DATE +%s) | $NETCAT $GRAPHITE_HOST $GRAPHITE_PORT
exit 0
elif [ "$STATUS" = "WARNING" ]; then
$ECHO $PREFIX".""$CHECK_NAME"."WARNING" $TIMEDIFF $($DATE +%s) | $NETCAT $GRAPHITE_HOST $GRAPHITE_PORT
exit 1
elif [ "$STATUS" = "CRITICAL" ]; then
$ECHO $PREFIX".""$CHECK_NAME"."CRITICAL" $TIMEDIFF $($DATE +%s) | $NETCAT $GRAPHITE_HOST $GRAPHITE_PORT
exit 2
fi
$ECHO $PREFIX".""$CHECK_NAME"."UNKNOWN" $TIMEDIFF $($DATE +%s) | $NETCAT $GRAPHITE_HOST $GRAPHITE_PORT
exit 3
}
### Main
# Input variables
while getopts w:c:u:n:s:N:n: option
do case "$option" in
w) WARN=$OPTARG;;
c) CRIT=$OPTARG;;
u) URL=$OPTARG;;
s) STRING_TO_BE_CONTAINED=$OPTARG;;
N) NOCERT=$OPTARG;;
n) CHECK_NAME=$OPTARG;;
*) ERROR="Illegal option used"
usage;;
esac
done
checkpaths
checkinputs
mktmpfile
checkopen
output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment