用perl取一段时间内cpu使用与load avg的最大值!
简单抓取uptime与mpstat输出中的数值,比较儿科。如果有更多需求,请上各款高档monitor类工具,如走snmp的cacti等...
#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw(strftime);
my $topavg = 0;
my $topcpu = 0;
my $procnum = $$;
$SIG{INT} = \&disp;
while(1) {
my $uptime = `uptime`;
my $mpstat = `mpstat -P ALL 1 1`;
my $loadavg = $1 if $uptime =~ /([0-9]+\.[0-9]+),/;
my $usercpu = $1 if $mpstat =~ /all\s+([0-9]+\.[0-9]+)/;
my $cur_time = strftime "%F %T", localtime;
print "$cur_time CPU: $usercpu\tLOAD: $loadavg\n";
$topavg = $loadavg if $loadavg > $topavg;
$topcpu = $usercpu if $usercpu > $topcpu;
sleep 1;
}
sub disp {
return if $procnum != $$;
print "\nStop...\n";
print "MAX CPU: $topcpu\tMAX LOAD: $topavg\n";
exit;
}