├── check-script.gif ├── README.md └── check /check-script.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/remy/check/HEAD/check-script.gif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Checks the status of a list of urls 2 | 3 | Usage: 4 | 5 | ```bash 6 | $ ./check 7 | $ ./check < url-list.txt 8 | $ ./check -- -k # pass -k to curl command 9 | ``` 10 | 11 | ![demo](check-script.gif) 12 | 13 | Runs `GET` requests against the list of URLs and prints: 14 | 15 | - status code 16 | - url 17 | - time to complete request 18 | - time to first byte (time_pretransfer - time_starttransfer) 19 | 20 | Note: this was written on a Mac in a hacky fashion (sorry), but also not 21 | tested on other *nix systems. If you see a problem, please open a pull request. 22 | -------------------------------------------------------------------------------- /check: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # version 1.1.1 4 | 5 | set -e 6 | 7 | function cleanup { 8 | for pid in "${PIDS[@]}" 9 | do 10 | kill -0 $pid 2&> /dev/null && kill $pid 11 | done 12 | exit 0 13 | } 14 | 15 | function position { 16 | # based on a script from http://invisible-island.net/xterm/xterm.faq.html 17 | exec < /dev/tty 18 | oldstty=$(stty -g) 19 | stty raw -echo min 0 20 | 21 | # on my system, the following line can be replaced by the line below it 22 | # echo -en "\033[6n" > /dev/tty 23 | tput u7 > /dev/tty # when TERM=xterm (and relatives) 24 | IFS=';' read -r -d R -a pos 25 | stty $oldstty 26 | 27 | # change from one-based to zero based so they work with: tput cup $row $col 28 | row=$((${pos[0]:2} - 1)) # strip off the esc-[ 29 | col=$((${pos[1]} - 1)) 30 | 31 | echo "$row" 32 | } 33 | 34 | function monitor { 35 | local url=$1 36 | local p=$((START_CURSOR+$2+1)) 37 | 38 | # -w "$GREEN%{http_code}$RESET $url \\033[${p};${TIMING_COL}f%{time_total}s TTFB: %{time_connect} + %{time_starttransfer} (%{size_download})" \ 39 | 40 | echo -e "\033[${p};0f$(curl ${CURL_ARGS} $url --compress -s -o /dev/null -w \ 41 | "%{http_code}|%{time_total}|%{time_pretransfer}|%{time_starttransfer}\n"\ 42 | | sed -n '/^/ s///p' \ 43 | | while IFS='|' read http_code time_total time_pretransfer time_starttransfer; 44 | do 45 | if [[ $http_code > 399 ]]; then 46 | http_code="$RED$http_code$RESET" 47 | else 48 | http_code="$GREEN$http_code$RESET" 49 | fi 50 | TTFB=$(echo -e "$time_starttransfer $time_pretransfer" | awk '{ printf "%.3fs", $1 - $2 }') 51 | echo -e "$http_code $url \\033[${p};${TIMING_COL}f${time_total}s (TTFB $TTFB)" 52 | done)\n\033[${LAST_CURSOR};0f" & 53 | 54 | PIDS=("${PIDS[@]}" "$!") 55 | } 56 | 57 | declare -a PIDS 58 | RED="\033[31m" 59 | RESET="\033[0m" 60 | GREEN="\033[32m" 61 | START_CURSOR=$(position) 62 | LAST_CURSOR=0 63 | TIMING_COL=0 64 | CURL_ARGS="" 65 | declare -a URLS 66 | 67 | if [ -t 0 ]; then # nothing on stdin 68 | if [ $# -eq 0 ] # if arg count is 0 - then error 69 | then 70 | echo -e "Usage:\n\n ${RED}check${RESET} \n cat urls.txt | \033[1mcheck${RESET}\n" 71 | exit 1 72 | fi 73 | else 74 | while read line # from stdin 75 | do 76 | URLS=("${URLS[@]}" $line) 77 | done 78 | fi 79 | 80 | # then double check the arguments on the command line 81 | for ((i=0 ; i < $# ; i++)) # read each url individually 82 | do 83 | if [ "$1" = "--" ] # if "end of options" 84 | then 85 | shift 86 | CURL_ARGS=$@ # the pass along the options to cURL 87 | break 88 | fi 89 | if [ -n "$1" ] 90 | then 91 | URLS=("${URLS[@]}" $1) 92 | fi 93 | shift 94 | done 95 | 96 | trap cleanup EXIT # so that we can put cURL in the background, but wait until it's finished 97 | 98 | for url in "${URLS[@]}" 99 | do 100 | length=${#url} 101 | if [[ $length > $TIMING_COL ]]; then TIMING_COL=$length; fi 102 | echo -e "... $url" 103 | done 104 | 105 | TIMING_COL=$(( $TIMING_COL + 6)) 106 | LAST_CURSOR=$(position) 107 | 108 | if [[ $LAST_CURSOR == $(( $(tput lines) - 1 )) ]] 109 | then 110 | START_CURSOR=$(( LAST_CURSOR - ${#URLS[@]})) 111 | fi 112 | 113 | for index in "${!URLS[@]}" 114 | do 115 | monitor ${URLS[index]} $index 116 | done 117 | 118 | wait ${PIDS[@]} 119 | --------------------------------------------------------------------------------