简单抓取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;
}
[~]# perl per.per
2010-06-22 17:21:04 CPU: 1.99 LOAD: 1.00
2010-06-22 17:21:06 CPU: 1.99 LOAD: 1.00
2010-06-22 17:21:08 CPU: 2.74 LOAD: 1.00
来源:http://blog.vetcafe.net/2009/10/perlcpuload_avg.html
#!/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;
}
[~]# perl per.per
2010-06-22 17:21:04 CPU: 1.99 LOAD: 1.00
2010-06-22 17:21:06 CPU: 1.99 LOAD: 1.00
2010-06-22 17:21:08 CPU: 2.74 LOAD: 1.00
来源:http://blog.vetcafe.net/2009/10/perlcpuload_avg.html
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://jackxiang.com/post/3226/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
评论列表