| File | plugins/geoip.pm | Statements Executed | 30 | Total Time | 0.029753 seconds |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine | |
|---|---|---|---|---|---|---|
| 1 | 1 | 1 | 8.8e-5 | 0.00077 | main:: | Init_geoip |
| 0 | 0 | 0 | 0 | 0 | main:: | BEGIN |
| 0 | 0 | 0 | 0 | 0 | main:: | GetCountryCodeByAddr_geoip |
| 0 | 0 | 0 | 0 | 0 | main:: | GetCountryCodeByName_geoip |
| 0 | 0 | 0 | 0 | 0 | main:: | ShowInfoHost_geoip |
| Line | Stmts. | 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 | |||
| 16 | 3 | 0.00018 | 5.9e-5 | use vars qw/ $type /; # spent 66µs making 1 call to vars::import |
| 17 | 1 | 3.0e-6 | 3.0e-6 | $type='geoip'; |
| 18 | 1 | 0.02734 | 0.02734 | if (!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 | # -----> | |||
| 29 | 6 | 0.00012 | 1.9e-5 | use 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. | |||
| 39 | 1 | 6.0e-6 | 6.0e-6 | my $PluginNeedAWStatsVersion="5.4"; |
| 40 | 1 | 2.0e-6 | 2.0e-6 | my $PluginHooksFunctions="GetCountryCodeByAddr GetCountryCodeByName ShowInfoHost"; |
| 41 | # -----> | |||
| 42 | ||||
| 43 | # <----- | |||
| 44 | # IF YOUR PLUGIN NEED GLOBAL VARIABLES, THEY MUST BE DECLARED HERE. | |||
| 45 | 1 | 1.2e-5 | 1.2e-5 | use vars qw/ # spent 64µs making 1 call to vars::import |
| 46 | %TmpDomainLookup | |||
| 47 | $gi | |||
| 48 | 2 | 0.00138 | 0.00069 | /; |
| 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 | |||
| 56 | 1 | 4.0e-6 | 4.0e-6 | my $InitParams=shift; |
| 57 | 1 | 1.5e-5 | 1.5e-5 | 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 | 1 | 1.4e-5 | 1.4e-5 | debug(" Plugin geoip: InitParams=$InitParams",1); # spent 20µs making 1 call to main::debug |
| 62 | 1 | 6.0e-6 | 6.0e-6 | my ($mode,$datafile)=split(/\s+/,$InitParams,2); |
| 63 | 1 | 2.0e-6 | 2.0e-6 | if (! $datafile) { $datafile="GeoIP.dat"; } |
| 64 | 1 | 3.0e-6 | 3.0e-6 | 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 | 1 | 3.0e-6 | 3.0e-6 | if ($mode eq '' || $mode eq 'GEOIP_MEMORY_CACHE') { $mode=Geo::IP::GEOIP_MEMORY_CACHE(); } |
| 69 | 1 | 1.7e-5 | 1.7e-5 | else { $mode=Geo::IP::GEOIP_STANDARD(); } # spent 13µs making 1 call to Geo::IP::GEOIP_STANDARD |
| 70 | } | |||
| 71 | 1 | 3.0e-6 | 3.0e-6 | %TmpDomainLookup=(); |
| 72 | 1 | 1.3e-5 | 1.3e-5 | debug(" Plugin geoip: GeoIP initialized type=$type mode=$mode",1); # spent 12µs making 1 call to main::debug |
| 73 | 1 | 3.0e-6 | 3.0e-6 | if ($type eq 'geoippureperl') { |
| 74 | $gi = Geo::IP::PurePerl->open($datafile, $mode); | |||
| 75 | } else { | |||
| 76 | 1 | 0.00060 | 0.00060 | $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 | 1 | 9.0e-6 | 9.0e-6 | 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 | #----------------------------------------------------------------------------- | |||
| 91 | sub 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 | #----------------------------------------------------------------------------- | |||
| 111 | sub 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 | #----------------------------------------------------------------------------- | |||
| 135 | sub 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> </td>"; | |||
| 188 | } | |||
| 189 | return 1; | |||
| 190 | # -----> | |||
| 191 | } | |||
| 192 | ||||
| 193 | ||||
| 194 | 1 | 2.5e-5 | 2.5e-5 | 1; # Do not remove this line |