Skip to content
Snippets Groups Projects
Select Git revision
  • 975b6823461891384f08bd7b8b3744338974ade3
  • master default protected
  • Legacy_Php7
3 results

inc_functions.php

Blame
  • check_apache_requests 5.36 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
    # 2022-08-31  v1.3  <axel.hahn@iml.unibe.ch>  add help; shellfix corrections
    # ======================================================================
    
    . $(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
    
    # 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
    
    # ----------------------------------------------------------------------
    # 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
    # ----------------------------------------------------------------------
    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 -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
    
    
    # --- 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
    
    # ----------------------------------------------------------------------