2024-03-28 20:50:10 -06:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
function usage {
|
|
|
|
echo "Usage:
|
2024-03-29 10:25:16 -06:00
|
|
|
-u [UNIT name] (required)
|
2024-03-29 10:06:57 -06:00
|
|
|
-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"
|
2024-03-28 20:50:10 -06:00
|
|
|
}
|
|
|
|
|
2024-03-28 21:12:08 -06:00
|
|
|
UNIT_NAME=""
|
2024-03-28 20:50:10 -06:00
|
|
|
IS_TIMER=false
|
2024-03-29 10:06:57 -06:00
|
|
|
IS_ALLOWED_DISABLED=false
|
2024-03-28 20:50:10 -06:00
|
|
|
|
|
|
|
# Parse command line arguments
|
2024-03-29 10:29:10 -06:00
|
|
|
while getopts "u:td" opt; do
|
2024-03-28 20:50:10 -06:00
|
|
|
case ${opt} in
|
|
|
|
u )
|
2024-03-28 21:12:08 -06:00
|
|
|
UNIT_NAME=$OPTARG
|
2024-03-28 20:50:10 -06:00
|
|
|
;;
|
|
|
|
t )
|
|
|
|
IS_TIMER=true
|
|
|
|
;;
|
2024-03-29 10:23:24 -06:00
|
|
|
d )
|
2024-03-29 10:06:57 -06:00
|
|
|
IS_ALLOWED_DISABLED=true
|
|
|
|
;;
|
2024-03-28 20:50:10 -06:00
|
|
|
\? )
|
2024-03-29 10:23:24 -06:00
|
|
|
usage
|
2024-03-28 20:50:10 -06:00
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
: )
|
|
|
|
echo "Invalid option: $OPTARG requires an argument" 1>&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $((OPTIND -1))
|
|
|
|
|
2024-03-28 21:12:08 -06:00
|
|
|
if [ -z "$UNIT_NAME" ]; then
|
2024-03-29 10:25:16 -06:00
|
|
|
echo "Missing unit name."
|
|
|
|
usage
|
|
|
|
exit -1
|
2024-03-28 20:50:10 -06:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Check if the unit is enabled
|
2024-03-28 21:12:08 -06:00
|
|
|
enabled=$(systemctl is-enabled "$UNIT_NAME")
|
2024-03-29 10:17:55 -06:00
|
|
|
if [ "$enabled" != "enabled" ] && [ "$IS_ALLOWED_DISABLED" = false ]; then
|
2024-03-28 21:12:08 -06:00
|
|
|
echo "CRITICAL - $UNIT_NAME is not enabled"
|
2024-03-28 20:50:10 -06:00
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Check if the unit is active
|
2024-03-28 21:12:08 -06:00
|
|
|
active=$(systemctl is-active "$UNIT_NAME")
|
|
|
|
substate=$(systemctl show "$UNIT_NAME" --property=SubState --value)
|
2024-03-28 22:40:46 -06:00
|
|
|
exit_code=$(systemctl show "$UNIT_NAME" --property=ExecMainStatus --value)
|
2024-03-28 20:50:10 -06:00
|
|
|
|
2024-03-28 20:57:40 -06:00
|
|
|
if [ "$active" = "activating" ]; then
|
2024-03-28 21:12:08 -06:00
|
|
|
echo "WARNING - $UNIT_NAME is activating"
|
2024-03-28 20:57:40 -06:00
|
|
|
exit 1
|
2024-03-28 22:40:46 -06:00
|
|
|
elif [ "$active" != "active" ] && [ "$IS_TIMER" = false ]; then
|
2024-03-28 21:12:08 -06:00
|
|
|
echo "CRITICAL - $UNIT_NAME is not active"
|
2024-03-28 20:50:10 -06:00
|
|
|
exit 2
|
2024-03-28 22:40:46 -06:00
|
|
|
elif [ "$IS_TIMER" = true ] && [ "$active" = "inactive" ] && [ "$exit_code" != "0" ]; then
|
|
|
|
echo "CRITICAL - $UNIT_NAME is inactive with non-zero exit code"
|
|
|
|
exit 2
|
2024-03-28 20:50:10 -06:00
|
|
|
fi
|
|
|
|
|
2024-03-28 21:12:08 -06:00
|
|
|
# Get service start time
|
|
|
|
start_time=$(systemctl show "$UNIT_NAME" --property=ExecMainStartTimestamp --value)
|
2024-03-28 22:40:46 -06:00
|
|
|
end_time=$(systemctl show "$UNIT_NAME" --property=ExecMainExitTimestamp --value)
|
2024-03-28 21:12:08 -06:00
|
|
|
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
|
|
|
|
2024-03-30 20:51:43 -06:00
|
|
|
if [ -n "$start_time" ] && [ "$start_time" != "n/a" ]; 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"
|
|
|
|
fi
|
|
|
|
if [ -n "$end_time" ] && [ "$end_time" != "n/a" ]; then
|
|
|
|
end_time_diff=$((current_time - $(date -d "$end_time" +%s)))
|
|
|
|
end_time_diff_perf="| downtime=${end_time_diff}s"
|
|
|
|
fi
|
2024-03-28 21:12:08 -06:00
|
|
|
|
2024-03-28 22:40:46 -06:00
|
|
|
if [ "$IS_TIMER" = true ] && { [ "$substate" = "exited" ] || [ "$active" = "inactive" ]; }; then
|
2024-03-30 20:16:18 -06:00
|
|
|
echo "OK - $UNIT_NAME is active and exited (returned $exit_code) and enabled. Exited at $end_time. $end_time_diff_perf"
|
2024-03-28 20:50:10 -06:00
|
|
|
else
|
2024-03-30 20:16:18 -06:00
|
|
|
echo "OK - $UNIT_NAME is active and enabled. Started at $start_time. $start_time_diff_perf"
|
2024-03-28 20:50:10 -06:00
|
|
|
fi
|
|
|
|
exit 0
|