Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

commands to get cpu load/usage

Status
Not open for further replies.

sourabhjha

Programmer
Jan 13, 2002
121
IN
Hi,
Wanted some commands for getting cpu usage and cpu load which should generally work on all unix platform(my Os is sun-solaris so it shud work on it for sure).
I know commands like top,vmstat.Just not comfortable with filtering out just that number(load/average)
Actually i will be firing these commands from remote machine,so the requirement is such that the command should return just the number so as i need not parse the result further.
Thanks,
-Sourabh
 
Hi Sourabh,

Also try:
uptime for a rolling load average
iostat for I/O statistics and CPU usage
mpstat for statistics per processor

See the man pages before using them. There are several other ..stat commands that gather statistics, as well.
You can also tailor the output from any of these commands by using awk and sed. Please re-post any specific questions to get specific answers.

I hope that helps.

Mike
 
I am happy with sar -q

it shows you the % of time you had programs wanting to execute but no available CPU to run them.

it also shows that when there IS at least one job waiting for a CPU, how many are waiting.

I tried to remain child-like, all I acheived was childish.
 
Thanks Mike,The commands you mentioned fits.
Since i wanted the Host level information but not pgm level, i have decided to use
"uptime" for getting the load
"iostat" for cpu usage
Can you help me out parsing the exact number out of these commands.I am not very comfortable using awk,sed.Little help from you will be useful to me.

I also had one question.The figure i see for cpu usage using the commands "top" and "iostat" are diff.
This is what i see using "top"
CPU states: 63.8% idle, 12.5% user, 23.6% kernel, 0.1% iowait, 0.0% swap

And this is what i see firing "iostat" at the same time:
cpu
us sy wt id
58 80 3 74

Now the over all cpu usage as per top is 100-63 = 37.As per iostat is it 58?

Thanks,
-Sourabh
 
Hi Sourabh,

In Solaris there is a qualifer ( -c ) which reduces the usual output to the infomation you want. The column headings are in the man pages ( man iostat ).

>>> The figure i see for cpu usage using the commands "top" and "iostat" are diff.

When you use the command iostat the first line of output is the average of the values since system boot, not the last few seconds. Which is going to be different from the output from top.
So use the command iostat -c 1 4 which gives three lines at one second intervals (see the man pages for details). I use this construct and average the last 3 lines, as follows:
Code:
#!/bin/ksh
cpu_user=0
cpu_system=0
cpu_waitio=0
iostat -c 1 4 | tail -3 | while read col1 col2 col3 col4
do
  ((cpu_user=cpu_user + col1))
  ((cpu_system=cpu_system + col2))
  ((cpu_waitio=cpu_waitio + col3))
done
((cpu_user=cpu_user / 3))
((cpu_system=cpu_system / 3))
((cpu_waitio=cpu_waitio / 3))

((cpu_busy_pcent=cpu_user + cpu_system + cpu_waitio))

Obviously tailor it to your own requirements.

I hope that helps. Feel free to post further questions.

Mike
 
Thanks Mike.That helps me a lot and gives me enough inputs to go ahead with my requirement.
 
Hi Mike,
Going ahead with my requiremenet i am stuck at one point.
I want to extract the cpu load figure(middle one out of the three displayed) from the "uptime" command.
I was using something like
uptime|cut -d":" -f3,3,4
to do that.
But i saw that the uptime command output differs on diff solaris system that i have here.
Mentioned below is the uptime command output and the "uname -a" for three machines

8:58pm up 126 day(s), 15:37, 7 users, load average: 0.17, 0.20, 0.23
5.8 Generic_108528-22 sun4u sparc SUNW,Ultra-80

8:58pm 7 users, load average: 6.90, 7.78, 7.48
SunOS gpseai01 5.6 Generic_105181-30 sun4u sparc SUNW,Ultra-80

8:59pm 5 users, load average: 1.54, 1.84, 1.90
SunOS gpsd024c 5.8 Generic_108528-19 sun4u sparc SUNW,Sun-Fire-15000


Is there any option in cut or awk which will let me parse the output from backwards.Then i could use comma as a field separator and then get the 2nd field from backwards.


Thanks
-Sourabh
 
Hi Sourabh,

>>> Is there any option in cut or awk which will let me parse the output from backwards

There is indeed. To extract the last field (comma separated) please try:
uptime | awk -F, '{print $NF}'

To extract the last but one field (comma separated) please try:
uptime | awk -F, '{print $(NF-1)}'

These work in Korn Shell on Solaris and Tru64 (I tried it).

I hope that helps.

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top