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

Merge branch 'add-systemdunit' into 'master'

check_systemdunit add -r REGEX

See merge request !170
parents 22ee0f49 2b92af61
No related branches found
No related tags found
1 merge request!170check_systemdunit add -r REGEX
...@@ -5,12 +5,13 @@ ...@@ -5,12 +5,13 @@
# #
# ------------------------------------------------------------------------------- # -------------------------------------------------------------------------------
# 2023-09-05 v1.0 <axel.hahn@unibe.ch> # 2023-09-05 v1.0 <axel.hahn@unibe.ch>
# 2020-09-08 v1.1 <axel.hahn@unibe.ch> add params -s, -l # 2020-09-06 v1.1 <axel.hahn@unibe.ch> add params -s, -l
# 2020-09-06 v1.2 <axel.hahn@unibe.ch> add param -r to use a regex
# ================================================================================ # ================================================================================
. $( dirname $0 )/inc_pluginfunctions . $( dirname $0 )/inc_pluginfunctions
export self_APPVERSION=1.1 export self_APPVERSION=1.2
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# FUNCTIONS # FUNCTIONS
...@@ -28,13 +29,18 @@ The status is unknown if the command systemctl is not found. ...@@ -28,13 +29,18 @@ 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.
SYNTAX: SYNTAX:
$_self [-h|-l|-s] UNIT $_self [-h|-l|-s|-r] UNIT
OPTIONS: OPTIONS:
-h this help -h this help
-l list all units -l list all units
-s list service units -s list service units
-r handle UNIT as a regex and search for a single unit. A uniq regex
is needed to match a single unit. The initial idea was to match a
servie that has different service names on differen os eg.
- apache2 vs httpd
- mysld vs mariadb
UNIT Name of a unit - see output of 'systemctl' UNIT Name of a unit - see output of 'systemctl'
...@@ -47,9 +53,18 @@ EXAMPLES: ...@@ -47,9 +53,18 @@ EXAMPLES:
$_self nginx.service $_self nginx.service
show status of nginx webservice show status of nginx webservice
$_self -r "(apache2|httpd)\.service"
Detect name of Apache httpd service by given regex and show status
of the found service name
EOF EOF
} }
# get the list of units
function _getUnits(){
systemctl --no-legend --no-pager
}
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# MAIN # MAIN
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
...@@ -67,8 +82,8 @@ ph.require "systemctl" ...@@ -67,8 +82,8 @@ ph.require "systemctl"
if ph.hasParamoption "l" "$@" ; then if ph.hasParamoption "l" "$@" ; then
echo "List of all systemd units:" echo "List of all systemd units:"
echo echo
_list=$( systemctl --no-legend --no-pager ) _list=$( _getUnits )
for mytype in $( awk '{ print $1 }' <<< "$_list" | grep '\.' | rev| cut -f 1 -d '.' | rev | grep -v '[^a-z]' | sort -u ) for mytype in $( awk '{ print $1 }' <<< "$_list" | rev| cut -f 1 -d '.' | rev | grep -v '[^a-z]' | sort -u )
do do
echo "---------- $mytype" echo "---------- $mytype"
grep "\.${mytype}" <<< "$_list" grep "\.${mytype}" <<< "$_list"
...@@ -85,8 +100,31 @@ if ph.hasParamoption "s" "$@" ; then ...@@ -85,8 +100,31 @@ if ph.hasParamoption "s" "$@" ; then
exit 0 exit 0
fi fi
# --- find a service by regex
if ph.hasParamoption "r" "$@" ; then
_regex="${2}"
_list=$( _getUnits | awk '{ print $1 }' | grep -v '[^a-z\.\-]' | sort )
_unit=$( grep -E "$_regex" <<< "$_list" )
if [ -z "$_unit" ]; then
ph.setStatus critical
ph.status "REGEX '$_regex' does not match any existing unit"
ph.exit
fi
if [ $( wc -l <<< "$_unit" ) != "1" ]; then
ph.setStatus critical
ph.status "REGEX '$_regex' matches on multiple units on this system:"
echo "$_unit" | nl
echo "You need to use a uniq regex."
ph.exit
fi
else
_unit="${1}"
fi
# --- check given unit # --- check given unit
_unit="${1}"
_status=$( systemctl --no-pager -l status "${_unit}" 2>&1 ) _status=$( systemctl --no-pager -l status "${_unit}" 2>&1 )
if ! grep "Active: active (running) " <<< "${_status}" >/dev/null; then if ! grep "Active: active (running) " <<< "${_status}" >/dev/null; then
......
...@@ -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.1 v1.2
(c) Institute for Medical Education - University of Bern (c) Institute for Medical Education - University of Bern
Licence: GNU GPL 3 Licence: GNU GPL 3
...@@ -30,13 +30,18 @@ The status is unknown if the command systemctl is not found. ...@@ -30,13 +30,18 @@ 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.
SYNTAX: SYNTAX:
check_systemdunit [-h|-l|-s] UNIT check_systemdunit [-h|-l|-s|-r] UNIT
OPTIONS: OPTIONS:
-h this help -h this help
-l list all units -l list all units
-s list service units -s list service units
-r handle UNIT as a regex and search for a single unit. A uniq regex
is needed to match a single unit. The initial idea was to match a
servie that has different service names on differen os eg.
- apache2 vs httpd
- mysld vs mariadb
UNIT Name of a unit - see output of 'systemctl' UNIT Name of a unit - see output of 'systemctl'
...@@ -49,6 +54,10 @@ EXAMPLES: ...@@ -49,6 +54,10 @@ EXAMPLES:
check_systemdunit nginx.service check_systemdunit nginx.service
show status of nginx webservice show status of nginx webservice
check_systemdunit -r "(apache2|httpd)\.service"
Detect name of Apache httpd service by given regex and show status
of the found service name
``` ```
## Examples ## Examples
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment