monitor.cgi 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/bin/sh
  2. # https://mro.name/radio-privatkopie
  3. #
  4. # checked hourly, see e.g. https://updown.io/44q5 join https://updown.io/r/c7a7S
  5. # https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
  6. set -eu
  7. cd "$(dirname "${0}")/../" || exit 1
  8. cr="\\n"
  9. # some nasty but fancy utf-8 emojis
  10. readonly ok="✅"
  11. readonly warn="⚠️"
  12. readonly fail="❌"
  13. msg=""
  14. error_exit () {
  15. cat <<EOF
  16. Status: ${1}
  17. Content-Type: text/plain; charset=utf-8
  18. https://mro.name/radio-privatkopie
  19. EOF
  20. echo "${msg}${cr}${2}"
  21. exit 1
  22. }
  23. readonly cmd0="stations/b2/app/html2broadcast-br-$(uname -s)-$(uname -m)"
  24. jq --version 1>/dev/null \
  25. || error_exit 500 "${fail} please install \$ sudo apt-get install jq"
  26. msg="${msg}${cr}${ok} jq"
  27. curl --version >/dev/null \
  28. || error_exit 500 "${fail} please install \$ sudo apt-get install curl"
  29. msg="${msg}${cr}${ok} curl"
  30. id3v2 --version 2>/dev/null >&2 \
  31. || error_exit 500 "${fail} please install \$ sudo apt-get install id3v2"
  32. msg="${msg}${cr}${ok} id3v2"
  33. ffmpeg -version 2>/dev/null >&2 \
  34. || error_exit 500 "${fail} please install \$ sudo apt-get install ffmpeg"
  35. msg="${msg}${cr}${ok} ffmpeg"
  36. # which ffmpeg 2> /dev/null >&2 \
  37. # || error_exit 500 "${fail} please install \$ sudo apt-get install ffmpeg"
  38. # msg="${msg}${cr}${ok} ffmpeg"
  39. xmllint --version 2>/dev/null \
  40. || error_exit 500 "${fail} please install \$ sudo apt-get install libxml2-utils"
  41. msg="${msg}${cr}${ok} xmllint"
  42. xsltproc --version 2>/dev/null >&2 \
  43. || error_exit 500 "${fail} please install \$ sudo apt-get install xsltproc"
  44. msg="${msg}${cr}${ok} xsltproc"
  45. [ "radio-pi www-data" = "$(id -Gn radio-pi)" ] \
  46. || error_exit 500 "${fail} please setup \$ sudo useradd -mG www-data radio-pi"
  47. msg="${msg}${cr}${ok} user radio-pi"
  48. txt="$(/usr/sbin/service atd status)"
  49. [ "atd is running." = "${txt}" ] \
  50. || error_exit 500 "${fail} please install dma and \$ sudo apt-get instal at"
  51. msg="${msg}${cr}${ok} ${txt}"
  52. txt="$(/usr/sbin/service cron status)"
  53. [ "cron is running." = "${txt}" ] \
  54. || error_exit 500 "${fail} please install \$ sudo apt-get install cron"
  55. msg="${msg}${cr}${ok} ${txt}"
  56. txt="$(/usr/sbin/service lighttpd status)"
  57. [ "lighttpd is running." = "${txt}" ] \
  58. || error_exit 500 "${fail} please install a webserver, e.g. \$ sudo apt-get install lighttpd"
  59. msg="${msg}${cr}${ok} ${txt}"
  60. txt="$(/usr/sbin/service ntp status)"
  61. [ "NTP server is running." = "${txt}" ] \
  62. || error_exit 500 "${fail} ${txt}"
  63. msg="${msg}${cr}${ok} ${txt}"
  64. [ ! -s "error.log" ] \
  65. || error_exit 500 "${fail} please inspect error.log - there are some."
  66. msg="${msg}${cr}${ok} error.log empty"
  67. [ "error.log" = "$(find error.log -mmin -70)" ] \
  68. || error_exit 500 "${fail} error.log is old."
  69. msg="${msg}${cr}${ok} error.log fresh"
  70. "${cmd0}" --version 2>/dev/null >&2 \
  71. || error_exit 500 "${fail} please install \$ ${cmd0}"
  72. msg="${msg}${cr}${ok} ${cmd0}"
  73. if [ 0 = 1 ] ; then
  74. if [ "" = "${REQUEST_SCHEME}" ] ; then
  75. base="http://rec.mro.name"
  76. else
  77. base="${REQUEST_SCHEME}://${HTTP_HOST}:${SERVER_PORT}"
  78. fi
  79. for sta in stations/b2/now
  80. do
  81. url="${base}/${sta}"
  82. if [ 0 = 1 ] && \
  83. [ "${sta}" = "stations/b5/now" ]
  84. then
  85. msg="${msg}${cr}${warn} GET ${url}"
  86. continue
  87. fi
  88. code="$(curl \
  89. --head \
  90. --location \
  91. --output /dev/null \
  92. --silent \
  93. --url "${url}" \
  94. --user-agent "https://mro.name/radio-privatkopie" \
  95. --write-out "%{http_code}" \
  96. 2>/dev/null)"
  97. [ "${code}" = "200" ] || error_exit 500 "${fail} status ${code} <= GET ${url}"
  98. msg="${msg}${cr}${ok} GET ${url}"
  99. done
  100. fi
  101. cat <<EOF
  102. Status: 200
  103. Content-Type: text/plain; charset=utf-8
  104. https://mro.name/radio-privatkopie
  105. EOF
  106. echo "${msg}"