2 ################################################################################
4 # File: $RCSfile: httpdwho,v $
5 # Revision: $Revision: 1.2 $
6 # Description: Parse Apache access.log and produce a report on the locations
7 # of the visitors to the site
8 # Author: Andrew@DeFaria.com
9 # Created: Thu Dec 21 21:49:54 CST 2006
10 # Modified: $Date: 2010/06/08 15:03:27 $
11 # Dependencies: GEOLite
14 # This product includes GeoLite data created by MaxMind, available from
15 # http://www.maxmind.com
17 # (C) Copyright 2006, ClearSCM, Inc., all rights reserved.
19 ################################################################################
26 use lib "$FindBin::Bin/../lib";
31 use Geo::IP::PurePerl;
37 display "Usage: $FindBin::Script: [ -verbose | -v ] <filename>";
39 display " -verbose | -v\tTurn on verbose mode (Default: verbose off)";
40 display " <filename>\tIs the Apache formated access logfile";
49 verbose_nolf "Processing $filename";
51 foreach (ReadFile ($filename)) {
55 my @ipaddrs = gethostbyname $fields [0];
57 next if !@ipaddrs; # Skip errors
59 my ($a, $b, $c, $d) = unpack "C4", $ipaddrs [4];
60 my $ipaddr = "$a.$b.$c.$d";
62 debug "Host: ${fields [0]} IP: $ipaddr";
64 if ($ipaddrs {$ipaddr}) {
65 $ipaddrs {$ipaddr}[1]++;
68 $domain_info [0] = $ipaddrs [0];
70 $ipaddrs {$ipaddr} = \@domain_info;
74 verbose "\nFinished processing $filename";
78 my $logfile = "/var/log/httpd/access_log";
80 my $result = GetOptions (
81 "file=s" => \$logfile,
82 "usage" => sub { Usage },
83 "verbose" => sub { set_verbose },
84 "debug" => sub { set_debug },
85 ) or Usage "Invalid option specified";
87 # Instantiate a new Geo::IP object
88 my $gi = Geo::IP::PurePerl->new (
89 "/usr/local/share/GeoIP/GeoIPCity.dat",
96 error "Unable to open $logfile", 1 if !-f $logfile;
98 my %ip_records = GetIPs $logfile;
100 foreach (sort keys %ip_records) {
112 = $gi->get_city_record ($_);
114 my @domain_info = @{$ip_records {$_}};
117 display_nolf $city ? "$city\t" : "*Unknown*\t";
118 display_nolf $postal_code ? "$postal_code\t" : "*Unknown*\t";
119 display_nolf $country_name ? "$country_name\t" : "*Unknown*\t";
120 display $domain_info [0] . " (" . $domain_info [1] . ")";
121 # print $country_code . "\n";
122 # print $country_code3 . "\n";
123 # print $country_name . "\n";
124 # print $region . "\n";
125 # print $city . "\n";
126 # print $postal_code . "\n";
127 # print $latitude . "\n";
128 # print $longitude . "\n";
129 # print $dma_code . "\n";
130 # print $area_code . "\n";