diff --git a/check_systemdunit b/check_systemdunit new file mode 100755 index 0000000000000000000000000000000000000000..514471f67619d5a53822d7b65c94289056a616e4 --- /dev/null +++ b/check_systemdunit @@ -0,0 +1,77 @@ +#!/bin/bash +# ================================================================================ +# +# CHECK A SINGLE SYSTEMD SERVICE +# +# (1) +# shows overview of important services - edit check_systemdservices.cfg to define +# them +# +# (2) +# resturns +# - OK if all systemd servises are running +# - UNKNOWN if systemctl command is not available +# - ERROR if any systemd service is not running +# +# ------------------------------------------------------------------------------- +# 2023-09-05 v01.0 <axel.hahn@unibe.ch> +# ================================================================================ + +. $( dirname $0 )/inc_pluginfunctions + +export self_APPVERSION=1.0 + +# ---------------------------------------------------------------------- +# 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] UNIT + +OPTIONS: + -h this help + UNIT Name of a unit - see output of 'systemctl' + +EXAMPLES: + + $_self mysql.service + show status of service mysql + +EOF +} + +# ---------------------------------------------------------------------- +# MAIN +# ---------------------------------------------------------------------- + +ph.hasParamoption "h" "$@"; bOptHelp=$? + +if [ $bOptHelp -eq 0 -o $# -eq 0 ]; then + showHelp + exit 0 +fi + +ph.require "systemctl" + +_service="${1}" +_status=$( systemctl --no-pager -l status "${_service}" 2>&1 ) + +if ! grep "Active: active (running) " <<< "${_status}" >/dev/null; then + ph.setStatus critical +fi +ph.status "${_status}" + +ph.exit + +# ---------------------------------------------------------------------- diff --git a/docs/20_Checks/_index.md b/docs/20_Checks/_index.md index c874e5f350c8cc2786e171dfc99fb9894abee22f..134d2b73d377dd1dc1001d1974c46c5cfa3d5599 100644 --- a/docs/20_Checks/_index.md +++ b/docs/20_Checks/_index.md @@ -49,6 +49,7 @@ There is one include script used by all checks: * check_ssl * check_ssl_certs * check_systemdservices +* [check_systemdunit](check_systemdunit.md) * check_timesync * check_uptime * hello diff --git a/docs/20_Checks/check_systemdunit.md b/docs/20_Checks/check_systemdunit.md new file mode 100644 index 0000000000000000000000000000000000000000..a73a107b9e3c717e77530d2ba4772de3e7973b1b --- /dev/null +++ b/docs/20_Checks/check_systemdunit.md @@ -0,0 +1,69 @@ +# Check Systemd unit + +## Introduction + +This check shows the status of a systemd unit. + +A unit is everything listed by systemctl command - services, timers, targets, ... + +## Requirements + +* `systemctl` binary + +## Syntax + +```txt +______________________________________________________________________ + +CHECK_SYSTEMDUNIT +v1.0 + +(c) Institute for Medical Education - University of Bern +Licence: GNU GPL 3 + +https://os-docs.iml.unibe.ch/icinga-checks/Checks/check_systemdunit.html +______________________________________________________________________ + +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: + check_systemdunit [-h] UNIT + +OPTIONS: + -h this help + UNIT Name of a unit - see output of 'systemctl' + +EXAMPLES: + + check_systemdunit mysql.service + show status of service mysql + +``` + +## Examples + +``$ ./check_systemdunit nginx`` returns + +```txt +OK: ● nginx.service - A high performance web server and a reverse proxy server + Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: disabled) + Active: active (running) since Tue 2023-09-05 08:04:45 CEST; 8h ago + Process: 577 ExecStart=/usr/bin/nginx -g pid /run/nginx.pid; error_log stderr; (code=exited, status=0/SUCCESS) + Main PID: 612 (nginx) + Tasks: 2 (limit: 18881) + Memory: 3.0M + CPU: 38ms + CGroup: /system.slice/nginx.service + ├─612 "nginx: master process /usr/bin/nginx -g pid /run/nginx.pid; error_log stderr;" + └─613 "nginx: worker process" +... +``` + +If a service does not exist: ``./check_systemdunit justadummy`` returns + +```txt +CRITICAL: Unit justadummy.service could not be found. +``` diff --git a/inc_pluginfunctions b/inc_pluginfunctions index aabe5d83d22535aca7d37ff5a6b2e258e5158e0e..3740c11bd49efa373bcdc0eb3ad91dc53bfc1360 100644 --- a/inc_pluginfunctions +++ b/inc_pluginfunctions @@ -39,6 +39,7 @@ # 2023-06-22 v1.11 <axel.hahn@unibe.ch> fix ph.toUnit with float values; shell fixes # 2023-08-24 v1.12 <axel.hahn@unibe.ch> toUnit got 3rd param for count of digits after "." # 2023-08-30 v1.13 <axel.hahn@unibe.ch> reverse return code in ph.hasParamoption to unix like return codes: 0=true; <>0 = false +# 2023-09-05 v1.14 <axel.hahn@unibe.ch> ph.require - show error below status line # ====================================================================== @@ -62,10 +63,11 @@ function ph.abort(){ # check required binaries in the path # param(s) string name of binary to check with "which" command function ph.require(){ - which $* >/dev/null - if [ $? -ne 0 ]; then + local _out; + if ! _out=$( which $* 2>&1 ); then ph.setStatus "unknown" ph.status "$0 requires the following tools to run: $*" + echo "$_out" ph.exit fi }