# 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";
# <-----
# 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
# <-----
# 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");
}
+#-----------------------------------------------------------------------------
+# 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)
$NewLinkParams =~ s/^&//; $NewLinkParams =~ s/&$//;
if ($NewLinkParams) { $NewLinkParams="${NewLinkParams}&"; }
+# print "<th width=\"80\">";
+# print "<a href=\"".($ENV{'GATEWAY_INTERFACE'} || !$StaticLinks?XMLEncode("$AWScript?${NewLinkParams}output=plugin_geoip_city_maxmind&suboutput=country"):"$PROG$StaticLinks.plugin_geoip_city_maxmind.country.$StaticExt")."\"$NewLinkTarget>GeoIP<br/>Country</a>";
+# print "</th>";
print "<th width=\"80\">";
print "<a href=\"".($ENV{'GATEWAY_INTERFACE'} || !$StaticLinks?XMLEncode("$AWScript?${NewLinkParams}output=plugin_geoip_city_maxmind"):"$PROG$StaticLinks.plugin_geoip_city_maxmind.$StaticExt")."\"$NewLinkTarget>GeoIP<br/>City</a>";
print "</th>";
$ip=6;
$key=$param;
}
- print "<td>";
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 "<td>";
+# if ($country) { print $DomainsHashIDLib{$country}?$DomainsHashIDLib{$country}:"<span style=\"color: #$color_other\">$Message[0]</span>"; }
+# else { print "<span style=\"color: #$color_other\">$Message[0]</span>"; }
+# print "</td>";
+ print "<td>";
if ($city) { print "$city"; }
else { print "<span style=\"color: #$color_other\">$Message[0]</span>"; }
+ print "</td>";
}
if ($key && $ip==6) {
+# print "<td>";
+# print "<span style=\"color: #$color_other\">$Message[0]</span>";
+# print "</td>";
+ print "<td>";
print "<span style=\"color: #$color_other\">$Message[0]</span>";
+ print "</td>";
}
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 "<td>";
+ if ($country) { print $DomainsHashIDLib{$country}?$DomainsHashIDLib{$country}:"<span style=\"color: #$color_other\">$Message[0]</span>"; }
+ else { print "<span style=\"color: #$color_other\">$Message[0]</span>"; }
+ print "</td>";
+ print "<td>";
if ($city) { print "$city"; }
else { print "<span style=\"color: #$color_other\">$Message[0]</span>"; }
+ print "</td>";
}
- print "</td>";
}
else {
print "<td> </td>";
+ print "<td> </td>";
}
return 1;
# ----->
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;
}
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;
}
# 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$
# <-----
# 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";
# ----->
# <-----
# <-----
# 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') {
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");
}
# <-----
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
}
# 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]";
# <-----
# 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]";
# <-----
# <-----
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}++; }