簡易ログ解析スクリプト(携帯)

携帯端末からのアクセスを集計する。

使い方

 ./this-script.pl /var/www/logs/access_log.200908* > 2009-08.csv
 

スクリプト

 #!/usr/local/bin/perl
 
 use strict;
 use warnings;
 use DateTime::Format::HTTP;
 use Data::Dumper;
 
 my %agent = (
     'SB'     => [ 'J-PHONE', 'Vodafone', 'SoftBank', 'MOT-C', 'MOT-V' ],
     'DOCOMO' => [ 'DoCoMo' ],
     'AU'     => [ 'KDDI-', 'UP\.Browser', ],
 );
 my @files = @ARGV;
 my %data;
 foreach my $file (@files) {
     open my $fh, '<', $file or die "$!: $file";
     while ( my $line = <$fh> ) {
         chomp $line;
         my ( $host, $ident, $user, $time, $request, $status, $bytes, $referer, $agent ) =
           ( $line =~ /^([^ ]*) ([^ ]*) ([^ ]*) \[([^]]*)\] "(.*?)" ([^ ]*) ([^ ]*) "(.*?)" "(.*?)"/ );
         my $dt = DateTime::Format::HTTP->parse_datetime( $time, 'Asia/Tokyo' );
         next unless $request =~ m#^(GET|POST) /index.html#;
         unless ( exists $data{ $dt->ymd } ) {
             $data{ $dt->ymd }->{$_} = 0 for qw/SB DOCOMO AU ALL/;
         }
         my $hit;
         if ( grep { $agent =~ /^$_/ } @{ $agent{SB} } ) {
             $data{ $dt->ymd }->{SB} = $data{ $dt->ymd }->{SB} + 1;
             $hit = 1;
         }
         elsif ( grep { $agent =~ /^$_/ } @{ $agent{DOCOMO} } ) {
             $data{ $dt->ymd }->{DOCOMO} = $data{ $dt->ymd }->{DOCOMO} + 1;
             $hit = 1;
         }
         elsif ( grep { $agent =~ /^$_/ } @{ $agent{AU} } ) {
             $data{ $dt->ymd }->{AU} = $data{ $dt->ymd }->{AU} + 1;
             $hit = 1;
         }
         if ($hit) {
             $data{ $dt->ymd }->{ALL} = $data{ $dt->ymd }->{ALL} + 1;
         }
     }
     close $fh;
 }
 #print Dumper \%data;
 print "date,DOCOMO,AU,SB,ALL\n";
 foreach my $ymd ( sort keys %data ) {
     printf "%s,%d,%d,%d,%d\n", $ymd, $data{$ymd}->{DOCOMO}, $data{$ymd}->{AU}, $data{$ymd}->{SB}, $data{$ymd}->{ALL};
 }

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS