用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - perl代码库

处理服务器日志

2012-10-15 作者: 神马举报

[perl]代码库

#-----------------------------
# download the following standalone program
#!/usr/bin/perl -w
# sumwww - summarize web server log activity

$lastdate = "";
daily_logs();
summary();
exit;

# read CLF files and tally hits from the host and to the URL
sub daily_logs
{
	while ( <> )
	{
		( $type, $what ) = /"(GET|POST)\s+(\S+?) \S+"/ or next;
		( $host, undef, undef, $datetime ) = split;
		( $bytes ) = /\s ( \d+ ) \s*$/ or next;
( $date )  = ( $datetime =~ /\[ ( [^:]* ) / );
		               $posts  += ( $type eq POST );
		               $home++ if m, / ,;
		               if ( $date ne $lastdate )
		{
			if ( $lastdate ) { write_report()     }
				else           { $lastdate = $date  }
			                             }
		                             $count++;
		$hosts {$host}++;
		$what {$what}++;
		$bytesum += $bytes;
	}
	write_report() if $count;
}

# use *typeglob aliasing of global variables for cheap copy
sub summary
{
$lastdate = "Grand Total";
*count   = *sumcount;
*bytesum = *bytesumsum;
*hosts   = *allhosts;
*posts   = *allposts;
*what    = *allwhat;
*home    = *allhome;
write;
}

# display the tallies of hosts and URLs, using formats
sub write_report
{
write;

# add to summary data
$lastdate    = $date;
$sumcount   += $count;
$bytesumsum += $bytesum;
$allposts   += $posts;
$allhome    += $home;

# reset daily data
$posts = $count = $bytesum = $home = 0;
@allwhat{keys %what}   = keys %what;
@allhosts{keys %hosts} = keys %hosts;
%hosts = %what = ();
}

format STDOUT_TOP =
    @|||||||||| @|||||| @||||||| @||||||| @|||||| @|||||| @|||||||||||||
    "Date",     "Hosts", "Accesses", "Unidocs", "POST", "Home", "Bytes"
    ----------- ------- -------- -------- ------- ------- --------------
    .

    format STDOUT =
        @>>>>>>>>>> @>>>>>> @>>>>>>> @>>>>>>> @>>>>>> @>>>>>> @>>>>>>>>>>>>>
        $lastdate,  scalar ( keys %hosts ),
        $count, scalar ( keys %what ),
        $posts,  $home,  $bytesum
        .

#-----------------------------
#     Date      Hosts  Accesses Unidocs   POST    Home       Bytes
#
# ----------- ------- -------- -------- ------- ------- --------------
#
# 19/May/1998     353     6447     3074     352      51       16058246
#
# 20/May/1998    1938    23868     4288     972     350       61879643
#
# 21/May/1998    1775    27872     6596    1064     376       64613798
#
# 22/May/1998    1680    21402     4467     735     285       52437374
#
# 23/May/1998    1128    21260     4944     592     186       55623059
#
# Grand Total    6050   100849    10090    3715    1248      250612120
#-----------------------------
# download the following standalone program
#!/usr/bin/perl -w
# aprept - report on Apache logs

        use Logfile::Apache;

$l = Logfile::Apache->new (
         File  => "-",                   # STDIN
         Group => [ Domain, File ] );

$l->report ( Group => Domain, Sort => Records );
$l->report ( Group => File,   List => [Bytes,Records] );

#-----------------------------
# Domain                  Records
#
# ===============================
#
# US Commercial        222 38.47%
#
# US Educational       115 19.93%
#
# Network               93 16.12%
#
# Unresolved            54  9.36%
#
# Australia             48  8.32%
#
# Canada                20  3.47%
#
# Mexico                 8  1.39%
#
# United Kingdom         6  1.04%
#
#
# File                               Bytes          Records
#
# =========================================================
#
# /                           13008  0.89%         6  1.04%
#
# /cgi-bin/MxScreen           11870  0.81%         2  0.35%
#
# /cgi-bin/pickcards          39431  2.70%        48  8.32%
#
# /deckmaster                143793  9.83%        21  3.64%
#
# /deckmaster/admin           54447  3.72%         3  0.52%
#-----------------------------


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...