From 2330f1551e338ad6aaafd3bd60eaed605b6c0563 Mon Sep 17 00:00:00 2001 From: Salt Date: Wed, 18 Aug 2021 18:44:27 -0500 Subject: [PATCH] Add check_systemd_unit --- check_systemd_unit | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100755 check_systemd_unit diff --git a/check_systemd_unit b/check_systemd_unit new file mode 100755 index 0000000..c0e88f3 --- /dev/null +++ b/check_systemd_unit @@ -0,0 +1,52 @@ +#! /bin/bash +# +# check_systemd_unit.sh +# Copyright (C) 2021 Vintage Salt +# +# Distributed under terms of the MIT license. +# + +# Usage: Set the first argument to be a systemd unit. The script will respond with the following: +# The unit is running OK +# The unit is dead + +service="$1" +# If the service is null, be critical +if [ -z "$service" ]; then + echo "UNIT CRITICAL: Service is undefined" + exit 1 +# If systemctl is not present, be critical +elif ! command -v systemctl > /dev/null 2>&1; then + echo "UNIT CRITICAL: systemctl missing from PATH" + exit 1 +# If the service is failed, be critical +elif systemctl is-failed --quiet "$service" > /dev/null 2>&1; then + echo "UNIT CRITICAL: $service is failed" + exit 2 +# Otherwise, see what its state is +else + systemctl status "$service" > /dev/null 2>&1 + case $? in + 0) + echo "UNIT OK: $service is running" + exit 0 + ;; + 1|2) + echo "UNIT CRITICAL: $service is dead" + exit 3 + ;; + 3) + # In this case, the unit is not running. Whether this is an issue depends on the type of service + if systemctl cat backup | grep -ie '^Type=' | grep -ie 'oneshot'; then + echo "UNIT OK: $service is not running" + else + echo "UNIT CRITICAL: $service is not running" + fi + exit 0 + ;; + 4) + echo "UNIT CRITICAL: $service is not present on this machine" + exit 50 + ;; + esac +fi