diff --git a/check_proc_mem b/check_proc_mem index 07428092fa9df567c093e3a0995e8c8b72968a84..cc73aebd101eff5ce1819846b35dfedaf5771854 100755 --- a/check_proc_mem +++ b/check_proc_mem @@ -13,91 +13,118 @@ # 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 +# 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.1 +export self_APPVERSION=1.3 -# --- tmp files for internal usage -tmpfile=/tmp/processlist1_$$ -tmpfile2=/tmp/processlist2_$$ - -outCritical=/tmp/processlist_critical_$$ -outWarning=/tmp/processlist_warning_$$ +typeset -i iSizeMB # --- limits -# iWarnLimit=100 -# iCriticalLimit=500 +typeset -i iWarnLimit=100 +typeset -i iCriticalLimit=500 typeset -i iCountWarning=0 typeset -i iCountCritical=0 -rm -f $tmpfile $tmpfile2 $outCritical $outWarning 2>/dev/null - # ---------------------------------------------------------------------- # 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 # ---------------------------------------------------------------------- -# set default / override from command line params -typeset -i iWarnLimit=$( ph.getValueWithParam 100 w "$@") -typeset -i iCriticalLimit=$( ph.getValueWithParam 500 c "$@") + +# --- 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 -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 +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 diff --git a/docs/20_Checks/_index.md b/docs/20_Checks/_index.md index c086de890b1fe51b92204b0326ee62c34832a428..fb67665af793dd83be7a7282a4ef7e36517fa666 100644 --- a/docs/20_Checks/_index.md +++ b/docs/20_Checks/_index.md @@ -37,7 +37,7 @@ There is one include script used by all checks: * [check_opencpu](check_opencpu.md) * [check_packages2install](check_packages2install.md) * [check_php-fpm-status](check_php-fpm-status.md) -* check_proc_mem +* [check_proc_mem](check_proc_mem.md) * check_proc_ressources * check_proc_zombie * [check_psqlserver](check_psqlserver.md) diff --git a/docs/20_Checks/check_proc_mem.md b/docs/20_Checks/check_proc_mem.md new file mode 100644 index 0000000000000000000000000000000000000000..57623ccc841d9206847acf9a1cacb7bce158e12e --- /dev/null +++ b/docs/20_Checks/check_proc_mem.md @@ -0,0 +1,72 @@ +# Check memory consuming processes + +## Introduction + +Show processes that consume the most memory. +You get a list with process name, total usage of all its processes, count of instances. + +## Requirements + +Nothing special. + +## Syntax + +```txt +______________________________________________________________________ + +CHECK_PROC_MEM +v1.3 + +(c) Institute for Medical Education - University of Bern +Licence: GNU GPL 3 + +https://os-docs.iml.unibe.ch/icinga-checks/Checks/check_proc_mem.html +______________________________________________________________________ + +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: + + check_proc_mem [-h] [-c VALUE] [-w VALUE] + +OPTIONS: + + -h, --help + this help + +PARAMETERS: + + -c, --critical VALUE + critical level in MB (default: 500) + + -w. --warning VALUE + warning level in MB (default: 100) + +EXAMPLE: + + check_proc_mem + Check processes with initial memory values + + check_proc_mem -w 1000 -c 2000 + Check processes consuming more than 1000 MB and mark those as critical + that use more than 2000 MB. + +``` + +## Examples + +### Default options + +`./check_proc_mem` returns + +```txt +CRITICAL: Memory consuming processes - 3 processes use 500 MB+ (critical) ... 1 processes use 100..500 MB +Critical mariadbd 2250 MB 1 2250.473 MB +Critical apache2 861 MB 12 71.748 MB +Critical php-fpm8.2 645 MB 37 17.444 MB +Warning icinga2 271 MB 3 90.365 MB +```