monitor.sh 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 "$MAIL_TO" ]; then
  13. echo 'missing $MAIL_TO'
  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. sendmail -t <<EOF
  28. From: $sender_address
  29. To: $MAIL_TO
  30. Subject: $ONION_SERVICE_HOST:$ONION_SERVICE_PORT $1
  31. EOF
  32. )}
  33. last_state=""
  34. while : ; do
  35. if ! nc -z -w "$TIMEOUT_SECONDS" "$TOR_HOST" "$TOR_PORT"; then
  36. echo "failed to connect to tor proxy at $TOR_HOST:$TOR_PORT"
  37. else
  38. if ping_onion_service; then
  39. if [ "$last_state" == "offline" ]; then
  40. echo went online
  41. send_report online
  42. fi
  43. last_state="online"
  44. else
  45. if [ "$last_state" == "online" ]; then
  46. echo went offline
  47. send_report offline
  48. fi
  49. last_state="offline"
  50. fi
  51. fi
  52. # flush queue
  53. dma -q1
  54. sleep "$SLEEP_DURATION"
  55. done