system-helpers/usr/local/bin/systemd_check

110 lines
3 KiB
Text
Raw 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 [CONFIG_FILE (absolute path)]
Checks if specific systemd SYSTEMD_CHECK_SERVICES are running and optionally sends a mail.
If no CONFIG_FILE is given, HOME/.systemd_check.conf is used. This fallback option
has to exist or the script will exit.
Configuration can be done in any file and any pre-defined variable can be overwritten.
The following are at least required for the script to work:
- SYSTEMD_CHECK_LIST_FILE="HOME/.systemd_check.list" // path to file to list services to check, one per line
The following are optional or have reasonable defaults:
- SYSTEMD_CHECK_MAIL_ENABLED=false // send email (SYSTEMD_CHECK_MAIL_ADDRESS has to be set to true)? false|true
- SYSTEMD_CHECK_MAIL_ADDRESS="" // mail.rc has to be configured
You can copy this script to '/usr/local/bin' and use create a custom CONFIG_FILE as user. Examples can be found in '/usr/share/doc/<scriptname>'.
EOF
)
echo "$SYSTEMD_CHECK_USAGE";
}
set -e;
# vars and defaults
SYSTEMD_CHECK_LIST_FILE="$HOME/.systemd_check.list"
SYSTEMD_CHECK_MAIL_ENABLED=fase
SYSTEMD_CHECK_MAIL_ADDRESS=""
SYSTEMD_CHECK_HOSTNAME=$(hostname)
SYSTEMD_CHECK_USER=$(whoami)
SYSTEMD_CHECK_SERVICES=()
# check for config file
2019-01-25 18:02:33 +00:00
source_config() {
local config=$1;
local configFallback=$2;
if [[ ! -f "$config" ]]; then
if [[ ! -f "$configFallback" ]]; then
echo "No config file specified and could not find default in '$configFallback'!";
echo "";
usage;
exit 1;
else
config=$configFallback;
fi
fi
set -a;
source "$config";
set +a;
}
source_config "$1" "$HOME/.systemd_check.conf"
2019-01-25 17:28:29 +00:00
if [ ! -f $SYSTEMD_CHECK_LIST_FILE ]; then
echo "$SYSTEMD_CHECK_LIST_FILE 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
echo "Scanning $SYSTEMD_CHECK_LIST_FILE for services...";
while IFS='' read -r line || [[ -n "$line" ]]; do
SYSTEMD_CHECK_SERVICES+=("$line")
echo "-> Found $line";
done < "$SYSTEMD_CHECK_LIST_FILE"
# 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
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
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";
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