]> git.ipfire.org Git - thirdparty/AWStats.git/commitdiff
New: When geoip_region_maxmind is enabled, it is also used for country chart.
authoreldy <>
Sun, 19 Mar 2006 23:14:38 +0000 (23:14 +0000)
committereldy <>
Sun, 19 Mar 2006 23:14:38 +0000 (23:14 +0000)
New: geoip_region_maxmind plugin can be used with Geo::IP::PurePerl.

wwwroot/cgi-bin/awstats.pl
wwwroot/cgi-bin/plugins/geoip.pm
wwwroot/cgi-bin/plugins/geoip_region_maxmind.pm

index 0206d2e70de1ce9afd5cf5cfbddde9f29d941213..9ecb43b03046254d65217c0e11d492538a598fea 100644 (file)
@@ -2010,7 +2010,7 @@ sub Read_Plugins {
                }
        }
        # In output mode, geo ip plugins are not loaded, so message changes are done here (can't be done in plugin init function)
-       if ($PluginsLoaded{'init'}{'geoip'} || $PluginsLoaded{'init'}{'geoipfree'}) { $Message[17]=$Message[25]=$Message[148]; }
+       if ($PluginsLoaded{'init'}{'geoip'} || $PluginsLoaded{'init'}{'geoip_region_maxmind'} || $PluginsLoaded{'init'}{'geoipfree'}) { $Message[17]=$Message[25]=$Message[148]; }
 }
 
 #------------------------------------------------------------------------------
@@ -6848,8 +6848,9 @@ if ($UpdateStats && $FrameName ne 'index' && $FrameName ne 'mainleft') {  # Updat
                        # $Host is an IP address and is not resolved (failed or not asked) or resolution gives an IP address
                        $HostResolved = $Host;
                        # Resolve Domain
-                       if ($PluginsLoaded{'GetCountryCodeByAddr'}{'geoipfree'}) { $Domain=GetCountryCodeByAddr_geoipfree($HostResolved); }
-                       elsif ($PluginsLoaded{'GetCountryCodeByAddr'}{'geoip'}) { $Domain=GetCountryCodeByAddr_geoip($HostResolved); }
+                       if ($PluginsLoaded{'GetCountryCodeByAddr'}{'geoip'})                   { $Domain=GetCountryCodeByAddr_geoip($HostResolved); }
+                       elsif ($PluginsLoaded{'GetCountryCodeByAddr'}{'geoip_region_maxmind'}) { $Domain=GetCountryCodeByAddr_geoip_region_maxmind($HostResolved); }
+                       elsif ($PluginsLoaded{'GetCountryCodeByAddr'}{'geoipfree'})            { $Domain=GetCountryCodeByAddr_geoipfree($HostResolved); }
             if ($AtLeastOneSectionPlugin) {
                        foreach my $pluginname (keys %{$PluginsLoaded{'SectionProcessIp'}})  {
 #                              my $function="SectionProcessIp_$pluginname(\$HostResolved)";
@@ -6864,8 +6865,9 @@ if ($UpdateStats && $FrameName ne 'index' && $FrameName ne 'mainleft') {  # Updat
                        $HostResolved = lc($HostResolved?$HostResolved:$Host);
                        # Resolve Domain
                        if ($ip) {  # If we have ip, we use it in priority instead of hostname
-                               if ($PluginsLoaded{'GetCountryCodeByAddr'}{'geoipfree'}) { $Domain=GetCountryCodeByAddr_geoipfree($Host); }
-                               elsif ($PluginsLoaded{'GetCountryCodeByAddr'}{'geoip'}) { $Domain=GetCountryCodeByAddr_geoip($Host); }
+                               if ($PluginsLoaded{'GetCountryCodeByAddr'}{'geoip'})                   { $Domain=GetCountryCodeByAddr_geoip($Host); }
+                               elsif ($PluginsLoaded{'GetCountryCodeByAddr'}{'geoip_region_maxmind'}) { $Domain=GetCountryCodeByAddr_geoip_region_maxmind($Host); }
+                               elsif ($PluginsLoaded{'GetCountryCodeByAddr'}{'geoipfree'})            { $Domain=GetCountryCodeByAddr_geoipfree($Host); }
                                elsif ($HostResolved =~ /\.(\w+)$/) { $Domain=$1; }
                 if ($AtLeastOneSectionPlugin) {
                        foreach my $pluginname (keys %{$PluginsLoaded{'SectionProcessIp'}})  {
@@ -6877,8 +6879,9 @@ if ($UpdateStats && $FrameName ne 'index' && $FrameName ne 'mainleft') {  # Updat
                 }
                        }
                        else {
-                               if ($PluginsLoaded{'GetCountryCodeByName'}{'geoipfree'}) { $Domain=GetCountryCodeByName_geoipfree($HostResolved); }
-                               elsif ($PluginsLoaded{'GetCountryCodeByName'}{'geoip'}) { $Domain=GetCountryCodeByName_geoip($HostResolved); }
+                               if ($PluginsLoaded{'GetCountryCodeByName'}{'geoip'})                   { $Domain=GetCountryCodeByName_geoip($HostResolved); }
+                               elsif ($PluginsLoaded{'GetCountryCodeByName'}{'geoip_region_maxmind'}) { $Domain=GetCountryCodeByName_geoip_region_maxmind($HostResolved); }
+                               elsif ($PluginsLoaded{'GetCountryCodeByName'}{'geoipfree'})            { $Domain=GetCountryCodeByName_geoipfree($HostResolved); }
                                elsif ($HostResolved =~ /\.(\w+)$/) { $Domain=$1; }
                 if ($AtLeastOneSectionPlugin) {
                        foreach my $pluginname (keys %{$PluginsLoaded{'SectionProcessHostname'}})  {
index 89d522864c222cb2a03c2e4abbcf28c8a32d3a3f..6dea75e0bf2da56e4fab8c9d91067639504ea3c9 100644 (file)
 # ENTER HERE THE USE COMMAND FOR ALL REQUIRED PERL MODULES
 use vars qw/ $type /;
 $type='geoip';
-if (!eval ('require "Geo/IP.pm";'))    {
+if (!eval ('require "Geo/IP.pm";')) {
+       $error1=$@;
        $type='geoippureperl';
-       if (!eval ('require "Geo/IP/PurePerl.pm";')) { return $@?"Error: $@":"Error: Need Perl module Geo::IP or Geo::IP::PurePerl"; }
+       if (!eval ('require "Geo/IP/PurePerl.pm";')) {
+               $error2=$@;
+               $ret=($error1||$error2)?"Error:\n$error1$error2":"";
+               $ret.="Error: Need Perl module Geo::IP or Geo::IP::PurePerl";
+               return $ret;
+       }
 }
 # ----->
 use strict;no strict "refs";
@@ -76,39 +82,40 @@ sub Init_geoip {
 
 
 #-----------------------------------------------------------------------------
-# PLUGIN FUNCTION: GetCountryCodeByName_pluginname
+# PLUGIN FUNCTION: GetCountryCodeByAddr_pluginname
 # UNIQUE: YES (Only one plugin using this function can be loaded)
-# GetCountryCodeByName is called to translate a host name into a country name.
+# GetCountryCodeByAddr is called to translate an ip into a country code in lower case.
 #-----------------------------------------------------------------------------
-sub GetCountryCodeByName_geoip {
+sub GetCountryCodeByAddr_geoip {
     my $param="$_[0]";
        # <-----
        my $res=$TmpDomainLookup{$param}||'';
        if (! $res) {
-               $res=lc($gi->country_code_by_name($param));
-               $TmpDomainLookup{$param}=$res || 'unknown';
-               if ($Debug) { debug("  Plugin geoip: GetCountryCodeByName for $param: [$res]",5); }
+               $res=lc($gi->country_code_by_addr($param)) || 'unknown';
+               $TmpDomainLookup{$param}=$res;
+               if ($Debug) { debug("  Plugin geoip: GetCountryCodeByAddr for $param: [$res]",5); }
        }
-       elsif ($Debug) { debug("  Plugin geoip: GetCountryCodeByName for $param: Already resolved to $res",5); }
+       elsif ($Debug) { debug("  Plugin geoip: GetCountryCodeByAddr for $param: Already resolved to [$res]",5); }
        # ----->
        return $res;
 }
 
+
 #-----------------------------------------------------------------------------
-# PLUGIN FUNCTION: GetCountryCodeByAddr_pluginname
+# PLUGIN FUNCTION: GetCountryCodeByName_pluginname
 # UNIQUE: YES (Only one plugin using this function can be loaded)
-# GetCountryCodeByAddr is called to translate an ip into a country name.
+# GetCountryCodeByName is called to translate a host name into a country code in lower case.
 #-----------------------------------------------------------------------------
-sub GetCountryCodeByAddr_geoip {
+sub GetCountryCodeByName_geoip {
     my $param="$_[0]";
        # <-----
        my $res=$TmpDomainLookup{$param}||'';
        if (! $res) {
-               $res=lc($gi->country_code_by_addr($param));
-               $TmpDomainLookup{$param}=$res || 'unknown';
-               if ($Debug) { debug("  Plugin geoip: GetCountryCodeByAddr for $param: $res",5); }
+               $res=lc($gi->country_code_by_name($param)) || 'unknown';
+               $TmpDomainLookup{$param}=$res;
+               if ($Debug) { debug("  Plugin geoip: GetCountryCodeByName for $param: [$res]",5); }
        }
-       elsif ($Debug) { debug("  Plugin geoip: GetCountryCodeByAddr for $param: Already resolved to $res",5); }
+       elsif ($Debug) { debug("  Plugin geoip: GetCountryCodeByName for $param: Already resolved to [$res]",5); }
        # ----->
        return $res;
 }
index 9c9ea804d34255fb8c29b2a87823543f5c132a7f..2f5619a804ee83c5da48ad2663deace7dedd6554 100755 (executable)
 
 # <-----
 # ENTER HERE THE USE COMMAND FOR ALL REQUIRED PERL MODULES
-if (!eval ('require "Geo/IP.pm";'))    {
-    return $@?"Error: $@":"Error: Need Perl module Geo::IP (Geo::IP::PurePerl is not yet supported)";
+use vars qw/ $type /;
+$type='geoip';
+if (!eval ('require "Geo/IP.pm";')) {
+       $error1=$@;
+       $type='geoippureperl';
+       if (!eval ('require "Geo/IP/PurePerl.pm";')) {
+               $error2=$@;
+               $ret=($error1||$error2)?"Error:\n$error1$error2":"";
+               $ret.="Error: Need Perl module Geo::IP or Geo::IP::PurePerl";
+               return $ret;
+       }
 }
 # ----->
 use strict;no strict "refs";
@@ -27,12 +36,13 @@ use strict;no strict "refs";
 # ENTER HERE THE MINIMUM AWSTATS VERSION REQUIRED BY YOUR PLUGIN
 # AND THE NAME OF ALL FUNCTIONS THE PLUGIN MANAGE.
 my $PluginNeedAWStatsVersion="6.2";
-my $PluginHooksFunctions="AddHTMLMenuLink AddHTMLGraph ShowInfoHost SectionInitHashArray SectionProcessIp SectionProcessHostname SectionReadHistory SectionWriteHistory";
+my $PluginHooksFunctions="GetCountryCodeByAddr GetCountryCodeByName AddHTMLMenuLink AddHTMLGraph ShowInfoHost SectionInitHashArray SectionProcessIp SectionProcessHostname SectionReadHistory SectionWriteHistory";
 # ----->
 
 # <-----
 # IF YOUR PLUGIN NEED GLOBAL VARIABLES, THEY MUST BE DECLARED HERE.
 use vars qw/
+%TmpDomainLookup
 $geoip_region_maxmind
 %_region_p
 %_region_h
@@ -139,15 +149,23 @@ sub Init_geoip_region_maxmind {
     
        # <-----
        # ENTER HERE CODE TO DO INIT PLUGIN ACTIONS
-       debug(" Plugin geoip_region_maxmind: InitParams=$InitParams",1);
-#    if ($UpdateStats) {
-       my ($mode,$datafile)=split(/\s+/,$InitParams,2);
-       if (! $datafile) { $datafile="GeoIPRegion.dat"; }
-       if ($mode eq '' || $mode eq 'GEOIP_MEMORY_CACHE')  { $mode=Geo::IP::GEOIP_MEMORY_CACHE(); }
-       else { $mode=Geo::IP::GEOIP_STANDARD(); }
-       debug(" Plugin geoip_region_maxmind: GeoIP initialized in mode $mode",1);
-        $geoip_region_maxmind = Geo::IP->open($datafile, $mode);
-#    }
+       debug(" Plugin geoip: InitParams=$InitParams",1);
+       my ($mode,$datafile)=split(/\s+/,$InitParams,2);
+       if (! $datafile) { $datafile="GeoIPRegion.dat"; }
+       if ($type eq 'geoippureperl') {
+               if ($mode eq '' || $mode eq 'GEOIP_MEMORY_CACHE')  { $mode=Geo::IP::PurePerl::GEOIP_MEMORY_CACHE(); }
+               else { $mode=Geo::IP::PurePerl::GEOIP_STANDARD(); }
+       } else {
+               if ($mode eq '' || $mode eq 'GEOIP_MEMORY_CACHE')  { $mode=Geo::IP::GEOIP_MEMORY_CACHE(); }
+               else { $mode=Geo::IP::GEOIP_STANDARD(); }
+       }
+       %TmpDomainLookup=();
+       debug(" Plugin geoip: GeoIP initialized in mode $type $mode",1);
+       if ($type eq 'geoippureperl') {
+               $geoip_region_maxmind = Geo::IP::PurePerl->open($datafile, $mode);
+       } else {
+               $geoip_region_maxmind = Geo::IP->open($datafile, $mode);
+       }
        # ----->
 
        return ($checkversion?$checkversion:"$PluginHooksFunctions");
@@ -269,6 +287,50 @@ sub AddHTMLGraph_geoip_region_maxmind {
 }
 
 
+#-----------------------------------------------------------------------------
+# PLUGIN FUNCTION: GetCountryCodeByAddr_pluginname
+# UNIQUE: YES (Only one plugin using this function can be loaded)
+# GetCountryCodeByAddr is called to translate an ip into a country code in lower case.
+#-----------------------------------------------------------------------------
+sub GetCountryCodeByAddr_geoip_region_maxmind {
+    my $param="$_[0]";
+       # <-----
+       my $res=$TmpDomainLookup{$param}||'';
+       if (! $res) {
+       my ($res1,$res2,$countryregion)=();
+       ($res1,$res2)=$geoip_region_maxmind->region_by_name($param) if $geoip_region_maxmind;
+       $res=lc($res1) || 'unknown';
+               $TmpDomainLookup{$param}=$res;
+       if ($Debug) { debug("  Plugin geoip_region_maxmind: GetCountryCodeByAddr for $param: [$res]",5); }
+       }
+       elsif ($Debug) { debug("  Plugin geoip_region_maxmind: GetCountryCodeByAddr for $param: Already resolved to [$res]",5); }
+       # ----->
+       return $res;
+}
+
+
+#-----------------------------------------------------------------------------
+# PLUGIN FUNCTION: GetCountryCodeByName_pluginname
+# UNIQUE: YES (Only one plugin using this function can be loaded)
+# GetCountryCodeByName is called to translate a host name into a country code in lower case.
+#-----------------------------------------------------------------------------
+sub GetCountryCodeByName_geoip_region_maxmind {
+    my $param="$_[0]";
+       # <-----
+       my $res=$TmpDomainLookup{$param}||'';
+       if (! $res) {
+       my ($res1,$res2,$countryregion)=();
+       ($res1,$res2)=$geoip_region_maxmind->region_by_name($param) if $geoip_region_maxmind;
+       $res=lc($res1) || 'unknown';
+               $TmpDomainLookup{$param}=$res;
+       if ($Debug) { debug("  Plugin geoip_region_maxmind: GetCountryCodeByName for $param: [$res]",5); }
+       }
+       elsif ($Debug) { debug("  Plugin geoip_region_maxmind: GetCountryCodeByName for $param: Already resolved to [$res]",5); }
+       # ----->
+       return $res;
+}
+
+
 #-----------------------------------------------------------------------------
 # PLUGIN FUNCTION: ShowInfoHost_pluginname
 # UNIQUE: NO (Several plugins using this function can be loaded)
@@ -329,6 +391,7 @@ sub ShowInfoHost_geoip_region_maxmind {
                 }
             }
             else {
+               # Show region
                 if ($res1 =~ /\w\w/ && $res2 =~ /\w\w/) {
                     print $region{lc($res1)}{uc($res2)};
                 }
@@ -354,6 +417,7 @@ sub ShowInfoHost_geoip_region_maxmind {
                 }
             }
             else {
+                # Show region
                 if ($res1 =~ /\w\w/ && $res2 =~ /\w\w/) {
                     print $region{lc($res1)}{uc($res2)};
                 }