Skip to content

Instantly share code, notes, and snippets.

@revolunet
Last active August 31, 2023 19:41
Show Gist options
  • Save revolunet/dbd69aed95392c5fa88e1a1b07408226 to your computer and use it in GitHub Desktop.
Save revolunet/dbd69aed95392c5fa88e1a1b07408226 to your computer and use it in GitHub Desktop.
downtime-monitor.sh : Monitor HTTP status changes on some URL with curl
#!/bin/bash
#
# request some URL and report HTTP status changes
# ./downtime-monitor.sh https://some.url
#
URL="$1"
INTERVAL=0.1
TIMEOUT=1
_COUNT=0
_LAST_STATUS="";
_LAST_STATUS_DATE="";
while (true); do
printf "."
_STATUS=$(curl -L --max-time $TIMEOUT --connect-timeout $TIMEOUT -o /dev/null --fail --silent --show-error -w "%{http_code}" "${URL}")
_COUNT=$((_COUNT+1));
NOW=$(date +"%Y%m%dT%H:%M:%S")
if [ "$_LAST_STATUS" != "$_STATUS" ]
then
if [ "$_LAST_STATUS" ]
then
printf "\n%s: status : %s -> %s after %s requests (since %s)" "$NOW" "$_LAST_STATUS" "$_STATUS" "$_COUNT" "$_LAST_STATUS_DATE"
else
printf "\n%s: status : %s" "$NOW" "$_STATUS"
fi
#printf "\n%s: ⚠️ status %s" "$NOW" "$_STATUS";
_COUNT=0
_LAST_STATUS_DATE="$NOW"
fi
_LAST_STATUS="$_STATUS";
sleep $INTERVAL;
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment