diff --git a/check_systemdunit b/check_systemdunit index 5ccb9773aafb48fbf09acc4efc95386d361e7f45..c54b275cb87e07b297935aa24ec8b9a9b1e826c7 100755 --- a/check_systemdunit +++ b/check_systemdunit @@ -10,11 +10,12 @@ # 2020-09-18 v1.3 <axel.hahn@unibe.ch> fix: list units --all to show all stopped units # 2020-09-18 v1.4 <axel.hahn@unibe.ch> replace pipes in systemctl status output # 2020-10-20 v1.5 <axel.hahn@unibe.ch> remove special chars from systemd status +# 2020-10-23 v1.6 <axel.hahn@unibe.ch> handle units with multiple instrances # ================================================================================ . $( dirname $0 )/inc_pluginfunctions -export self_APPVERSION=1.5 +export self_APPVERSION=1.6 # ---------------------------------------------------------------------- # FUNCTIONS @@ -28,8 +29,11 @@ $( 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. +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. + +When checking a service with multiple instances you get status "warning" +if not all instances are active. SYNTAX: $_self [-h|-l|-s|-r] UNIT @@ -60,6 +64,14 @@ EXAMPLES: Detect name of Apache httpd service by given regex and show status of the found service name + $_self something@* + Check if all instances of a service "something@.service" are active. + If not all instances are running you get status "warning", if none + is running "critical". + + $_self something@2.service + Check instance 2 of service "something". + EOF } @@ -131,12 +143,30 @@ fi _data=$( systemctl --no-pager -l status "${_unit}" 2>&1 | tr '|' ':' ) _status=$( echo "$_data" | head -1 | sed "s#^[^a-zA-Z]*##g" ) -if ! grep "Active: active (running) " <<< "${_data}" >/dev/null; then - ph.setStatus critical +_out=$( echo "$_data" | sed -n "2,\$p") + +# get "running" instances +typeset -i _iActive; _iActive=$( grep -c "Active: active (running) " <<< "${_data}" ) + +if [ $_iActive -eq 0 ]; then + ph.setStatus critical +else + # when unit contains a @ char and * then check multiple instances of a service + if grep "@\*" <<< "$_unit" >/dev/null; then + typeset -i _iInstances; _iInstances=$( grep -c "Loaded: .*@" <<< "${_data}" ) + + _status="$_iActive of $_iInstances ${_unit} units are active" + _out="$_data" + + if [ $_iInstances -ne $_iActive ]; then + ph.setStatus warning + fi + fi + fi ph.status "${_status}" -echo "$_data" | sed -n "2,\$p" | tr -d '└├' | tr '─' '-' +echo "$_out" | tr -d '└├●' | tr '─' '-' ph.exit diff --git a/docs/20_Checks/check_systemdunit.md b/docs/20_Checks/check_systemdunit.md index 414b562549fe5b9d5e25a804c3f3e5b393d64970..884ccf92fa44f7f1fe5443fee82b15aee96a9c86 100644 --- a/docs/20_Checks/check_systemdunit.md +++ b/docs/20_Checks/check_systemdunit.md @@ -8,7 +8,7 @@ A unit is everything listed by systemctl command - services, timers, targets, .. ## Requirements -* `systemctl` binary +* `systemctl` binary (which is on each systemd based linux system) ## Syntax @@ -16,7 +16,7 @@ A unit is everything listed by systemctl command - services, timers, targets, .. ______________________________________________________________________ CHECK_SYSTEMDUNIT -v1.5 +v1.6 (c) Institute for Medical Education - University of Bern Licence: GNU GPL 3 @@ -26,8 +26,11 @@ ______________________________________________________________________ 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. +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. + +When checking a service with multiple instances you get status "warning" +if not all instances are active. SYNTAX: check_systemdunit [-h|-l|-s|-r] UNIT @@ -58,6 +61,14 @@ EXAMPLES: Detect name of Apache httpd service by given regex and show status of the found service name + check_systemdunit something@* + Check if all instances of a service "something@.service" are active. + If not all instances are running you get status "warning", if none + is running "critical". + + check_systemdunit something@2.service + Check instance 2 of service "something". + ``` ## Examples @@ -82,6 +93,10 @@ List of service units: ... ``` +### Other units + +With ``$ ./check_systemdunit -l`` you get a grouped list of all unit types. check_systemdunit handles all types - not only services. + ### Check a service To check a single service you need to add the unit name in the 1st column. @@ -109,10 +124,6 @@ If a service does not exist: ``./check_systemdunit justadummy`` returns CRITICAL: Unit justadummy.service could not be found. ``` -### Other units - -With ``$ ./check_systemdunit -l`` you get a grouped list of all unit types. check_systemdunit handles all types - not only services. - ### Regex examples Here are a few examples for services with regex: @@ -125,3 +136,25 @@ Here are a few examples for services with regex: * ``check_systemdunit -r 'ssh[d]{0,1}.service'`` * Placeholders for version numbers * ``check_systemdunit -r 'php.*fpm\.service'`` + +### Check a service with multiple instances + +Systemd services with multiple instances contain an @ char. +In the list of the systemctl command the @ char is followed by a number of the instance. + +#### All instances + +To check if all instances are running use ``@*`` at the end of the servicename (like you would do with ``systemctl status myservice@*``). + +The command ``check_systemdunit myservice@*`` will return a status line how many active and existing instanecs were found: + +```txt +OK: 4 of 4 myservice@* units are active +... +``` + +#### A single instance + +To check if all instances are running use ``@[number]`` at the end of the servicename. + +The command ``check_systemdunit myservice@2`` checks the 2nd instance. It is handled like single service check.