eibgrad

tomato-ovpn-client-watchdog.sh

Apr 10th, 2021 (edited)
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.82 KB | None | 0 0
  1. #!/bin/sh
  2. #DEBUG=; set -x # uncomment/comment to enable/disable debug mode
  3.  
  4. #          name: tomato-ovpn-client-watchdog.sh
  5. #       version: 3.1.0, 13-oct-2024, by eibgrad
  6. #       purpose: (re)start failed/stopped/unresponsive openvpn client(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 client(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 -- MPnU5WrK init
  14. #       or
  15. #         wget -qO - bit.ly/tomato-installer|tr -d '\r'|sh -s -- MPnU5WrK init
  16. #    5. (optional) use vi editor to modify options:
  17. #         vi /jffs/etc/config/tomato-ovpn-client-watchdog.init
  18. #    6. reboot
  19. (
  20. # ------------------------------ BEGIN OPTIONS ------------------------------- #
  21.  
  22. # participating openvpn client(s) [ '1 2 3'=only | '*'=all | ''=none ]
  23. PARTICIPANTS='*'
  24.  
  25. # time (in secs) between checks for failed/stopped/unresponsive openvpn client(s)
  26. CHECK_INTERVAL=60
  27.  
  28. # remote host used for ping checks
  29. PING_HOST='8.8.8.8'
  30.  
  31. # time (in secs) between failed ping attempts
  32. PING_INTERVAL=10
  33.  
  34. # number of consecutive failed ping attempts required for restart
  35. PING_NUMFAIL=3 # (3 recommended, 0 disables ping checks)
  36.  
  37. # uncomment/comment to enable/disable
  38. #   when enabled, verify PING_HOST is reachable via bridge!
  39. #SW_PING_TAP_VIA_BRIDGE=
  40.  
  41. # ------------------------------- END OPTIONS -------------------------------- #
  42.  
  43. # ---------------------- DO NOT CHANGE BELOW THIS LINE ----------------------- #
  44.  
  45. LOCK="/tmp/$(basename $0).lock"
  46.  
  47. # function ping_check( client-num )
  48. ping_check() {
  49.     [ $PING_NUMFAIL -gt 0 ] || return 0
  50.  
  51.     local i=1
  52.     local conf="/tmp/etc/openvpn/client${1}/config.ovpn"
  53.     local vpn_if="$(awk '/^ *dev /{v=$2};END{print v}' $conf 2>/dev/null)"
  54.  
  55.     [ "$vpn_if" ] || { echo "error: line ${LINENO}: vpn_if=NULL"; return 0; }
  56.  
  57.     # we can't ping a bridged tunnel assigned to a bridge (e.g., br0)
  58.     if [ "${vpn_if:0:3}" == 'tap' ]; then
  59.         local br_if="$(echo /sys/class/net/*/brif/$vpn_if | cut -d/ -f5)"
  60.  
  61.         if [ "$br_if" != '*' ]; then
  62.             if [ ${SW_PING_TAP_VIA_BRIDGE+x} ]; then
  63.                 # ping bridge assignment instead of underlying tunnel
  64.                 vpn_if="$br_if"
  65.             else
  66.                 # skip ping checks
  67.                 return 0
  68.             fi
  69.         fi
  70.     fi
  71.  
  72.     # tip: ping multiple times to minimize risk of reporting false negative
  73.     while :; do
  74.         ping -qc1 -W3 -I $vpn_if $PING_HOST &>/dev/null && return 0
  75.         [ $((i++)) -ge $PING_NUMFAIL ] && break || sleep $PING_INTERVAL
  76.     done
  77.  
  78.     # fall-through == failure
  79.     return 1
  80. }
  81.  
  82. # reject additional instances
  83. mkdir $LOCK &>/dev/null || exit 0
  84.  
  85. # catch unexpected exit and cleanup
  86. trap "rmdir $LOCK; exit 0" SIGHUP SIGINT SIGTERM
  87.  
  88. # wait for *reliable* internet connection
  89. until ping -qc1 -W3 8.8.8.8 &>/dev/null; do sleep 10; done
  90.  
  91. while sleep $CHECK_INTERVAL; do
  92.     # process all "enable on start" openvpn clients
  93.     for i in $(nvram get vpn_client_eas | tr ',' ' '); do
  94.         # confirm openvpn client is participant
  95.         echo "$PARTICIPANTS" | grep -q "[*$i]" || continue
  96.  
  97.         # check for failed/stopped/unresponsive openvpn client
  98.         pidof vpnclient${i} &>/dev/null && ping_check $i && continue
  99.  
  100.         # confirm openvpn client is still enabled
  101.         $(nvram get vpn_client_eas | grep -q $i) || continue
  102.  
  103.         # fall-through == failure; (re)start openvpn client
  104.         service vpnclient${i} restart >/dev/null && sleep 5
  105.         echo "info: openvpn client #$i (re)started @ $(date)"
  106.     done
  107. done
  108.  
  109. ) 2>&1 | logger -t "$(basename $0 | grep -Eo '^.{0,23}')[$$]" &
Add Comment
Please, Sign In to add comment