Skip to content

Instantly share code, notes, and snippets.

@leshniak
Created August 17, 2016 15:25
Show Gist options
  • Save leshniak/1dda36126300457ab085e080baacbf5c to your computer and use it in GitHub Desktop.
Save leshniak/1dda36126300457ab085e080baacbf5c to your computer and use it in GitHub Desktop.
simple http-monitor
#!/usr/local/bin/bash
# -- init
timestamp=false
bell=false
kill=false
delay="1s"
cleanup ()
{
echo
exit 0
}
trap cleanup SIGINT SIGTERM
while getopts ":hbktd:" option; do
case "$option" in
h) echo "usage: $0 [-h] [-k] [-b] [-t] [-d delay] URL ..."; exit ;;
t) timestamp=true ;;
b) bell=true ;;
k) kill=true ;;
d) delay=$OPTARG ;;
?) echo "error: option -$OPTARG is not implemented"; exit ;;
esac
done
shift $(($OPTIND - 1))
# -- main
url="$1"
echo -e "Starting monitor for \e[34m$url\e[0m with interval $delay..."
while true; do
[ "$timestamp" = true ] && echo -en "\n`date` ➡ "
http_status=`curl -X GET -Ifso /dev/null -w "%{http_code}" "$url" 2> /dev/null`
if [ "$http_status" == "200" ]; then
echo -ne "[\e[32m✔\e[0m]"
else
echo -ne "[\e[31m✖\e[0m$http_status]"
[ "$bell" = true ] && tput bel
[ "$kill" = true ] && cleanup
fi
sleep $delay
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment