4
4
# This plugin allow you to add a city report.
5
5
# Need the licensed city database from Maxmind.
6
6
#-----------------------------------------------------------------------------
7
# Perl Required Modules: Geo::IP (Geo::IP::PurePerl is not yet supported)
7
# Perl Required Modules: Geo::IP or Geo::IP::PurePerl
8
8
#-----------------------------------------------------------------------------
9
# $Revision: 1.13 $ - $Author: eldy $ - $Date: 2005/12/14 23:44:33 $
9
# $Revision: 1.20 $ - $Author: eldy $ - $Date: 2006/09/04 06:04:04 $
13
13
# ENTER HERE THE USE COMMAND FOR ALL REQUIRED PERL MODULES
14
if (!eval ('require "Geo/IP.pm";')) {
15
return $@?"Error: $@":"Error: Need Perl module Geo::IP (Geo::IP::PurePerl is not yet supported)";
16
if (!eval ('require "Geo/IP.pm";')) {
18
$type='geoippureperl';
19
if (!eval ('require "Geo/IP/PurePerl.pm";')) {
21
$ret=($error1||$error2)?"Error:\n$error1$error2":"";
22
$ret.="Error: Need Perl module Geo::IP or Geo::IP::PurePerl";
18
27
use strict;no strict "refs";
26
35
# ENTER HERE THE MINIMUM AWSTATS VERSION REQUIRED BY YOUR PLUGIN
27
36
# AND THE NAME OF ALL FUNCTIONS THE PLUGIN MANAGE.
28
my $PluginNeedAWStatsVersion="6.2";
37
my $PluginNeedAWStatsVersion="6.5";
29
38
my $PluginHooksFunctions="AddHTMLMenuLink AddHTMLGraph ShowInfoHost SectionInitHashArray SectionProcessIp SectionProcessHostname SectionReadHistory SectionWriteHistory";
33
42
# IF YOUR PLUGIN NEED GLOBAL VARIABLES, THEY MUST BE DECLARED HERE.
35
45
$geoip_city_maxmind
4267
my $countrycode = shift || "";
4268
my $regioncode = shift || "";
4270
if ($countrycode eq "us") { return $regus{uc $regioncode} || ""; }
4271
if ($countrycode eq "ca") { return $regca{uc $regioncode} || ""; }
4273
return $regall{uc($countrycode."_".$regioncode)} || "";
4255
4277
#-----------------------------------------------------------------------------
4256
4278
# PLUGIN FUNCTION: Init_pluginname
4257
4279
#-----------------------------------------------------------------------------
4264
4286
# ENTER HERE CODE TO DO INIT PLUGIN ACTIONS
4265
4287
debug(" Plugin geoip_city_maxmind: InitParams=$InitParams",1);
4266
# if ($UpdateStats) {
4267
my ($mode,$datafile)=split(/\s+/,$InitParams,2);
4268
if (! $datafile) { $datafile="GeoIPCity.dat"; }
4269
if ($mode eq '' || $mode eq 'GEOIP_MEMORY_CACHE') { $mode=Geo::IP::GEOIP_MEMORY_CACHE(); }
4270
else { $mode=Geo::IP::GEOIP_STANDARD(); }
4271
debug(" Plugin geoip_city_maxmind: GeoIP initialized in mode $mode",1);
4272
$geoip_city_maxmind = Geo::IP->open($datafile, $mode);
4288
my ($mode,$datafile)=split(/\s+/,$InitParams,2);
4289
if (! $datafile) { $datafile="GeoIPCity.dat"; }
4290
if ($type eq 'geoippureperl') {
4291
if ($mode eq '' || $mode eq 'GEOIP_MEMORY_CACHE') { $mode=Geo::IP::PurePerl::GEOIP_MEMORY_CACHE(); }
4292
else { $mode=Geo::IP::PurePerl::GEOIP_STANDARD(); }
4294
if ($mode eq '' || $mode eq 'GEOIP_MEMORY_CACHE') { $mode=Geo::IP::GEOIP_MEMORY_CACHE(); }
4295
else { $mode=Geo::IP::GEOIP_STANDARD(); }
4297
%TmpDomainLookup=();
4298
debug(" Plugin geoip_city_maxmind: GeoIP initialized type=$type mode=$mode",1);
4299
if ($type eq 'geoippureperl') {
4300
$geoip_city_maxmind = Geo::IP::PurePerl->open($datafile, $mode);
4302
$geoip_city_maxmind = Geo::IP->open($datafile, $mode);
4304
# Fails on some GeoIP version
4305
# debug(" Plugin geoip_city_maxmind: GeoIP initialized database_info=".$geoip_city_maxmind->database_info());
4276
4308
return ($checkversion?$checkversion:"$PluginHooksFunctions");
4348
4380
$city=~s/%20/ /g;
4349
4381
# if ($countrycode ne $country) { next; }
4350
4382
my $p_p; my $p_h;
4351
if ($TotalPages) { $p_p=int($_city_p{$key}/$TotalPages*1000)/10; }
4383
if ($TotalPages) { $p_p=int(($_city_p{$key}||0)/$TotalPages*1000)/10; }
4352
4384
if ($TotalHits) { $p_h=int($_city_h{$key}/$TotalHits*1000)/10; }
4354
4386
print "<td class=\"aws\">".$DomainsHashIDLib{$countrycode}."</td>";
4355
print "<td class=\"aws\">".($regall{uc($countrycode."_".$regioncode)}?$regall{uc($countrycode."_".$regioncode)}:" ")."</td>";
4387
my $regionlib=RegionName($countrycode, $regioncode);
4388
print "<td class=\"aws\">".($regionlib?$regionlib:' ')."</td>";
4356
4389
print "<td class=\"aws\">".ucfirst($city)."</td>";
4357
4390
if ($ShowCities =~ /P/i) { print "<td>".($_city_p{$key}?$_city_p{$key}:" ")."</td>"; }
4358
4391
if ($ShowCities =~ /P/i) { print "<td>".($_city_p{$key}?"$p_p %":' ')."</td>"; }
4405
4438
#-----------------------------------------------------------------------------
4439
# PLUGIN FUNCTION: GetCountryCodeByAddr_pluginname
4440
# UNIQUE: YES (Only one plugin using this function can be loaded)
4441
# GetCountryCodeByAddr is called to translate an ip into a country code in lower case.
4442
#-----------------------------------------------------------------------------
4444
sub GetCountryCodeByAddr_geoip_city_maxmind {
4447
my $res=$TmpDomainLookup{$param}||'';
4448
if ($type eq 'geoippureperl') {
4451
@record=$geoip_city_maxmind->get_city_record($param) if $geoip_city_maxmind;
4453
$country=$record[0] if @record;
4454
$res=lc($country) || 'unknown';
4455
$TmpDomainLookup{$param}=$res;
4456
if ($Debug) { debug(" Plugin geoip_region_maxmind: GetCountryCodeByAddr for $param: [$res]",5); }
4458
elsif ($Debug) { debug(" Plugin geoip_region_maxmind: GetCountryCodeByAddr for $param: Already resolved to [$res]",5); }
4464
$record=$geoip_city_maxmind->record_by_addr($param) if $geoip_city_maxmind;
4466
$country=$record->country if $record;
4467
$res=lc($country) || 'unknown';
4468
$TmpDomainLookup{$param}=$res;
4469
if ($Debug) { debug(" Plugin geoip_region_maxmind: GetCountryCodeByAddr for $param: [$res]",5); }
4471
elsif ($Debug) { debug(" Plugin geoip_region_maxmind: GetCountryCodeByAddr for $param: Already resolved to [$res]",5); }
4478
#-----------------------------------------------------------------------------
4479
# PLUGIN FUNCTION: GetCountryCodeByName_pluginname
4480
# UNIQUE: YES (Only one plugin using this function can be loaded)
4481
# GetCountryCodeByName is called to translate a host name into a country code in lower case.
4482
#-----------------------------------------------------------------------------
4484
sub GetCountryCodeByName_geoip_city_maxmind {
4487
my $res=$TmpDomainLookup{$param}||'';
4488
if ($type eq 'geoippureperl') {
4491
@record=$geoip_city_maxmind->get_city_record($param) if $geoip_city_maxmind;
4493
$country=$record[0] if @record;
4494
$res=lc($country) || 'unknown';
4495
$TmpDomainLookup{$param}=$res;
4496
if ($Debug) { debug(" Plugin geoip_region_maxmind: GetCountryCodeByName for $param: [$res]",5); }
4498
elsif ($Debug) { debug(" Plugin geoip_region_maxmind: GetCountryCodeByName for $param: Already resolved to [$res]",5); }
4504
$record=$geoip_city_maxmind->record_by_name($param) if $geoip_city_maxmind;
4506
$country=$record->country if $record;
4507
$res=lc($country) || 'unknown';
4508
$TmpDomainLookup{$param}=$res;
4509
if ($Debug) { debug(" Plugin geoip_region_maxmind: GetCountryCodeByName for $param: [$res]",5); }
4511
elsif ($Debug) { debug(" Plugin geoip_region_maxmind: GetCountryCodeByName for $param: Already resolved to [$res]",5); }
4518
#-----------------------------------------------------------------------------
4406
4519
# PLUGIN FUNCTION: ShowInfoHost_pluginname
4407
4520
# UNIQUE: NO (Several plugins using this function can be loaded)
4408
4521
# Function called to add additionnal columns to the Hosts report.
4430
4544
$NewLinkParams =~ s/^&//; $NewLinkParams =~ s/&$//;
4431
4545
if ($NewLinkParams) { $NewLinkParams="${NewLinkParams}&"; }
4547
# print "<th width=\"80\">";
4548
# 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>";
4433
4550
print "<th width=\"80\">";
4434
4551
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>";
4440
4558
if ($param =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) { # IPv4 address
4449
4566
if ($key && $ip==4) {
4451
$record=$geoip_city_maxmind->record_by_addr($param) if $geoip_city_maxmind;
4452
if ($Debug) { debug(" Plugin geoip_city_maxmind: GetCityByIp for $param: [$record]",5); }
4454
$city=$record->city if $record;
4569
if ($type eq 'geoippureperl')
4572
@record=$geoip_city_maxmind->get_city_record($param) if $geoip_city_maxmind;
4573
if ($Debug) { debug(" Plugin geoip_city_maxmind: GetCityByIp for $param: [@record]",5); }
4574
$country=$record[0] if @record;
4575
$city=$record[4] if @record;
4580
$record=$geoip_city_maxmind->record_by_addr($param) if $geoip_city_maxmind;
4581
if ($Debug) { debug(" Plugin geoip_city_maxmind: GetCityByIp for $param: [$record]",5); }
4582
$country=$record->country_code if $record;
4583
$city=$record->city if $record;
4586
# if ($country) { print $DomainsHashIDLib{$country}?$DomainsHashIDLib{$country}:"<span style=\"color: #$color_other\">$Message[0]</span>"; }
4587
# else { print "<span style=\"color: #$color_other\">$Message[0]</span>"; }
4455
4590
if ($city) { print "$city"; }
4456
4591
else { print "<span style=\"color: #$color_other\">$Message[0]</span>"; }
4458
4594
if ($key && $ip==6) {
4596
# print "<span style=\"color: #$color_other\">$Message[0]</span>";
4459
4599
print "<span style=\"color: #$color_other\">$Message[0]</span>";
4463
$record=$geoip_city_maxmind->record_by_name($param) if $geoip_city_maxmind;
4464
if ($Debug) { debug(" Plugin geoip_city_maxmind: GetCityByHostname for $param: [$record]",5); }
4466
$city=$record->city if $record;
4605
if ($type eq 'geoippureperl')
4608
@record=$geoip_city_maxmind->get_city_record($param) if $geoip_city_maxmind;
4609
if ($Debug) { debug(" Plugin geoip_city_maxmind: GetCityByHostname for $param: [@record]",5); }
4610
$country=$record[0] if @record;
4611
$city=$record[4] if @record;
4616
$record=$geoip_city_maxmind->record_by_name($param) if $geoip_city_maxmind;
4617
if ($Debug) { debug(" Plugin geoip_city_maxmind: GetCityByHostname for $param: [$record]",5); }
4618
$country=$record->country_code if $record;
4619
$city=$record->city if $record;
4622
# if ($country) { print $DomainsHashIDLib{$country}?$DomainsHashIDLib{$country}:"<span style=\"color: #$color_other\">$Message[0]</span>"; }
4623
# else { print "<span style=\"color: #$color_other\">$Message[0]</span>"; }
4467
4626
if ($city) { print "$city"; }
4468
4627
else { print "<span style=\"color: #$color_other\">$Message[0]</span>"; }
4633
# print "<td> </td>";
4473
4634
print "<td> </td>";
4498
4659
sub SectionProcessIp_geoip_city_maxmind {
4499
4660
my $param="$_[0]"; # Param must be an IP
4502
$record=$geoip_city_maxmind->record_by_addr($param) if $geoip_city_maxmind;
4503
if ($Debug) { debug(" Plugin geoip_city_maxmind: GetCityByIp for $param: [$record]",5); }
4505
my $city=$record->city;
4506
# if ($PageBool) { $_city_p{$city}++; }
4508
my $countrycity=($record->country_code).'_'.$city;
4509
$countrycity=~s/ /%20/g;
4510
if ($record->region) { $countrycity.='_'.$record->region; }
4511
$_city_h{lc($countrycity)}++;
4513
$_city_h{'unknown'}++;
4515
# if ($timerecord > $_city_l{$city}) { $_city_l{$city}=$timerecord; }
4517
$_city_h{'unknown'}++;
4662
if ($type eq 'geoippureperl') {
4664
@record=$geoip_city_maxmind->get_city_record($param) if $geoip_city_maxmind;
4665
if ($Debug) { debug(" Plugin geoip_city_maxmind: GetCityByName for $param: [@record]",5); }
4667
my $city=$record[4];
4669
my $countrycity=$record[0].'_'.$city;
4670
$countrycity=~s/ /%20/g;
4671
if ($record[3]) { $countrycity.='_'.$record[3]; }
4672
$_city_h{lc($countrycity)}++;
4674
$_city_h{'unknown'}++;
4677
$_city_h{'unknown'}++;
4683
$record=$geoip_city_maxmind->record_by_addr($param) if $geoip_city_maxmind;
4684
if ($Debug) { debug(" Plugin geoip_city_maxmind: GetCityByIp for $param: [$record]",5); }
4686
my $city=$record->city;
4687
# if ($PageBool) { $_city_p{$city}++; }
4689
my $countrycity=($record->country_code).'_'.$city;
4690
$countrycity=~s/ /%20/g;
4691
if ($record->region) { $countrycity.='_'.$record->region; }
4692
$_city_h{lc($countrycity)}++;
4694
$_city_h{'unknown'}++;
4696
# if ($timerecord > $_city_l{$city}) { $_city_l{$city}=$timerecord; }
4698
$_city_h{'unknown'}++;
4528
4710
sub SectionProcessHostname_geoip_city_maxmind {
4529
4711
my $param="$_[0]"; # Param must be an IP
4532
$record=$geoip_city_maxmind->record_by_name($param) if $geoip_city_maxmind;
4533
if ($Debug) { debug(" Plugin geoip_city_maxmind: GetCityByName for $param: [$record]",5); }
4535
my $city=$record->city;
4536
# if ($PageBool) { $_city_p{$city}++; }
4538
my $countrycity=($record->country_code).'_'.$city;
4539
$countrycity=~s/ /%20/g;
4540
if ($record->region) { $countrycity.='_'.$record->region; }
4541
$_city_h{lc($countrycity)}++;
4543
$_city_h{'unknown'}++;
4545
# if ($timerecord > $_city_l{$city}) { $_city_l{$city}=$timerecord; }
4547
$_city_h{'unknown'}++;
4713
if ($type eq 'geoippureperl') {
4715
@record=$geoip_city_maxmind->get_city_record($param) if $geoip_city_maxmind;
4716
if ($Debug) { debug(" Plugin geoip_city_maxmind: GetCityByName for $param: [@record]",5); }
4718
my $city=$record[4];
4720
my $countrycity=$record[0].'_'.$city;
4721
$countrycity=~s/ /%20/g;
4722
if ($record[3]) { $countrycity.='_'.$record[3]; }
4723
$_city_h{lc($countrycity)}++;
4725
$_city_h{'unknown'}++;
4728
$_city_h{'unknown'}++;
4734
$record=$geoip_city_maxmind->record_by_name($param) if $geoip_city_maxmind;
4735
if ($Debug) { debug(" Plugin geoip_city_maxmind: GetCityByName for $param: [$record]",5); }
4737
my $city=$record->city;
4738
# if ($PageBool) { $_city_p{$city}++; }
4740
my $countrycity=($record->country_code).'_'.$city;
4741
$countrycity=~s/ /%20/g;
4742
if ($record->region) { $countrycity.='_'.$record->region; }
4743
$_city_h{lc($countrycity)}++;
4745
$_city_h{'unknown'}++;
4747
# if ($timerecord > $_city_l{$city}) { $_city_l{$city}=$timerecord; }
4749
$_city_h{'unknown'}++;