2024-03-28 20:50:10 -06:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
function usage {
|
|
|
|
echo "Usage:
|
|
|
|
-u [UNIT name]
|
|
|
|
-t Service is triggered by a timer and is allowed to be inactive"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Initialize variables
|
|
|
|
UNIT=""
|
|
|
|
IS_TIMER=false
|
|
|
|
|
|
|
|
# Parse command line arguments
|
|
|
|
while getopts "u:t" opt; do
|
|
|
|
case ${opt} in
|
|
|
|
u )
|
|
|
|
UNIT=$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" ]; then
|
|
|
|
usage
|
|
|
|
exit -1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Check if the unit is enabled
|
|
|
|
enabled=$(systemctl is-enabled "$UNIT")
|
|
|
|
if [ "$enabled" != "enabled" ]; then
|
|
|
|
echo "CRITICAL - $UNIT is not enabled"
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Check if the unit is active
|
|
|
|
active=$(systemctl is-active "$UNIT")
|
|
|
|
substate=$(systemctl show "$UNIT" --property=SubState --value)
|
|
|
|
|
2024-03-28 20:57:40 -06:00
|
|
|
if [ "$active" = "activating" ]; then
|
|
|
|
echo "WARNING - $UNIT is activating"
|
|
|
|
exit 1
|
|
|
|
elif [ "$active" != "active" ] || { [ "$substate" = "exited" ] && [ "$IS_TIMER" = false ]; }; then
|
2024-03-28 20:50:10 -06:00
|
|
|
echo "CRITICAL - $UNIT is not active"
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$IS_TIMER" = true ] && [ "$substate" = "exited" ]; then
|
|
|
|
echo "OK - $UNIT is active (exited) and enabled"
|
|
|
|
else
|
|
|
|
echo "OK - $UNIT is active and enabled"
|
|
|
|
fi
|
|
|
|
exit 0
|