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

Linux CPU Utilization 1

Status
Not open for further replies.

nix45

MIS
Nov 21, 2002
478
US
I need a simple script that will run periodically via cron that will record the CPU utilization percentage. I was going to use the vmstat command and grab the CPU idle time field and subtract that from 100, but on some servers it seems that the vmstat numbers are way off...
Code:
@vmstat = `/usr/bin/vmstat`;
foreach (@vmstat) {
  chomp;
  if ($_ =~ /\d+.+\s+(\d+)$/) {
     $idlecpu = $1;
  }
}
$cputime = 100 - $idlecpu;
print "CPU Utilization: $cputime%\n";
I tested this on some servers that do nothing and the idle time from vmstat is reported as 8, meaning a 92% cpu utilization rate, which is way off. It does seem accurate on a handful of busy servers however.

Is there a better way to get the CPU utilization? What about from /proc/uptime? Any modules that already do this type of thing?

Thanks,
Chris
 
Would you believe
Linux::Cpuinfo::Cpu


HTH
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
I already saw the Linux::CPUInfo module on CPAN but all it seems to be is an interface to the /proc/cpuinfo file which only tells you general information (bus speed, cache, make, model, etc) about the CPU. It doesn't show current utilization, which is what I'm looking for.

Thanks,
Chris
 
/usr/bin/vmstat without arguments gives averages since last boot. To get a look at the current state:

/usr/bin/vmstat 5 2

This tells vmstat to use a 5 second interval and give you two readings. The first reading is always averages since last boot, the second line will be averages over the 5 seconds preceeding its output. Parse accordingly.



Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
Rod, didn't realize that the first vmstat line was the average since the last boot. I was wondering why it never changed when you ran vmstat with no options. Is it possible to not show the first line so I don't have to parse through it? Also, would you say that taking the CPU idle field from vmstat (last field) and subtracting it from 100 will give you an accurate current CPU utilization value?

Thanks,
Chris
 
how about this?

`vmstat 5 2 | tail -1`

BTW, on every vmstat I've seen, the last column (which you're grabbing) is "wait %", which is a type of idle, but not all of it. The next to last, "idle %" needs to be added to it to get the true idle time.

"idle" means the processor has nothing to do, period.
"wait" means the processor has nothing to do, but only because it's having to wait for an i/o to complete.



Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
`vmstat 5 2 | tail -1`
duhh, how stupid am I? I need to go home, didn't get much sleep last night:)

On my RH72 system here, vmstat looks like this...

[root@voyager sbin]# vmstat
procs memory swap io system cpu
r b w swpd free buff cache si so bi bo in cs us sy id
1 0 0 509308 10608 8804 477724 11 19 21 5 10 23 20 11 14


From the vmstat man page...

CPU
These are percentages of total CPU time.
us: user time
sy: system time
id: idle time



Is yours different?


thanks dude
 
ahh, on my RHEL AS 3.0 system, it is different...

root@ranger root# vmstat
procs memory swap io system cpu
r b swpd free buff cache si so bi bo in cs us sy id wa
0 0 0 3293288 119540 191440 0 0 0 1 16 13 0 0 15 0


 
I have "id" and "wa" separately on AIX 4.3.3, Mandrake 9.0, and Gentoo 2004.1.

Sure enough, though, I only have "id" on my RH 9 box.

I guess your job just got a (just a little) bit trickier if you want the script to be portable. :)


Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
I guess your job just got a (just a little) bit trickier if you want the script to be portable.
No big deal, it will only run on these two RH72 servers for a few days.

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top