Wireguard Tunnel Monitoring & Auto-Restart with Shoutrr Notifications
Automate Wireguard Tunnel Monitoring, Restart, and Receive Shoutrr Notifications.
I use this script which pairs a BASH script running as a cronjob with Shoutrr to monitor and attempt to auto-correct Wireguard tunnel faults. The script uses Shoutrr to notify me via Discord if the tunnel went down, if the tunnel restarted, or if the tunnel failed to restart.
Install go - find the latest version here: https://go.dev/dl/
wget https://go.dev/dl/go1.21.4.linux-amd64.tar.gz && rm -rf /usr/local/go && tar -C /usr/local -xzf go1.21.4.linux-amd64.tar.gz && export PATH=$PATH:/usr/local/go/bin
Install Shoutrr using go
GOBIN=/usr/local/bin/ go install github.com/containrrr/shoutrrr/shoutrrr@latest
Create a new shell file and paste in the following script. I store my scripts at /root/scripts/.
This script will need tailored to your environment. You'll need to set a Shoutrr service URL. See Shoutrr docs: https://containrrr.dev/shoutrrr/v0.8/services/discord/. You'll also need to set an IP address for 'testip' to an IP that will be pingable on the other end of the tunnel to monitor tunnel state.
mkdir -p /root/scripts
cd /root/scripts
nano wg-check.sh
Add a Crontab for the script which triggers every minute or change to the interval that works best for your environment.
See https://crontab.guru/ for configuring crontabs quickly.
* * * * * /root/scripts/wg-check.sh
Give the monitoring script a test by faulting the Wireguard tunnel on either end - bring the tunnel down. The script should restart the tunnel automatically and send appropriate alerts.