perl 監控log統計腳本

老大布置了任務,要統計天天的故障狀況,計算服務穩定指標
根據監控記錄的log,來進行統計

log的格式大概以下:
corp_resin_a:10.11.15.35:6805 is down! 2008-08-02-20:55:16
mx7.cmail.sogou.com:10.11.15.34:6805 is down! 2008-08-02-20:55:26
mx5.cmail.sogou.com:10.11.15.46:6805 is down! 2008-08-02-20:55:26
mx10.cmail.sogou.com:192.168.95.143:6805 is down! 2008-08-02-20:55:26
corp_resin_d:192.168.132.189:6805 is down! 2008-08-02-20:55:26
corp_resin_d:192.168.132.89:6805 is down! 2008-08-02-20:55:26
corp_resin_a:10.11.15.47:6805 is down! 2008-08-02-20:55:26
corp_resin_d:192.168.131.164:6805 is down! 2008-08-02-20:55:26
corp_resin_a:10.11.15.20:6805 is down! 2008-08-02-20:55:26
mx10.cmail.sogou.com:192.168.95.143:6805 is down! 2008-08-02-20:55:37

監控腳本每分鐘運行一次,所以能夠認爲出現一次log就算一分鐘故障時間

採用perl來寫,沒啥別的目的,就是練手,日誌格式爲主機名,ip,端口 日期,時間
#!/usr/bin/perl
        my $web=0;
        my $pop3=0;
        my $smtp=0;
        my $master=0;
        my $slave=0;
        my $resin=0;
        my($curlogfile) =@ARGV;
    open(FILE,$curlogfile);

while(<FILE>){
        if ($_=~/down/){
        $totaltimes++;
        }
        chomp();
        @items=split(/\ /);
        my $service=$items[0];
        my $date=$items[3];
        @newitem1=split(/\:/,$service);
        $ip=$newitem1[1];
#        print $ip."\n";
#        sleep (5);
        my $port=$newitem1[2];
        @newitem2=split(/\-/,$date);
        my $time=$newitem2[3];
        if ($port eq "2000"){
            $master=$master+1;
        }
        if ($port eq "9002"){
            $slave=$slave+1;
        }
        if ($port eq "80"){
            $web=$web+1;
        }
        if ($port eq "25"){
            $smtp=$smtp+1;
        }
        if ($port eq "110"){
            $pop3=$pop3+1;
        }
        if ($port=~/6802|6803|6804|6805/){
            $resin=$resin+1;
        }
        if ( defined( $totalip{$ip} ) ){
            $totalip{$ip}=$totalip{$ip}+1;
        }else{
            $totalip{$ip}=1;

        }
#        print $ip."    ".$port."    ".$port{$ip}."\n";

}

close(FILE);

print "總故障次數:".$totaltimes."\n";
if ($web gt 0){
print "WEB故障次數:".$web."\n";
}
if ($pop3 gt 0){
print "POP3故障次數:".$pop3."\n";
}
if ($smtp gt 0){
print "SMTP故障次數:".$smtp."\n";
}
if ($resin gt 0){
print "RESIN故障次數:".$resin."\n";
}
if ($master gt 0){
print "MASTER故障次數:".$master."\n";
}
if ($slave gt 0){
print "SLAVE故障次數:".$slave."\n";
}
print "故障ip:"."      "."故障次數\n";
foreach $key (sort keys %totalip) {
  $num = $totalip{$key};
  print $key."  ".$num."\n";
}


最後統計的結果以下:

coolerfeng@mail:~/log$ ./log.pl net-2008-08-02.txt 總故障次數:642 WEB故障次數:1 POP3故障次數:1 RESIN故障次數:39 MASTER故障次數:601 故障ip:      故障次數 10.10.71.50  1 10.10.71.92  2 10.11.15.20  2 10.11.15.34  2 10.11.15.35  2 10.11.15.46  2 10.11.15.47  2 192.168.131.164  9 192.168.131.76  1 192.168.132.189  8 192.168.132.89  7 192.168.41.194  601 192.168.95.143  3
這臺MASTER壞的時間太長了,嚴重影響了穩定性,嘿嘿
相關文章
相關標籤/搜索