From 28f81925c9906f59c86cf5a95d999ef0f7eb0329 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Thu, 24 Aug 2023 14:55:34 +0200
Subject: [PATCH] update help; do not use tmp file anymore

---
 check_php-fpm-status | 69 +++++++++++++++-----------------------------
 1 file changed, 23 insertions(+), 46 deletions(-)

diff --git a/check_php-fpm-status b/check_php-fpm-status
index eb92cc7..37ca114 100755
--- a/check_php-fpm-status
+++ b/check_php-fpm-status
@@ -13,12 +13,15 @@
 # 2022-04-01  v0.6  <axel.hahn@iml.unibe.ch>  use wget default params; shell fixes
 # 2022-05-09  v0.7  <axel.hahn@iml.unibe.ch>  use wget default params
 # 2022-07-08  v0.8  <axel.hahn@iml.unibe.ch>  eliminate division by 0
-# 2022-10-21  v1.3  <axel.hahn@unibe.ch>      remove grep: warning: stray \ before white space
+# 2022-10-21  v1.0  <axel.hahn@unibe.ch>      remove grep: warning: stray \ before white space
+# 2023-08-24  v1.1  <axel.hahn@unibe.ch>      update help; do not use tmp file anymore
 # ======================================================================
 
-. $(dirname $0)/inc_pluginfunctions
+. $( dirname $0 )/inc_pluginfunctions
 
-tmpfile=/tmp/check_fpm_processes_1
+export self_APPVERSION=1.1
+
+outFpmStatus=
 defaulturl=localhost/status
 
 sDeltaunit="min"
@@ -29,16 +32,16 @@ sDeltaunit="min"
 
 # get service data from json output
 function _getServicedata(){
-    cat $tmpfile | jq | grep '^  "' | grep -v "\[" | cut -f 1 -d ","
+    echo "$outFpmStatus" | jq | grep '^  "' | grep -v "\[" | cut -f 1 -d ","
 }
 
 function _getWorkerStates(){
-    cat $tmpfile | jq | grep '"state": ' | cut -f 2 -d ":" | cut -f 1 -d "," | sort -u
+    echo "$outFpmStatus" | jq | grep '"state": ' | cut -f 2 -d ":" | cut -f 1 -d "," | sort -u
 }
 
 
 function _getWorkerOfState(){
-    cat $tmpfile | jq -c ".processes[] | select(.state == \"$1\" )"
+    echo "$outFpmStatus" | jq -c ".processes[] | select(.state == \"$1\" )"
 }
 
 # get a value from fpm status
@@ -61,7 +64,6 @@ function _getWorkerOfState(){
 #
 # param  string  variable (part before ":")
 function _getvalue(){
-        # grep "^$1:" $tmpfile | cut -d ":" -f 2 | awk '{ print $1 }'
         _getServicedata | grep "^  \"$1\":" | cut -d ":" -f 2 | awk '{ print $1 }'
 }
 
@@ -69,14 +71,7 @@ function _getvalue(){
 function showHelp(){
     local _self=$(basename $0)
 cat <<EOF
-______________________________________________________________________
-
-CHECK_PHP-FPM-Status
-Get counters from PHP-FPM status output for active/ idle processes.
-
-(c) Institute for Medical Education - University of Bern
-Licence: GNU GPL 3
-______________________________________________________________________
+$( ph.showImlHelpHeader )
 
 The check fetches several counters from php-fm-status page.
 
@@ -141,11 +136,14 @@ url=$( ph.getValueWithParam $defaulturl u "$@" )
 
 paramsWget="-T 5 -t 1 --no-check-certificate"
 
-# --- get /server-status page
-wget $paramsWget -O $tmpfile "$url?full&json" 2>/dev/null
-if [ $? -ne 0 ]; then
-   rm -f $tmpfile
-   ph.abort "UNKNOWN: request to url $url failed. $(wget $paramsWget -O - -S $url)"
+# --- get fpm status page
+if ! outFpmStatus=$( wget $paramsWget -O - "$url?full&json" 2>/dev/null ); then
+    
+   ph.abort "UNKNOWN: request to url '$url' failed.
+
+   DEBUG: repeating the request showing header and STDERR
+   $( wget $paramsWget -O - -S $url 2>&1 )"
+   
 fi
 
 # ----------------------------------------------------------------------
@@ -180,32 +178,13 @@ typeset -i iWorkerIdle=$(    _getWorkerOfState "Idle"            | wc -l )
 # ----------------------------------------------------------------------
 # set status
 # ----------------------------------------------------------------------
-# damn, count of slots is in the config only - not in status output
-# iUsage=$iActive*100/$iSlots
-# ph.setStatusByLimit $iUsage $iWarnLimit $iCriticalLimit
-if [ $iQueue -gt 0 ]; then
-    ph.setStatus warning
-fi
 
-# remove switch to warning level if just one slow request was detected
-# if [ $iSlow -gt 0 ]; then
-#     ph.setStatus warning
-# fi
+# something queued and waiting for a free worker?
+test $iQueue -gt 0 &&  ph.setStatus warning
 
-# 5601 - remove warning for this limit (it is a flag and won't remove)
-# if [ $iMaxChilds -gt 0 ]; then
-#     # ph.setStatus critical
-#     ph.setStatus warning
-# fi
-
-if [ $iWorkerIdle -eq 0 ]; then
-    ph.setStatus warning
-fi
+# free worker down to 0?
+test $iWorkerIdle -eq 0 && ph.setStatus warning
 
-# seems not to be useful
-# if [ $iWorkerReading -eq 0 ]; then
-#     ph.setStatus warning
-# fi
 
 # ----------------------------------------------------------------------
 # output
@@ -242,7 +221,7 @@ fi
 #     echo
 # fi
 if [ $iQueue -gt 0 ]; then
-    echo $hint
+    echo "$hint"
     echo "WARNING: $iQueue queued requests were found. Maximum of queued items is $iMaxQueue (since last start of fpm service)."
     echo
 fi
@@ -275,8 +254,6 @@ ph.perfadd "php-fpm-idle"      "${iWorkerIdle}"      "" "" 0 0
 ph.perfadd "php-fpm-running"   "${iWorkerRunning}"   "" "" 0 0
 ph.perfadd "php-fpm-reading"   "${iWorkerReading}"   "" "" 0 0
 
-
-rm -f $tmpfile
 ph.exit
 
 # ----------------------------------------------------------------------
-- 
GitLab