diff --git a/check_apache_requests b/check_apache_requests index 2cb4c3817b2be27a2ae7a7ee2dbb25c1d426055c..7945cead09f5dde8980f06ebc6c0e9f91f1b0e03 100755 --- a/check_apache_requests +++ b/check_apache_requests @@ -10,14 +10,13 @@ # 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 +# 2022-08-31 v1.4 <axel.hahn@iml.unibe.ch> add help; shellfix corrections +# 2023-06-19 v1.5 <axel.hahn@unibe.ch> no more tmpfile # ====================================================================== . $(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 +self_APPVERSION=1.5 # url=`hostname -f`/server-status url=localhost/server-status @@ -124,9 +123,8 @@ ph.require wget # ---------------------------------------------------------------------- # --- get /server-status page -wget $paramsWget -O $tmpfile $url 2>/dev/null +data=$( wget $paramsWget -O - $url 2>/dev/null ) if [ $? -ne 0 ]; then - rm -f $tmpfile ph.abort "UNKNOWN: request to url $url failed. $(wget $paramsWget -O - -S $url 2>&1)" fi @@ -136,32 +134,30 @@ 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>' <<< "$data" | cut -f 1 -d ':') +iEnd=$( grep -n '</pre>' <<< "$data" | cut -f 1 -d ':') if [ $iStart -eq 0 -o $iEnd -eq 0 ]; then - rm -f $tmpfile ph.abort "UNKNOWN: url $url has no PRE tag for apache scroreboard. I guess it is not a server-status page." fi -sed -n "${iStart},${iEnd}p" $tmpfile | sed 's#<.*>##g' | tr -d "\n" >$tmpfile2 +dataPre=$( sed -n "${iStart},${iEnd}p" <<< "$data" | sed 's#<.*>##g' | tr -d "\n" ) # --- count slots in the scoreboard # total slots available -iSlots=$(cat $tmpfile2 | wc -m) +iSlots=$( echo -n "$dataPre" | wc -m ) if [ $iSlots -eq 0 ]; then - rm -f $tmpfile $tmpfile2 ph.abort "UNKNOWN: url $url has no count of slots. I guess it is not a server-status page or option for Extended status is off." fi # running apache processes waiting for a request -iIdle=iCount=$(sed -e "s/[^_]//g" $tmpfile2 | wc -m) +iIdle=iCount=$(echo -n "$dataPre" | sed -e "s/[^_]//g" | wc -m) # count of processes apache still can create -iUnused=iCount=$(sed -e "s/[^\.]//g" $tmpfile2 | wc -m) +iUnused=iCount=$(echo -n "$dataPre" | sed -e "s/[^\.]//g" | wc -m) # count of actively used slots iActive=$iSlots-$iIdle-$iUnused @@ -172,7 +168,7 @@ ph.setStatusByLimit $iUsage $iWarnLimit $iCriticalLimit # --- output ph.status "Apache: $iSlots slots ... active: $iActive wait: $iIdle unused: $iUnused ($iUsage % usage)" -grep "^<dt" $tmpfile | sed 's#<dt>##'| sed 's#</dt>##' +grep "^<dt" <<< "$data" | sed 's#<dt>##'| sed 's#</dt>##' echo # --- add performnce data @@ -181,16 +177,13 @@ 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) + iCount=$(echo -n "$dataPre" | sed -e "s/[^${mychar}]//g" | wc -m) label=$(echo apache-${mychar} | tr [:upper:] [:lower:] | sed "s#_#idle#" | sed "s#\.#unused#") echo " - ${mychar}: ${iCount}" ph.perfadd "${label}" "${iCount}" "" "" 0 $iSlots done - -rm -f $tmpfile $tmpfile2 - ph.exit # ---------------------------------------------------------------------- diff --git a/docs/20_Checks/check_apache_requests.md b/docs/20_Checks/check_apache_requests.md index e62b5bdab1bc80a0a9101a8f7ff893b7b4b3ca5f..21d2bd1b0ded2091d5810421d552ccb9e4acc598 100644 --- a/docs/20_Checks/check_apache_requests.md +++ b/docs/20_Checks/check_apache_requests.md @@ -5,7 +5,7 @@ ______________________________________________________________________ CHECK_APACHE_REQUESTS -v1.4 +v1.5 (c) Institute for Medical Education - University of Bern Licence: GNU GPL 3 diff --git a/inc_pluginfunctions b/inc_pluginfunctions index 3b4ce9e4a6d623ecf32209eeb1dfe1ef984046aa..dad46e80ad1951b54d87179bad3d32e482bcadbe 100644 --- a/inc_pluginfunctions +++ b/inc_pluginfunctions @@ -36,6 +36,7 @@ # 2023-04-24 v1.8 <axel.hahn@unibe.ch> fix unit conversion # 2023-05-05 v1.9 <axel.hahn@unibe.ch> user specific counter directory # 2023-05-17 v1.10 <axel.hahn@unibe.ch> ph.getOS searches in os-release first +# 2023-06-22 v1.11 <axel.hahn@unibe.ch> fix ph.toUnit with float values; shell fixes # ====================================================================== @@ -45,13 +46,9 @@ typeset -i ph_cfg__EXIT_WARNING=1 typeset -i ph_cfg__EXIT_CRITICAL=2 typeset -i ph_cfg__EXIT_UNKNOWN=3 -declare ph_perfdatafile= -# declare -a ph_perfdata -typeset -i ph_perfcounter=0 - -# save command line params -ph_cmd__params="$@" +typeset -i ph_cfg__EXIT_CODE +declare ph_perfdatafile= # abort a check and exit with status "unknown" function ph.abort(){ @@ -86,24 +83,24 @@ function ph.exit(){ function ph.getOS(){ local distro= - if [ -z $distro ]; then + if [ -z "$distro" ]; then # centos7, debian, manjaro, ubuntu distro=$( grep "^ID=" /etc/os-release | cut -f 2 -d "=" ) fi - if [ -z $distro ]; then + if [ -z "$distro" ]; then distro=$( grep "^ID=" /etc/*-release | cut -f 2 -d "=" ) fi - if [ -z $distro ]; then + if [ -z "$distro" ]; then # debian6,7, ubuntu 10,12 .. maybe unneeded. distro=$( head -1 /etc/issue | grep "^[a-zA-Z]" | cut -f 1 -d " " ) fi # sanitize: lowercase, remove " - distro=$( echo $distro | tr -d '"' | tr [:upper:] [:lower:] ) + distro=$( echo "$distro" | tr -d '"' | tr [:upper:] [:lower:] ) - if [ -z $distro ]; then + if [ -z "$distro" ]; then ph.abort "UNKNOWN: distro was not detected." fi @@ -190,19 +187,19 @@ function ph.hasParamoption(){ # param integer|string 0..3 or ok|warning|critical|unknown function ph.setStatus(){ case $1 in - $ph_cfg__EXIT_OK|"ok"): + "$ph_cfg__EXIT_OK"|"ok"): ph_cfg__EXIT_CODE=$ph_cfg__EXIT_OK ph_cfg__EXIT_STATUS="OK" ;; - $ph_cfg__EXIT_WARNING|"warning"): + "$ph_cfg__EXIT_WARNING"|"warning"): ph_cfg__EXIT_CODE=$ph_cfg__EXIT_WARNING ph_cfg__EXIT_STATUS="WARNING" ;; - $ph_cfg__EXIT_CRITICAL|"critical"): + "$ph_cfg__EXIT_CRITICAL"|"critical"): ph_cfg__EXIT_CODE=$ph_cfg__EXIT_CRITICAL ph_cfg__EXIT_STATUS="CRITICAL" ;; - $ph_cfg__EXIT_UNKNOWN|"unknown"): + "$ph_cfg__EXIT_UNKNOWN"|"unknown"): ph_cfg__EXIT_CODE=$ph_cfg__EXIT_UNKNOWN ph_cfg__EXIT_STATUS="UNKNOWN" ;; @@ -267,7 +264,7 @@ function ph.status(){ # param value with ending scale [none]=1 K=Kilo M=Mega G=Giga function ph._getExp(){ local _unit - _unit=$( echo $1 | sed "s#[0-9]##g" ) + _unit=${1//[0-9\.]/} test -z "$_unit" && echo 1 @@ -300,11 +297,11 @@ function ph.toUnit(){ local _value=$1 local _unit=$2 - local _multiply=`ph._getExp $_value` - local _divisor=`ph._getExp $_unit` + local _multiply; _multiply=$( ph._getExp "$_value" ) + local _divisor; _divisor=$( ph._getExp "$_unit" ) # echo "DEBUG ... $_divisor .. $_multiply" - echo "$(echo $_value | tr -d "[:alpha:]" )*${_multiply}/$_divisor" | bc + echo "$(echo "$_value" | tr -d "[:alpha:]" )*${_multiply}/$_divisor" | bc } @@ -346,7 +343,7 @@ function ph._getStorefile(){ function ph._savecounter() { local varName=$1 local value=$2 - local sStoreFile=$(ph._getStorefile "${varName}") + local sStoreFile; sStoreFile=$(ph._getStorefile "${varName}") #echo "DEBUG: `date +%s`:${value} \> ${sStoreFile}" # echo "`date +%s`:${value}" > "${sStoreFile}" echo ${value} > "${sStoreFile}" @@ -357,7 +354,7 @@ function ph._savecounter() { # param string varName variable name of a value to store function ph._getageoflastvalue() { local varName=$1 - local sStoreFile=$(ph._getStorefile "${varName}") + local sStoreFile; sStoreFile=$(ph._getStorefile "${varName}") ph.getFileAge "${sStoreFile}" # local ilast=`cat "${sStoreFile}" 2>/dev/null | cut -f 1 -d ":" ` # local inow=`date +%s` @@ -368,7 +365,7 @@ function ph._getageoflastvalue() { # param string varName variable name of a value to store function ph._readlastvalue(){ local varName=$1 - local sStoreFile=$(ph._getStorefile "${varName}") + local sStoreFile; sStoreFile=$(ph._getStorefile "${varName}") # cat "${sStoreFile}" 2>/dev/null | cut -f 2 -d ":" cat "${sStoreFile}" 2>/dev/null } @@ -420,11 +417,11 @@ function ph.perfdeltaspeed(){ # retvalue="[reset data]" retvalue="" else - local iage=$(ph._getageoflastvalue "${varName}") - test $iage = 0 && iage=1 + local iage; iage=$(ph._getageoflastvalue "${varName}") + test "$iage" = "0" && iage=1 - local delta=$(echo "${value}-$lastvalue" | bc $bcParam ) - local deltaspeed=$(echo "${delta}*${deltaFactor}/(${iage})" | bc $bcParam) + local delta; delta=$(echo "${value}-$lastvalue" | bc $bcParam ) + local deltaspeed; deltaspeed=$(echo "${delta}*${deltaFactor}/(${iage})" | bc $bcParam) retvalue=$deltaspeed fi fi