Created
August 17, 2016 15:25
simple http-monitor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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