icinga2-checks/check_systemd_service.sh

114 lines
3.0 KiB
Bash
Raw Normal View History

#!/bin/bash
function usage {
echo "Usage:
2024-03-29 10:25:16 -06:00
-u [UNIT name] (required)
-t Service is triggered by a timer or is a oneshot service. Is allowed to be inactive
-d A service is allowed to not be enabled"
}
UNIT_NAME=""
IS_TIMER=false
IS_ALLOWED_DISABLED=false
# Parse command line arguments
2024-03-29 10:29:10 -06:00
while getopts "u:td" opt; do
case ${opt} in
u )
UNIT_NAME=$OPTARG
;;
t )
IS_TIMER=true
;;
2024-03-29 10:23:24 -06:00
d )
IS_ALLOWED_DISABLED=true
;;
\? )
2024-03-29 10:23:24 -06:00
usage
exit 1
;;
: )
echo "Invalid option: $OPTARG requires an argument" 1>&2
exit 1
;;
esac
done
shift $((OPTIND -1))
if [ -z "$UNIT_NAME" ]; then
2024-03-29 10:25:16 -06:00
echo "Missing unit name."
usage
exit -1
fi
# Check if the unit exists
if ! systemctl list-units --full -all 2>/dev/null | grep -Pq "\b$UNIT_NAME\b"; then
echo "CRITICAL - $UNIT_NAME does not exist"
exit 2
fi
# Check if the unit is enabled
enabled=$(systemctl is-enabled "$UNIT_NAME")
2024-03-29 10:17:55 -06:00
if [ "$enabled" != "enabled" ] && [ "$IS_ALLOWED_DISABLED" = false ]; then
echo "CRITICAL - $UNIT_NAME is not enabled"
exit 2
fi
# Check if the unit is active
active=$(systemctl is-active "$UNIT_NAME")
substate=$(systemctl show "$UNIT_NAME" --property=SubState --value)
exit_code=$(systemctl show "$UNIT_NAME" --property=ExecMainStatus --value)
if [ "$active" = "activating" ]; then
echo "WARNING - $UNIT_NAME is activating"
exit 1
elif [ "$active" != "active" ] && [ "$IS_TIMER" = false ]; then
echo "CRITICAL - $UNIT_NAME is not active"
exit 2
elif [ "$IS_TIMER" = true ] && [ "$active" = "inactive" ] && [ "$exit_code" != "0" ]; then
echo "CRITICAL - $UNIT_NAME is inactive with non-zero exit code"
exit 2
fi
# Get service start time
start_time=$(systemctl show "$UNIT_NAME" --property=ExecMainStartTimestamp --value)
end_time=$(systemctl show "$UNIT_NAME" --property=ExecMainExitTimestamp --value)
current_time=$(date +%s)
2024-03-30 20:52:53 -06:00
start_time_diff_perf=""
end_time_diff_perf=""
2024-03-30 20:16:18 -06:00
start_time_str=""
if [ -n "$start_time" ] && [ "$start_time" != "n/a" ] && [ -z "$start_time" ]; then
2024-03-30 20:16:18 -06:00
start_time_diff=$((current_time - $(date -d "$start_time" +%s)))
start_time_diff_perf="| uptime=${start_time_diff}s"
start_time_str=" Started at $start_time."
2024-03-30 20:16:18 -06:00
fi
end_time_str=""
if [ -n "$end_time" ] && [ "$end_time" != "n/a" ] && [ -z "$end_time" ]; then
2024-03-30 20:16:18 -06:00
end_time_diff=$((current_time - $(date -d "$end_time" +%s)))
end_time_diff_perf="| downtime=${end_time_diff}s"
end_time_str=" Exited at $end_time."
2024-03-30 20:16:18 -06:00
fi
# =============
# Build output
if [ "$IS_TIMER" = true ] && { [ "$substate" = "exited" ] || [ "$active" = "inactive" ]; }; then
status="exited (returned $exit_code)"
else
status="active"
fi
if [ "$enabled" = "enabled" ] || [ "$IS_ALLOWED_DISABLED" != true ]; then
status+=" and enabled"
fi
if [ "$IS_TIMER" = true ] && { [ "$substate" = "exited" ] || [ "$active" = "inactive" ]; }; then
echo "OK - $UNIT_NAME is $status.$end_time_str $end_time_diff_perf"
else
echo "OK - $UNIT_NAME is $status.$start_time_str $start_time_diff_perf"
fi
exit 0