Skip to content
Snippets Groups Projects
check_apache_requests 4.02 KiB
#!/bin/bash
# ======================================================================
#
# NAGIOS CLIENT CHECK :: apache requests
#
# ----------------------------------------------------------------------
# script checks output of "/server-status" and counts scoreboard chars
# ----------------------------------------------------------------------
# 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
# ======================================================================

. `dirname $0`/inc_pluginfunctions

tmpfile=/tmp/check_apache_processes_1
tmpfile2=/tmp/check_apache_processes_2

# url=`hostname -f`/server-status
url=localhost/server-status

typeset -i iStart=0
typeset -i iEnd
typeset -i iCount=0

typeset -i iSlots=0
typeset -i iActive=0
typeset -i iIdle=0
typeset -i iUnused=0
typeset -i iUsage=0

paramsWget="-T 5 -t 1 --no-check-certificate"
#            ^    ^
#            |    tries = 1
#            timeout in seconds

# ----------------------------------------------------------------------
# pre checks
# ----------------------------------------------------------------------
ph.require wget

# ----------------------------------------------------------------------
# check output
# ----------------------------------------------------------------------

# --- get /server-status page
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`"
fi

# set default / override from command line params
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 ':'`

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

# <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`

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`

# count of processes apache still can create
iUnused=iCount=`sed -e "s/[^\.]//g" $tmpfile2 | wc -m`

# count of actively used slots
iActive=$iSlots-$iIdle-$iUnused

iUsage=$iActive*100/$iSlots
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>##'
echo

# --- add performnce data
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#"`

  echo "  - ${mychar}: ${iCount}"
  ph.perfadd "${label}"    "${iCount}" "" "" 0 $iSlots
done


rm -f $tmpfile $tmpfile2

ph.exit

# ----------------------------------------------------------------------