system-helpers/usr/local/bin/systemd_check
Alexander Schäferdiek 71977c9dca
All checks were successful
continuous-integration/drone/push Build is passing
Fail properly in all scripts when required commands are not available
2023-01-25 09:42:22 +01:00

133 lines
3.1 KiB
Bash
Executable file

#!/usr/bin/env bash
# usage
usage() {
SYSTEMD_CHECK_USAGE=$(cat <<EOF
Usage: systemd_check.sh [CONFIG_FILE (absolute path)]
Checks a set of systemd services, targets, mounts of timers, and notifies if their state is not up and running. Please see man systemd_check.
EOF
)
echo "$SYSTEMD_CHECK_USAGE";
}
set -e;
# vars and defaults
SYSTEMD_CHECK_LIST_FILE="$HOME/.systemd_check.list"
SYSTEMD_CHECK_GLOBAL_LIST_FILE="/etc/systemd_check.list"
SYSTEMD_CHECK_MAIL_ENABLED=fase
SYSTEMD_CHECK_MAIL_ADDRESS=""
SYSTEMD_CHECK_SERVICES=()
# check for config file
apply_config() {
local config=$1;
if [[ ! -f "$config" ]]; then
echo "No config file specified";
echo "";
usage;
exit 1;
fi
set -a;
# shellcheck disable=SC1090
source "$config";
set +a;
}
source_config() {
local config=$1;
local configFallback=$2;
local configGlobalFallback=$3;
if [[ -f "$config" ]]; then
apply_config "$config";
return;
fi
if [[ -f "$configFallback" ]]; then
apply_config "$configFallback";
return;
fi
if [[ -f "$configGlobalFallback" ]]; then
apply_config "$configGlobalFallback";
return;
fi
}
check_required() {
type hostname &> /dev/null || { echo "Requiring 'hostname' but it's not installed"; exit 1; }
type mailx &> /dev/null || { echo "Requiring 'mailx' but it's not installed"; exit 1; }
type whoami &> /dev/null || { echo "Requiring 'whoami' but it's not installed"; exit 1; }
type systemctl &> /dev/null || { echo "Requiring 'systemctl' but it's not installed"; exit 1; }
}
source_config "$1" "$HOME/.systemd_check.conf" "/etc/systemd_check.conf"
check_required
SYSTEMD_CHECK_HOSTNAME=$(hostname)
SYSTEMD_CHECK_USER=$(whoami)
listFileToUse=""
if [[ -f "$SYSTEMD_CHECK_LIST_FILE" ]]; then
listFileToUse="$SYSTEMD_CHECK_LIST_FILE"
else
listFileToUse="$SYSTEMD_CHECK_GLOBAL_LIST_FILE"
fi
if [[ ! -f $listFileToUse ]]; then
echo "$listFileToUse doesn't exist. Add a file which holds one systemd service or timer per line.";
echo "";
usage;
exit 1;
fi
echo "Scanning $listFileToUse for services...";
while IFS='' read -r line || [[ -n "$line" ]]; do
SYSTEMD_CHECK_SERVICES+=("$line")
echo "-> Found $line";
done < "$listFileToUse"
# get status for a service
function checkService() {
local SERVICE=$1;
local MESSAGE="$SERVICE is not running for user $SYSTEMD_CHECK_USER";
local ROOT_COMMAND="systemctl is-active --quiet $SERVICE";
local USER_COMMAND="systemctl --user is-active --quiet $SERVICE";
local RESULT=0;
if [ "$(id -u)" != "0" ]; then
# shellcheck disable=SC2091
if ! $($USER_COMMAND); then
RESULT=1;
fi
else
# shellcheck disable=SC2091
if ! $($ROOT_COMMAND) && ! $($USER_COMMAND); then
RESULT=1;
fi
fi
if [ "$RESULT" -gt 0 ]; then
local SUBJECT="[systemd $SYSTEMD_CHECK_HOSTNAME for $SYSTEMD_CHECK_USER] CRITICAL $SERVICE";
echo "-> $SUBJECT: $MESSAGE";
if [ "$SYSTEMD_CHECK_MAIL_ENABLED" = true ]; then
echo "$MESSAGE"|mailx -Ssendwait -s "$SUBJECT" "$SYSTEMD_CHECK_MAIL_ADDRESS";
fi
fi
return;
}
# execute check for defined SYSTEMD_CHECK_SERVICES
for service in "${SYSTEMD_CHECK_SERVICES[@]}"; do
echo "Checking $service...";
checkService "$service"
done