Skip to content
Snippets Groups Projects
Select Git revision
  • 16bbe10817a7d26e32923a4bd9f9fe8bbaf4672e
  • master default protected
  • simple-task/7248-eol-check-add-node-22
  • 6877_check_iml_deployment
4 results

check_php-fpm-status

Blame
  • user avatar
    Hahn Axel (hahn) authored
    16bbe108
    History
    check_php-fpm-status 6.52 KiB
    #!/bin/bash
    # ======================================================================
    #
    # NAGIOS CLIENT CHECK :: php-fpm requests
    #
    # ----------------------------------------------------------------------
    # script checks output of fpm "/status" and counts scoreboard chars
    # ----------------------------------------------------------------------
    # 2021-09-22  v0.1  <axel.hahn@iml.unibe.ch>  initial version
    # ======================================================================
    
    . `dirname $0`/inc_pluginfunctions
    
    tmpfile=/tmp/check_fpm_processes_1
    
    defaulturl=localhost/status
    
    # ----------------------------------------------------------------------
    # functions
    # ----------------------------------------------------------------------
    
    # get a value from fpm status
    #
    # example output:
    # pool:                 www
    # process manager:      dynamic
    # start time:           21/Sep/2021:16:01:12 +0200
    # start since:          65914
    # accepted conn:        34
    # listen queue:         0
    # max listen queue:     0
    # listen queue len:     0
    # idle processes:       6
    # active processes:     3
    # total processes:      9
    # max active processes: 6
    # max children reached: 0
    # slow requests:        0
    #
    # param  string  variable (part before ":")
    function _getvalue(){
            grep "^$1:" $tmpfile | cut -d ":" -f 2 | awk '{ print $1 }'
    }
    
    function showHelp(){
    cat <<EOF
    ______________________________________________________________________
    
    CHECK_PHP-FPM-Status
    Get counters from PHP-FPM status output for active/ idle processes.
    
    (c) Institute for Medical Education - Univerity of Bern
    Licence: GNU GPL 3
    ______________________________________________________________________
    
    The check fetches several counters from php-fm-status page.
    
    It shows a short status in a single line and then the dump of the 
    status page.
    For performance data it eachos:
    
        php-fpm-active     count of active workers
        php-fpm-maxactive  max active processes
        php-fpm-idle       count of idle workers
        php-fpm-queue      count of items in the queue
        php-fpm-maxqueue   max listen queue
        php-fpm-slow       slow requests per second (since last execution of this check)
        php-fpm-speed      requests per second (since last execution of this check)
    
    SYNTAX:
    `basename $0` [-u URL]
    
    OPTIONS:
    
        -u  url to fpm status page  (optional; default: $defaulturl)
        -h or --help   show this help.
    
    PARAMETERS:
    
        None.
    
    EXAMPLE:
    `basename $0` -u http://localhost/my-custom-fpm-statuspage.php
    
    EOF
    }
    
    # ----------------------------------------------------------------------
    # check help
    # ----------------------------------------------------------------------
    
    case "$1" in
        "--help"|"-h")
            showHelp
            exit 0
            ;;
        *)
    esac
    
    # ----------------------------------------------------------------------
    # pre checks
    # ----------------------------------------------------------------------
    ph.require wget
    
    
    # ----------------------------------------------------------------------
    # check params
    # ----------------------------------------------------------------------
    
    # set default / override from command line params
    typeset -i iWarnLimit=`     ph.getValueWithParam 75 w "$@"`
    typeset -i iCriticalLimit=` ph.getValueWithParam 90 c "$@"`
    url=$( ph.getValueWithParam $defaulturl u "$@" )
    
    # --- get /server-status page
    wget --no-check-certificate -O $tmpfile $url 2>/dev/null
    if [ $? -ne 0 ]; then
        rm -f $tmpfile
        ph.abort "UNKNOWN: request to url $url failed. `wget --no-check-certificate -O - -S $url`"
    fi
    
    # ----------------------------------------------------------------------
    # get values from status output
    # ----------------------------------------------------------------------
    
    # --- handled requests per sec
    typeset -i iConn=$(      _getvalue "accepted conn")
    typeset -i iSpeed=$(     ph.perfdeltaspeed "fpm-accepted" $iConn )
    
    # --- count slots
    typeset -i iActive=$(    _getvalue "active processes" )
    typeset -i iMaxActive=$( _getvalue "max active processes" )
    typeset -i iIdle=$(      _getvalue "idle processes")
    
    # --- experimental: generate warning / error 
    typeset -i iQueue=$(     _getvalue "listen queue len")
    typeset -i iMaxQueue=$(  _getvalue "max listen queue")
    typeset -i iSlowTotal=$( _getvalue "slow requests")
    typeset -i iSlow=$(      ph.perfdeltaspeed "fpm-slow" $iSlowTotal )
    typeset -i iMaxChilds=$( _getvalue "max children reached")
    
    
    # ----------------------------------------------------------------------
    # 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 -o $iSlow -gt 0 ]; then
        ph.setStatus warning
    fi
    if [ $iMaxChilds -gt 0 ]; then
        # ph.setStatus critical
        ph.setStatus warning
    fi
    if [ $iIdle -eq 0 ]; then
        ph.setStatus critical
    fi
    
    
    # ----------------------------------------------------------------------
    # output
    # ----------------------------------------------------------------------
    ph.status "PHP-FPM: active: $iActive (max: $iMaxActive) .. idle workers: $iIdle .. queue: $iQueue (max: $iMaxQueue) .. speed: $iSpeed req per sec ... slow: $iSlow req per sec (total: $iSlowTotal)"
    echo
    hint="!! IMPORTANT !! Any non-OK status is still experimmental."
    if [ $iIdle -eq 0 ]; then
        echo $hint
        echo "WARNING: $iIdle idle workers available."
        echo "         Maybe there is a current peak only."
        echo "         Or count of allowed workers (pm.max_children) or spare servers (pm.XXX_spare_servers) is too low."
        echo
    fi
    if [ $iMaxChilds -gt 0 ]; then
        echo $hint
        echo "WARNING: Max. count of children was reached: $iMaxChilds. Maximum of active workers was $iMaxActive - maybe count of allowed workers (pm.max_children) is too low."
        echo
    fi
    if [ $iQueue -gt 0 ]; then
        echo $hint
        echo "WARNING: $iQueue queued requests were found. Maximum of queued items is $iMaxQueue."
        echo
    fi
    if [ $iSlow -gt 0 ]; then
        echo $hint
        echo "WARNING: $iSlow slow requests were found... $iSlowTotal since restart of fpm service."
        echo
    fi
    
    cat $tmpfile
    echo
    
    # --- add performnce data
    ph.perfadd "php-fpm-active"    "${iActive}"    "" "" 0 0
    ph.perfadd "php-fpm-maxactive" "${iMaxActive}" "" "" 0 0
    ph.perfadd "php-fpm-idle"      "${iIdle}"      "" "" 0 0
    ph.perfadd "php-fpm-queue"     "${iQueue}"     "" "" 0 0
    ph.perfadd "php-fpm-maxqueue"  "${iMaxQueue}"  "" "" 0 0
    ph.perfadd "php-fpm-slow"      "${iSlow}"      "" "" 0 0
    ph.perfadd "php-fpm-speed"     "${iSpeed}"     "" "" 0 0
    
    rm -f $tmpfile
    
    ph.exit
    
    # ----------------------------------------------------------------------