Skip to content
Snippets Groups Projects
Commit 330a238f authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

handle units with multiple instrances

parent 8cd3f7b0
Branches
No related tags found
1 merge request!199handle units with multiple instrances
...@@ -10,11 +10,12 @@ ...@@ -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.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-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-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 . $( dirname $0 )/inc_pluginfunctions
export self_APPVERSION=1.5 export self_APPVERSION=1.6
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# FUNCTIONS # FUNCTIONS
...@@ -28,8 +29,11 @@ $( ph.showImlHelpHeader ) ...@@ -28,8 +29,11 @@ $( ph.showImlHelpHeader )
Check a unit using systemctl status. Check a unit using systemctl status.
The status is unknown if the command systemctl is not found. 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 "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: SYNTAX:
$_self [-h|-l|-s|-r] UNIT $_self [-h|-l|-s|-r] UNIT
...@@ -60,6 +64,14 @@ EXAMPLES: ...@@ -60,6 +64,14 @@ EXAMPLES:
Detect name of Apache httpd service by given regex and show status Detect name of Apache httpd service by given regex and show status
of the found service name 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 EOF
} }
...@@ -131,12 +143,30 @@ fi ...@@ -131,12 +143,30 @@ fi
_data=$( systemctl --no-pager -l status "${_unit}" 2>&1 | tr '|' ':' ) _data=$( systemctl --no-pager -l status "${_unit}" 2>&1 | tr '|' ':' )
_status=$( echo "$_data" | head -1 | sed "s#^[^a-zA-Z]*##g" ) _status=$( echo "$_data" | head -1 | sed "s#^[^a-zA-Z]*##g" )
if ! grep "Active: active (running) " <<< "${_data}" >/dev/null; then _out=$( echo "$_data" | sed -n "2,\$p")
ph.setStatus critical
# 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 fi
ph.status "${_status}" ph.status "${_status}"
echo "$_data" | sed -n "2,\$p" | tr -d '└├' | tr '─' '-' echo "$_out" | tr -d '└├' | tr '─' '-'
ph.exit ph.exit
......
...@@ -8,7 +8,7 @@ A unit is everything listed by systemctl command - services, timers, targets, .. ...@@ -8,7 +8,7 @@ A unit is everything listed by systemctl command - services, timers, targets, ..
## Requirements ## Requirements
* `systemctl` binary * `systemctl` binary (which is on each systemd based linux system)
## Syntax ## Syntax
...@@ -16,7 +16,7 @@ A unit is everything listed by systemctl command - services, timers, targets, .. ...@@ -16,7 +16,7 @@ A unit is everything listed by systemctl command - services, timers, targets, ..
______________________________________________________________________ ______________________________________________________________________
CHECK_SYSTEMDUNIT CHECK_SYSTEMDUNIT
v1.5 v1.6
(c) Institute for Medical Education - University of Bern (c) Institute for Medical Education - University of Bern
Licence: GNU GPL 3 Licence: GNU GPL 3
...@@ -26,8 +26,11 @@ ______________________________________________________________________ ...@@ -26,8 +26,11 @@ ______________________________________________________________________
Check a unit using systemctl status. Check a unit using systemctl status.
The status is unknown if the command systemctl is not found. 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 "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: SYNTAX:
check_systemdunit [-h|-l|-s|-r] UNIT check_systemdunit [-h|-l|-s|-r] UNIT
...@@ -58,6 +61,14 @@ EXAMPLES: ...@@ -58,6 +61,14 @@ EXAMPLES:
Detect name of Apache httpd service by given regex and show status Detect name of Apache httpd service by given regex and show status
of the found service name 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 ## Examples
...@@ -82,6 +93,10 @@ List of service units: ...@@ -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 ### Check a service
To check a single service you need to add the unit name in the 1st column. 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 ...@@ -109,10 +124,6 @@ If a service does not exist: ``./check_systemdunit justadummy`` returns
CRITICAL: Unit justadummy.service could not be found. 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 ### Regex examples
Here are a few examples for services with regex: Here are a few examples for services with regex:
...@@ -125,3 +136,25 @@ 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'`` * ``check_systemdunit -r 'ssh[d]{0,1}.service'``
* Placeholders for version numbers * Placeholders for version numbers
* ``check_systemdunit -r 'php.*fpm\.service'`` * ``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.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment