From 082bc8a29df79ce4274ed2ce827e6e36125a74a2 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@iml.unibe.ch>
Date: Wed, 31 Aug 2022 15:35:25 +0200
Subject: [PATCH] add help; shellfix corrections

---
 check_apache_requests                   | 115 +++++++++++++++++++-----
 docs/20_Checks/check_apache_requests.md |  98 ++++++++++++++++++++
 2 files changed, 189 insertions(+), 24 deletions(-)
 create mode 100644 docs/20_Checks/check_apache_requests.md

diff --git a/check_apache_requests b/check_apache_requests
index 7b4de07..2cb4c38 100755
--- a/check_apache_requests
+++ b/check_apache_requests
@@ -9,9 +9,12 @@
 # 2020-04-09  v1.0  <axel.hahn@iml.unibe.ch>
 # 2021-03-11  v1.1  <axel.hahn@iml.unibe.ch>  more error checks for output data
 # 2021-11-18  v1.2  <axel.hahn@iml.unibe.ch>  add timeout and max tries; use localhost instead of FQDN
+# 2022-08-31  v1.3  <axel.hahn@iml.unibe.ch>  add help; shellfix corrections
 # ======================================================================
 
-. `dirname $0`/inc_pluginfunctions
+. $(dirname $0)/inc_pluginfunctions
+self_APPNAME=$( basename $0 | tr [:lower:] [:upper:] )
+self_APPVERSION=1.4
 
 tmpfile=/tmp/check_apache_processes_1
 tmpfile2=/tmp/check_apache_processes_2
@@ -34,6 +37,83 @@ paramsWget="-T 5 -t 1 --no-check-certificate"
 #            |    tries = 1
 #            timeout in seconds
 
+# ----------------------------------------------------------------------
+# functions
+# ----------------------------------------------------------------------
+
+function showHelp(){
+cat <<EOF
+______________________________________________________________________
+
+$self_APPNAME 
+v$self_APPVERSION
+
+(c) Institute for Medical Education - University of Bern
+Licence: GNU GPL 3
+______________________________________________________________________
+
+Get Apache httpd server-status page $url.
+You get output with the server status and values for each slot type.
+
+The plugin sends performance data for
+  - each slot type
+  - active, idle, unused slots
+
+The response is UNKNOWN if url was not reached within 5 seconds or if
+the response does not contain a scoreboard.
+
+You get a WARNING if the count of used slots (excluding idle and unused 
+slots) is greater 75% of available slots.
+You get an CRITICAL response if it is greater 90% of available slots.
+
+Slot types of apache httpd:
+    _ Waiting for Connection, 
+    S Starting up, 
+    R Reading Request
+    W Sending Reply, 
+    K Keepalive (read), 
+    D DNS Lookup
+    C Closing connection, 
+    L Logging, 
+    G Gracefully finishing
+    I Idle cleanup of worker 
+    . Open slot with no current process
+
+
+SYNTAX:
+$(basename $0) [-w WARN_LIMIT] [-c CRITICAL_LIMIT]
+
+OPTIONS:
+
+    -w VALUE       warning level  (default: 75)
+    -c VALUE       critical level (default: 90)
+
+    -h or --help   show this help.
+
+PARAMETERS:
+
+    None.
+
+
+EXAMPLE:
+$(basename $0) -w 60 -c 80
+
+EOF
+}
+
+# ----------------------------------------------------------------------
+# MAIN
+# ----------------------------------------------------------------------
+
+# --- check param -h
+case "$1" in
+    "--help"|"-h")
+        showHelp
+        exit 0
+        ;;
+    *)
+esac
+
 # ----------------------------------------------------------------------
 # pre checks
 # ----------------------------------------------------------------------
@@ -47,17 +127,17 @@ ph.require wget
 wget $paramsWget -O $tmpfile $url 2>/dev/null
 if [ $? -ne 0 ]; then
     rm -f $tmpfile
-    ph.abort "UNKNOWN: request to url $url failed. `wget $paramsWget --no-check-certificate -O - -S $url 2>&1`"
+    ph.abort "UNKNOWN: request to url $url failed. $(wget $paramsWget -O - -S $url 2>&1)"
 fi
 
 # set default / override from command line params
-typeset -i iWarnLimit=`     ph.getValueWithParam 75 w "$@"`
-typeset -i iCriticalLimit=` ph.getValueWithParam 90 c "$@"`
+typeset -i iWarnLimit=$(     ph.getValueWithParam 75 w "$@")
+typeset -i iCriticalLimit=$( ph.getValueWithParam 90 c "$@")
 
 
 # --- extract scoreboard
-iStart=` grep -n '<pre>'  $tmpfile | cut -f 1 -d ':'`
-iEnd=`   grep -n '</pre>' $tmpfile | cut -f 1 -d ':'`
+iStart=$( grep -n '<pre>'  $tmpfile | cut -f 1 -d ':')
+iEnd=$(   grep -n '</pre>' $tmpfile | cut -f 1 -d ':')
 
 if [ $iStart -eq 0 -o $iEnd -eq 0 ]; then
     rm -f $tmpfile
@@ -66,24 +146,11 @@ fi
 
 sed -n "${iStart},${iEnd}p" $tmpfile | sed 's#<.*>##g' | tr -d "\n" >$tmpfile2
 
-# <p>Scoreboard Key:<br />
-# "<b><code>_</code></b>" Waiting for Connection, 
-# "<b><code>S</code></b>" Starting up, 
-# "<b><code>R</code></b>" Reading Request,<br />
-# "<b><code>W</code></b>" Sending Reply, 
-# "<b><code>K</code></b>" Keepalive (read), 
-# "<b><code>D</code></b>" DNS Lookup,<br />
-# "<b><code>C</code></b>" Closing connection, 
-# "<b><code>L</code></b>" Logging, 
-# "<b><code>G</code></b>" Gracefully finishing,<br /> 
-# "<b><code>I</code></b>" Idle cleanup of worker, 
-# "<b><code>.</code></b>" Open slot with no current process<br />
-
 
 # --- count slots in the scoreboard
 
 # total slots available
-iSlots=`cat $tmpfile2 | wc -m`
+iSlots=$(cat $tmpfile2 | wc -m)
 
 if [ $iSlots -eq 0 ]; then
     rm -f $tmpfile $tmpfile2
@@ -91,10 +158,10 @@ if [ $iSlots -eq 0 ]; then
 fi
 
 # running apache processes waiting for a request
-iIdle=iCount=`sed -e "s/[^_]//g" $tmpfile2 | wc -m`
+iIdle=iCount=$(sed -e "s/[^_]//g" $tmpfile2 | wc -m)
 
 # count of processes apache still can create
-iUnused=iCount=`sed -e "s/[^\.]//g" $tmpfile2 | wc -m`
+iUnused=iCount=$(sed -e "s/[^\.]//g" $tmpfile2 | wc -m)
 
 # count of actively used slots
 iActive=$iSlots-$iIdle-$iUnused
@@ -114,8 +181,8 @@ ph.perfadd "apache-active"    "${iActive}" "" "" 0 $iSlots
 echo "Slots:"
 for mychar in S R W K D C L G I _ .
 do
-  iCount=`sed -e "s/[^${mychar}]//g" $tmpfile2 | wc -m`
-  label=`echo apache-${mychar} | tr [:upper:] [:lower:] | sed "s#_#idle#" | sed "s#\.#unused#"`
+  iCount=$(sed -e "s/[^${mychar}]//g" $tmpfile2 | wc -m)
+  label=$(echo apache-${mychar} | tr [:upper:] [:lower:] | sed "s#_#idle#" | sed "s#\.#unused#")
 
   echo "  - ${mychar}: ${iCount}"
   ph.perfadd "${label}"    "${iCount}" "" "" 0 $iSlots
diff --git a/docs/20_Checks/check_apache_requests.md b/docs/20_Checks/check_apache_requests.md
new file mode 100644
index 0000000..e62b5bd
--- /dev/null
+++ b/docs/20_Checks/check_apache_requests.md
@@ -0,0 +1,98 @@
+# Check_apache_requests
+
+## Introduction
+
+______________________________________________________________________
+
+CHECK_APACHE_REQUESTS 
+v1.4
+
+(c) Institute for Medical Education - University of Bern
+Licence: GNU GPL 3
+______________________________________________________________________
+
+Get Apache httpd server-status page localhost/server-status.
+You get output with the server status and values for each slot type.
+
+The plugin sends performance data for
+  - each slot type
+  - active, idle, unused slots
+
+The response is UNKNOWN if url was not reached within 5 seconds or if
+the response does not contain a scoreboard.
+
+You get a WARNING if the count of used slots (excluding idle and unused 
+slots) is greater 75% of available slots.
+You get an CRITICAL response if it is greater 90% of available slots.
+
+Slot types of apache httpd:
+    `_` Waiting for Connection, 
+    `S` Starting up, 
+    `R` Reading Request
+    `W` Sending Reply, 
+    `K` Keepalive (read), 
+    `D` DNS Lookup
+    `C` Closing connection, 
+    `L` Logging, 
+    `G` Gracefully finishing
+    `I` Idle cleanup of worker 
+    `.` Open slot with no current process
+
+## Syntax
+
+```txt
+check_apache_requests [-w WARN_LIMIT] [-c CRITICAL_LIMIT]
+
+OPTIONS:
+
+    -w VALUE       warning level  (default: 75)
+    -c VALUE       critical level (default: 90)
+
+    -h or --help   show this help.
+
+PARAMETERS:
+
+    None.
+```
+
+## Examples
+
+`check_apache_requests`
+
+`check_apache_requests -w 60 -c 80`
+
+Output example:
+
+```txt
+OK: Apache: 10000 slots ...  active: 1  wait: 524  unused: 9475 (0 % usage)
+Server MPM: event
+Server Built: Apr  6 2022 14:54:37
+Current Time: Wednesday, 31-Aug-2022 15:09:31 CEST
+Restart Time: Monday, 11-Jul-2022 05:07:15 CEST
+Parent Server Config. Generation: 52
+Parent Server MPM Generation: 51
+Server uptime:  51 days 10 hours 2 minutes 16 seconds
+Server load: 0.83 0.83 0.82
+Total accesses: 3368214 - Total Traffic: 44.1 GB - Total Duration: 258025460
+CPU Usage: u650.56 s621.39 cu55050.6 cs32047.5 - 1.99% CPU load
+.758 requests/sec - 10.4 kB/second - 13.7 kB/request - 76.606 ms/request
+1 requests currently being processed, 524 idle workers
+
+Slots:
+  - S: 0
+  - R: 0
+  - W: 1
+  - K: 0
+  - D: 0
+  - C: 0
+  - L: 0
+  - G: 0
+  - I: 0
+  - _: 524
+  - .: 9475
+ |apache-active=1;;;0;10000 apache-s=0;;;0;10000 apache-r=0;;;0;10000 apache-w=1;;;0;10000 apache-k=0;;;0;10000 apache-d=0;;;0;10000 apache-c=0;;;0;10000 apache-l=0;;;0;10000 apache-g=0;;;0;10000 apache-i=0;;;0;10000 apache-idle=524;;;0;10000 apache-unused=9475;;;0;10000 
+ ```
+
+## Graph
+
+See <https://git-repo.iml.unibe.ch/iml-open-source/icinga-graphite-templates>
\ No newline at end of file
-- 
GitLab