From b528add15b4e37ce8b4025577ef9367ba556beea Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Tue, 22 Aug 2023 14:13:32 +0200
Subject: [PATCH] netio: small fixes

---
 check_netio | 78 ++++++++++++++++++++++++++---------------------------
 1 file changed, 39 insertions(+), 39 deletions(-)

diff --git a/check_netio b/check_netio
index 9b5cc8d..bf456a8 100755
--- a/check_netio
+++ b/check_netio
@@ -7,12 +7,13 @@
 # ----------------------------------------------------------------------
 # 2020-07-08  v1.0  <axel.hahn@iml.unibe.ch>
 # 2023-08-21  v1.1  <axel.hahn@unibe.ch>      add help; add param -i
+# 2023-08-22  v1.2  <axel.hahn@unibe.ch>      send perf data on single interface too; interfaces in alphabetic order; no more tmp file
 # ======================================================================
 
 
 . $( dirname $0 )/inc_pluginfunctions
 
-export self_APPVERSION=1.1
+export self_APPVERSION=1.2
 
 
 # ----------------------------------------------------------------------
@@ -62,8 +63,6 @@ if [ $bOptHelp -eq 1 ]; then
     exit 0
 fi
 
-tmpfile2=`mktemp`
-
 typeset -i iCountInterfaces
 
 typeset -i iRead=0
@@ -76,60 +75,61 @@ typeset -i iTotalTrans=0
 data=$( cat /proc/net/dev | grep -E "(eth|en[ops][0-9]*)" )
 
 allInterfaces=$( cut -f 1 -d ":" <<< "$data" | tr -d " " )
-sInterface=$(   ph.getValueWithParam "$allInterfaces" "i" "$@")
+sInterface=$(   ph.getValueWithParam "$allInterfaces" "i" "$@" | sort )
 
+# ----- loop over all interfaces
 iCountInterfaces=$( wc -w <<< "$sInterface" )
-for myinterface in $sInterface
-do
-    echo >>"$tmpfile2"
-    echo "--- ${myinterface}:" >> "$tmpfile2"
-    line=$( grep "${myinterface}:" <<< "$data" )
-    if [ -n "$line" ] ; then
+out=$( 
+    for myinterface in $sInterface
+    do
+        echo
+        echo "--- ${myinterface}:"
+        line=$( grep "${myinterface}:" <<< "$data" )
+        if [ -n "$line" ] ; then
 
-        # echo "SOURCE: $line" >> $tmpfile2
+            # echo "SOURCE: $line"
 
-        # total value of byte
-        iRead=$( echo "$line" | awk '{ print $2 }')
-        iTrans=$(echo "$line" | awk '{ print $10 }')
+            # total value of byte
+            iRead=$( echo "$line" | awk '{ print $2 }')
+            iTrans=$(echo "$line" | awk '{ print $10 }')
 
-        # speed in byte per sec based on last stored value and its age
-        iSpeedRead=$( ph.perfdeltaspeed "netio-${myinterface}-rx" $iRead)
-        iSpeedTrans=$(ph.perfdeltaspeed "netio-${myinterface}-tx" $iTrans)
+            # speed in byte per sec based on last stored value and its age
+            iSpeedRead=$( ph.perfdeltaspeed "netio-${myinterface}-rx" $iRead)
+            iSpeedTrans=$(ph.perfdeltaspeed "netio-${myinterface}-tx" $iTrans)
 
-        printf "%15s %15s %10s %-10s \n"  "receive:"   $iRead  $iSpeedRead  "byte/s" >> "$tmpfile2"
-        printf "%15s %15s %10s %-10s \n"  "transmit:"  $iTrans $iSpeedTrans "byte/s" >> "$tmpfile2"
+            printf "%15s %15s %10s %-10s \n"  "receive:"   $iRead  $iSpeedRead  "byte/s"
+            printf "%15s %15s %10s %-10s \n"  "transmit:"  $iTrans $iSpeedTrans "byte/s"
 
-        # to store data for each interface
-        if [ $iCountInterfaces -gt 1 ]; then
             ph.perfadd "rx-${myinterface}"   "$iSpeedRead"
             ph.perfadd "tx-${myinterface}"   "$iSpeedTrans"
-        fi
 
-        iTotalRead=$iTotalRead+$iSpeedRead
-        iTotalTrans=$iTotalTrans+$iSpeedTrans
-    else
-        ph.setStatus "unknown"
-        echo "UNKNOWN: the interface ${myinterface} does not exist." >> "$tmpfile2"
-    fi
-
-done
+            iTotalRead=$iTotalRead+$iSpeedRead
+            iTotalTrans=$iTotalTrans+$iSpeedTrans
+        else
+            ph.setStatus "unknown"
+            echo "The interface [${myinterface}] does not exist."
+        fi
 
-echo "" >> "$tmpfile2"
+    done
+    echo
+)
 
+# ----- total
 if [ $iCountInterfaces -gt 1 ]; then
-    echo "--- total:"
-    printf "%15s %10s %-10s \n"  "receive:"   $iTotalRead  "byte/s"
-    printf "%15s %10s %-10s \n"  "transmit:"  $iTotalTrans "byte/s"
-fi >> "$tmpfile2"
 
+    out+=$(
+        echo "--- total:"
+        printf "%15s %10s %-10s \n"  "receive:"   $iTotalRead  "byte/s"
+        printf "%15s %10s %-10s \n"  "transmit:"  $iTotalTrans "byte/s"
+    )
+   
+fi
 ph.perfadd "rx"   "$iTotalRead"
 ph.perfadd "tx"   "$iTotalTrans"
 
+# ----- output
 ph.status "Network IO ... IN $(ph.toUnit $iTotalRead M) MB/s >> [host] >> $(ph.toUnit $iTotalTrans M) MB/s OUT"
 
-cat "$tmpfile2"
-
-rm -f "$tmpfile2"
+echo "$out"
 
 ph.exit
-
-- 
GitLab