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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to capture CPU usage?

Status
Not open for further replies.

volcano

Programmer
Aug 29, 2000
136
HK
Hi all, I want to write a simple scheduled script to monitor the CPU usage of an Unix machine. I guess I can make use of "TOP" command and grep CPU row (eg. top | grep CPU) and then make script processing. But I find I can't capture anything. But if i grep memory or Load, it returns figures. So is there any correct way to capture CPU usage? Thanks.
 
b'coz he is not using AIX? actually, volcano didn't tell us what is the environment (which unix)..

Cheers.

Chacal, Inc.[wavey]
 
oh! I see it now.. thanks!

well, I'm pretty sure that the commands I posted are in Solaris.

All the best,

Chacal, Inc.[wavey]
 
Hi, thanks for your updates. I also know that vmstat/iostat can capture CPU usage. But I just think TOP command is much easier for me to process the content by script then other those commands...haha. My machine is Solaris. BTW, my machine has no any SAR installed. Isn't it bundled in OS?
 
You probably will have sar, it's just not turned on to record system activity. You can run it interactively for snapshot purposes using something like sar 5 5
 
volcano, on Solaris 9 'sar' is part of the SUNWaccu (System Accounting) package.

Annihilannic.
 
To get [tt]sar[/tt] to record system activity, put the following commands into the crontab for the user [tt]sys[/tt].
Code:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /usr/lib/sa/sa1
56 23 * * * /usr/lib/sa/sa2 -A
They may already be there, just commented out. Uncomment them if they are. The first cron entry will collect the performance data. The more often you run it, the finer the granularity of the data you will get. My example is collecting every five minutes.

You may also have to uncomment some lines in a file called [tt]/etc/init.d/perf[/tt]. This is required to have the collection continue after a reboot.

Hope this helps.
 
Just thought I'd drop my two penneth in.....
If I'm stating the obvious just ignore me.

vmstat 2 |awk '{print $20,$21,$22}'

on solaris 9 (for me at least)
lists just the user, system and idle stats for cpu, where 2 in the the time delay in seconds.

Regards

Steve
 
You say you get figures displayed but can't capture
thertefore a crude example could be:
vmstat 2 50 > filecapture

Run 50 times every 2 secs, data is entered into the file filecapture.



 
By default top use a curses package and thus display on a tty (not stdout).
Have a look at the -b option.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top