71 lines
1.6 KiB
Bash
Executable File
71 lines
1.6 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
function usage {
|
|
echo "Usage:
|
|
-u [UNIT name]
|
|
-t Service is triggered by a timer and is allowed to be inactive"
|
|
}
|
|
|
|
UNIT_NAME=""
|
|
IS_TIMER=false
|
|
|
|
# Parse command line arguments
|
|
while getopts "u:t" opt; do
|
|
case ${opt} in
|
|
u )
|
|
UNIT_NAME=$OPTARG
|
|
;;
|
|
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
|
|
if [ -z "$UNIT_NAME" ]; then
|
|
usage
|
|
exit -1
|
|
fi
|
|
|
|
# Check if the unit is enabled
|
|
enabled=$(systemctl is-enabled "$UNIT_NAME")
|
|
if [ "$enabled" != "enabled" ]; 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)
|
|
|
|
if [ "$active" = "activating" ]; then
|
|
echo "WARNING - $UNIT_NAME is activating"
|
|
exit 1
|
|
elif [ "$active" != "active" ] || { [ "$substate" = "exited" ] && [ "$IS_TIMER" = false ]; }; then
|
|
echo "CRITICAL - $UNIT_NAME is not active"
|
|
exit 2
|
|
fi
|
|
|
|
# Get service start time
|
|
start_time=$(systemctl show "$UNIT_NAME" --property=ExecMainStartTimestamp --value)
|
|
current_time=$(date +%s)
|
|
time_diff=$((current_time - $(date -d "$start_time" +%s)))
|
|
uptime_str="Started at $start_time. | uptime=${time_diff}s"
|
|
|
|
|
|
if [ "$IS_TIMER" = true ] && [ "$substate" = "exited" ]; then
|
|
echo "OK - $UNIT_NAME is active (exited) and enabled. $uptime_str"
|
|
else
|
|
echo "OK - $UNIT_NAME is active and enabled. $uptime_str"
|
|
fi
|
|
exit 0
|