2

I am currently using the below method to extract CPU usage idle value from top command and subtracting the value from 100. Is this method correct and is there a better way to achieve the same.

Also, my linux VM is a stripped down version and has only few basic tools like top. Installing other tools is not an option as the package manager is also removed.

CPU_IDLE="$(top -bn2 | grep -F '%Cpu' | tail -n 4 | gawk '{print $8 $9}' | tr -s '\n\:\,[:alpha:]' ' '| gawk '{print $2}'),"
0

Yes, your method alright.

But, you could use mpstat to get more details.

It is included in sysstat package, and if you don't have it installed...

On CentOS / RHEL:

sudo yum install sysstat

On Ubuntu / Debian:

sudo apt-get install sysstat

Use bellow command if you don't have any permission to install a package

grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'
  • Sorry, I forgot to add that my linux VM is a stripped down version and has only few basic tools like top. Installing other tools is not an option as the package manager is also removed. – Bandi Sandeep Apr 12 '17 at 9:48
  • You could extract a deb package and copy files manually, or compile from source code. – Michael D. Apr 12 '17 at 10:11
  • Hey Rakib, looks like the command is not giving actual values. Yesterday ,I ran a script to log the CPU usage for almost 8 hours and I plotted a graph from the datapoints. However, it didn't match to the CPU history shown on my hyper visor for this linux VM. – Bandi Sandeep Apr 13 '17 at 4:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.