monitor.sh 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/bin/sh
  2. set -e
  3. if [ -z "$TOR_HOST" ]; then
  4. echo 'missing $TOR_HOST'
  5. exit 1
  6. elif [ -z "$ONION_SERVICE_HOST" ]; then
  7. echo 'missing $ONION_SERVICE_HOST'
  8. exit 1
  9. elif [ -z "$ONION_SERVICE_PORT" ]; then
  10. echo 'missing $ONION_SERVICE_PORT'
  11. exit 1
  12. elif [ -z "$RECIPIENT_ADDRESS" ]; then
  13. echo 'missing $RECIPIENT_ADDRESS'
  14. exit 1
  15. fi
  16. sender_address="onion-service-status@$(hostname).local"
  17. ping_onion_service() {(
  18. if [ ! -z "$VERBOSE" ]; then
  19. set -x
  20. fi
  21. nc -z -w "$TIMEOUT_SECONDS" -x "$TOR_HOST:$TOR_PORT" "$ONION_SERVICE_HOST" "$ONION_SERVICE_PORT"
  22. )}
  23. send_report() {(
  24. if [ ! -z "$VERBOSE" ]; then
  25. set -x
  26. fi
  27. echo -e "From: ${sender_address}\nSubject: $ONION_SERVICE_HOST:$ONION_SERVICE_PORT $1\n" \
  28. | sendmail "$RECIPIENT_ADDRESS"
  29. )}
  30. last_state=""
  31. while : ; do
  32. if ! nc -z -w "$TIMEOUT_SECONDS" "$TOR_HOST" "$TOR_PORT"; then
  33. echo "failed to connect to tor proxy at $TOR_HOST:$TOR_PORT"
  34. else
  35. if ping_onion_service; then
  36. if [ "$last_state" == "offline" ]; then
  37. echo went online
  38. send_report online
  39. fi
  40. last_state="online"
  41. else
  42. if [ "$last_state" == "online" ]; then
  43. echo went offline
  44. send_report offline
  45. fi
  46. last_state="offline"
  47. fi
  48. fi
  49. # flush queue
  50. dma -q1
  51. sleep "$SLEEP_DURATION"
  52. done