-1
cpu= $(mpstat | awk '$12 ~ /[0-9.]+/ { printf("%d%%",100 - $12) }' | cut -d. -f1)

How to refrain the output in numeric?

It's failing while comparing % string with numeric.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • #!/bin/sh cpu= $(mpstat | awk '$12 ~ /[0-9.]+/ { printf("%d%%",100 - $12) }' | cut -d. -f1) if [[ "$cpu" -ge 5 ]]; then message="CPU Utilization is exceeded in Ec2-user Server.\nCurrent use is $cpu ." echo -e "$message" | mail -s "CPU Utilization monitoring" "xxx@yyy.com" fi – Ankkur Singh Jun 27 '18 at 19:16
  • 2
    Don't try to put code in comments, edit the question if you need to clarify. Use Control-k to mark code in questions. – Barmar Jun 27 '18 at 19:17
  • 7
    You can't have a space after `=` in shell assignments. – Barmar Jun 27 '18 at 19:18
  • 2
    Also see [How to use Shellcheck](https://github.com/koalaman/shellcheck), [How to debug a bash script?](https://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](https://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](https://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Jun 27 '18 at 19:55
  • Also, `idle` is always the last column so you can do `mpstat|awk 'p{print int(100-$NF)}/%idle/{p=1}'` – kvantour Jun 28 '18 at 13:06

1 Answers1

1

Don't put the % in the awk output. Add it when you're creating the email. You can also use awk's int() function to remove the fraction from the output, rather than piping to cut.

Also, make sure you don't have any spaces around the = in the cpu assignment.

cpu=$(mpstat | awk '$12 ~ /[0-9.]+/ {print int(100 - $12) }')
if (( $cpu > 5 ))
then mail -s "CPU Utilization monitoring" "xxx@yyy.com" <<EOF
CPU Utilization is exceeded in Ec2-user Server.
Current use is $cpu%.
EOF
fi
Barmar
  • 741,623
  • 53
  • 500
  • 612