eibgrad

tomato-ovpn-server-watchdog.sh

Oct 7th, 2024 (edited)
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.13 KB | None | 0 0
  1. #!/bin/sh
  2. #DEBUG=; set -x # uncomment/comment to enable/disable debug mode
  3.  
  4. #          name: tomato-ovpn-server-watchdog.sh
  5. #       version: 1.0.0, 07-oct-2024, by eibgrad
  6. #       purpose: (re)start failed/stopped openvpn server(s)
  7. #   script type: init (autostart)
  8. #  installation:
  9. #    1. enable jffs (administration->jffs)
  10. #    2. enable syslog (status->logs->logging configuration->syslog)
  11. #    3. enable 'enable on start' option for openvpn server(s) to be monitored
  12. #    4. use shell (telnet/ssh) to execute one of the following commands:
  13. #         curl -kLs bit.ly/tomato-installer|tr -d '\r'|sh -s -- tbQ1yAR0 init
  14. #       or
  15. #         wget -qO - bit.ly/tomato-installer|tr -d '\r'|sh -s -- tbQ1yAR0 init
  16. #    5. (optional) use vi editor to modify options:
  17. #         vi /jffs/etc/config/tomato-ovpn-server-watchdog.init
  18. #    6. reboot
  19. (
  20. # ------------------------------ BEGIN OPTIONS ------------------------------- #
  21.  
  22. # participating openvpn server(s) [ '1 2'=only | '*'=all | ''=none ]
  23. PARTICIPANTS='*'
  24.  
  25. # time (in secs) between checks for failed/stopped openvpn server(s)
  26. CHECK_INTERVAL=60
  27.  
  28. # ------------------------------- END OPTIONS -------------------------------- #
  29.  
  30. # ---------------------- DO NOT CHANGE BELOW THIS LINE ----------------------- #
  31.  
  32. LOCK="/tmp/$(basename $0).lock"
  33.  
  34. # reject additional instances
  35. mkdir $LOCK &>/dev/null || exit 0
  36.  
  37. # catch unexpected exit and cleanup
  38. trap "rmdir $LOCK; exit 0" SIGHUP SIGINT SIGTERM
  39.  
  40. # wait for *reliable* internet connection
  41. until ping -qc1 -W3 8.8.8.8 &>/dev/null; do sleep 10; done
  42.  
  43. while sleep $CHECK_INTERVAL; do
  44.     # process all "enable on start" openvpn servers
  45.     for i in $(nvram get vpn_server_eas | tr ',' ' '); do
  46.         # confirm openvpn server is participant
  47.         echo "$PARTICIPANTS" | grep -q "[*$i]" || continue
  48.  
  49.         pidof vpnserver${i} &>/dev/null && continue
  50.  
  51.         # fall-through means failure; restart the openvpn server
  52.         service vpnserver${i} restart >/dev/null && sleep 5
  53.         echo "info: openvpn server #$i (re)started @ $(date)"
  54.     done
  55. done
  56.  
  57. ) 2>&1 | logger -t "$(basename $0 | grep -Eo '^.{0,23}')[$$]" &
Add Comment
Please, Sign In to add comment