system-helpers/usr/local/bin/systemd_check

121 lines
2.7 KiB
Text
Raw Permalink Normal View History

2019-01-25 17:28:29 +00:00
#!/usr/bin/env bash
# usage
usage() {
SYSTEMD_CHECK_USAGE=$(cat <<EOF
Usage: systemd_check.sh
2019-01-25 17:28:29 +00:00
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.
2019-01-25 17:28:29 +00:00
EOF
)
echo "$SYSTEMD_CHECK_USAGE";
}
set -e;
# check for config file
2022-07-10 16:29:44 +00:00
apply_config() {
local config=$1;
if [[ ! -f "$config" ]]; then
echo "No config file specified";
echo "";
usage;
exit 1;
fi
set -a;
# shellcheck disable=SC1090
2022-07-10 16:29:44 +00:00
source "$config";
set +a;
}
2019-01-25 18:02:33 +00:00
source_config() {
local configFallback=$1;
local configGlobalFallback=$2;
2022-07-10 16:29:44 +00:00
if [[ -f "$configFallback" ]]; then
apply_config "$configFallback";
2022-07-10 16:33:00 +00:00
return;
2022-07-10 16:29:44 +00:00
fi
if [[ -f "$configGlobalFallback" ]]; then
apply_config "$configGlobalFallback";
2022-07-10 16:33:00 +00:00
return;
2022-07-10 16:29:44 +00:00
fi
2019-01-25 18:02:33 +00:00
}
check_required() {
type hostname &> /dev/null || { echo "Requiring 'hostname' 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; }
}
SYSTEMD_CHECK_LIST_FILE="$HOME/.systemd_check.list"
SYSTEMD_CHECK_GLOBAL_LIST_FILE="/etc/systemd_check.list"
SYSTEMD_CHECK_SERVICES=()
source_config "$HOME/.systemd_check.conf" "/etc/systemd_check.conf"
check_required
SYSTEMD_CHECK_HOSTNAME=$(hostname)
SYSTEMD_CHECK_USER=$(whoami)
2022-07-10 16:29:44 +00:00
listFileToUse=""
if [[ -f "$SYSTEMD_CHECK_LIST_FILE" ]]; then
listFileToUse="$SYSTEMD_CHECK_LIST_FILE"
else
listFileToUse="$SYSTEMD_CHECK_GLOBAL_LIST_FILE"
fi
2019-01-25 17:28:29 +00:00
2022-07-10 16:29:44 +00:00
if [[ ! -f $listFileToUse ]]; then
echo "$listFileToUse doesn't exist. Add a file which holds one systemd service or timer per line.";
2019-01-25 18:02:33 +00:00
echo "";
usage;
2019-01-25 17:28:29 +00:00
exit 1;
fi
2022-07-10 16:29:44 +00:00
echo "Scanning $listFileToUse for services...";
2019-01-25 17:28:29 +00:00
while IFS='' read -r line || [[ -n "$line" ]]; do
SYSTEMD_CHECK_SERVICES+=("$line")
echo "-> Found $line";
2022-07-10 16:29:44 +00:00
done < "$listFileToUse"
2019-01-25 17:28:29 +00:00
# get status for a service
function checkService() {
local SERVICE=$1;
local MESSAGE="$SERVICE is not running for user $SYSTEMD_CHECK_USER";
2019-01-25 18:59:38 +00:00
local ROOT_COMMAND="systemctl is-active --quiet $SERVICE";
local USER_COMMAND="systemctl --user is-active --quiet $SERVICE";
2019-01-25 19:00:45 +00:00
local RESULT=0;
2019-01-25 17:28:29 +00:00
if [ "$(id -u)" != "0" ]; then
# shellcheck disable=SC2091
2019-01-25 19:17:48 +00:00
if ! $($USER_COMMAND); then
2019-01-25 19:00:45 +00:00
RESULT=1;
2019-01-25 18:52:59 +00:00
fi
2019-01-25 18:43:00 +00:00
else
# shellcheck disable=SC2091
2019-01-25 19:06:57 +00:00
if ! $($ROOT_COMMAND) && ! $($USER_COMMAND); then
2019-01-25 19:00:45 +00:00
RESULT=1;
2019-01-25 18:43:00 +00:00
fi
2019-01-25 17:28:29 +00:00
fi
2019-01-25 18:52:59 +00:00
if [ "$RESULT" -gt 0 ]; then
2019-01-25 17:28:29 +00:00
local SUBJECT="[systemd $SYSTEMD_CHECK_HOSTNAME for $SYSTEMD_CHECK_USER] CRITICAL $SERVICE";
echo "-> $SUBJECT: $MESSAGE";
notifier "${SUBJECT}" "${MESSAGE}";
2019-01-25 17:28:29 +00:00
fi
return;
}
# execute check for defined SYSTEMD_CHECK_SERVICES
for service in "${SYSTEMD_CHECK_SERVICES[@]}"; do
echo "Checking $service...";
checkService "$service"
done