]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/logs.cgi/firewalllogcountry.dat
701abab2c84987d65a7f872483c2bdbb5dd49f1f
[ipfire-2.x.git] / html / cgi-bin / logs.cgi / firewalllogcountry.dat
1 #!/usr/bin/perl
2 #
3 # SmoothWall CGIs
4 #
5 # This code is distributed under the terms of the GPL
6 #
7 # JC HERITIER
8 # page inspired from the initial firewalllog.dat
9 #
10 # Modified for IPFire by Christian Schmidt
11 # and Michael Tremer (www.ipfire.org)
12
13 use strict;
14 use Getopt::Std;
15
16 # enable only the following on debugging purpose
17 #use warnings;
18 #use CGI::Carp 'fatalsToBrowser';
19
20 require '/var/ipfire/general-functions.pl';
21 require "${General::swroot}/location-functions.pl";
22 require "${General::swroot}/lang.pl";
23 require "${General::swroot}/header.pl";
24
25 # Libloc database handle.
26 my $libloc_db_handle = &Location::Functions::init();
27
28 use POSIX();
29
30 my %cgiparams=();
31 my %settings=();
32 my $pienumber;
33 my $otherspie;
34 my $showpie;
35 my $sortcolumn;
36 my $errormessage = '';
37
38 $cgiparams{'pienumber'} = 10;
39 $cgiparams{'otherspie'} = 1;
40 $cgiparams{'showpie'} = 1;
41 $cgiparams{'sortcolumn'} = 1;
42
43 my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
44 'Sep', 'Oct', 'Nov', 'Dec' );
45 my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
46 $Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
47 $Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
48 $Lang::tr{'december'} );
49
50 my @now = localtime();
51 my $dow = $now[6];
52 my $doy = $now[7];
53 my $tdoy = $now[7];
54 my $year = $now[5]+1900;
55
56 $cgiparams{'DAY'} = $now[3];
57 $cgiparams{'MONTH'} = $now[4];
58 $cgiparams{'ACTION'} = '';
59
60 &General::readhash("${General::swroot}/fwlogs/ipsettings", \%settings);
61 if ($settings{'pienumber'} != 0) { $cgiparams{'pienumber'} = $settings{'pienumber'} };
62 if ($settings{'otherspie'} != 0) { $cgiparams{'otherspie'} = $settings{'otherspie'} };
63 if ($settings{'showpie'} != 0) { $cgiparams{'showpie'} = $settings{'showpie'} };
64 if ($settings{'sortcolumn'} != 0) { $cgiparams{'sortcolumn'} = $settings{'sortcolumn'} };
65
66 &Header::getcgihash(\%cgiparams);
67 if ($cgiparams{'pienumber'} != 0) { $settings{'pienumber'} = $cgiparams{'pienumber'} };
68 if ($cgiparams{'otherspie'} != 0) { $settings{'otherspie'} = $cgiparams{'otherspie'} };
69 if ($cgiparams{'showpie'} != 0) { $settings{'showpie'} = $cgiparams{'showpie'} };
70 if ($cgiparams{'sortcolumn'} != 0) { $settings{'sortcolumn'} = $cgiparams{'sortcolumn'} };
71
72 if ($cgiparams{'ACTION'} eq $Lang::tr{'save'})
73 {
74 &General::writehash("${General::swroot}/fwlogs/ipsettings", \%settings);
75 }
76
77 my $start = -1;
78 if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
79 {
80 my @temp = split(',',$ENV{'QUERY_STRING'});
81 $start = $temp[0];
82 $cgiparams{'MONTH'} = $temp[1];
83 $cgiparams{'DAY'} = $temp[2];
84 }
85
86 if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
87 !($cgiparams{'DAY'} =~ /^(1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)$/))
88 {
89 $cgiparams{'DAY'} = $now[3];
90 $cgiparams{'MONTH'} = $now[4];
91 }
92 elsif($cgiparams{'ACTION'} eq '>>')
93 {
94 my @temp_then=();
95 my @temp_now = localtime(time);
96 $temp_now[4] = $cgiparams{'MONTH'};
97 $temp_now[3] = $cgiparams{'DAY'};
98 @temp_then = localtime(POSIX::mktime(@temp_now) + 86400);
99 ## Retrieve the same time on the next day -
100 ## 86400 seconds in a day
101 $cgiparams{'MONTH'} = $temp_then[4];
102 $cgiparams{'DAY'} = $temp_then[3];
103 }
104 elsif($cgiparams{'ACTION'} eq '<<')
105 {
106 my @temp_then=();
107 my @temp_now = localtime(time);
108 $temp_now[4] = $cgiparams{'MONTH'};
109 $temp_now[3] = $cgiparams{'DAY'};
110 @temp_then = localtime(POSIX::mktime(@temp_now) - 86400);
111 ## Retrieve the same time on the previous day -
112 ## 86400 seconds in a day
113 $cgiparams{'MONTH'} = $temp_then[4];
114 $cgiparams{'DAY'} = $temp_then[3];
115 }
116
117 if (($cgiparams{'DAY'} ne $now[3]) || ($cgiparams{'MONTH'} ne $now[4]))
118 {
119 my @then = ();
120 if ( ( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
121 ( $cgiparams{'MONTH'} > $now[4] ) ) {
122 @then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 ));
123 } else {
124 @then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 ));
125 }
126 $tdoy = $then[7];
127 my $lastleap=($year-1)%4;
128 if ($tdoy>$doy) {
129 if ($lastleap == 0 && $tdoy < 60) {
130 $doy=$tdoy+366;
131 } else {
132 $doy=$doy+365;
133 }
134 }
135 }
136
137 my $datediff=0;
138 my $dowd=0;
139 my $multifile=0;
140 if ($tdoy ne $doy) {
141 $datediff=int(($doy-$tdoy)/7);
142 $dowd=($doy-$tdoy)%7;
143 if (($dow-$dowd)<1) {
144 $datediff=$datediff+1;
145 }
146 if (($dow-$dowd)==0) {
147 $multifile=1;
148 }
149 }
150
151 my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
152 my $longmonthstr = $longmonths[$cgiparams{'MONTH'}];
153 my $day = $cgiparams{'DAY'};
154 my $daystr='';
155 if ($day <= 9) {
156 $daystr = " $day";
157 }
158 else {
159 $daystr = $day;
160 }
161
162 my $skip=0;
163 my $filestr='';
164 if ($datediff==0) {
165 $filestr="/var/log/messages";
166 } else {
167 $filestr="/var/log/messages.$datediff";
168 $filestr = "$filestr.gz" if -f "$filestr.gz";
169 }
170
171 if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
172 $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
173 $skip=1;
174 # Note: This is in case the log does not exist for that date
175 }
176 my $lines = 0;
177 my @log=();
178
179 if (!$skip)
180 {
181 while (<FILE>)
182 {
183 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
184 $log[$lines] = $_;
185 $lines++;
186 }
187 }
188 close (FILE);
189 }
190
191 $skip=0;
192 if ($multifile) {
193 $datediff=$datediff-1;
194 if ($datediff==0) {
195 $filestr="/var/log/messages";
196 } else {
197 $filestr="/var/log/messages.$datediff";
198 $filestr = "$filestr.gz" if -f "$filestr.gz";
199 }
200 if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
201 $errormessage="$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
202 $skip=1;
203 }
204 if (!$skip) {
205 while (<FILE>) {
206 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
207 $log[$lines] = $_;
208 $lines++;
209 }
210 }
211 close (FILE);
212 }
213 }
214
215 my $MODNAME="fwlogs";
216
217 &Header::showhttpheaders();
218 &Header::openpage($Lang::tr{'firewall log country'}, 1, '');
219 &Header::openbigbox('100%', 'left', '', $errormessage);
220
221
222 if ($errormessage) {
223 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
224 print "<font class='base'>$errormessage&nbsp;</font>\n";
225 &Header::closebox();
226 }
227
228 &Header::openbox('100%', 'left', "$Lang::tr{'settings'}");
229
230 print <<END
231 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
232 <table width='100%'>
233 <tr>
234 <td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
235 <td width='10%'>
236 <select name='MONTH'>
237 END
238 ;
239 my $month;
240 for ($month = 0; $month < 12; $month++)
241 {
242 print "\t<option ";
243 if ($month == $cgiparams{'MONTH'}) {
244 print "selected='selected' "; }
245 print "value='$month'>$longmonths[$month]</option>\n";
246 }
247 print <<END
248 </select>
249 </td>
250 <td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
251 <td width='40%'>
252 <select name='DAY'>
253 END
254 ;
255 for ($day = 1; $day <= 31; $day++)
256 {
257 print "\t<option ";
258 if ($day == $cgiparams{'DAY'}) {
259 print "selected='selected' "; }
260 print "value='$day'>$day</option>\n";
261 }
262
263 if( $cgiparams{'pienumber'} != 0){$pienumber=$cgiparams{'pienumber'};}
264 if( $cgiparams{'otherspie'} != 0){$otherspie=$cgiparams{'otherspie'};}
265 if( $cgiparams{'showpie'} != 0){$showpie=$cgiparams{'showpie'};}
266 if( $cgiparams{'sortcolumn'} != 0){$sortcolumn=$cgiparams{'sortcolumn'};}
267 print <<END
268 </select>
269 </td>
270 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
271 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
272 <td width='20%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
273 </tr>
274 <tr>
275 <td colspan='3' align='left' valign="left">$Lang::tr{'Number of Countries for the pie chart'}:</td>
276 <td colspan='3' align='left' valign="center"><input type='text' name='pienumber' value='$pienumber' size='4'></td>
277 <td align='right'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
278 </tr>
279 </table>
280 </form>
281 END
282 ;
283
284 &Header::closebox();
285
286 &Header::openbox('100%', 'left', $Lang::tr{'firewall graph country'});
287 print "<p><b>$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines</b></p>";
288
289 my $red_interface = &General::get_red_interface();
290 my $linesjc = 0;
291 my %tabjc;
292
293 if ($pienumber == -1 || $pienumber > $lines || $sortcolumn == 2) { $pienumber = $lines; };
294 $lines = 0;
295
296 foreach $_ (@log)
297 {
298 # If ipv6 uses bridge, use PHYSIN for iface, otherwise IN
299 if (/^... (..) (..:..:..) [\w\-]+ kernel:(.*)(PHYSIN=.*)$/) {}
300 elsif (/^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/) {}
301 my $packet = $4;
302 my $iface = '';
303 if ($packet =~ /PHYSIN=(\w+)/) { $iface = $1; } elsif ($packet =~ /IN=(\w+)/) { $iface = $1; }
304 if ( $1 =~ /2./ ) { $iface=''; }
305 my $srcaddr = '';
306 # Find ipv4 and ipv6 addresses
307 if ($packet =~ /SRC\=(([\d]{1,3})(\.([\d]{1,3})){3})/) { $srcaddr = $1; }
308 elsif ($packet =~ /SRC\=(([0-9a-fA-F]{0,4})(\:([0-9a-fA-F]{0,4})){2,7})/) { $srcaddr = $1; }
309
310 if($iface eq $red_interface) {
311 # Traffic from red
312 if($srcaddr ne '') {
313 # srcaddr is set
314 my $ccode = &Location::Functions::lookup_country_code($libloc_db_handle, $srcaddr);
315 if ($ccode eq '') {
316 $ccode = 'unknown';
317 }
318 $tabjc{$ccode} = $tabjc{$ccode} + 1;
319 if(($tabjc{$ccode} == 1) && ($lines < $pienumber)) { $lines = $lines + 1; }
320 $linesjc++;
321 }
322 }
323 else {
324 # Traffic not from red
325 if($iface ne '') {
326 $tabjc{$iface} = $tabjc{$iface} + 1 ;
327 if(($tabjc{$iface} == 1) && ($lines < $pienumber)) { $lines = $lines + 1; }
328 $linesjc++;
329 }
330 else {
331 # What to do with empty iface lines?
332 # This probably is traffic from ipfire itself (IN= OUT=XY)?
333 }
334 }
335 }
336
337 $pienumber = $lines;
338
339 my @keytabjc = keys %tabjc;
340
341 my @slice;
342 my $go;
343 my $nblinejc;
344
345 if( $cgiparams{'linejc'} eq 'all' ){ $nblinejc = $linesjc; $go=1; }
346 if( ($cgiparams{'linejc'} != 0) && ($cgiparams{'linejc'} ne 'all') ){ $nblinejc = $cgiparams{'linejc'}; $go=1;}
347 if( $go != 1){ $nblinejc = 1000; }
348
349 my @key;
350 my @value;
351 my $indice=0;
352 my @tabjc2;
353
354 if ($sortcolumn == 1) {
355 @tabjc2 = sort { $b <=> $a } values (%tabjc);
356 }
357 else {
358 @tabjc2 = sort { $a <=> $b } keys (%tabjc);
359 }
360
361 my $colour=1;
362
363 ##############################################
364 #pie chart generation
365 use GD::Graph::pie;
366 use GD::Graph::colour;
367 #ips sort by hits number
368 my $v;
369
370 if ($sortcolumn == 1) {
371 for ($v=0;$v<$pienumber;$v++){
372 findkey($tabjc2[$v]);
373 }
374 }
375 else {
376 foreach $v (@tabjc2) {
377 $key[$indice] = $v;
378 $value[$indice] = $tabjc{$v};
379 $indice++;
380 }
381 }
382
383 my @ips;
384 my @numb;
385
386 @ips = @key;
387 @numb = @value;
388
389 my $o;
390
391 if($cgiparams{'otherspie'} == 2 ){}
392 else{
393 my $numothers;
394 for($o=0;$o<$pienumber;$o++){
395 $numothers = $numothers + $numb[$o];
396 }
397 $numothers = $linesjc - $numothers;
398 if ($numothers > 0) {
399 $ips[$pienumber]="$Lang::tr{'otherip'}";
400 $numb[$pienumber] = $numothers;
401 }
402 }
403
404 my @data = (\@ips,\@numb);
405 use GD::Graph::colour qw( :files );
406
407 my $color=0;
408 my %color = ();
409 my %mainsettings = ();
410 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
411 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
412
413 if ($showpie != 2 && $pienumber <= 50 && $pienumber != 0) {
414 my $mygraph = GD::Graph::pie->new(500, 350);
415 $mygraph->set(
416 'title' => '',
417 'pie_height' => 50,
418 'start_angle' => 89
419 ) or warn $mygraph->error;
420
421 $mygraph->set_value_font(GD::gdMediumBoldFont);
422 $mygraph->set( dclrs => [ "$color{'color1'}" , "$color{'color2'}" , "$color{'color3'}" , "$color{'color4'}" , "$color{'color5'}" , "$color{'color6'}" , "$color{'color7'}" , "$color{'color8'}" , "$color{'color9'}" , "$color{'color10'}" ] );
423 my $myimage = $mygraph->plot(\@data) or die $mygraph->error;
424
425 my @filenames = glob("/srv/web/ipfire/html/graphs/fwlog-country*.png");
426 unlink(@filenames);
427 my $imagerandom = rand(1000000);
428 my $imagename = "/srv/web/ipfire/html/graphs/fwlog-country$imagerandom.png";
429 open(FILE,">$imagename");
430 print FILE $myimage->png;
431 close(FILE);
432 #####################################################
433 print "<div style='text-align:center;'>";
434 print "<img src='/graphs/fwlog-country$imagerandom.png'>";
435 print "</div>";
436 }
437 print <<END
438 <table width='100%' class='tbl'>
439 <tr>
440 <th width='10%' align='center' class='boldbase'></th>
441 <th width='30%' align='center' class='boldbase'><b>$Lang::tr{'country'}</b></th>
442 <th width='30%' align='center' class='boldbase'><b>Count</b></th>
443 <th width='30%' align='center' class='boldbase'><b>$Lang::tr{'percentage'}</b></th>
444 </tr>
445 END
446 ;
447
448 my $total=0;
449 my $show=0;
450
451 my $s;
452 my $percent;
453 my $col="";
454
455 for($s=0;$s<$lines;$s++)
456 {
457 $show++;
458 $percent = $value[$s] * 100 / $linesjc;
459 $percent = sprintf("%.f", $percent);
460 $total = $total + $value[$s];
461 # colors are numbered 1 to 10
462 my $colorIndex = ($color % 10) + 1;
463 $col="bgcolor='$color{\"color$colorIndex\"}'";
464 $color++;
465 print "<tr>";
466
467 print "<td align='center' $col>";
468
469 # Dont show details button for "unknown" location.
470 if ($key[$s] ne 'unknown') {
471 print"<form method='post' action='showrequestfromcountry.dat'>";
472 print"<input type='hidden' name='MONTH' value='$cgiparams{'MONTH'}'>";
473 print"<input type='hidden' name='DAY' value='$cgiparams{'DAY'}'>";
474 print"<input type='hidden' name='country' value='$key[$s]'>";
475 print"<input type='submit' value='$Lang::tr{'details'}'></form>";
476 }
477 elsif ($key[$s] eq 'unknown') {
478 print "unknown";
479 }
480 # Looks dangerous to use hardcoded interface names here. Probably needs fixing.
481 if ($key[$s] eq 'blue0' || $key[$s] eq 'green0' || $key[$s] eq 'orange0' ) {
482 print "<td align='center' $col>$key[$s]</td>";
483 }
484 else {
485 my $fcode = lc($key[$s]);
486
487 # Get flag icon for of the country.
488 my $flag_icon = &Location::Functions::get_flag_icon($fcode);
489
490 if($flag_icon) {
491 print "<td align='center' $col><a href='/cgi-bin/country.cgi#$fcode'><img src='$flag_icon' border='0' align='absmiddle' alt='$key[$s]' title='$key[$s]'></a></td>";
492 } else {
493 print "<td align='center' $col>$key[$s]</td>";
494 }
495 }
496 print "<td align='center' $col>$value[$s]</td>";
497 print "<td align='center' $col>$percent</td>";
498 print "</tr>";
499 }
500
501 if($cgiparams{'otherspie'} == 2 ){}
502 else{
503 # colors are numbered 1 to 10
504 my $colorIndex = ($color % 10) + 1;
505 $col="bgcolor='$color{\"color$colorIndex\"}'";
506 print "<tr>";
507
508 if ( $linesjc ne "0")
509 {
510 my $dif;
511 $dif = $linesjc - $total;
512 $percent = $dif * 100 / $linesjc;
513 $percent = sprintf("%.f", $percent);
514 print <<END
515 <td align='center' $col></TD>
516 <td align='center' $col>$Lang::tr{'other countries'}</td>
517 <td align='center' $col>$dif</TD>
518 <td align='center' $col>$percent</TD>
519 </tr>
520 END
521 ;
522 }
523 }
524 print <<END
525 </TABLE>
526 END
527 ;
528
529 &Header::closebox();
530 &Header::closebigbox();
531 &Header::closepage();
532
533 sub findkey {
534 my $v;
535 foreach $v (@keytabjc) {
536 if ($tabjc{$v} eq $_[0]) {
537 delete $tabjc{$v};
538 $key[$indice] = "$v";
539 $value[$indice] = $_[0];
540 $indice++;
541 last;
542 }
543 }
544 }
545 sub checkversion {
546 #Automatic Updates is disabled
547 return "0","0";
548 }
549