From: eldy <> Date: Thu, 23 Mar 2006 12:36:32 +0000 (+0000) Subject: Now geoip plugins works both with GeoIP C API or GeoIP PurePerl API. X-Git-Tag: AWSTATS_6_6_BETA~60 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=74808cebe63e758236e684ce5125d20ea98cdc6b;p=thirdparty%2FAWStats.git Now geoip plugins works both with GeoIP C API or GeoIP PurePerl API. --- diff --git a/wwwroot/cgi-bin/plugins/geoip.pm b/wwwroot/cgi-bin/plugins/geoip.pm index 6dea75e0..497dac81 100644 --- a/wwwroot/cgi-bin/plugins/geoip.pm +++ b/wwwroot/cgi-bin/plugins/geoip.pm @@ -69,12 +69,13 @@ sub Init_geoip { else { $mode=Geo::IP::GEOIP_STANDARD(); } } %TmpDomainLookup=(); - debug(" Plugin geoip: GeoIP initialized in mode $type $mode",1); + debug(" Plugin geoip: GeoIP initialized type=$type mode=$mode",1); if ($type eq 'geoippureperl') { $gi = Geo::IP::PurePerl->open($datafile, $mode); } else { $gi = Geo::IP->open($datafile, $mode); } + debug(" Plugin geoip: GeoIP initialized database_info=".$gi->database_info()); # -----> return ($checkversion?$checkversion:"$PluginHooksFunctions"); diff --git a/wwwroot/cgi-bin/plugins/geoip_city_maxmind.pm b/wwwroot/cgi-bin/plugins/geoip_city_maxmind.pm index 50e9a7da..051c6680 100755 --- a/wwwroot/cgi-bin/plugins/geoip_city_maxmind.pm +++ b/wwwroot/cgi-bin/plugins/geoip_city_maxmind.pm @@ -4,15 +4,24 @@ # This plugin allow you to add a city report. # Need the licensed city database from Maxmind. #----------------------------------------------------------------------------- -# Perl Required Modules: Geo::IP (Geo::IP::PurePerl is not yet supported) +# Perl Required Modules: Geo::IP or Geo::IP::PurePerl #----------------------------------------------------------------------------- # $Revision$ - $Author$ - $Date$ # <----- # 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"; @@ -25,13 +34,14 @@ 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 $PluginNeedAWStatsVersion="6.5"; my $PluginHooksFunctions="AddHTMLMenuLink AddHTMLGraph ShowInfoHost SectionInitHashArray SectionProcessIp SectionProcessHostname SectionReadHistory SectionWriteHistory"; # -----> # <----- # IF YOUR PLUGIN NEED GLOBAL VARIABLES, THEY MUST BE DECLARED HERE. use vars qw/ +%TmpDomainLookup $geoip_city_maxmind %_city_p %_city_h @@ -4263,14 +4273,23 @@ sub Init_geoip_city_maxmind { # <----- # ENTER HERE CODE TO DO INIT PLUGIN ACTIONS debug(" Plugin geoip_city_maxmind: InitParams=$InitParams",1); -# if ($UpdateStats) { - my ($mode,$datafile)=split(/\s+/,$InitParams,2); - if (! $datafile) { $datafile="GeoIPCity.dat"; } - if ($mode eq '' || $mode eq 'GEOIP_MEMORY_CACHE') { $mode=Geo::IP::GEOIP_MEMORY_CACHE(); } - else { $mode=Geo::IP::GEOIP_STANDARD(); } - debug(" Plugin geoip_city_maxmind: GeoIP initialized in mode $mode",1); - $geoip_city_maxmind = Geo::IP->open($datafile, $mode); -# } + my ($mode,$datafile)=split(/\s+/,$InitParams,2); + if (! $datafile) { $datafile="GeoIPCity.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_city_maxmind: GeoIP initialized type=$type mode=$mode",1); + if ($type eq 'geoippureperl') { + $geoip_city_maxmind = Geo::IP::PurePerl->open($datafile, $mode); + } else { + $geoip_city_maxmind = Geo::IP->open($datafile, $mode); + } + debug(" Plugin geoip_city_maxmind: GeoIP initialized database_info=".$gi->database_info()); # -----> return ($checkversion?$checkversion:"$PluginHooksFunctions"); @@ -4402,6 +4421,86 @@ sub AddHTMLGraph_geoip_city_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. +#----------------------------------------------------------------------------- +# Rem: not used +sub GetCountryCodeByAddr_geoip_city_maxmind { + my $param="$_[0]"; + # <----- + my $res=$TmpDomainLookup{$param}||''; + if ($type eq 'geoippureperl') { + if (! $res) { + my @record = (); + @record=$geoip_city_maxmind->get_city_record($param) if $geoip_city_maxmind; + my $country; + $country=$record[0] if @record; + $res=lc($country) || '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); } + } + else + { + if (! $res) { + my $record=(); + $record=$geoip_city_maxmind->record_by_addr($param) if $geoip_city_maxmind; + my $country; + $country=$record->country if $record; + $res=lc($country) || '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. +#----------------------------------------------------------------------------- +# Rem: not used +sub GetCountryCodeByName_geoip_city_maxmind { + my $param="$_[0]"; + # <----- + my $res=$TmpDomainLookup{$param}||''; + if ($type eq 'geoippureperl') { + if (! $res) { + my @record = (); + @record=$geoip_city_maxmind->get_city_record($param) if $geoip_city_maxmind; + my $country; + $country=$record[0] if @record; + $res=lc($country) || '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); } + } + else + { + if (! $res) { + my $record=(); + $record=$geoip_city_maxmind->record_by_name($param) if $geoip_city_maxmind; + my $country; + $country=$record->country if $record; + $res=lc($country) || '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) @@ -4430,6 +4529,9 @@ sub ShowInfoHost_geoip_city_maxmind { $NewLinkParams =~ s/^&//; $NewLinkParams =~ s/&$//; if ($NewLinkParams) { $NewLinkParams="${NewLinkParams}&"; } +# print ""; +# print "GeoIP
Country
"; +# print ""; print ""; print "GeoIP
City
"; print ""; @@ -4445,32 +4547,74 @@ sub ShowInfoHost_geoip_city_maxmind { $ip=6; $key=$param; } - print ""; if ($key && $ip==4) { - my $record=(); - $record=$geoip_city_maxmind->record_by_addr($param) if $geoip_city_maxmind; - if ($Debug) { debug(" Plugin geoip_city_maxmind: GetCityByIp for $param: [$record]",5); } - my $city; - $city=$record->city if $record; + my $country; + my $city; + if ($type eq 'geoippureperl') + { + my @record = (); + @record=$geoip_city_maxmind->get_city_record($param) if $geoip_city_maxmind; + if ($Debug) { debug(" Plugin geoip_city_maxmind: GetCityByIp for $param: [@record]",5); } + $country=$record[0] if @record; + $city=$record[4] if @record; + } + else + { + my $record=(); + $record=$geoip_city_maxmind->record_by_addr($param) if $geoip_city_maxmind; + if ($Debug) { debug(" Plugin geoip_city_maxmind: GetCityByIp for $param: [$record]",5); } + $country=$record->country_code if $record; + $city=$record->city if $record; + } +# print ""; +# if ($country) { print $DomainsHashIDLib{$country}?$DomainsHashIDLib{$country}:"$Message[0]"; } +# else { print "$Message[0]"; } +# print ""; + print ""; if ($city) { print "$city"; } else { print "$Message[0]"; } + print ""; } if ($key && $ip==6) { +# print ""; +# print "$Message[0]"; +# print ""; + print ""; print "$Message[0]"; + print ""; } if (! $key) { - my $record=(); - $record=$geoip_city_maxmind->record_by_name($param) if $geoip_city_maxmind; - if ($Debug) { debug(" Plugin geoip_city_maxmind: GetCityByHostname for $param: [$record]",5); } - my $city; - $city=$record->city if $record; + my $country; + my $city; + if ($type eq 'geoippureperl') + { + my @record = (); + @record=$geoip_city_maxmind->get_city_record($param) if $geoip_city_maxmind; + if ($Debug) { debug(" Plugin geoip_city_maxmind: GetCityByHostname for $param: [@record]",5); } + $country=$record[0] if @record; + $city=$record[4] if @record; + } + else + { + my $record=(); + $record=$geoip_city_maxmind->record_by_name($param) if $geoip_city_maxmind; + if ($Debug) { debug(" Plugin geoip_city_maxmind: GetCityByHostname for $param: [$record]",5); } + $country=$record->country_code if $record; + $city=$record->city if $record; + } + print ""; + if ($country) { print $DomainsHashIDLib{$country}?$DomainsHashIDLib{$country}:"$Message[0]"; } + else { print "$Message[0]"; } + print ""; + print ""; if ($city) { print "$city"; } else { print "$Message[0]"; } + print ""; } - print ""; } else { print " "; + print " "; } return 1; # -----> @@ -4498,24 +4642,45 @@ sub SectionInitHashArray_geoip_city_maxmind { sub SectionProcessIp_geoip_city_maxmind { my $param="$_[0]"; # Param must be an IP # <----- - my $record=(); - $record=$geoip_city_maxmind->record_by_addr($param) if $geoip_city_maxmind; - if ($Debug) { debug(" Plugin geoip_city_maxmind: GetCityByIp for $param: [$record]",5); } - if ($record) { - my $city=$record->city; -# if ($PageBool) { $_city_p{$city}++; } - if ($city) { - my $countrycity=($record->country_code).'_'.$city; - $countrycity=~s/ /%20/g; - if ($record->region) { $countrycity.='_'.$record->region; } - $_city_h{lc($countrycity)}++; - } else { - $_city_h{'unknown'}++; - } -# if ($timerecord > $_city_l{$city}) { $_city_l{$city}=$timerecord; } - } else { - $_city_h{'unknown'}++; - } + if ($type eq 'geoippureperl') { + my @record = (); + @record=$geoip_city_maxmind->get_city_record($param) if $geoip_city_maxmind; + if ($Debug) { debug(" Plugin geoip_city_maxmind: GetCityByName for $param: [@record]",5); } + if (@record) { + my $city=$record[4]; + if ($city) { + my $countrycity=$record[0].'_'.$city; + $countrycity=~s/ /%20/g; + if ($record[3]) { $countrycity.='_'.$record[3]; } + $_city_h{lc($countrycity)}++; + } else { + $_city_h{'unknown'}++; + } + } else { + $_city_h{'unknown'}++; + } + } + else + { + my $record=(); + $record=$geoip_city_maxmind->record_by_addr($param) if $geoip_city_maxmind; + if ($Debug) { debug(" Plugin geoip_city_maxmind: GetCityByIp for $param: [$record]",5); } + if ($record) { + my $city=$record->city; + # if ($PageBool) { $_city_p{$city}++; } + if ($city) { + my $countrycity=($record->country_code).'_'.$city; + $countrycity=~s/ /%20/g; + if ($record->region) { $countrycity.='_'.$record->region; } + $_city_h{lc($countrycity)}++; + } else { + $_city_h{'unknown'}++; + } + # if ($timerecord > $_city_l{$city}) { $_city_l{$city}=$timerecord; } + } else { + $_city_h{'unknown'}++; + } + } # -----> return; } @@ -4528,24 +4693,45 @@ sub SectionProcessIp_geoip_city_maxmind { sub SectionProcessHostname_geoip_city_maxmind { my $param="$_[0]"; # Param must be an IP # <----- - my $record=(); - $record=$geoip_city_maxmind->record_by_name($param) if $geoip_city_maxmind; - if ($Debug) { debug(" Plugin geoip_city_maxmind: GetCityByName for $param: [$record]",5); } - if ($record) { - my $city=$record->city; -# if ($PageBool) { $_city_p{$city}++; } - if ($city) { - my $countrycity=($record->country_code).'_'.$city; - $countrycity=~s/ /%20/g; - if ($record->region) { $countrycity.='_'.$record->region; } - $_city_h{lc($countrycity)}++; - } else { - $_city_h{'unknown'}++; - } -# if ($timerecord > $_city_l{$city}) { $_city_l{$city}=$timerecord; } - } else { - $_city_h{'unknown'}++; - } + if ($type eq 'geoippureperl') { + my @record = (); + @record=$geoip_city_maxmind->get_city_record($param) if $geoip_city_maxmind; + if ($Debug) { debug(" Plugin geoip_city_maxmind: GetCityByName for $param: [@record]",5); } + if (@record) { + my $city=$record[4]; + if ($city) { + my $countrycity=$record[0].'_'.$city; + $countrycity=~s/ /%20/g; + if ($record[3]) { $countrycity.='_'.$record[3]; } + $_city_h{lc($countrycity)}++; + } else { + $_city_h{'unknown'}++; + } + } else { + $_city_h{'unknown'}++; + } + } + else + { + my $record=(); + $record=$geoip_city_maxmind->record_by_name($param) if $geoip_city_maxmind; + if ($Debug) { debug(" Plugin geoip_city_maxmind: GetCityByName for $param: [$record]",5); } + if ($record) { + my $city=$record->city; + # if ($PageBool) { $_city_p{$city}++; } + if ($city) { + my $countrycity=($record->country_code).'_'.$city; + $countrycity=~s/ /%20/g; + if ($record->region) { $countrycity.='_'.$record->region; } + $_city_h{lc($countrycity)}++; + } else { + $_city_h{'unknown'}++; + } + # if ($timerecord > $_city_l{$city}) { $_city_l{$city}=$timerecord; } + } else { + $_city_h{'unknown'}++; + } + } # -----> return; } diff --git a/wwwroot/cgi-bin/plugins/geoip_region_maxmind.pm b/wwwroot/cgi-bin/plugins/geoip_region_maxmind.pm index 2f5619a8..b9f2288e 100755 --- a/wwwroot/cgi-bin/plugins/geoip_region_maxmind.pm +++ b/wwwroot/cgi-bin/plugins/geoip_region_maxmind.pm @@ -5,7 +5,7 @@ # from a Geographical database (US and Canada). # Need the licensed region database from Maxmind. #----------------------------------------------------------------------------- -# Perl Required Modules: Geo::IP (Geo::IP::PurePerl is not yet supported) +# Perl Required Modules: Geo::IP or Geo::IP::PurePerl #----------------------------------------------------------------------------- # $Revision$ - $Author$ - $Date$ @@ -35,8 +35,8 @@ 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="GetCountryCodeByAddr GetCountryCodeByName AddHTMLMenuLink AddHTMLGraph ShowInfoHost SectionInitHashArray SectionProcessIp SectionProcessHostname SectionReadHistory SectionWriteHistory"; +my $PluginNeedAWStatsVersion="6.5"; +my $PluginHooksFunctions="AddHTMLMenuLink AddHTMLGraph ShowInfoHost SectionInitHashArray SectionProcessIp SectionProcessHostname SectionReadHistory SectionWriteHistory"; # -----> # <----- @@ -149,7 +149,7 @@ sub Init_geoip_region_maxmind { # <----- # ENTER HERE CODE TO DO INIT PLUGIN ACTIONS - debug(" Plugin geoip: InitParams=$InitParams",1); + debug(" Plugin geoip_region_maxmind: InitParams=$InitParams",1); my ($mode,$datafile)=split(/\s+/,$InitParams,2); if (! $datafile) { $datafile="GeoIPRegion.dat"; } if ($type eq 'geoippureperl') { @@ -160,13 +160,14 @@ sub Init_geoip_region_maxmind { else { $mode=Geo::IP::GEOIP_STANDARD(); } } %TmpDomainLookup=(); - debug(" Plugin geoip: GeoIP initialized in mode $type $mode",1); + debug(" Plugin geoip_region_maxmind: GeoIP initialized type=$type mode=$mode",1); if ($type eq 'geoippureperl') { $geoip_region_maxmind = Geo::IP::PurePerl->open($datafile, $mode); } else { $geoip_region_maxmind = Geo::IP->open($datafile, $mode); } - # -----> + debug(" Plugin geoip_region_maxmind: GeoIP initialized database_info=".$gi->database_info()); + # -----> return ($checkversion?$checkversion:"$PluginHooksFunctions"); } @@ -184,7 +185,7 @@ sub AddHTMLMenuLink_geoip_region_maxmind { # <----- if ($Debug) { debug(" Plugin geoip_region_maxmind: AddHTMLMenuLink"); } if ($categ eq 'who') { - $menu->{'plugin_geoip_region_maxmind'}=2.1; # Pos + $menu->{'plugin_geoip_region_maxmind'}=2.1; # Pos $menulink->{'plugin_geoip_region_maxmind'}=2; # Type of link $menutext->{'plugin_geoip_region_maxmind'}="Regions"; # Text } @@ -292,6 +293,7 @@ sub AddHTMLGraph_geoip_region_maxmind { # 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. #----------------------------------------------------------------------------- +# Rem: Not used sub GetCountryCodeByAddr_geoip_region_maxmind { my $param="$_[0]"; # <----- @@ -314,6 +316,7 @@ sub GetCountryCodeByAddr_geoip_region_maxmind { # 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. #----------------------------------------------------------------------------- +# Rem: Not used sub GetCountryCodeByName_geoip_region_maxmind { my $param="$_[0]"; # <----- @@ -476,7 +479,7 @@ sub SectionProcessHostname_geoip_region_maxmind { # <----- my ($res1,$res2,$countryregion)=(); ($res1,$res2)=$geoip_region_maxmind->region_by_name($param) if $geoip_region_maxmind; - if ($Debug) { debug(" Plugin geoip_region_maxmind: GetRegionByName for $param: [{$res1}_{$res2}]",5); } + if ($Debug) { debug(" Plugin geoip_region_maxmind: GetRegionByName for $param: [${res1}_${res2}]",5); } if ($res2 =~ /\w\w/) { $countryregion=lc("${res1}_${res2}"); } else { $countryregion='unknown'; } # if ($PageBool) { $_region_p{$countryregion}++; }