Skip to content
Snippets Groups Projects
check_proc_mem 3.06 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
# ======================================================================

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

. `dirname $0`/inc_pluginfunctions

# ----------------------------------------------------------------------
# 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 | fgrep -v "/"`
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

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