免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2057 | 回复: 2
打印 上一主题 下一主题

第一次写perl [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-06 18:53 |只看该作者 |倒序浏览
要求:
2台web机器日志/var/log/http120080102-access.log /var/log/http220080102-accesslog
有文件/etc/test.conf 有一行 LogFile="abcdefghijklmn20080304"
取得日期$awsday = 04
如果脚本执行日期 $myday 大于 $awsday. 则
去掉两台web机器log文件里有互相访问的记录. (去掉含有 "1.2.3.4 4.3.2.1"或"4.3.2.1 1.2.3.4"的行)
然后把剩下记录写入httplog(/var/log/http_log20080*0*)
第一次写 肯定很烂
有没有高手愿意帮小弟简化或重写下  3q
  1. #!/usr/bin/perl

  2. open (myfile,"/etc/awstats/awstats.test.conf");

  3. ($sec,$min,$hr,$day,$mon,$year)=localtime();
  4. $mon=1+$mon;
  5. if($day =~ /[0-9]{1}/)
  6. {
  7.   $myday="0".$day;
  8. }

  9. if( $mon =~ /[0-9]{1}/ )
  10. {
  11.   $mymon="0".$mon;
  12. }

  13. $myyear=1900+$year;
  14. while($awsday=<myfile>)
  15. {
  16.   if ($awsday =~ s/(^LogFile.*)([0-9]{6})([0-9]{2})(\")/$3/sg)
  17.   {
  18.     $nextday=$awsday+1;
  19.     if ( $nextday =~ /[0-9]{1} )
  20.     {
  21.       $nextday="0".$nextday;
  22.     }
  23.     while ( $myday >= $nextday )
  24.     {
  25.       if ( !-e "/var/log/http1$myyear-$mymon-$nextday-access.log" )
  26.       {
  27.         print "log file http1$myyear-$mymon-$nextday-access.log not exist";
  28.         exit;
  29.       }

  30.       if ( !-e "/var/log/http2$myyear-$mymon-$nextday-access.log" )
  31.       {
  32.         print "log file http2$myyear-$mymon-$nextday-access.log not exist";
  33.         exit;
  34.       }
  35.       open (1file,"/var/log/http1$myyear-$mymon-$nextday-access.log ");
  36.       open (2file,"/var/log/http2$myyear-$mymon-$nextday-access.log ");
  37.       open (httplog,"<</var/log/http_log$myyear$mymon$nextday"
  38.       while( $file1=<1file> )
  39.       {
  40.         if ( !$file1 =~ /1.2.3.4 4.3.2.1/ && !$file1 =~ /4.3.2.1 1.2.3.4/ )
  41.         {
  42.           print httplog $file1;
  43.         }
  44.       }
  45.       while( $file2=<2file> )
  46.       {
  47.         if ( !$file2 =~ /1.2.3.4 4.3.2.1/ && !$file2 =~ /4.3.2.1 1.2.3.4/ )
  48.         {
  49.           print httplog $file2;
  50.         }
  51.       }
  52.       close 1file;
  53.       close 2file;
  54.       close httplog;
  55.     }
  56.   }
  57.   exit;
  58. }
  59. close myfile;
复制代码

[ 本帖最后由 root_man 于 2008-5-6 18:55 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-05-06 20:14 |只看该作者
不知道这样是不是符合你的要求
#!/usr/bin/perl


use strict;
use warnings;

my $conf_file = '/etc/awstats/awstats.test.conf';

open CONF, "<$conf_file"
&nbsp;&nbsp;&nbsp;&nbsp;or die "Can not open $conf_file: $!\n";

my @time = localtime;

my $day  = $time[3] + 1;
my $mon  = $time[4] + 1;
my $year = $time[5] + 1900;

while (<CONF>) {
&nbsp;&nbsp;&nbsp;&nbsp;chomp;
&nbsp;&nbsp;&nbsp;&nbsp;my $awsday = $1 if /^LogFile\D*\d{6}(\d+)/;
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;if ($day >= $awsday) {
&nbsp;&nbsp;&nbsp;&nbsp;    my $log_file1 = sprintf "%s%d%02d%02d%s", "/var/log/http1", $year, $mon, $awsday, "-access.log";
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my $log_file2 = sprintf "%s%d%02d%02d%s", "/var/log/http2", $year, $mon, $awsday, "-access.log";
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my $httplog = sprintf "%s%d%02d%02d", "/var/log/http_log", $year, $mon, $awsday;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;open LOG1, "<$log_file1"
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    or die "Can not open $log_file1: $!\n";
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;open LOG2, "<$log_file2"
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    or die "Can not open $log_file2: $!\n";
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;open HTTP_LOG, ">>$httplog"
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    or die "Can not open $httplog: $!\n";

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while (<LOG1>) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chomp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    next if /1.2.3.4 4.3.2.1/;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next if /4.3.2.1 1.2.3.4/;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print HTTP_LOG;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while (<LOG2>) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chomp
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    next if /1.2.3.4 4.3.2.1/;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next if /4.3.2.1 1.2.3.4/;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print HTTP_LOG;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
}


[ 本帖最后由 cobrawgl 于 2008-5-6 20:16 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2008-05-06 22:55 |只看该作者
good
thanks
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP