|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +declare -i time=60 |
| 4 | +declare -i delay=5 |
| 5 | +declare -i count=5 |
| 6 | +declare -i okCount=0 |
| 7 | +declare -i elapsed=0 |
| 8 | +declare isUp="false" |
| 9 | + |
| 10 | +# Display usage |
| 11 | +usage(){ |
| 12 | +echo -e "\e[32m╭──────────────────────────────────────────────────────────────╮" |
| 13 | +echo -e "│ 🌍 \e[94murl-check.sh \e[96mCheck URL endpoint for HTTP responses 🚀\e[32m │" |
| 14 | +echo -e "╰──────────────────────────────────────────────────────────────╯" |
| 15 | +echo -e "\n\e[95mParameters:\e[37m" |
| 16 | +echo -e " -u, --url \e[33mURL to check (required)\e[37m" |
| 17 | +echo -e " [-t, --time] \e[33mMaximum number of seconds to poll for \e[92m(default: 60)\e[37m" |
| 18 | +echo -e " [-d, --delay] \e[33mDelay in seconds between requests \e[92m(default: 5)\e[37m" |
| 19 | +echo -e " [-c, --count] \e[33mHow many successes to receive before exiting \e[92m(default: 5)\e[37m" |
| 20 | +echo -e " [-s, --search] \e[33mOptional content check, grep for this string in HTTP body \e[92m(default: none)\e[37m" |
| 21 | +echo -e " [-h, --help] \e[33mShow this help text\e[37m" |
| 22 | +} |
| 23 | + |
| 24 | +OPTS=`getopt -o u:t:d:c:s:h --long url:,time:,delay:,count:,search:,help -n 'parse-options' -- "$@"` |
| 25 | +if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; usage; exit 1 ; fi |
| 26 | +eval set -- "$OPTS" |
| 27 | + |
| 28 | +while true; do |
| 29 | + case "$1" in |
| 30 | + -u | --url ) url="$2"; shift; shift;; |
| 31 | + -t | --time ) time="$2"; shift; shift;; |
| 32 | + -d | --delay ) delay="$2"; shift; shift;; |
| 33 | + -c | --count ) count="$2"; shift; shift;; |
| 34 | + -s | --search ) search="$2"; shift; shift;; |
| 35 | + -h | --help ) HELP=true; shift ;; |
| 36 | + -- ) shift; break ;; |
| 37 | + * ) break ;; |
| 38 | + esac |
| 39 | +done |
| 40 | + |
| 41 | +if [[ ${HELP} = true ]] || [ -z ${url} ]; then |
| 42 | + usage |
| 43 | + exit 0 |
| 44 | +fi |
| 45 | + |
| 46 | +# Check for impossible parameter combination ie. too many checks and delays in given time limit |
| 47 | +if (( $delay * $count > $time)); then |
| 48 | + echo -e "\e[31m### Error! The time ($time) provided is too short given the delay ($delay) and count ($count)\e[0m" |
| 49 | + exit 1 |
| 50 | +fi |
| 51 | + |
| 52 | +echo -e "\n\e[36m### Polling \e[33m$url\e[36m for ${time}s, to get $count OK results, with a ${delay}s delay\e[0m\n" |
| 53 | + |
| 54 | +# Generate tmp filename |
| 55 | +tmpfile=$(echo $url | md5sum) |
| 56 | + |
| 57 | +# Main loop |
| 58 | +while [ "$isUp" != "true" ] |
| 59 | +do |
| 60 | + # Break out of loop if max time has elapsed |
| 61 | + if (( $elapsed >= $time )); then break; fi |
| 62 | + timestamp=$(date "+%Y/%m/%d %H:%M:%S") |
| 63 | + |
| 64 | + # Main CURL test, output to file and return http_code |
| 65 | + urlstatus=$(curl -o "/tmp/$tmpfile" --silent --write-out '%{http_code}' "$url") |
| 66 | + |
| 67 | + if [ $urlstatus -eq 000 ]; then |
| 68 | + # Code 000 means DNS, network error or malformed URL |
| 69 | + msg="\e[95mSite not found or other error" |
| 70 | + else |
| 71 | + if (( $urlstatus >= 200 )) && (( $urlstatus < 300 )); then |
| 72 | + # Check returned content with grep if check specified |
| 73 | + if [ ! -z "$search" ]; then |
| 74 | + grep -q "$search" "/tmp/$tmpfile" |
| 75 | + # Only count as a success if string grep passed |
| 76 | + if (( $? == 0)); then |
| 77 | + ((okCount=okCount + 1)) |
| 78 | + msg="✅ \e[32m$urlstatus 🔍 Content check for '$search' passed" |
| 79 | + else |
| 80 | + msg="❌ \e[91m$urlstatus 🔍 Content check for '$search' failed" |
| 81 | + fi |
| 82 | + else |
| 83 | + # Good status code |
| 84 | + ((okCount=okCount + 1)) |
| 85 | + msg="✅ \e[32m$urlstatus " |
| 86 | + fi |
| 87 | + |
| 88 | + if (( $okCount >= $count )); then isUp="true"; fi |
| 89 | + else |
| 90 | + # Bad status code |
| 91 | + msg="❌ \e[91m$urlstatus " |
| 92 | + fi |
| 93 | + fi |
| 94 | + |
| 95 | + # Output message + timestamp then delay |
| 96 | + echo -e "### $timestamp: $msg\e[0m" |
| 97 | + sleep $delay |
| 98 | + ((elapsed=elapsed + delay)) |
| 99 | +done |
| 100 | + |
| 101 | +rm "/tmp/$tmpfile" |
| 102 | +# Final result check |
| 103 | +if [ "$isUp" == "true" ]; then |
| 104 | + echo -e "\n\e[32m### Result: $url is UP! 🤩\e[0m" |
| 105 | + exit 0 |
| 106 | +else |
| 107 | + echo -e "\n\e[91m### Result: $url is DOWN! 😢\e[0m" |
| 108 | + exit 1 |
| 109 | +fi |
0 commit comments