Select Git revision
check_systemdunit
Hahn Axel (hahn) authored
check_systemdunit 2.35 KiB
#!/bin/bash
# ================================================================================
#
# CHECK A SINGLE SYSTEMD SERVICE
#
# -------------------------------------------------------------------------------
# 2023-09-05 v1.0 <axel.hahn@unibe.ch>
# 2020-09-08 v1.1 <axel.hahn@unibe.ch> add params -s, -l
# ================================================================================
. $( dirname $0 )/inc_pluginfunctions
export self_APPVERSION=1.1
# ----------------------------------------------------------------------
# FUNCTIONS
# ----------------------------------------------------------------------
# show help text
function showHelp(){
local _self; _self=$(basename $0)
cat <<EOF
$( ph.showImlHelpHeader )
Check a unit using systemctl status.
The status is unknown if the command systemctl is not found.
The status is critical if the service does not exist or is not running.
SYNTAX:
$_self [-h|-l|-s] UNIT
OPTIONS:
-h this help
-l list all units
-s list service units
UNIT Name of a unit - see output of 'systemctl'
EXAMPLES:
$_self -s
list all existing services. For a unit check you need to add the name
in the 1st column.
$_self nginx.service
show status of nginx webservice
EOF
}
# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------
ph.hasParamoption "h" "$@"; bOptHelp=$?
if [ $bOptHelp -eq 0 -o $# -eq 0 ]; then
showHelp
exit 0
fi
ph.require "systemctl"
# --- list all units
if ph.hasParamoption "l" "$@" ; then
echo "List of all systemd units:"
echo
_list=$( systemctl --no-legend --no-pager )
for mytype in $( awk '{ print $1 }' <<< "$_list" | grep '\.' | rev| cut -f 1 -d '.' | rev | grep -v '[^a-z]' | sort -u )
do
echo "---------- $mytype"
grep "\.${mytype}" <<< "$_list"
echo
done
exit 0
fi
# --- list service units
if ph.hasParamoption "s" "$@" ; then
echo "List of service units:"
echo
systemctl --no-legend --no-pager --type service
exit 0
fi
# --- check given unit
_unit="${1}"
_status=$( systemctl --no-pager -l status "${_unit}" 2>&1 )
if ! grep "Active: active (running) " <<< "${_status}" >/dev/null; then
ph.setStatus critical
fi
ph.status "${_status}"
ph.exit
# ----------------------------------------------------------------------