• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

扫描apache日志,寻找type==1的日志,写到数据库里.

Mysql 水墨上仙 1427次浏览

扫描apache日志,寻找type==1的日志,写到数据库里.


	#! /usr/bin/perl -w
	use DBI;
	use Benchmark;
	$apache_log_file_path="/usr/local/apache/logs/access_log";
	my $WARN_SCAN_TIME="/usr/local/apache/logs/warn_scan_time";
	$date_pattern="\\d+\\/\\w+\\/\\d+\:\\d+\:\\d+\:\\d+";
	if(open(WARN_TIME,$WARN_SCAN_TIME)){
	print "open warn_sacn_time file success\n";
	}else{
	die "can't open warn_sacn_time file";
	}
	$warn_line=<WARN_TIME>;
	$warn_line=~ qr/$date_pattern/;
	my $time_string=$&;
	
	if(open(APACHE_LOG_FILE,$apache_log_file_path)){
	print "open apache log sucess";
	}else{
	die "can't open apache_log_file $!\n";
	}
	$date_time;
	$time_flag=1;
	my $startime = new Benchmark;
	my $type;
	$pattern="([\\w+||_]+=\\w+)([&]?[\\w+||_]+=[\\w||\\[||\\]||\\%||\\\\||_||\"||\:||=||-]+)*";
	my $dbh = DBI->connect("DBI:mysql:database=botest:host=192.168.65.128","root","",{"RaiseError" => 1});
	my $sqr;
	my $sqr_query;
	LINE: while($line=<APACHE_LOG_FILE>){
	if($line=~ qr/$date_pattern/){
	$date_time=$&;
	}
	if($time_flag==1){
	if($date_time eq $time_string){
	$time_flag=0;
	next LINE if $time_flag==0;
	}
	}
	if($time_flag==0){
	if($line=~ qr/type=([0||1||2||3])/) {
	$type=$1;
	}
	next LINE if $type!=1;
	$taskId;
	if($line=~ qr/task_id=([0-9]+)/){
	$taskId=$1;
	}
	$sqr_query=$dbh->prepare("select * from warn_set where task_id=?");
	$sqr_query->execute($taskId);
	($id,$warn_method,$interval_time,$warn_total_times,$task_id) = $sqr_query->fetchrow_array;
	$sqr_query->finish();
	if($line=~ qr/$pattern/){
	$temp_line=$&;
	$temp_line =~ s/%20/ /g;
	my @array=(split('&',$temp_line));
	my $temp;
	my @sql_field_value_array;
	my $sql_field="(warn_method,last_warn_time,interval_time,warn_total_times,warn_times,";
	my $sql_value="('$warn_method.',CURRENT_TIMESTAMP,'$interval_time','$warn_total_times','0',";
	my $flage=1;
	my $sql="insert into ";
	foreach $temp (@array){
	@sql_field_value_array=(split('=',$temp));
	if($flage==0){
	$sql_field.=",".$sql_field_value_array[0];
	$sql_value.=","."'$sql_field_value_array[1]'";
	}else{
	$sql_field.=$sql_field_value_array[0];
	$sql_value.="'".$sql_field_value_array[1]."'";
	$flage=0;
	}
	}
	$sql_field.=",status)";
	$sql_value.=",'1')";
	$sql.="warn $sql_field values $sql_value";
	$sqr=$dbh->prepare($sql);
	$sqr->execute();
	}
	}
	}
	open(OUTFILE, ">$WARN_SCAN_TIME") || die("Cannot open files\n");
	print OUTFILE ($date_time);
	printf "** %s\n\n", timestr(timediff(new Benchmark, $startime));
	$dbh->disconnect();


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明扫描apache日志,寻找type==1的日志,写到数据库里.
喜欢 (0)
加载中……