Operating System - Linux
1821638 Members
2973 Online
109633 Solutions
New Discussion юеВ

script to calculate mpstat output

 
kholikt
Super Advisor

script to calculate mpstat output

I have one Solaris machine that output the mpstat output. I have another HPUX server will pick up and process the file.

Bascially the mpstat show the individual CPU utilization of the core. I need to write some alert script for CPU performance when any of the 3 CPU reach average 90% busy.
abc
4 REPLIES 4
Antonio Cardoso_1
Trusted Contributor

Re: script to calculate mpstat output

Hi,
could be something like following script.
save it to some-name.pl and set +x permission,
then run:
some-name.pl < your-input-file

hope this helps.
antonio.

---------------------------------------
#!/bin/env perl

my @lines = ;
my %cpusld=();
my $cpu, $load;
foreach (@lines) {
if (m/\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/) {
# print "== $1 - $13 - $14 \n";
$cpu=$1;
$load=$13+$14;
if (exists $cpusld{$cpu}) {
$cpusld{$cpu}->{'load'} += $load;
$cpusld{$cpu}->{'nb'} ++;
} else {
$cpusld{$cpu}->{'load'} = $load;
$cpusld{$cpu}->{'nb'} = 1;
}
# print $cpu."==>".$cpusld{$cpu}->{'load'}." - ".$cpusld{$cpu}->{'nb'}."\n"
}
}

my $mail_text="";
foreach $cpu (sort(keys %cpusld)) {
my $avld=$cpusld{$cpu}->{'load'} / $cpusld{$cpu}->{'nb'};
print "average CPU load ($cpu) ==> $avld\n";
if ($avld > 90) {
$mail_text.="average CPU load ($cpu) ==> $avld\n";
}
}

if ($mail_text ne "") {
open(FD,">/tmp/tmpmail.sh") || die ("-E- can't open file: /tmp/tmpmail.txt for writing ($!)");
print FD "#!/bin/sh\n";
print FD 'mail -s "overload-problem" user@domain-name << EOF'."\n";
print FD $mail_text;
print FD "EOF\n";
close FD;
`chmod +x /tmp/tmpmail.sh`;
system("/tmp/tmpmail.sh");
} else {
print "no alert \n";
}
Ninad_1
Honored Contributor

Re: script to calculate mpstat output

Kholikt,

Your requirement is not very clear. You say average of 3 CPU - but the outputs you have shown seem to be for 2 different servers - one having 5 CPUs and other 8 CPUs, so what do you mean by average of 3 CPUs ?

Regards,
Ninad
kholikt
Super Advisor

Re: script to calculate mpstat output

the file is copy and paste error. I only monitored one server. This particular solaris servers has 8 core. From the mpstat output I need to monitor 3 out of this 8 core is having CPU utilization of 90% then I will get the alert
abc
Hein van den Heuvel
Honored Contributor

Re: script to calculate mpstat output


Hmmm, that seems a rather arbitrary alert condition. It suggests that you do not trust the scheduler to 'do the right thing' or that the application has extensive scheduling control.

Anyway...

If I understand your description correctly, then the following perl script can detect the condition.
You woudld have to combine it with Antonio's code to make it send out mail. And you may or might not augment it to report the actual exceeding amounts.

hth,
Hein.


----
$number = shift @ARGV;
$maxuse = shift @ARGV or die "Please pass and numbers";
while (<>) {
if (/^CPU/) {$n = 0; $text=""; $block++; next};
($cpu, $usr,$sys)= (split)[0,12,13];
$use = $usr + $sys;
if (($use) > $maxuse) {
$text .= " $cpu:$use";
$n++;
if ($n==$number) {
print "Max cpu exceeded in block $block. $text\n";
$alerts++;
}
}
}
print "\nTotal number of alert conditions: $alerts\n" if $alerts;


---- usage sample based on second part of attachment ---

C:\Temp>perl tmp.pl 3 50 tmp.txt
Max cpu exceeded in block 2. 0:63 1:99 2:99
Max cpu exceeded in block 3. 0:54 1:95 2:79

C:\Temp>perl tmp.pl 2 90 tmp.txt
Max cpu exceeded in block 2. 1:99 2:99