Last active
March 30, 2024 12:59
-
-
Save jpedro/2c2bfb027a7938d4664f247846135ca2 to your computer and use it in GitHub Desktop.
Quick and dirty HTTP status check
This file contains hidden or 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/bin/env bash | |
set -uo pipefail | |
STATUS_CHUNKS="${STATUS_CHUNKS:-10}" | |
URL="${1:-}" | |
if [[ "$URL" == "" ]] | |
then | |
echo >&2 "Usage: $0 URL" | |
exit 1 | |
fi | |
COUNT=0 | |
RESET="\033[0m" | |
PALE="$RESET\033[2m" | |
YELLOW="$RESET\033[33;1m" | |
GRAY="$RESET\033[38;5;242m" | |
declare -A COUNTERS | |
declare -A TIMERS | |
summary() { | |
echo -en '\r' | |
echo " " | |
echo "SUMMARY" | |
for code in ${!COUNTERS[@]} | |
do | |
count=${COUNTERS[${code}]} | |
total=${TIMERS[${code}]} | |
milis=$(( total / count )) | |
echo -e " $(color $code) $count reqs in $total ms (Average $milis ms)" | |
done | |
exit | |
} | |
record() { | |
local code=$1 | |
local time=$2 | |
local count=1 | |
local total=0 | |
if [[ -n "${COUNTERS[$code]:-}" ]] | |
then | |
count=${COUNTERS[$code]} | |
fi | |
COUNTERS[$code]=$(( count + 1 )) | |
if [[ -n "${TIMERS[$code]:-}" ]] | |
then | |
total=${TIMERS[$code]} | |
fi | |
TIMERS[$code]=$(( total + time )) | |
} | |
color() { | |
local code=$1 | |
if [[ "$code" -ge 400 ]] | |
then | |
echo "$RESET\033[41;1m $code $RESET" | |
elif [[ "$CODE" -ge 300 ]] | |
then | |
echo "$RESET\033[43;38;5;232;1m $code $RESET" | |
else | |
echo "$RESET\033[32;1m $code $RESET" | |
fi | |
} | |
trap summary SIGINT SIGABRT SIGHUP SIGQUIT | |
echo | |
echo -e "Checking ${YELLOW}${URL}${RESET}" | |
while true | |
do | |
COUNT=$(( COUNT + 1 )) | |
RESULT=$(curl -so /dev/null $URL -w "%{http_code}@%{time_total}") | |
CODE="$(echo "$RESULT" | cut -d@ -f1)" | |
TIME="$(echo "$RESULT" | cut -d@ -f2)" | |
COLOR="$(color $CODE)" | |
if [[ "$(( COUNT % STATUS_CHUNKS ))" == "0" ]] | |
then | |
echo | |
echo -e "Still checking ${YELLOW}${URL}${RESET}" | |
fi | |
NOW=$(date +'%H:%M:%S') | |
MILI="$(echo $TIME | awk '{print int($1 * 1000)}')" | |
HEAD="$GRAY %6s $COLOR $PALE%5.0f ms$GRAY at %s$RESET\n" | |
printf "$HEAD" "$COUNT" "$MILI" "$NOW" | |
record $CODE $MILI | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment