Skip to content

Instantly share code, notes, and snippets.

@rmasci
Forked from larryclaman/polling.sh
Last active September 2, 2021 15:47
Show Gist options
  • Save rmasci/02c791ee8f215fa13c07047572260b3e to your computer and use it in GitHub Desktop.
Save rmasci/02c791ee8f215fa13c07047572260b3e to your computer and use it in GitHub Desktop.
Fixed polling.sh
#!/bin/bash
declare -i duration=1
declare hasUrl=""
declare endpoint
usage() {
cat <<END
polling.sh [-i] [-h] endpoint
Report the health status of the endpoint
-i: include Uri for the format
-h: help
END
}
while getopts "ih" opt; do
case $opt in
i)
hasUrl=true
;;
h)
usage
exit 0
;;
\?)
echo "Unknown option: -${OPTARG}" >&2
exit 1
;;
esac
done
shift $((OPTIND -1))
if [[ $1 ]]; then
endpoint=$1
else
echo "Please specify the endpoint."
usage
exit 1
fi
healthcheck() {
declare url=$1
result=$(curl --http1.1 -i $url 2>/dev/null | grep HTTP/1.1)
echo $result
}
for i in {1..10};do
result=`healthcheck $endpoint`
declare status
if [[ -z $result ]]; then
status="N/A"
else
status=${result:9:3}
fi
timestamp=$(date "+%Y%m%d-%H%M%S")
if [[ -z $hasUrl ]]; then
echo "$timestamp | $i | $status "
else
echo "$timestamp | $i |$status | $endpoint "
fi
echo $status | egrep "[2|4]0[0-9]" > /dev/null && exit
sleep $duration
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment