From 4b6e20f463084d7d1260dc7bfbff969447fba149 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Fri, 22 Dec 2023 13:54:45 +0100
Subject: [PATCH] add help; do not use tmp file anymore; 5..10 times faster

---
 check_proc_mem | 155 +++++++++++++++++++++++++++++--------------------
 1 file changed, 92 insertions(+), 63 deletions(-)

diff --git a/check_proc_mem b/check_proc_mem
index 0742809..7b6eee3 100755
--- a/check_proc_mem
+++ b/check_proc_mem
@@ -13,91 +13,120 @@
 # 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 rum.
+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 -d /data/mybackups
+      Check iso files a given directory
+    
+  $_self -d /data/mybackups -w 14 -c 28
+      Check iso files a given directory and customized limits
+
+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
 
-- 
GitLab