diff --git a/check_proc_ressources b/check_proc_ressources index fbe0fcdeee17ad53facce909753d2d77575cf4d0..02521b8a681627c727b687ff05a82be4acece873 100755 --- a/check_proc_ressources +++ b/check_proc_ressources @@ -15,16 +15,12 @@ # 2020-03-03 v1.0 inititial version # 2020-03-05 v1.1 <axel.hahn@iml.unibe.ch> switch to ph.* helper functions # 2020-06-06 v1.2 <axel.hahn@iml.unibe.ch> for cpu: limits are multiplicated with count of cpu +# 2023-12-22 v1.3 <axel.hahn@unibe.ch> update help; no temp files # ====================================================================== -_version="1.0" +. $( dirname $0 )/inc_pluginfunctions -# --- tmp files for internal usage -tmpfile=/tmp/processlist1_$$ -tmpfile2=/tmp/processlist2_$$ - -outCritical=/tmp/processlist_critical_$$ -outWarning=/tmp/processlist_warning_$$ +export self_APPVERSION=1.3 # --- limits iWarnLimitCpu=70 @@ -36,37 +32,47 @@ iCriticalLimitMem=90 typeset -i iCountWarning=0 typeset -i iCountCritical=0 -rm -f $tmpfile $tmpfile2 $outCritical $outWarning 2>/dev/null - -. `dirname $0`/inc_pluginfunctions +typeset -i iCriticalLimit=0 +typeset -i iWarnLimit=0 +typeset -i iMulti=1 # ---------------------------------------------------------------------- # FUNCTIONS # ---------------------------------------------------------------------- + +# show help function showHelp(){ cat <<EOH -______________________________________________________________________ - -SHOW PROCESSES USING A LOT OF CPU OR MEMORY :: v${_version} - -(c) Institute for Medical Education - University of Bern -Licence: GNU GPL 3 -______________________________________________________________________ +$( ph.showImlHelpHeader ) SYNTAX: check_proc_ressources -m METHOD [-w WARNLIMIT] [-c CRITICALLIMIT] - METHOD string identify what to check; one of cpu|mem - WARNLIMIT integer optional: value in percent; - default: $iWarnLimitCpu (cpu) | $iWarnLimitMem (mem) - CRITICALLIMIT integer optional: value in percent - default: $iCriticalLimitCpu (cpu) | $iCriticalLimitMem (mem) - Remark: for cpu the given value is multiplicated with count of cpu +EXAMPLE: + + -h, --help + show help + + -c, --critical VALUE + critical value; defaults: + * cpu $iCriticalLimitCpu + * mem $iCriticalLimitMem + + -m, --mode VALUE + identify what to check; one of cpu|mem + Remark: for cpu the given limits are multiplicated with count of cpu. + + -w. --warning VALUE + warning limit value; defaults: + * cpu $iWarnLimitCpu + * mem $iWarnLimitMem EXAMPLE: check_proc_ressources -m mem -w 50 -c 70 + Show processes consuming more than 50% of memory; mark as critical + when using 70% and more. EOH } @@ -78,20 +84,19 @@ EOH # ----- check limits -typeset -i iCriticalLimit=0 -typeset -i iWarnLimit=0 -typeset -i iMulti=1 - -# set default / override from command line params -typeset -i iWarnLimit=` ph.getValueWithParam 70 w "$@"` -typeset -i iCriticalLimit=` ph.getValueWithParam 90 c "$@"` -sMode=` ph.getValueWithParam '' m "$@"` +while [[ "$#" -gt 0 ]]; do case $1 in + -h|--help) showHelp; exit 0;; + -m|--mode) sMode=$2; shift ;shift;; + -c|--critcal) iCriticalLimit=$2; shift ;shift;; + -w|--warning) iWarnLimit=$2; shift ;shift;; + *) echo "ERROR: Unknown parameter: $1"; showHelp; exit 1; +esac; done case "${sMode}" in "cpu") scanfield="pcpu" - unit="CPU" + unit="Cpu" test $iWarnLimit -eq 0 && iWarnLimit=$iWarnLimitCpu test $iCriticalLimit -eq 0 && iCriticalLimit=$iCriticalLimitCpu @@ -111,7 +116,7 @@ esac if [ "$sMode" = "cpu" ]; then - typeset -i iCpucount=`cat /proc/cpuinfo | grep vendor_id | wc -l` + typeset -i iCpucount=$(cat /proc/cpuinfo | grep vendor_id | wc -l) if [ $iMulti -eq 0 ]; then ph.abort "ABORT: count of CPUs was not detected." fi @@ -121,36 +126,47 @@ fi # ----- read processlist and create helper table -for line in `ps -eo $scanfield,pid,comm | awk -v limit=$iWarnLimit '$1 > limit {print $1":"$2":"$3 }'` -do - Value=`echo $line | cut -f 1 -d ':'` - iValue=`echo $line | cut -f 1 -d ':' | sed "s#\..*##"` - iProcessId=`echo $line | cut -f 2 -d ':'` - processname=`echo $line | cut -f 3- -d ':'` - if [ $iValue -ge $iCriticalLimit ]; then - iCountCritical=$iCountCritical+1 - echo "Critical: $Value % $unit by process id $iProcessId $processname" >> $outCritical - else - iCountWarning=$iCountWarning+1 - echo "Warning : $Value % $unit by process id $iProcessId $processname" >> $outWarning - fi -done +out+=$( + for line in $(ps -eo ${scanfield},pid,comm | awk -v limit=$iWarnLimit '$1 > limit {print $1":"$2":"$3 }' | sort -k 1 -t ":" -n -r ) + do + + Value=$(echo $line | cut -f 1 -d ':') + iValue=$(echo $line | cut -f 1 -d ':' | sed "s#\..*##") + iProcessId=$(echo $line | cut -f 2 -d ':') + processname=$(echo $line | cut -f 3- -d ':') + + test $iValue -lt $iCountWarning && break + + test $iValue -ge $iCountWarning && status="Warning" + test $iValue -ge $iCriticalLimit && status="Critical" + + # echo "${status}: $Value % $unit by process id $iProcessId $processname" + printf "%-9s %5s %s %-10s %6s %s\n" ${status} $Value % $unit $iProcessId $processname + + done +) # ----- Status output +iCountCritical=$( grep -c "^Critical" <<< "$out" ) +iCountWarning=$( grep -c "^Warning" <<< "$out" ) + if [ $iCountCritical -gt 0 ]; then ph.setStatus "critical" ph.status "$iCountCritical processes use $iCriticalLimit % (critical) or more" echo "$iCountWarning processes use $iWarnLimit % .. $iCriticalLimit % $unit" echo - cat $outCritical | sort -n -k 2 -r - cat $outWarning 2>/dev/null| sort -n -k 2 -r + echo "Level Usage process id process" + echo "-----------------------------------------------------" + echo "$out" elif [ $iCountWarning -gt 0 ]; then ph.setStatus "warning" ph.status "$iCountWarning processes use $iWarnLimit % .. $iCriticalLimit % $unit" echo - cat $outWarning | sort -n -k 2 -r + echo "Level Usage process id process" + echo "-----------------------------------------------------" + echo "$out" else ph.setStatus "ok" ph.status "all processes below warning limit $iWarnLimit % $unit .. (and critical limit $iCriticalLimit %)" @@ -161,19 +177,14 @@ fi case "${sMode}" in "cpu") echo - echo Remark: $iCpucount CPUs detected + echo Cpus detected: $iCpucount ;; "mem") echo - egrep '^(MemTotal|MemFree|MemAvailable|Boffers|Cached):' /proc/meminfo + grep -E '^(MemTotal|MemFree|MemAvailable|Boffers|Cached):' /proc/meminfo ;; esac - -# ----- cleanup temp stuff - -rm -f $tmpfile $outCritical $outWarning 2>/dev/null - ph.exit # ---------------------------------------------------------------------- diff --git a/docs/20_Checks/_index.md b/docs/20_Checks/_index.md index fb67665af793dd83be7a7282a4ef7e36517fa666..5cb5fc2666c0f117ada8701ba583a687aaf73a24 100644 --- a/docs/20_Checks/_index.md +++ b/docs/20_Checks/_index.md @@ -38,7 +38,7 @@ There is one include script used by all checks: * [check_packages2install](check_packages2install.md) * [check_php-fpm-status](check_php-fpm-status.md) * [check_proc_mem](check_proc_mem.md) -* check_proc_ressources +* [check_proc_ressources](check_proc_ressources.md) * check_proc_zombie * [check_psqlserver](check_psqlserver.md) * [check_rearbackup](check_rearbackup.md) diff --git a/docs/20_Checks/check_proc_ressources.md b/docs/20_Checks/check_proc_ressources.md new file mode 100644 index 0000000000000000000000000000000000000000..e0722fb201ded80262494a2043a3e8c567394c33 --- /dev/null +++ b/docs/20_Checks/check_proc_ressources.md @@ -0,0 +1,84 @@ +# Check process resources regarding cpu and memory + +## Introduction + +Show processes that consume the most memory or cpu. +You get a list with usage, process id and process + +## Requirements + +Nothing special. + +## Syntax + +```txt +______________________________________________________________________ + +CHECK_PROC_RESSOURCES +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_ressources.html +______________________________________________________________________ + +SYNTAX: + + check_proc_ressources -m METHOD [-w WARNLIMIT] [-c CRITICALLIMIT] + +EXAMPLE: + + -h, --help + show help + + -c, --critical VALUE + critical value; defaults: + * cpu 90 + * mem 90 + + -m, --mode VALUE + identify what to check; one of cpu|mem + Remark: for cpu the given limits are multiplicated with count of cpu. + + -w. --warning VALUE + warning limit value; defaults: + * cpu 70 + * mem 70 + +EXAMPLE: + + check_proc_ressources -m mem -w 50 -c 70 + Show processes consuming more than 50% of memory; mark as critical + when using 70% and more. + +``` + +## Examples + +### CPU check + +`./check_proc_ressources -m cpu -w 50 -c 70` returns + +```txt +OK: all processes below warning limit 200 % Cpu .. (and critical limit 280 %) + +Cpus detected: 4 +``` + +### Memory check + +`./check_proc_ressources -m mem -w 10 -c 70` returns + +```txt +WARNING: 1 processes use 10 % .. 70 % Memory + +Level Usage process id process +----------------------------------------------------- +Warning 10.9 % Memory 3807 pamac-manager + +MemTotal: 16135088 kB +MemFree: 1420576 kB +MemAvailable: 3913212 kB +Cached: 4560964 kB +```