← Index
Performance Profile   « block view • line view • sub view »
For ./awstats.pl
  Run on Wed Feb 11 19:11:27 2009
Reported on Thu Feb 12 02:07:42 2009

Fileplugins/geoip.pm
Statements Executed30
Total Time0.029753 seconds

Subroutines — ordered by exclusive time
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
1118.8e-50.00077main::Init_geoip
00000main::BEGIN
00000main::GetCountryCodeByAddr_geoip
00000main::GetCountryCodeByName_geoip
00000main::ShowInfoHost_geoip

LineStmts.Exclusive
Time
Avg.Code
1#!/usr/bin/perl
2#-----------------------------------------------------------------------------
3# GeoIp Maxmind AWStats plugin
4# This plugin allow you to get country report with countries detected
5# from a Geographical database (GeoIP internal database) instead of domain
6# hostname suffix.
7# Need the country database from Maxmind (free).
8#-----------------------------------------------------------------------------
9# Perl Required Modules: Geo::IP or Geo::IP::PurePerl
10#-----------------------------------------------------------------------------
11# $Revision: 1.24 $ - $Author: eldy $ - $Date: 2006/05/06 02:51:34 $
12
13
14# <-----
15# ENTER HERE THE USE COMMAND FOR ALL REQUIRED PERL MODULES
1630.000185.9e-5use vars qw/ $type /;
# spent 66µs making 1 call to vars::import
1713.0e-63.0e-6$type='geoip';
1810.027340.02734if (!eval ('require "Geo/IP.pm";')) {
19 $error1=$@;
20 $type='geoippureperl';
21 if (!eval ('require "Geo/IP/PurePerl.pm";')) {
22 $error2=$@;
23 $ret=($error1||$error2)?"Error:\n$error1$error2":"";
24 $ret.="Error: Need Perl module Geo::IP or Geo::IP::PurePerl";
25 return $ret;
26 }
27}
28# ----->
2960.000121.9e-5use strict;no strict "refs";
# spent 37µs making 1 call to strict::unimport # spent 19µs making 1 call to strict::import
30
31
32
33#-----------------------------------------------------------------------------
34# PLUGIN VARIABLES
35#-----------------------------------------------------------------------------
36# <-----
37# ENTER HERE THE MINIMUM AWSTATS VERSION REQUIRED BY YOUR PLUGIN
38# AND THE NAME OF ALL FUNCTIONS THE PLUGIN MANAGE.
3916.0e-66.0e-6my $PluginNeedAWStatsVersion="5.4";
4012.0e-62.0e-6my $PluginHooksFunctions="GetCountryCodeByAddr GetCountryCodeByName ShowInfoHost";
41# ----->
42
43# <-----
44# IF YOUR PLUGIN NEED GLOBAL VARIABLES, THEY MUST BE DECLARED HERE.
45use vars qw/
# spent 64µs making 1 call to vars::import
46%TmpDomainLookup
47$gi
4830.001390.00046/;
49# ----->
50
51
52#-----------------------------------------------------------------------------
53# PLUGIN FUNCTION: Init_pluginname
54#-----------------------------------------------------------------------------
55
# spent 774µs (88+686) within main::Init_geoip which was called # once (88µs+686µs) at line 1 of (eval 3)[./awstats.pl:1991] at line 1991 of awstats.pl
sub Init_geoip {
56130.000695.3e-5 my $InitParams=shift;
57 my $checkversion=&Check_Plugin_Version($PluginNeedAWStatsVersion);
# spent 51µs making 1 call to main::Check_Plugin_Version
58
59 # <-----
60 # ENTER HERE CODE TO DO INIT PLUGIN ACTIONS
61 debug(" Plugin geoip: InitParams=$InitParams",1);
# spent 20µs making 1 call to main::debug
62 my ($mode,$datafile)=split(/\s+/,$InitParams,2);
63 if (! $datafile) { $datafile="GeoIP.dat"; }
64 if ($type eq 'geoippureperl') {
65 if ($mode eq '' || $mode eq 'GEOIP_MEMORY_CACHE') { $mode=Geo::IP::PurePerl::GEOIP_MEMORY_CACHE(); }
66 else { $mode=Geo::IP::PurePerl::GEOIP_STANDARD(); }
67 } else {
68 if ($mode eq '' || $mode eq 'GEOIP_MEMORY_CACHE') { $mode=Geo::IP::GEOIP_MEMORY_CACHE(); }
69 else { $mode=Geo::IP::GEOIP_STANDARD(); }
# spent 13µs making 1 call to Geo::IP::GEOIP_STANDARD
70 }
71 %TmpDomainLookup=();
72 debug(" Plugin geoip: GeoIP initialized type=$type mode=$mode",1);
# spent 12µs making 1 call to main::debug
73 if ($type eq 'geoippureperl') {
74 $gi = Geo::IP::PurePerl->open($datafile, $mode);
75 } else {
76 $gi = Geo::IP->open($datafile, $mode);
# spent 590µs making 1 call to Geo::IP::open
77 }
78# Fails on some GeoIP version
79# debug(" Plugin geoip: GeoIP initialized database_info=".$gi->database_info());
80 # ----->
81
82 return ($checkversion?$checkversion:"$PluginHooksFunctions");
83}
84
85
86#-----------------------------------------------------------------------------
87# PLUGIN FUNCTION: GetCountryCodeByAddr_pluginname
88# UNIQUE: YES (Only one plugin using this function can be loaded)
89# GetCountryCodeByAddr is called to translate an ip into a country code in lower case.
90#-----------------------------------------------------------------------------
91sub GetCountryCodeByAddr_geoip {
92 my $param="$_[0]";
93 # <-----
94 my $res=$TmpDomainLookup{$param}||'';
95 if (! $res) {
96 $res=lc($gi->country_code_by_addr($param)) || 'unknown';
97 $TmpDomainLookup{$param}=$res;
98 if ($Debug) { debug(" Plugin geoip: GetCountryCodeByAddr for $param: [$res]",5); }
99 }
100 elsif ($Debug) { debug(" Plugin geoip: GetCountryCodeByAddr for $param: Already resolved to [$res]",5); }
101 # ----->
102 return $res;
103}
104
105
106#-----------------------------------------------------------------------------
107# PLUGIN FUNCTION: GetCountryCodeByName_pluginname
108# UNIQUE: YES (Only one plugin using this function can be loaded)
109# GetCountryCodeByName is called to translate a host name into a country code in lower case.
110#-----------------------------------------------------------------------------
111sub GetCountryCodeByName_geoip {
112 my $param="$_[0]";
113 # <-----
114 my $res=$TmpDomainLookup{$param}||'';
115 if (! $res) {
116 $res=lc($gi->country_code_by_name($param)) || 'unknown';
117 $TmpDomainLookup{$param}=$res;
118 if ($Debug) { debug(" Plugin geoip: GetCountryCodeByName for $param: [$res]",5); }
119 }
120 elsif ($Debug) { debug(" Plugin geoip: GetCountryCodeByName for $param: Already resolved to [$res]",5); }
121 # ----->
122 return $res;
123}
124
125
126#-----------------------------------------------------------------------------
127# PLUGIN FUNCTION: ShowInfoHost_pluginname
128# UNIQUE: NO (Several plugins using this function can be loaded)
129# Function called to add additionnal columns to the Hosts report.
130# This function is called when building rows of the report (One call for each
131# row). So it allows you to add a column in report, for example with code :
132# print "<TD>This is a new cell for $param</TD>";
133# Parameters: Host name or ip
134#-----------------------------------------------------------------------------
135sub ShowInfoHost_geoip {
136 my $param="$_[0]";
137 # <-----
138 if ($param eq '__title__') {
139 my $NewLinkParams=${QueryString};
140 $NewLinkParams =~ s/(^|&)update(=\w*|$)//i;
141 $NewLinkParams =~ s/(^|&)output(=\w*|$)//i;
142 $NewLinkParams =~ s/(^|&)staticlinks(=\w*|$)//i;
143 $NewLinkParams =~ s/(^|&)framename=[^&]*//i;
144 my $NewLinkTarget='';
145 if ($DetailedReportsOnNewWindows) { $NewLinkTarget=" target=\"awstatsbis\""; }
146 if (($FrameName eq 'mainleft' || $FrameName eq 'mainright') && $DetailedReportsOnNewWindows < 2) {
147 $NewLinkParams.="&framename=mainright";
148 $NewLinkTarget=" target=\"mainright\"";
149 }
150 $NewLinkParams =~ tr/&/&/s; $NewLinkParams =~ s/^&//; $NewLinkParams =~ s/&$//;
151 if ($NewLinkParams) { $NewLinkParams="${NewLinkParams}&"; }
152
153 print "<th width=\"80\">";
154 print "<a href=\"#countries\">GeoIP<br />Country</a>";
155 print "</th>";
156 }
157 elsif ($param) {
158 my $ip=0;
159 my $key;
160 if ($param =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) { # IPv4 address
161 $ip=4;
162 $key=$param;
163 }
164 elsif ($param =~ /^[0-9A-F]*:/i) { # IPv6 address
165 $ip=6;
166 $key=$param;
167 }
168 print "<td>";
169 if ($key && $ip==4) {
170 my $res=lc($gi->country_code_by_addr($param)) if $gi;
171 if ($Debug) { debug(" Plugin geoip: GetCountryByIp for $param: [$res]",5); }
172 if ($res) { print $DomainsHashIDLib{$res}?$DomainsHashIDLib{$res}:"<span style=\"color: #$color_other\">$Message[0]</span>"; }
173 else { print "<span style=\"color: #$color_other\">$Message[0]</span>"; }
174 }
175 if ($key && $ip==6) {
176 print "<span style=\"color: #$color_other\">$Message[0]</span>";
177 }
178 if (! $key) {
179 my $res=lc($gi->country_code_by_name($param)) if $gi;
180 if ($Debug) { debug(" Plugin geoip: GetCountryByHostname for $param: [$res]",5); }
181 if ($res) { print $DomainsHashIDLib{$res}?$DomainsHashIDLib{$res}:"<span style=\"color: #$color_other\">$Message[0]</span>"; }
182 else { print "<span style=\"color: #$color_other\">$Message[0]</span>"; }
183 }
184 print "</td>";
185 }
186 else {
187 print "<td>&nbsp;</td>";
188 }
189 return 1;
190 # ----->
191}
192
193
19412.5e-52.5e-51; # Do not remove this line