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...
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
Code:
@vmstat = `/usr/bin/vmstat`;
foreach (@vmstat) {
chomp;
if ($_ =~ /\d+.+\s+(\d+)$/) {
$idlecpu = $1;
}
}
$cputime = 100 - $idlecpu;
print "CPU Utilization: $cputime%\n";
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