1 rizwank 1.1 #!/usr/bin/perl
2 #-----------------------------------------------------------------------------
3 # GeoIp_Isp_Maxmind AWStats plugin
4 # This plugin allow you to add a city report.
5 # Need the licensed ISP database from Maxmind.
6 #-----------------------------------------------------------------------------
7 # Perl Required Modules: Geo::IP (Geo::IP::PurePerl is not yet supported)
8 #-----------------------------------------------------------------------------
9 # $Revision: 1.3 $ - $Author: eldy $ - $Date: 2004/12/10 22:28:39 $
10
11
12 # <-----
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 }
17 # ----->
18 use strict;no strict "refs";
19
20
21
22 rizwank 1.1 #-----------------------------------------------------------------------------
23 # PLUGIN VARIABLES
24 #-----------------------------------------------------------------------------
25 # <-----
26 # ENTER HERE THE MINIMUM AWSTATS VERSION REQUIRED BY YOUR PLUGIN
27 # AND THE NAME OF ALL FUNCTIONS THE PLUGIN MANAGE.
28 my $PluginNeedAWStatsVersion="6.2";
29 my $PluginHooksFunctions="AddHTMLMenuLink AddHTMLGraph ShowInfoHost SectionInitHashArray SectionProcessIp SectionProcessHostname SectionReadHistory SectionWriteHistory";
30 # ----->
31
32 # <-----
33 # IF YOUR PLUGIN NEED GLOBAL VARIABLES, THEY MUST BE DECLARED HERE.
34 use vars qw/
35 $geoip_isp_maxmind
36 %_isp_p
37 %_isp_h
38 %_isp_k
39 %_isp_l
40 $MAXNBOFSECTIONGIR
41 /;
42 # ----->
43 rizwank 1.1
44
45 #-----------------------------------------------------------------------------
46 # PLUGIN FUNCTION: Init_pluginname
47 #-----------------------------------------------------------------------------
48 sub Init_geoip_isp_maxmind {
49 my $InitParams=shift;
50 my $checkversion=&Check_Plugin_Version($PluginNeedAWStatsVersion);
51 $MAXNBOFSECTIONGIR=10;
52
53 # <-----
54 # ENTER HERE CODE TO DO INIT PLUGIN ACTIONS
55 debug(" Plugin geoip_isp_maxmind: InitParams=$InitParams",1);
56 # if ($UpdateStats) {
57 my ($mode,$datafile)=split(/\s+/,$InitParams,2);
58 if (! $datafile) { $datafile="GeoIPISP.dat"; }
59 if ($mode eq '' || $mode eq 'GEOIP_MEMORY_CACHE') { $mode=Geo::IP::GEOIP_MEMORY_CACHE(); }
60 else { $mode=Geo::IP::GEOIP_STANDARD(); }
61 debug(" Plugin geoip_isp_maxmind: GeoIP initialized in mode $mode",1);
62 $geoip_isp_maxmind = Geo::IP->open($datafile, $mode);
63 # }
64 rizwank 1.1 # ----->
65
66 return ($checkversion?$checkversion:"$PluginHooksFunctions");
67 }
68
69
70 #-----------------------------------------------------------------------------
71 # PLUGIN FUNCTION: AddHTMLMenuLink_pluginname
72 # UNIQUE: NO (Several plugins using this function can be loaded)
73 #-----------------------------------------------------------------------------
74 sub AddHTMLMenuLink_geoip_isp_maxmind {
75 my $categ=$_[0];
76 my $menu=$_[1];
77 my $menulink=$_[2];
78 my $menutext=$_[3];
79 # <-----
80 if ($Debug) { debug(" Plugin geoip_isp_maxmind: AddHTMLMenuLink"); }
81 if ($categ eq 'who') {
82 $menu->{'plugin_geoip_isp_maxmind'}=0.6; # Pos
83 $menulink->{'plugin_geoip_isp_maxmind'}=2; # Type of link
84 $menutext->{'plugin_geoip_isp_maxmind'}="ISP"; # Text
85 rizwank 1.1 }
86 # ----->
87 return 0;
88 }
89
90
91 #-----------------------------------------------------------------------------
92 # PLUGIN FUNCTION: AddHTMLGraph_pluginname
93 # UNIQUE: NO (Several plugins using this function can be loaded)
94 #-----------------------------------------------------------------------------
95 sub AddHTMLGraph_geoip_isp_maxmind {
96 my $categ=$_[0];
97 my $menu=$_[1];
98 my $menulink=$_[2];
99 my $menutext=$_[3];
100 # <-----
101 my $ShowISP='H';
102 $MinHit{'Isp'}=1;
103 my $total_p; my $total_h; my $total_k;
104 my $rest_p; my $rest_h; my $rest_k;
105
106 rizwank 1.1 if ($Debug) { debug(" Plugin geoip_isp_maxmind: AddHTMLGraph $categ $menu $menulink $menutext"); }
107 my $title='ISP';
108 &tab_head("$title",19,0,'isp');
109 print "<tr bgcolor=\"#$color_TableBGRowTitle\"><th>ISP : ".((scalar keys %_isp_h)-($_isp_h{'unknown'}?1:0))."</th>";
110 if ($ShowISP =~ /P/i) { print "<th bgcolor=\"#$color_p\" width=\"80\">$Message[56]</th>"; }
111 if ($ShowISP =~ /P/i) { print "<th bgcolor=\"#$color_p\" width=\"80\">$Message[15]</th>"; }
112 if ($ShowISP =~ /H/i) { print "<th bgcolor=\"#$color_h\" width=\"80\">$Message[57]</th>"; }
113 if ($ShowISP =~ /H/i) { print "<th bgcolor=\"#$color_h\" width=\"80\">$Message[15]</th>"; }
114 if ($ShowISP =~ /B/i) { print "<th bgcolor=\"#$color_k\" width=\"80\">$Message[75]</th>"; }
115 if ($ShowISP =~ /L/i) { print "<th width=\"120\">$Message[9]</th>"; }
116 print "</tr>\n";
117 $total_p=$total_h=$total_k=0;
118 my $count=0;
119 &BuildKeyList($MaxRowsInHTMLOutput,$MinHit{'Isp'},\%_isp_h,\%_isp_h);
120 foreach my $key (@keylist) {
121 if ($key eq 'unknown') { next; }
122 my $p_p; my $p_h;
123 if ($TotalPages) { $p_p=int($_isp_p{$key}/$TotalPages*1000)/10; }
124 if ($TotalHits) { $p_h=int($_isp_h{$key}/$TotalHits*1000)/10; }
125 print "<tr>";
126 my $isp=$key; $isp =~ s/_/ /g;
127 rizwank 1.1 print "<td class=\"aws\">".ucfirst($isp)."</td>";
128 if ($ShowISP =~ /P/i) { print "<td>".($_isp_p{$key}?$_isp_p{$key}:" ")."</td>"; }
129 if ($ShowISP =~ /P/i) { print "<td>".($_isp_p{$key}?"$p_p %":' ')."</td>"; }
130 if ($ShowISP =~ /H/i) { print "<td>".($_isp_h{$key}?$_isp_h{$key}:" ")."</td>"; }
131 if ($ShowISP =~ /H/i) { print "<td>".($_isp_h{$key}?"$p_h %":' ')."</td>"; }
132 if ($ShowISP =~ /B/i) { print "<td>".Format_Bytes($_isp_k{$key})."</td>"; }
133 if ($ShowISP =~ /L/i) { print "<td>".($_isp_p{$key}?Format_Date($_isp_l{$key},1):'-')."</td>"; }
134 print "</tr>\n";
135 $total_p += $_isp_p{$key}||0;
136 $total_h += $_isp_h{$key};
137 $total_k += $_isp_k{$key}||0;
138 $count++;
139 }
140 if ($Debug) { debug("Total real / shown : $TotalPages / $total_p - $TotalHits / $total_h - $TotalBytes / $total_h",2); }
141 $rest_p=0;
142 $rest_h=$TotalHits-$total_h;
143 $rest_k=0;
144 if ($rest_p > 0 || $rest_h > 0 || $rest_k > 0) { # All other cities
145 # print "<tr>";
146 # print "<td class=\"aws\"> </td>";
147 # if ($ShowISP =~ /P/i) { print "<td> </td>"; }
148 rizwank 1.1 # if ($ShowISP =~ /P/i) { print "<td> </td>"; }
149 # if ($ShowISP =~ /H/i) { print "<td> </td>"; }
150 # if ($ShowISP =~ /H/i) { print "<td> </td>"; }
151 # if ($ShowISP =~ /B/i) { print "<td> </td>"; }
152 # if ($ShowISP =~ /L/i) { print "<td> </td>"; }
153 # print "</tr>\n";
154
155 my $p_p; my $p_h;
156 if ($TotalPages) { $p_p=int($rest_p/$TotalPages*1000)/10; }
157 if ($TotalHits) { $p_h=int($rest_h/$TotalHits*1000)/10; }
158 print "<tr>";
159 print "<td class=\"aws\"><span style=\"color: #$color_other\">$Message[2]/$Message[0]</span></td>";
160 if ($ShowISP =~ /P/i) { print "<td>".($rest_p?$rest_p:" ")."</td>"; }
161 if ($ShowISP =~ /P/i) { print "<td>".($rest_p?"$p_p %":' ')."</td>"; }
162 if ($ShowISP =~ /H/i) { print "<td>".($rest_h?$rest_h:" ")."</td>"; }
163 if ($ShowISP =~ /H/i) { print "<td>".($rest_h?"$p_h %":' ')."</td>"; }
164 if ($ShowISP =~ /B/i) { print "<td>".Format_Bytes($rest_k)."</td>"; }
165 if ($ShowISP =~ /L/i) { print "<td> </td>"; }
166 print "</tr>\n";
167 }
168 &tab_end();
169 rizwank 1.1
170 # ----->
171 return 0;
172 }
173
174
175 #-----------------------------------------------------------------------------
176 # PLUGIN FUNCTION: ShowInfoHost_pluginname
177 # UNIQUE: NO (Several plugins using this function can be loaded)
178 # Function called to add additionnal columns to the Hosts report.
179 # This function is called when building rows of the report (One call for each
180 # row). So it allows you to add a column in report, for example with code :
181 # print "<TD>This is a new cell for $param</TD>";
182 # Parameters: Host name or ip
183 #-----------------------------------------------------------------------------
184 sub ShowInfoHost_geoip_isp_maxmind {
185 my $param="$_[0]";
186 # <-----
187 if ($param eq '__title__') {
188 my $NewLinkParams=${QueryString};
189 $NewLinkParams =~ s/(^|&)update(=\w*|$)//i;
190 rizwank 1.1 $NewLinkParams =~ s/(^|&)output(=\w*|$)//i;
191 $NewLinkParams =~ s/(^|&)staticlinks(=\w*|$)//i;
192 $NewLinkParams =~ s/(^|&)framename=[^&]*//i;
193 my $NewLinkTarget='';
194 if ($DetailedReportsOnNewWindows) { $NewLinkTarget=" target=\"awstatsbis\""; }
195 if (($FrameName eq 'mainleft' || $FrameName eq 'mainright') && $DetailedReportsOnNewWindows < 2) {
196 $NewLinkParams.="&framename=mainright";
197 $NewLinkTarget=" target=\"mainright\"";
198 }
199 $NewLinkParams =~ tr/&/&/s; $NewLinkParams =~ s/^&//; $NewLinkParams =~ s/&$//;
200 if ($NewLinkParams) { $NewLinkParams="${NewLinkParams}&"; }
201
202 print "<th width=\"80\">";
203 print "<a href=\"".($ENV{'GATEWAY_INTERFACE'} || !$StaticLinks?XMLEncode("$AWScript?${NewLinkParams}output=plugin_geoip_isp_maxmind"):"$PROG$StaticLinks.plugin_geoip_isp_maxmind.$StaticExt")."\"$NewLinkTarget>GeoIP<br>ISP</a>";
204 print "</th>";
205 }
206 elsif ($param) {
207 my $ip=0;
208 my $key;
209 if ($param =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) { # IPv4 address
210 $ip=4;
211 rizwank 1.1 $key=$param;
212 }
213 elsif ($param =~ /^[0-9A-F]*:/i) { # IPv6 address
214 $ip=6;
215 $key=$param;
216 }
217 print "<td>";
218 if ($key && $ip==4) {
219 my $isp=$geoip_isp_maxmind->org_by_addr($param) if $geoip_isp_maxmind;
220 if ($Debug) { debug(" Plugin geoip_isp_maxmind: GetIspByIp for $param: [$isp]",5); }
221 if ($isp) { print "$isp"; }
222 else { print "<span style=\"color: #$color_other\">$Message[0]</span>"; }
223 }
224 if ($key && $ip==6) {
225 print "<span style=\"color: #$color_other\">$Message[0]</span>";
226 }
227 if (! $key) {
228 my $isp=$geoip_isp_maxmind->org_by_name($param) if $geoip_isp_maxmind;
229 if ($Debug) { debug(" Plugin geoip_isp_maxmind: GetIspByHostname for $param: [$isp]",5); }
230 if ($isp) { print "$isp"; }
231 else { print "<span style=\"color: #$color_other\">$Message[0]</span>"; }
232 rizwank 1.1 }
233 print "</td>";
234 }
235 else {
236 print "<td> </td>";
237 }
238 return 1;
239 # ----->
240 }
241
242
243 #-----------------------------------------------------------------------------
244 # PLUGIN FUNCTION: SectionInitHashArray_pluginname
245 # UNIQUE: NO (Several plugins using this function can be loaded)
246 #-----------------------------------------------------------------------------
247 sub SectionInitHashArray_geoip_isp_maxmind {
248 my $param="$_[0]";
249 # <-----
250 if ($Debug) { debug(" Plugin geoip_isp_maxmind: Init_HashArray"); }
251 %_isp_p = %_isp_h = %_isp_k = %_isp_l =();
252 # ----->
253 rizwank 1.1 return 0;
254 }
255
256
257 #-----------------------------------------------------------------------------
258 # PLUGIN FUNCTION: SectionProcessIP_pluginname
259 # UNIQUE: NO (Several plugins using this function can be loaded)
260 #-----------------------------------------------------------------------------
261 sub SectionProcessIp_geoip_isp_maxmind {
262 my $param="$_[0]"; # Param must be an IP
263 # <-----
264 my $isp = $geoip_isp_maxmind->org_by_addr($param) if $geoip_isp_maxmind;
265 if ($Debug) { debug(" Plugin geoip_isp_maxmind: GetIspByIp for $param: [$isp]",5); }
266 if ($isp) {
267 $isp =~ s/\s/_/g;
268 $_isp_h{$isp}++;
269 } else {
270 $_isp_h{'unknown'}++;
271 }
272 # if ($timerecord > $_isp_l{$city}) { $_isp_l{$city}=$timerecord; }
273 # ----->
274 rizwank 1.1 return;
275 }
276
277
278 #-----------------------------------------------------------------------------
279 # PLUGIN FUNCTION: SectionProcessHostname_pluginname
280 # UNIQUE: NO (Several plugins using this function can be loaded)
281 #-----------------------------------------------------------------------------
282 sub SectionProcessHostname_geoip_isp_maxmind {
283 my $param="$_[0]"; # Param must be an IP
284 # <-----
285 my $isp = $geoip_isp_maxmind->org_by_name($param) if $geoip_isp_maxmind;
286 if ($Debug) { debug(" Plugin geoip_isp_maxmind: GetIspByHostname for $param: [$isp]",5); }
287 if ($isp) {
288 $isp =~ s/\s/_/g;
289 $_isp_h{$isp}++;
290 } else {
291 $_isp_h{'unknown'}++;
292 }
293 # if ($timerecord > $_isp_l{$city}) { $_isp_l{$city}=$timerecord; }
294 # ----->
295 rizwank 1.1 return;
296 }
297
298
299 #-----------------------------------------------------------------------------
300 # PLUGIN FUNCTION: SectionReadHistory_pluginname
301 # UNIQUE: NO (Several plugins using this function can be loaded)
302 #-----------------------------------------------------------------------------
303 sub SectionReadHistory_geoip_isp_maxmind {
304 my $issectiontoload=shift;
305 my $xmlold=shift;
306 my $xmleb=shift;
307 my $countlines=shift;
308 # <-----
309 if ($Debug) { debug(" Plugin geoip_isp_maxmind: Begin of PLUGIN_geoip_isp_maxmind section"); }
310 my @field=();
311 my $count=0;my $countloaded=0;
312 do {
313 if ($field[0]) {
314 $count++;
315 if ($issectiontoload) {
316 rizwank 1.1 $countloaded++;
317 if ($field[2]) { $_isp_h{$field[0]}+=$field[2]; }
318 }
319 }
320 $_=<HISTORY>;
321 chomp $_; s/\r//;
322 @field=split(/\s+/,($xmlold?CleanFromTags($_):$_));
323 $countlines++;
324 }
325 until ($field[0] eq 'END_PLUGIN_geoip_isp_maxmind' || $field[0] eq "${xmleb}END_PLUGIN_geoip_isp_maxmind" || ! $_);
326 if ($field[0] ne 'END_PLUGIN_geoip_isp_maxmind' && $field[0] ne "${xmleb}END_PLUGIN_geoip_isp_maxmind") { error("History file is corrupted (End of section PLUGIN not found).\nRestore a recent backup of this file (data for this month will be restored to backup date), remove it (data for month will be lost), or remove the corrupted section in file (data for at least this section will be lost).","","",1); }
327 if ($Debug) { debug(" Plugin geoip_isp_maxmind: End of PLUGIN_geoip_isp_maxmind section ($count entries, $countloaded loaded)"); }
328 # ----->
329 return 0;
330 }
331
332 #-----------------------------------------------------------------------------
333 # PLUGIN FUNCTION: SectionWriteHistory_pluginname
334 # UNIQUE: NO (Several plugins using this function can be loaded)
335 #-----------------------------------------------------------------------------
336 sub SectionWriteHistory_geoip_isp_maxmind {
337 rizwank 1.1 my ($xml,$xmlbb,$xmlbs,$xmlbe,$xmlrb,$xmlrs,$xmlre,$xmleb,$xmlee)=(shift,shift,shift,shift,shift,shift,shift,shift,shift);
338 if ($Debug) { debug(" Plugin geoip_isp_maxmind: SectionWriteHistory_geoip_isp_maxmind start - ".(scalar keys %_isp_h)); }
339 # <-----
340 print HISTORYTMP "\n";
341 if ($xml) { print HISTORYTMP "<section id='plugin_geoip_isp_maxmind'><sortfor>$MAXNBOFSECTIONGIR</sortfor><comment>\n"; }
342 print HISTORYTMP "# Plugin key - Pages - Hits - Bandwidth - Last access\n";
343 #print HISTORYTMP "# The $MaxNbOfExtra[$extranum] first number of hits are first\n";
344 $ValueInFile{'plugin_geoip_isp_maxmind'}=tell HISTORYTMP;
345 print HISTORYTMP "${xmlbb}BEGIN_PLUGIN_geoip_isp_maxmind${xmlbs}".(scalar keys %_isp_h)."${xmlbe}\n";
346 &BuildKeyList($MAXNBOFSECTIONGIR,1,\%_isp_h,\%_isp_h);
347 my %keysinkeylist=();
348 foreach (@keylist) {
349 $keysinkeylist{$_}=1;
350 #my $page=$_isp_p{$_}||0;
351 #my $bytes=$_isp_k{$_}||0;
352 #my $lastaccess=$_isp_l{$_}||'';
353 print HISTORYTMP "${xmlrb}$_${xmlrs}0${xmlrs}", $_isp_h{$_}, "${xmlrs}0${xmlrs}0${xmlre}\n"; next;
354 }
355 foreach (keys %_isp_h) {
356 if ($keysinkeylist{$_}) { next; }
357 #my $page=$_isp_p{$_}||0;
358 rizwank 1.1 #my $bytes=$_isp_k{$_}||0;
359 #my $lastaccess=$_isp_l{$_}||'';
360 print HISTORYTMP "${xmlrb}$_${xmlrs}0${xmlrs}", $_isp_h{$_}, "${xmlrs}0${xmlrs}0${xmlre}\n"; next;
361 }
362 print HISTORYTMP "${xmleb}END_PLUGIN_geoip_isp_maxmind${xmlee}\n";
363 # ----->
364 return 0;
365 }
366
367
368
369 1; # Do not remove this line
|