- 论坛徽章:
- 0
|
#!/usr/bin/perl -w
use strict;
sub GetCPUState{
open FILE, "/proc/stat" or die "cannot open /proc/stat file ";
my @info = split (/ +/ ,<FILE>);
my @result ;
$result[0] = $info[4] ; # IDLE time
for(1..$#info){
$result[1] += $info[$_];
}
close FILE;
return @result;
}
my (@newCPU,@oldCPU);
@newCPU = &GetCPUState;
while(1){
sleep(5);
@oldCPU = @newCPU;
@newCPU = GetCPUState;
my $cpu_used = 100 * ( 1 - (($newCPU[0] - $oldCPU[0])/($newCPU[1] - $oldCPU[1])) );
printf "CPU used: %3.2f%% \n", $cpu_used;
@oldCPU = @newCPU;
} |
|