Skip to content

Instantly share code, notes, and snippets.

@wolever
Created October 23, 2012 00:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolever/3935850 to your computer and use it in GitHub Desktop.
Save wolever/3935850 to your computer and use it in GitHub Desktop.
OpenVPN watchdog which will restart the OpenVPN daemon if it decides to go away
#!/bin/bash
IFS="`printf "\n\t"`"
set -eu
cd "`dirname "$0"`"
outfile="/dev/null"
if [[ "${1-}" == "-v" ]]; then
outfile="/dev/stdout"
verbose=true
shift
fi
if [[ -z "${1-}" || "${1-}" =~ "^-" ]]; then
echo "usage: $0 [-v] HOST"
echo "Exits with a non-zero status if HOST is down"
exit 1
fi
ping -W5 -c3 "$1" &> "$outfile" && true
down="$?"
if [[ ! -z "${verbose-}" ]]; then
if [[ "$down" -ne "0" ]]; then
msg="down"
else
msg="up"
fi
echo "$1 is $msg"
fi
exit "$down"
#!/bin/bash
IFS="`printf "\n\t"`"
set -eu
cd "`dirname "$0"`"
chkhost="$(which chkhost 2>/dev/null || echo ./chkhost)"
server="${1-}"
if [[ -z "$server" || "$server" =~ "^-" ]]; then
echo "usage: $0 SERVER_INTERNAL_IP"
echo "example: $0 10.2.0.1"
echo "most useful when added to a crontab:"
echo " */5 * * * * debian_openvpn_watchdog 10.2.0.1 > /dev/null"
exit 2
fi
echo "Checking $server..."
if ! "$chkhost" -v "$server"; then
echo "restarting openvpn..."
/etc/init.d/openvpn restart
fi
@shazow
Copy link

shazow commented Oct 23, 2012

supervisord? :D

@brunorey
Copy link

Hey,
I have to test connectivity in a network where ping is disabled. I made a small modification to your chkhost script to test if a port is reachable (with nc), I guess it can be helpful for someone:

#!/bin/bash

IFS="`printf "\n\t"`"
set -eu
cd "`dirname "$0"`"

outfile="/dev/null"

if [[ "${1-}" == "-v" ]]; then
    outfile="/dev/stdout"
    verbose=true
    shift
fi

if [[ -z "${1-}" || "${1-}" =~ "^-" ]]; then
    echo "usage: $0 [-v] HOST PORT"
    echo "Exits with a non-zero status if PORT of HOST is reachable"
    exit 1
fi

nc -zv "$1" "$2" &> "$outfile" && true
down="$?"

if [[ ! -z "${verbose-}" ]]; then
    if [[ "$down" -ne "0" ]]; then
        msg="down"
    else
        msg="up"
    fi
    echo "$1 is $msg"
fi

exit "$down"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment