add extendable script to test hyrule's critical systemd services

This commit is contained in:
Emile Clark-Boman 2025-07-26 18:17:51 +10:00
parent e0174e669c
commit 9fd929ae7b

82
scripts/testvps Executable file
View file

@ -0,0 +1,82 @@
#!/usr/bin/env bash
# Requires: systemctl
set -u
function match {
[[ "$1" =~ $2 ]]
}
function padlen {
local MAX=0
for KEY in "$@"; do
local LEN=${#KEY}
((LEN > MAX)) && MAX=$LEN
done
echo $MAX
}
function pad {
local PAD="$1"
if [ -z "$PAD" ]; then
exit 1
fi
for ((i=0; i<PAD; i++)); do
printf " "
done
}
function fmt_print {
set +u
local SERVICE=$1
local LPAD=$2
local ACTIVE=$3
local ENABLED=$4
if [ "$ACTIVE" = "NOTFOUND" ]; then
ACTIVE="\e[1m\e[31m$ACTIVE\e[0m"
elif [ "$ACTIVE" = "inactive" ]; then
ACTIVE="\e[1m\e[31m$ACTIVE\e[0m"
else
ACTIVE="\e[32m$ACTIVE\e[0m"
fi
if [ "$ENABLED" = "enabled" ]; then
ENABLED="[\e[32m$ENABLED\e[0m]"
elif [ "$ENABLED" = "disabled" ]; then
ENABLED="[\e[1m\e[33m$ENABLED\e[0m]"
fi
set -u
echo -e "\e[35m[*]\e[0m $SERVICE:$LPAD $ACTIVE $ENABLED"
}
function fmt_test_service (
local SERVICE=$1
local LPAD_SZ=$2 # service name lpad size
local LPAD=$(pad $LPAD_SZ)
local EXISTS=0
local STAT=$(systemctl status "$SERVICE" 2>/dev/null)
if [ -z "$STAT" ]; then
fmt_print "$SERVICE" "$LPAD" "NOTFOUND"
exit 1
fi
local STAT_LOADED=$(head -n2 <<< "$STAT" | tail -n1)
# NOTE: "active " intentionally contains right padding
local ENABLED=$(match "$STAT_LOADED" "^\s*Loaded: loaded \(.*; enabled; .*\)" && echo "enabled" || echo "disabled")
local ACTIVE=$(match "$STAT" "\s*Active: active \(running\)" && echo "active " || echo "inactive")
fmt_print "$SERVICE" "$LPAD" "$ACTIVE" "$ENABLED"
)
function test_services {
local PAD_ALIGN=$(padlen $@)
# fmt_test_service
for SERVICE in "$@"; do
local LPAD=$((PAD_ALIGN - ${#SERVICE}))
fmt_test_service "$SERVICE" $LPAD
done
}
test_services nginx forgejo vaultwarden