Skip to content

Instantly share code, notes, and snippets.

@DnaX
Last active January 27, 2024 14:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DnaX/4597186 to your computer and use it in GitHub Desktop.
Save DnaX/4597186 to your computer and use it in GitHub Desktop.
This shell script check the availability of web sites probing them with curl. With HTTP status code >= 400 the site is considered unavailable (and the script return 1). The script check also response time without affect script status code. On terminal the output is colored instead on redirect (for example when log script results into file) the o…
#!/bin/bash
# sites to check
site=(
"www.google.com"
"www.github.com"
# insert here other sites...
)
# color costants
GREEN='\e[1;32m'
RED='\e[1;31m'
DARKCYAN='\e[0;36m'
CYAN='\e[1;36m'
RESET='\e[00m'
# counters
SITES_DOWN=0
SITES_SLOW=0
# do not print colors if we are redirecting output
COLOR(){
if [ -t 1 ] ; then
echo -ne $1
fi
}
# total of sites
LEN=${#site[@]}
echo "Checking $LEN sites..."
echo
for element in $(seq 0 $(($LEN - 1)))
do
# do not print this if we are redirecting output
if [ -t 1 ] ; then
echo -ne "${DARKCYAN}Probing ${CYAN}${site[$element]}${DARKCYAN}...${RESET}"
fi
# run curl
RET=`curl -sL -w "%{http_code} %{time_total}\\n" "${site[$element]}" -o /dev/null`
HTTP_CODE=`echo $RET | cut -d' ' -f1`
TIME=`echo $RET | cut -d' ' -f2`
# put cursor at 1st column
echo -en "\033[1G";
if [ "$HTTP_CODE" -lt "400" ]; then
# site up :)
COLOR $GREEN
else
# site down :(
SITES_DOWN=`expr $SITES_DOWN + 1`
COLOR $RED
fi
# print HTTP status code
echo -en $HTTP_CODE
COLOR $RESET
# print site name
echo -en " "${site[$element]}
# check if time is < 5 seconds
if [ "`echo $TIME | cut -d',' -f1`" -gt "5" ]
then
# site very slow (> 5s)
COLOR "\e[1;33m"
SITES_SLOW=`expr $SITES_SLOW + 1`
elif [ "`echo $TIME | cut -d',' -f1`" -gt "0" ]
then
# site slow (1 < time < 5)
COLOR "\e[0;33m"
else
# site ok (< 1s)
COLOR "\e[0;34m"
fi
# print time
echo -en " [${TIME}s]"
COLOR $RESET
echo
done
# summary
echo
COLOR $DARKCYAN
echo -n "Sites probed: "
COLOR $CYAN
echo -n $LEN
COLOR $RESET
# sites down summary
if [ $SITES_DOWN -gt 0 ]; then
COLOR $DARKCYAN
echo -n ", "
COLOR $RED
echo -n $SITES_DOWN
COLOR $DARKCYAN
echo -n " down"
fi
# sites slow summery
if [ $SITES_SLOW -gt 0 ]; then
COLOR $DARKCYAN
echo -n ", "
COLOR '\e[1;33m'
echo -n $SITES_SLOW
COLOR $DARKCYAN
echo -n " slow"
fi
echo
if [ $SITES_DOWN -gt 0 ]; then
# bonus: send desktop notification
# DISPLAY=:0 notify-send -i dialog-error "Site check" "${SITES_DOWN} site(s) are not available"
exit 1
else
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment