random bash scripts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.2 KiB

  1. #!/bin/bash
  2. set -e
  3. function cleanup() {
  4. for pid in "${PIDS[@]}"; do
  5. kill -0 "$pid" 2 &>/dev/null && kill "$pid"
  6. done
  7. exit 0
  8. }
  9. function position() {
  10. # based on a script from http://invisible-island.net/xterm/xterm.faq.html
  11. exec </dev/tty
  12. oldstty=$(stty -g)
  13. stty raw -echo min 0
  14. # on my system, the following line can be replaced by the line below it
  15. # echo -en "\033[6n" > /dev/tty
  16. tput u7 >/dev/tty # when TERM=xterm (and relatives)
  17. IFS=';' read -r -d R -a pos
  18. stty "$oldstty"
  19. # change from one-based to zero based so they work with: tput cup $row $col
  20. row=$((${pos[0]:2} - 1)) # strip off the esc-[
  21. echo "$row"
  22. }
  23. function monitor() {
  24. local url="$1"
  25. local p=$((START_CURSOR + $2 + 1))
  26. # -w "$GREEN%{http_code}$RESET $url \\033[${p};${TIMING_COL}f%{time_total}s TTFB: %{time_connect} + %{time_starttransfer} (%{size_download})" \
  27. echo -e "\\033[$p;0f$(curl "$url" --compress -s -o /dev/null -w \
  28. "%{http_code}|%{time_total}|%{time_pretransfer}|%{time_starttransfer}\\n" |
  29. sed -n '/^/ s///p' |
  30. while IFS='|' read -r http_code time_total time_pretransfer time_starttransfer; do
  31. if [[ $http_code -gt 399 ]]; then
  32. http_code="$RED$http_code$RESET"
  33. else
  34. http_code="$GREEN$http_code$RESET"
  35. fi
  36. TTFB=$(echo -e "$time_starttransfer $time_pretransfer" | awk '{ printf "%.3fs", $1 - $2 }')
  37. echo -e "$http_code $url \\033[$p;${TIMING_COL}f${time_total}s (TTFB $TTFB)"
  38. done)\\n\\033[$LAST_CURSOR;0f" &
  39. PIDS=("${PIDS[@]}" "$!")
  40. }
  41. declare -a PIDS
  42. RED="\\033[31m"
  43. RESET="\\033[0m"
  44. GREEN="\\033[32m"
  45. START_CURSOR=$(position)
  46. LAST_CURSOR=0
  47. TIMING_COL=0
  48. declare -a URLS
  49. if [ -t 0 ]; then
  50. if [ $# -eq 0 ]; then
  51. echo -e "Usage:\\n\\n ${RED}check$RESET <url>\\n cat urls.txt | \\033[1mcheck$RESET\\n"
  52. exit 1
  53. else
  54. URLS=("$1")
  55. fi
  56. else
  57. while read -r line; do
  58. URLS=("${URLS[@]}" "$line")
  59. done
  60. fi
  61. trap cleanup EXIT
  62. for url in "${URLS[@]}"; do
  63. length=${#url}
  64. if [[ $length > $TIMING_COL ]]; then TIMING_COL=$length; fi
  65. echo -e "... $url"
  66. done
  67. TIMING_COL=$((TIMING_COL + 6))
  68. LAST_CURSOR=$(position)
  69. if [[ $LAST_CURSOR == $(($(tput lines) - 1)) ]]; then
  70. START_CURSOR=$((LAST_CURSOR - ${#URLS[@]}))
  71. fi
  72. for index in "${!URLS[@]}"; do
  73. monitor "${URLS[index]}" "$index"
  74. done
  75. wait "${PIDS[@]}"