From 5db23fb0e5a8be7a1abd0d1b9344350c4d6372c6 Mon Sep 17 00:00:00 2001 From: YuruC3 <98943911+YuruC3@@users.noreply.github.com> Date: Thu, 8 May 2025 00:00:48 +0200 Subject: [PATCH] Added newest edits. --- README.md | 30 ++++++++++++++++++++++++++++-- bck_watchdog.sh | 39 +++++++++++++++++++++++++++++++++++++++ watchdog.sh | 2 +- 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 bck_watchdog.sh diff --git a/README.md b/README.md index 53c366c..51669a3 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ A bash script working as a helthcheck for a VM. Takes in a single argument in form of VMID. +`./watchdog.sh ` + ## Requirements Copy/clone code into your directory @@ -11,9 +13,33 @@ Add exec permission on watchdog.sh `chmod u+x /path/to/watchdog.sh` -## Examples +## What's bck_watchdog +It's for a pretty specific usecase when you have HA opnsense running in 2 vm's. -`./watchdog.sh ` +If both VM's go down then this would start a third "emergency" VM running OPNSense. + +## Examples `./watchdog.sh 100` +### Crontab + +` +*/5 * * * * /bin/bash /path/to/watchdog.sh 100 +` + +This will check every 5 minutes if a VM us runninig. + +### Crontab with bck_watchdog + +``` +*/5 * * * * /bin/bash /path/to/watchdog.sh 100 + +*/2 * * * * /bin/bash /path/to/watchdog.sh 1003 + +*/2 * * * * /bin/bash /path/to/watchdog.sh 1004 +``` + +This will check the HA 2 nodes every 2 minutes and backup one every 5 minutes. + +where 1003 and 1004 are main HA nodes and 100 is the backup. \ No newline at end of file diff --git a/bck_watchdog.sh b/bck_watchdog.sh new file mode 100644 index 0000000..44f8692 --- /dev/null +++ b/bck_watchdog.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +ORIGINGW= +GW1= +GW2= + +# check if $ORIGINGW exists + +if [[ -z $ORIGINGW ]] +then + echo "Pass a VMID." + exit 1 +fi + +#------------------------------------------------ +# CONSTS +VMID="$1" +QMSTAT="$(/usr/sbin/qm status $ORIGINGW )" > /dev/null + +#------------------------------------------------ +# CODE + +# check if container exists +if echo $QMSTAT | grep "not exist" +then + printf "\nNo such VM!\n" + exit 1 +fi + + +#VMSTATE=$(echo "$QMSTAT" | awk '{print $2}') +#echo $VMSTATE + +if [[ $(/bin/bash /root/./vm_check.sh $GW1) == 1 ]] && [[ $(/bin/bash /root/./vm_check.sh $GW2) == 1 ]] +then + #/usr/sbin/qm start "$VMID" + echo "EMERGENCY START OF ORIGINAL GATEWAY" + exit 0 +fi diff --git a/watchdog.sh b/watchdog.sh index 15d7380..e2392df 100644 --- a/watchdog.sh +++ b/watchdog.sh @@ -39,7 +39,7 @@ case $VMSTATE in printf "\nStarting VM %s" "$VMID\n" #echo "" # To lazy to fix output spacing /usr/sbin/qm start "$VMID" - exit 0 + exit 1 ;;