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

check_proc_mem

Blame
  • check_proc_mem 3.40 KiB
    #!/bin/bash
    # ======================================================================
    #
    # NAGIOS CLIENT CHECK :: SHOW PROCESSES USING A LOT OF MEMORY
    #
    # ----------------------------------------------------------------------
    #
    # SYNTAX:
    #  ./check_proc_mem [-w WARNLIMIT] [-c CRITICALLIMIT]
    #  WARNLIMIT, CRITICALLIMIT are integer values in MB
    #
    # ----------------------------------------------------------------------
    # 2020-03-02  v1.0  initial version
    # 2020-03-05  v1.1  <axel.hahn@iml.unibe.ch>  switch to ph.* helper functions
    # 2023-02-13  v1.2  <axel.hahn@unibe.ch>      some shell fixes
    # 2023-08-24  v1.3  <axel.hahn@unibe.ch>      add help; do not use tmp file anymore
    # ======================================================================
    
    . $( dirname $0 )/inc_pluginfunctions
    
    export self_APPVERSION=1.1
    
    # --- tmp files for internal usage
    tmpfile=/tmp/processlist1_$$
    tmpfile2=/tmp/processlist2_$$
    
    outCritical=/tmp/processlist_critical_$$
    outWarning=/tmp/processlist_warning_$$
    
    # --- limits
    # iWarnLimit=100
    # iCriticalLimit=500
    
    typeset -i iCountWarning=0
    typeset -i iCountCritical=0
    
    rm -f $tmpfile $tmpfile2 $outCritical $outWarning 2>/dev/null
    
    # ----------------------------------------------------------------------
    # functions
    # ----------------------------------------------------------------------
    
    
    # ----------------------------------------------------------------------
    # MAIN
    # ----------------------------------------------------------------------
    
    # set default / override from command line params
    typeset -i iWarnLimit=$(     ph.getValueWithParam 100 w "$@")
    typeset -i iCriticalLimit=$( ph.getValueWithParam 500 c "$@")
    
    # --- read processlist and create helper table
    ps -yle >$tmpfile
    for processname in $(cat $tmpfile | awk {'print $13'} | sort -u | grep -Fv "/")
    do
        #echo -n "$processname; ">>$tmpfile2
        ps -ylC $processname | awk '
           {x += $8;y += 1; process=$13}
           END {printf "%-15s %10.3f %5s %10.3f\n", process, x/1024, y-1, x/((y-1)*1024)}' 2>/dev/null >>$tmpfile2
    
    done
    
    # --- check limits
    while read line 
    do
        typeset -i iSizeMB=$(echo $line | awk '{ print $2 }' | sed "s#\..*##")
        if [ $iSizeMB -ge $iWarnLimit ]; then
            processname=$(echo $line | awk '{ print $1 }')
            processcount=$(echo $line | awk '{ print $3 }')
            if [ $iSizeMB -ge $iCriticalLimit ]; then
                iCountCritical=$iCountCritical+1
                echo "Critical: $iSizeMB MB - $processname ($processcount)" >>$outCritical
            else
                iCountWarning=$iCountWarning+1
                echo "Warning : $iSizeMB MB - $processname ($processcount)" >>$outWarning
            fi
        fi
    done < $tmpfile2
    
    
    # --- Status output
    
    if [ $iCountCritical -gt 0 ]; then
        ph.setStatus "critical"
        ph.status "$iCountCritical processes use $iCriticalLimit MB (critical) or more"
        echo "$iCountWarning processes use $iWarnLimit MB .. $iCriticalLimit MB"
        echo
        cat $outCritical | sort -n -k 2 -r
        cat $outWarning 2>/dev/null| sort -n -k 2 -r
    elif [ $iCountWarning -gt 0 ]; then
        ph.setStatus "warning"
        ph.status "$iCountWarning processes use $iWarnLimit MB .. $iCriticalLimit MB"
        echo
        cat $outWarning | sort -n -k 2 -r
    else    
        ph.setStatus "ok"
        ph.status "all processes below warning limit $iWarnLimit MB .. (and critical limit $iCriticalLimit MB)"
    fi
    
    rm -f $tmpfile $tmpfile2 $outCritical $outWarning 2>/dev/null
    
    ph.exit
    
    # ----------------------------------------------------------------------