Skip to content

Instantly share code, notes, and snippets.

@chtzvt
Created November 1, 2021 15:58
Show Gist options
  • Save chtzvt/55fc3f3165b88099cef5aa74baeb3e97 to your computer and use it in GitHub Desktop.
Save chtzvt/55fc3f3165b88099cef5aa74baeb3e97 to your computer and use it in GitHub Desktop.
WireGuard un-borker
#!/bin/bash
STATUS_FILE='/tmp/.wg_fixer_state_unhealthy'
REBOOT_MINIMUM_UPTIME='300' # 5 minutes
SYSTEM_UPTIME=`cat /proc/uptime | cut -d ' ' -f1 | cut -d '.' -f1`
systemctl status wg-quick@wg0 2>&1 >/dev/null
WG_QUICK_STATUS=$?
WG_PEERS=`wg | wc -l`
if [ $WG_PEERS -ne 0 ] && [ $WG_QUICK_STATUS -eq 0 ]
then
echo "[`date`] WireGuard status appears healthy! Cleaning any status files..."
rm -f $STATUS_FILE
exit 0
fi
if [ $WG_PEERS -eq 0 ] || [ $WG_QUICK_STATUS -eq 1 ]
then
echo "[`date`] Something is wrong with WireGuard :(" 1>&2
if [ -f $STATUS_FILE ] && [ $SYSTEM_UPTIME -ge $REBOOT_MINIMUM_UPTIME ]
then
echo "[`date`] WireGuard service unhealthy state has not recovered, and exceeded uptime threshold. Rebooting..." 1>&2
rm -f $STATUS_FILE
/sbin/shutdown -r +0
exit 1
fi
echo "[`date`] Restarting wg-quick systemd units..." 1>&2
systemctl daemon-reload
systemctl stop wg-quick@wg0
systemctl start wg-quick@wg0
if [ $? -eq 0 ]
then
echo "[`date`] Note: wg-quick unit returned 0, service may have recovered ..?" 1>&2
fi
touch $STATUS_FILE
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment