#!/usr/bin/perl # name: logcalc.pl # calc the amount of access url use strict; use warnings; use diagnostics; use File::Basename; my ($arg, $arg_only); my $FILE; my ($year, $month, $day, $mon); my ($time, $method, $url); my ($amount, $hour, $key, $value); my (%part, %hash); my @array; $arg = shift @ARGV; $arg_only = basename($arg); @array = split /_/, $arg_only; $array[1] =~ /^(\d{4})(\d{2})(\d{2})$/; # print $&; $year = $1; $month = $2; $day = $3; %hash = ( "01" => "Jan", "02" => "Feb", "03" => "Mar", "04" => "Apr", "05" => "May", "06" => "Jun", "07" => "Jul", "08" => "Aug", "09" => "Sep", "10" => "Oct", "11" => "Nov", "12" => "Dec", ); $mon = $hash{$month}; open $FILE, "<$arg" || die "can not open the log file\n$!"; while (<$FILE>) { chomp; my(undef, undef, $time, undef, undef, $method, $url, undef) = split /\s+/; # print "$time, $method, $url\n"; if($time =~ m/$day\/$mon\/$year:(..)/){ $hour=$1; if($method=~/GET/&&$url=~/^\/p\//){ $amount++; $part{$hour}++; } } } printf("Total amount is %d\n", $amount); =pod while(($key, $value) = each %part){ printf("The amount of %sh is %d\n", $key, $value); } =cut for $key (sort keys %part){ printf("The amount of %sh is %d\n", $key, $part{$key}); }
Perl统计nginx日志信息并分析每个小时的数据
时间: 2024-11-05 19:32:48