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