2024-03-28 20:50:10 -06:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
function usage {
|
|
|
|
echo "Usage:
|
|
|
|
-u [UNIT name]
|
2024-03-28 22:40:46 -06:00
|
|
|
-t Service is triggered by a timer or is a oneshot service. Is allowed to be inactive"
|
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
|
|
|
|
|
|
|
|
# Parse command line arguments
|
|
|
|
while getopts "u:t" opt; do
|
|
|
|
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
|
|
|
|
;;
|
|
|
|
\? )
|
|
|
|
echo "Invalid option: $OPTARG" 1>&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
: )
|
|
|
|
echo "Invalid option: $OPTARG requires an argument" 1>&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $((OPTIND -1))
|
|
|
|
|
|
|
|
# Check if UNIT name is provided
|
2024-03-28 21:12:08 -06:00
|
|
|
if [ -z "$UNIT_NAME" ]; then
|
2024-03-28 20:50:10 -06:00
|
|
|
usage
|
|
|
|
exit -1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Check if the unit is enabled
|
2024-03-28 21:12:08 -06:00
|
|
|
enabled=$(systemctl is-enabled "$UNIT_NAME")
|
2024-03-28 20:50:10 -06:00
|
|
|
if [ "$enabled" != "enabled" ]; 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-28 22:40:46 -06:00
|
|
|
start_time_diff=$((current_time - $(date -d "$start_time" +%s)))
|
|
|
|
end_time_diff=$((current_time - $(date -d "$end_time" +%s)))
|
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
|
|
|
|
echo "OK - $UNIT_NAME is active and exited (returned $exit_code) and enabled. Exited at $end_time. | downtime=${end_time_diff}s"
|
2024-03-28 20:50:10 -06:00
|
|
|
else
|
2024-03-28 22:40:46 -06:00
|
|
|
echo "OK - $UNIT_NAME is active and enabled. Started at $start_time. | uptime=${start_time_diff}s"
|
2024-03-28 20:50:10 -06:00
|
|
|
fi
|
|
|
|
exit 0
|