#!/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-12-22 v1.3 <axel.hahn@unibe.ch> add help; do not use tmp file anymore; 5..10 times faster # ====================================================================== . $( dirname $0 )/inc_pluginfunctions export self_APPVERSION=1.3 typeset -i iSizeMB # --- limits typeset -i iWarnLimit=100 typeset -i iCriticalLimit=500 typeset -i iCountWarning=0 typeset -i iCountCritical=0 # ---------------------------------------------------------------------- # functions # ---------------------------------------------------------------------- # show help text function showHelp(){ local _self; _self=$(basename $0) cat <<EOF $( ph.showImlHelpHeader ) Show processes that consume the most memory. You get a list with processes consuming * more than the critical limit * between warning and critical limit SYNTAX: $_self [-h] [-c VALUE] [-w VALUE] OPTIONS: -h, --help this help PARAMETERS: -c, --critical VALUE critical level in MB (default: $iCriticalLimit) -w. --warning VALUE warning level in MB (default: $iWarnLimit) EXAMPLE: $_self Check processes with initial memory values $_self -w 1000 -c 2000 Check processes consuming more than 1000 MB and mark those as critical that use more than 2000 MB. EOF } # ---------------------------------------------------------------------- # MAIN # ---------------------------------------------------------------------- # --- check param -h while [[ "$#" -gt 0 ]]; do case $1 in -h|--help) showHelp; exit 0;; -c|--critcal) iCriticalLimit=$2; shift ;shift;; -w|--warning) iWarnLimit=$2; shift ;shift;; *) echo "ERROR: Unknown parameter: $1"; showHelp; exit 1; esac; done # --- read processlist and create helper table pslist=$( ps -yle ) pslist2=$( for processname in $( echo "$pslist" | awk {'print $13'} | sort -u | grep -Fv "/") do grep " ${processname}$" <<< "$pslist"| awk ' {x += $8;y += 1; process=$13} END {printf "%-15s %10.0f %5s %10.3f\n", process, x/1024, y, x/(y*1024)}' 2>/dev/null done | sort -k 2 -n -r ) # --- generate output with most consuming processes out=$( echo "$pslist2" | while read -r processname iSizeMB processcount do test $iSizeMB -lt $iWarnLimit && break test $iSizeMB -ge $iWarnLimit && status="Warning" test $iSizeMB -ge $iCriticalLimit && status="Critical" printf "%-9s %-20s %6s MB %4s %10s MB\n" $status $processname $iSizeMB $processcount done ) iCountCritical=$( grep -c "^Critical" <<< "$out" ) iCountWarning=$( grep -c "^Warning" <<< "$out" ) test $iCountWarning -gt 0 && ph.setStatus "warning" test $iCountCritical -gt 0 && ph.setStatus "critical" # --- output ph.status "Memory consuming processes - $iCountCritical processes use ${iCriticalLimit} MB+ (critical) ... $iCountWarning processes use ${iWarnLimit}..${iCriticalLimit} MB" echo "$out" ph.exit # ----------------------------------------------------------------------