All,
I've been working on a way to parse /proc/stat to get the raw CPU jiffies and then calculate the difference between reads in a predefined interval. I have the following so far and was hoping I could get some help from this forum on debugging. The problem that I get is that every interval the aggregate data is correct but all other cpu's are zero. I'm sure it is becuase of my flawed logic as I am not real good with hashes or arrays just yet but KevinADC was a big help in with that. Here is what I have so far. My desire is to have a delta calculation for each cpu.
#!/usr/bin/perl
# This is the format of the data I want
# CPU User nice sys idle iowait irq softirq
# cpu 3530 111 1837 289668 3771 715 0
# cpu0 3530 111 1837 289668 3771 715 0
sub getcpu
# Routine to capture /proc/stat and store in a hash table or hash tables
{
my ($cpu,$user,$nice,$system,$idle,$iowait,$irq,$softirq);
my %HoH = ();
my $file = '/proc/stat';
open ( CPUSTAT, "<$file" ) or die "Cannot open file";
while ( <CPUSTAT> ) {
# Look for lines with the cpu information
if ( $_ =~ /^(cpu\d?)/ ) {
# Read in data and split into fields
($cpu,$user,$nice,$system,$idle,$iowait,$irq,
$softirq) = split(' ');
# Declare the hash table; $cpu will be key and the rest are elements of hash
# store in variables
$HoH{ $cpu }{ 'user' } = $user;
$HoH{ $cpu }{ 'nice' } = $nice;
$HoH{ $cpu }{ 'system' } = $system;
$HoH{ $cpu }{ 'idle' } = $idle;
$HoH{ $cpu }{ 'iowait' } = $iowait;
$HoH{ $cpu }{ 'irq' } = $irq;
$HoH{ $cpu }{ 'softirq' } = $softirq;
}
}
close CPUSTAT;
# Provide hash to calls to this function
return \%HoH;
}
# This is where the rubber meets the road
$iteration = 0;
while ( $iteration >= 0 ) {
# Declare a variable to store return value from getcpu function call
my $rHoH = getcpu();
my ( $user,$nice,$system,$idle,$iowait,$irq,$softirq );
foreach $cpu ( sort(keys %$rHoH) ) {
# Declare variables to store hash elements
$user = $rHoH->{ $cpu }{ 'user' };
$nice = $rHoH->{ $cpu }{ 'nice' };
$system = $rHoH->{ $cpu }{ 'system' };
$idle = $rHoH->{ $cpu }{ 'idle' };
$iowait = $rHoH->{ $cpu }{ 'iowait' };
$irq = $rHoH->{ $cpu }{ 'irq' };
$softirq= $rHoH->{ $cpu }{ 'softirq' };
#print "$cpu,$user,$system,$nice,$idle,$iowait,$irq,$softirq";
if ( $iteration > 0 ) {
$deltauser = $user - $olduser;
$deltasystem = $system - $oldsystem;
$deltanice = $nice - $oldnice;
$deltaidle = $idle - $oldidle;
$deltaiowait = $iowait - $oldiowait;
$deltairq = $irq - $oldirq;
$deltasoftirq = $softirq - $oldsoftirq;
print "$iteration,$cpu,$deltauser,$deltasystem,$deltanice,$deltaidle,$deltaiowait,$deltairq,$deltasoftirq,$olduser,$oldsystem,$iteration\n";
}
$olduser = $user;
$oldsystem = $system;
$oldnice = $nice;
$oldidle = $idle;
$oldiowait = $iowait;
$oldoldirq = $irq;
$oldsoftirq = $softirq;
}
$iteration++;
sleep 1;
}
I've been working on a way to parse /proc/stat to get the raw CPU jiffies and then calculate the difference between reads in a predefined interval. I have the following so far and was hoping I could get some help from this forum on debugging. The problem that I get is that every interval the aggregate data is correct but all other cpu's are zero. I'm sure it is becuase of my flawed logic as I am not real good with hashes or arrays just yet but KevinADC was a big help in with that. Here is what I have so far. My desire is to have a delta calculation for each cpu.
#!/usr/bin/perl
# This is the format of the data I want
# CPU User nice sys idle iowait irq softirq
# cpu 3530 111 1837 289668 3771 715 0
# cpu0 3530 111 1837 289668 3771 715 0
sub getcpu
# Routine to capture /proc/stat and store in a hash table or hash tables
{
my ($cpu,$user,$nice,$system,$idle,$iowait,$irq,$softirq);
my %HoH = ();
my $file = '/proc/stat';
open ( CPUSTAT, "<$file" ) or die "Cannot open file";
while ( <CPUSTAT> ) {
# Look for lines with the cpu information
if ( $_ =~ /^(cpu\d?)/ ) {
# Read in data and split into fields
($cpu,$user,$nice,$system,$idle,$iowait,$irq,
$softirq) = split(' ');
# Declare the hash table; $cpu will be key and the rest are elements of hash
# store in variables
$HoH{ $cpu }{ 'user' } = $user;
$HoH{ $cpu }{ 'nice' } = $nice;
$HoH{ $cpu }{ 'system' } = $system;
$HoH{ $cpu }{ 'idle' } = $idle;
$HoH{ $cpu }{ 'iowait' } = $iowait;
$HoH{ $cpu }{ 'irq' } = $irq;
$HoH{ $cpu }{ 'softirq' } = $softirq;
}
}
close CPUSTAT;
# Provide hash to calls to this function
return \%HoH;
}
# This is where the rubber meets the road
$iteration = 0;
while ( $iteration >= 0 ) {
# Declare a variable to store return value from getcpu function call
my $rHoH = getcpu();
my ( $user,$nice,$system,$idle,$iowait,$irq,$softirq );
foreach $cpu ( sort(keys %$rHoH) ) {
# Declare variables to store hash elements
$user = $rHoH->{ $cpu }{ 'user' };
$nice = $rHoH->{ $cpu }{ 'nice' };
$system = $rHoH->{ $cpu }{ 'system' };
$idle = $rHoH->{ $cpu }{ 'idle' };
$iowait = $rHoH->{ $cpu }{ 'iowait' };
$irq = $rHoH->{ $cpu }{ 'irq' };
$softirq= $rHoH->{ $cpu }{ 'softirq' };
#print "$cpu,$user,$system,$nice,$idle,$iowait,$irq,$softirq";
if ( $iteration > 0 ) {
$deltauser = $user - $olduser;
$deltasystem = $system - $oldsystem;
$deltanice = $nice - $oldnice;
$deltaidle = $idle - $oldidle;
$deltaiowait = $iowait - $oldiowait;
$deltairq = $irq - $oldirq;
$deltasoftirq = $softirq - $oldsoftirq;
print "$iteration,$cpu,$deltauser,$deltasystem,$deltanice,$deltaidle,$deltaiowait,$deltairq,$deltasoftirq,$olduser,$oldsystem,$iteration\n";
}
$olduser = $user;
$oldsystem = $system;
$oldnice = $nice;
$oldidle = $idle;
$oldiowait = $iowait;
$oldoldirq = $irq;
$oldsoftirq = $softirq;
}
$iteration++;
sleep 1;
}