Files
Server-Scripts/startuphealthcheck.sh

135 lines
3.7 KiB
Bash

#!/bin/bash
#vars
NF="\e[0m"
FB="\e[1m"
CG="\e[38;5;40m"
CR="\e[38;5;9m"
CY="\033[38;5;226m"
HOST=$(hostnamectl --static)
PUSHOVERAPI="https://api.pushover.net"
APP_TOKEN="apipushoverapptoken"
USER_TOKEN="apipushoverusertoken"
#functions
guardband() {
if [ $? -ne 0 ]; then
echo -e "${CR}Health check - ${FB}FAIL${NF} ${CR}- Services start aborted.${NF}"
PushoverReturnBootError
exit 0
else
echo -e "${CG}Health check - ${FB}PASS${NF}"
fi
}
verifyZFS() {
if [[ $1 = "--delta-foxtrot" ]]; then
echo -e "${CY}Verify ZFS...${NF}"
if [ -f "/mnt/POOL1/.check" ]; then
echo -e "${CG}POOL1 - OK"
else
echo -e "${CR}POOL1 - FAIL"
return 1
fi
if [ -f "/mnt/POOL2/.check" ]; then
echo -e "${CG}POOL2 - OK"
else
echo -e "${CR}POOL2 - FAIL"
return 1
fi
if [ -f "/mnt/POOL3/.check" ]; then
echo -e "${CG}POOL3 - OK"
else
echo -e "${CR}POOL3 - FAIL"
return 1
fi
if [ -f "/mnt/POOL4/.check" ]; then
echo -e "${CG}POOL4 - OK"
else
echo -e "${CR}POOL4 - FAIL"
return 1
fi
fi
if [[ $1 = "--nasperon" ]]; then
if [ -f "/mnt/POOL1/.check" ]; then
echo -e "${CG}POOL1 - OK"
else
echo -e "${CR}POOL1 - FAIL"
return 1
fi
if [ -f "/mnt/POOL2/.check" ]; then
echo -e "${CG}POOL2 - OK"
else
echo -e "${CR}POOL2 - FAIL"
return 1
fi
if [ -f "/mnt/POOL3/.check" ]; then
echo -e "${CG}POOL3 - OK"
else
echo -e "${CR}POOL3 - FAIL"
return 1
fi
fi
}
PushoverReturnBootOK() {
if ! wget --spider "$PUSHOVERAPI" 2>/dev/null; then
echo -e "${CR}Pushover API unreachable - Message will retry.${NF}"
sleep 600
TITLE="$HOST Boot health check PASS"
APP_TOKEN="apipushoverapptoken"
USER_TOKEN="apipushoverusertoken"
MESSAGE="$HOST has successfully passed all healthchecks after bootup."
curl -s -F "token=$APP_TOKEN" -F "user=$USER_TOKEN" -F "title=$TITLE" -F "message=$MESSAGE" https://api.pushover.net/1/messages.json
if [ $? -ne 0 ]; then
echo -e "${CR}Pushover API still unreachable - Message not sent.${NF}"
fi
else
TITLE="$HOST Boot health check PASS"
MESSAGE="$HOST has successfully passed all healthchecks after bootup."
curl -s -F "token=$APP_TOKEN" -F "user=$USER_TOKEN" -F "title=$TITLE" -F "message=$MESSAGE" https://api.pushover.net/1/messages.json
fi
}
PushoverReturnBootError() {
if ! wget --spider "$PUSHOVERAPI" 2>/dev/null; then
echo -e "${CR}Pushover API unreachable - Message will retry.${NF}"
systemctl stop syncthing@root
systemctl stop cronicle
systemctl stop docker.socket
systemctl stop ampinstmgr
if [ "$HOST" = "Delta" ]
then
systemctl stop jellyfin #delta only
fi
systemctl stop rsyncd
sleep 600
TITLE="$HOST Boot health check FAIL"
APP_TOKEN="apipushoverapptoken"
USER_TOKEN="apipushoverusertoken"
MESSAGE="$HOST has failed a healthcheck after bootup - Services will not be initiated. Manual Intervention Required."
curl -s -F "token=$APP_TOKEN" -F "user=$USER_TOKEN" -F "title=$TITLE" -F "message=$MESSAGE" https://api.pushover.net/1/messages.json
if [ $? -ne 0 ]; then
echo -e "${CR}Pushover API still unreachable - Message not sent.${NF}"
fi
else
TITLE="$HOST Boot health check FAIL"
APP_TOKEN="apipushoverapptoken"
USER_TOKEN="apipushoverusertoken"
MESSAGE="$HOST has failed a healthcheck after bootup - Services will not be initiated. Manual Intervention Required."
curl -s -F "token=$APP_TOKEN" -F "user=$USER_TOKEN" -F "title=$TITLE" -F "message=$MESSAGE" https://api.pushover.net/1/messages.json
systemctl stop syncthing@root
systemctl stop cronicle
systemctl stop docker.socket
systemctl stop ampinstmgr
if [ "$HOST" = "Delta" ]
then
systemctl stop jellyfin #delta only
fi
systemctl stop rsyncd
fi
}
sleep 60
verifyZFS
guardband
PushoverReturnBootOK