Perl 采集监控日志插入数据库

#!/usr/bin/perl 
use POSIX;
use DBI; 
my $dbName = 'oadb';  
my $dbUser = 'system';  
my $dbUserPass = 'oracle';  
my $dbh = DBI->connect("dbi:Oracle:$dbName", $dbUser, $dbUserPass) or die "can't connect to database " ;
use Encode qw/encode decode/;
use CGI;
my $dir  = '/root/mon/';
my $file = 'catalina.out';
my $tom_home='/usr/local/apache-tomcat-7.0.55_8081/webapps/mon';
$mon_file = "$dir/$file";
$IPMACHINE = "MBANKtest1";
$RTXNO="04657";
my $SDATE = strftime("%Y%m%d%H%M%S",localtime());
$UPLOADPATH = "/data/swap/mbanklog/$IPMACHINE-$SDATE$file";
print "########################################################################################################################
";
print "######监控日志文件:          $mon_file
";
print "######监控服务器:            $IPMACHINE
";
print "######报警通知腾讯通号码:    $RTXNO
";
print "######当前报警时间戳:        $SDATE
";

##########监控关键字,以空格隔开################
@warn_arr = qw/Exception 登陆失败/;
if (-f "$tom_home/file.html"){
open FILE,">$tom_home/file.html";
close FILE;};
foreach $a (@warn_arr) {
    undef @err_info;
    open( C, "<", "$a.tmp" );
    while (<C>) {
        $count = $_;
    }
print "=========================================================================================================================
";
print "######监控异常关键字:      "$a"---数量=$count
";
    open( A, "<", "$mon_file" ) || die "$!
";
    $i   = 0;
    $num = 0;
    $b = 0;
	
    while (<A>) {
        $num++;
        if ( $_ =~ /$a/i ) { $i++; $b = $num }
};
        if ( $i != 0 && $i != $count && defined($count) ) {
        open( B, "<", "$mon_file" ) || die "$!
";
        while (<B>) {
        if  (($. >= "$b" -2) and  ($. <= "$b" + 2) ){push( @err_info, $_ );
        if ($_ =~ /.*username=(.*?)&password.*/){$user = $1;print "$user is $user
";};
        if ($_ =~ /登陆失败/){chomp ($_);$value=$_;print "$value is $value
";};
      $dbh->do("INSERT INTO err_info VALUES ('$user',to_date($SDATE,YYYY-MM-DD),$value)");

   };

        };
        print "######错误信息:@err_info
";
         my $q = CGI->new;
        open (C ,">>$tom_home/file.html");
        print C ( $q->start_html('warn info'));
        print C ($q->h1("$a--err_info is @err_info"));
       print C ($q->end_html);
       close C;

#        system("wget -q --spider -t 1 http://10.130.130.248:8012/sendnotify.cgi?title="$IPMACHINE"\&msg="$SDATE$file\n@err_info"\&receiver="$RTXNO"");
##        sleep(5);
##        system("./putlog "$mon_file" "$UPLOADPATH"");
#        print "######日志文件已上传至       $UPLOADPATH
";
       close B;
    }else {
  my $q = CGI->new;
        open (C ,">>$tom_home/file.html");
        print C ( $q->start_html('warn info'));
        print C ($q->h1("$a IS OK"));
       print C ($q->end_html);
       close C;
}
  open( D, ">", "$a.tmp" );
    print D ("$i
");
};




原文地址:https://www.cnblogs.com/hzcya1995/p/13351717.html