Hi ,
i am trying to set up an alert, when CPU usage (0.2%us in below output) is more than 40%
top | head | grep '^Cpu'
Cpu(s): 0.2%us, 0.2%sy, 0.0%ni, 99.1%id, 0.6%wa, 0.0%hi, 0.0%si, 0.0%st
using CUT, i pulled the value 0.2 and assigned to CPU (variable)
CPU=$(expr `top | head -10 | grep "Cpu" | cut -d ' ' -f3 | cut -d '%' -f1`)
if [ ${CPU} -gt 1 ]
then
echo "HIGH CPU USAGE"
fi
this gives me error
tst.sh: line 3: [[: 0.2: syntax error: invalid arithmetic operator (error token is ".2")
Please advise
and no want to compare with 40.
As far as I know, Bash can't handle floating point calculations, Look up "bc", for example look at this link.
Check out: Bash Math | HACKTUX
Only the ksh93 and zsh shells handle floating point.
I found a cool top trick, but I can't post a URL until 5 posts
try this
CPU=$(expr `top | head -10 | grep "Cpu" | cut -d ' ' -f3 | cut -d '%' -f1`)
let x=$CPU*10
if [ ${x} -gt 10 ]
then
echo "HIGH CPU USAGE"
fi
change multiplication factor if you digits after decimal point is more than 1
top is an interactive program and its output may be full of escape sequences. Its output really isn't meant to be parsed or even saved.