From 72ef09a59a92b2afabb7c0ebef2675d2541fa356 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@iml.unibe.ch>
Date: Tue, 22 Mar 2022 15:41:25 +0100
Subject: [PATCH] fix syntax error on 100% idle

---
 check_cpu | 36 ++++++++++++++++++++++++------------
 1 file changed, 24 insertions(+), 12 deletions(-)

diff --git a/check_cpu b/check_cpu
index 3f418ff..14b6a93 100755
--- a/check_cpu
+++ b/check_cpu
@@ -17,6 +17,7 @@
 # 2021-12-10  v1.6  <axel.hahn@iml.unibe.ch> show processes with status D to find cpu waits
 # 2022-03-09  v1.7  <axel.hahn@iml.unibe.ch> show most cpu intensive processes
 # 2022-03-10  v1.8  <axel.hahn@iml.unibe.ch> add cli param -p; update help
+# 2022-03-22  v1.9  <axel.hahn@iml.unibe.ch> fix syntax error on 100% idle
 # ======================================================================
 
 
@@ -108,19 +109,30 @@ typeset -i iMinCpuUsageToShowProcesses=$(  ph.getValueWithParam 50 p "$@")
 #   st : time stolen from this vm by the hypervisor
 # top -b -n 1 | head -5 | grep "^\%Cpu" >$tmpfile
 # FIX read cpu from 2nd output of top
-top -b -n 2 -d 0.1 | grep -i "^\%Cpu" | tail -1 >$tmpfile
-
-cpuUser=$(   awk '{ print $2 }' $tmpfile)
-cpuSystem=$( awk '{ print $4 }' $tmpfile)
-cpuNice=$(   awk '{ print $6 }' $tmpfile)
-cpuIdle=$(   awk '{ print $8 }' $tmpfile)
-cpuWait=$(   awk '{ print $10 }' $tmpfile)
-cpuHi=$(     awk '{ print $12 }' $tmpfile)
-cpuSi=$(     awk '{ print $14 }' $tmpfile)
-cpuSt=$(     awk '{ print $16 }' $tmpfile)
-cpuNonIdle=$(echo 100-$cpuIdle | bc)
 
-rm -f $tmpfile
+# top -b -n 2 -d 0.1 | grep -i "^\%Cpu" | tail -1 >$tmpfile
+# cpuUser=$(   awk '{ print $2 }' $tmpfile)
+# cpuSystem=$( awk '{ print $4 }' $tmpfile)
+# cpuNice=$(   awk '{ print $6 }' $tmpfile)
+# cpuIdle=$(   awk '{ print $8 }' $tmpfile)
+# cpuWait=$(   awk '{ print $10 }' $tmpfile)
+# cpuHi=$(     awk '{ print $12 }' $tmpfile)
+# cpuSi=$(     awk '{ print $14 }' $tmpfile)
+# cpuSt=$(     awk '{ print $16 }' $tmpfile)
+# rm -f $tmpfile
+
+data=$( top -b -n 2 -d 0.1 | grep -i "^\%Cpu" | tail -1 | cut -f 2- -d ':' | tr ',' "\n" )
+
+cpuUser=$(   echo "$data" | grep "us" | awk '{ print $1 }' )
+cpuSystem=$( echo "$data" | grep "sy" | awk '{ print $1 }' )
+cpuNice=$(   echo "$data" | grep "ni" | awk '{ print $1 }' )
+cpuIdle=$(   echo "$data" | grep "id" | awk '{ print $1 }' )
+cpuWait=$(   echo "$data" | grep "wa" | awk '{ print $1 }' )
+cpuHi=$(     echo "$data" | grep "hi" | awk '{ print $1 }' )
+cpuSi=$(     echo "$data" | grep "si" | awk '{ print $1 }' )
+cpuSt=$(     echo "$data" | grep "st" | awk '{ print $1 }' )
+
+cpuNonIdle=$(echo 100-$cpuIdle | bc)
 
 sInfo="INFO  : cpu is in normal ranges."
 if [ "$(echo "${cpuWait} > ${iCriticalWait}" | bc)" -eq 1 ]; then
-- 
GitLab