From 2b92af616a28c086960e68e12d7bbbc6be914413 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Wed, 6 Sep 2023 14:29:53 +0200
Subject: [PATCH] check_systemdunit add -r REGEX

---
 check_systemdunit                   | 50 +++++++++++++++++++++++++----
 docs/20_Checks/check_systemdunit.md | 13 ++++++--
 2 files changed, 55 insertions(+), 8 deletions(-)

diff --git a/check_systemdunit b/check_systemdunit
index 9820c3e..e04f6c3 100755
--- a/check_systemdunit
+++ b/check_systemdunit
@@ -5,12 +5,13 @@
 #
 # -------------------------------------------------------------------------------
 # 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
 
-export self_APPVERSION=1.1
+export self_APPVERSION=1.2
 
 # ----------------------------------------------------------------------
 # FUNCTIONS
@@ -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.
 
 SYNTAX:
-  $_self [-h|-l|-s] UNIT
+  $_self [-h|-l|-s|-r] UNIT
 
 OPTIONS:
 
   -h     this help
   -l     list all 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' 
 
@@ -47,9 +53,18 @@ EXAMPLES:
   $_self nginx.service
          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
 }
 
+# get the list of units
+function _getUnits(){
+  systemctl --no-legend --no-pager
+}
+
 # ----------------------------------------------------------------------
 # MAIN
 # ----------------------------------------------------------------------
@@ -67,8 +82,8 @@ ph.require "systemctl"
 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 )
+  _list=$( _getUnits )
+  for mytype in $( awk '{ print $1 }' <<< "$_list" | rev| cut -f 1 -d '.' | rev | grep -v '[^a-z]' | sort -u )
   do
     echo "---------- $mytype"
     grep "\.${mytype}" <<< "$_list"
@@ -85,8 +100,31 @@ if ph.hasParamoption "s" "$@" ; then
   exit 0
 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
-_unit="${1}"
+
 _status=$( systemctl --no-pager -l status "${_unit}" 2>&1 )
 
 if ! grep "Active: active (running) " <<< "${_status}" >/dev/null; then
diff --git a/docs/20_Checks/check_systemdunit.md b/docs/20_Checks/check_systemdunit.md
index e534d15..b0fce25 100644
--- a/docs/20_Checks/check_systemdunit.md
+++ b/docs/20_Checks/check_systemdunit.md
@@ -16,7 +16,7 @@ A unit is everything listed by systemctl command - services, timers, targets, ..
 ______________________________________________________________________
 
 CHECK_SYSTEMDUNIT
-v1.1
+v1.2
 
 (c) Institute for Medical Education - University of Bern
 Licence: GNU GPL 3
@@ -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.
 
 SYNTAX:
-  check_systemdunit [-h|-l|-s] UNIT
+  check_systemdunit [-h|-l|-s|-r] UNIT
 
 OPTIONS:
 
   -h     this help
   -l     list all 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' 
 
@@ -49,6 +54,10 @@ EXAMPLES:
   check_systemdunit nginx.service
          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
-- 
GitLab