Mojo 分析日志接口

时间:2021-01-11 16:12:14
#!/usr/bin/perl
#取文件行数
##循环开始清空文件
use POSIX;
use DBI;
my $dir = '/data01/applog_backup';
my $file = "$ARGV[0]";
my $SDATE = strftime("%Y-%m-%d",localtime());
my $XDATE = strftime("%Y%m%d%H%M%S",localtime());
my $mon_file = "$dir/$file";
my $USER="$ARGV[1]";
my $IP="$ARGV[2]";
my $ENV="$ARGV[3]"; my $dbUser='DEVOPS';
my $user="root";
my $passwd="1234567";
my $dbh = DBI->connect("dbi:mysql:database=$dbUser;host=192.168.32.161;port=3306",$user,$passwd) or die "can't connect to database ". DBI-errstr;
$dbh->do("SET NAMES utf8");
open FILE,">tmp.out";
close FILE;
open( A, "<", "count.txt" );
while (<A>)
{
$count = $_;
};
print "上次记录的记录数为$count\n"; open(my $fh, '<', "$mon_file");
$. = 0;
while (<$fh>) {
($num=$.) if eof;
};
print "文件最新的记录数为$num"."\n";
open( B, ">", "count.txt" );
print B ("$num\n"); ###获取增量记录
if ( $count && $num != $count && $num !=0 )
{
print "开始处理\n";
open( C, "<", "$mon_file" ) || die "$!\n";
while (<C>)
{
if ($. > "$count" )
{
open( D, ">>", "tmp.out" );
print D ("$_");
}
}
};
close D;
##########监控关键字,以空格隔开################
my @warn_arr = qw/Exception Err/;
my @exp=qw/testscan HandleProxyTradeGroup/; ##$i 是匹配的次数,$b是最后一个匹配的行HandleProxyTradeGroup
foreach $a (@warn_arr)
{
my $i = 0;
my $num = 0;
my $b = 0;
open( D, "<", "tmp.out" ) || die "$!\n";
while (<D>)
{
my @err_info=();
$num++;
if (( $_ =~ /$a/i ) and ($_ !~ /HandleProxyTradeGroup/))
{
$b = $num ;
print "\$b is $b\n";
open( E, "<", "tmp.out" ) || die "$!\n";
while (<E>) {
$_ =~ s/'//g;
if (($. >= "$b") and ($. <= "$b" + 5) ){push( @err_info, $_ ) };};
close E;
chomp @err_info; print @err_info;
print "\n";
$dbh->do("INSERT INTO err_info(host_name,host_ip,v_date,info,env) values('$USER','$IP','$XDATE','@err_info','$ENV')") or die($DBI::errstr); };
};
close D;
};