]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/logs.cgi/firewalllogcountry.dat
5 # This code is distributed under the terms of the GPL
8 # page inspired from the initial firewalllog.dat
10 # Modified for IPFire by Christian Schmidt
11 # and Michael Tremer (www.ipfire.org)
16 # enable only the following on debugging purpose
18 #use CGI::Carp 'fatalsToBrowser';
20 require '/var/ipfire/general-functions.pl';
21 require "${General::swroot}/geoip-functions.pl";
22 require "${General::swroot}/lang.pl";
23 require "${General::swroot}/header.pl";
33 my $errormessage = '';
35 $cgiparams{'pienumber'} = 10;
36 $cgiparams{'otherspie'} = 1;
37 $cgiparams{'showpie'} = 1;
38 $cgiparams{'sortcolumn'} = 1;
40 my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
41 'Sep', 'Oct', 'Nov', 'Dec' );
42 my @longmonths = ( $Lang::tr
{'january'}, $Lang::tr
{'february'}, $Lang::tr
{'march'},
43 $Lang::tr
{'april'}, $Lang::tr
{'may'}, $Lang::tr
{'june'}, $Lang::tr
{'july'}, $Lang::tr
{'august'},
44 $Lang::tr
{'september'}, $Lang::tr
{'october'}, $Lang::tr
{'november'},
45 $Lang::tr
{'december'} );
47 my @now = localtime();
51 my $year = $now[5]+1900;
53 $cgiparams{'DAY'} = $now[3];
54 $cgiparams{'MONTH'} = $now[4];
55 $cgiparams{'ACTION'} = '';
57 &General
::readhash
("${General::swroot}/fwlogs/ipsettings", \
%settings);
58 if ($settings{'pienumber'} != 0) { $cgiparams{'pienumber'} = $settings{'pienumber'} };
59 if ($settings{'otherspie'} != 0) { $cgiparams{'otherspie'} = $settings{'otherspie'} };
60 if ($settings{'showpie'} != 0) { $cgiparams{'showpie'} = $settings{'showpie'} };
61 if ($settings{'sortcolumn'} != 0) { $cgiparams{'sortcolumn'} = $settings{'sortcolumn'} };
63 &Header
::getcgihash
(\
%cgiparams);
64 if ($cgiparams{'pienumber'} != 0) { $settings{'pienumber'} = $cgiparams{'pienumber'} };
65 if ($cgiparams{'otherspie'} != 0) { $settings{'otherspie'} = $cgiparams{'otherspie'} };
66 if ($cgiparams{'showpie'} != 0) { $settings{'showpie'} = $cgiparams{'showpie'} };
67 if ($cgiparams{'sortcolumn'} != 0) { $settings{'sortcolumn'} = $cgiparams{'sortcolumn'} };
69 if ($cgiparams{'ACTION'} eq $Lang::tr
{'save'})
71 &General
::writehash
("${General::swroot}/fwlogs/ipsettings", \
%settings);
75 if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr
{'update'})
77 my @temp = split(',',$ENV{'QUERY_STRING'});
79 $cgiparams{'MONTH'} = $temp[1];
80 $cgiparams{'DAY'} = $temp[2];
83 if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
84 !($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)$/))
86 $cgiparams{'DAY'} = $now[3];
87 $cgiparams{'MONTH'} = $now[4];
89 elsif($cgiparams{'ACTION'} eq '>>')
92 my @temp_now = localtime(time);
93 $temp_now[4] = $cgiparams{'MONTH'};
94 $temp_now[3] = $cgiparams{'DAY'};
95 @temp_then = localtime(POSIX
::mktime
(@temp_now) + 86400);
96 ## Retrieve the same time on the next day -
97 ## 86400 seconds in a day
98 $cgiparams{'MONTH'} = $temp_then[4];
99 $cgiparams{'DAY'} = $temp_then[3];
101 elsif($cgiparams{'ACTION'} eq '<<')
104 my @temp_now = localtime(time);
105 $temp_now[4] = $cgiparams{'MONTH'};
106 $temp_now[3] = $cgiparams{'DAY'};
107 @temp_then = localtime(POSIX
::mktime
(@temp_now) - 86400);
108 ## Retrieve the same time on the previous day -
109 ## 86400 seconds in a day
110 $cgiparams{'MONTH'} = $temp_then[4];
111 $cgiparams{'DAY'} = $temp_then[3];
114 if (($cgiparams{'DAY'} ne $now[3]) || ($cgiparams{'MONTH'} ne $now[4]))
117 if ( ( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
118 ( $cgiparams{'MONTH'} > $now[4] ) ) {
119 @then = localtime(POSIX
::mktime
( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 ));
121 @then = localtime(POSIX
::mktime
( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 ));
124 my $lastleap=($year-1)%4;
126 if ($lastleap == 0 && $tdoy < 60) {
138 $datediff=int(($doy-$tdoy)/7);
139 $dowd=($doy-$tdoy)%7;
140 if (($dow-$dowd)<1) {
141 $datediff=$datediff+1;
143 if (($dow-$dowd)==0) {
148 my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
149 my $longmonthstr = $longmonths[$cgiparams{'MONTH'}];
150 my $day = $cgiparams{'DAY'};
162 $filestr="/var/log/messages";
164 $filestr="/var/log/messages.$datediff";
165 $filestr = "$filestr.gz" if -f
"$filestr.gz";
168 if (!(open (FILE
,($filestr =~ /.gz$/ ?
"gzip -dc $filestr |" : $filestr)))) {
169 $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
171 # Note: This is in case the log does not exist for that date
180 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
190 $datediff=$datediff-1;
192 $filestr="/var/log/messages";
194 $filestr="/var/log/messages.$datediff";
195 $filestr = "$filestr.gz" if -f
"$filestr.gz";
197 if (!(open (FILE
,($filestr =~ /.gz$/ ?
"gzip -dc $filestr |" : $filestr)))) {
198 $errormessage="$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
203 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
212 my $MODNAME="fwlogs";
214 &Header
::showhttpheaders
();
215 &Header
::openpage
($Lang::tr
{'firewall log country'}, 1, '');
216 &Header
::openbigbox
('100%', 'left', '', $errormessage);
220 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
221 print "<font class='base'>$errormessage </font>\n";
225 &Header
::openbox
('100%', 'left', "$Lang::tr{'settings'}");
228 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
231 <td width='10%' class='base'>$Lang::tr{'month'}: </td>
233 <select name='MONTH'>
237 for ($month = 0; $month < 12; $month++)
240 if ($month == $cgiparams{'MONTH'}) {
241 print "selected='selected' "; }
242 print "value='$month'>$longmonths[$month]</option>\n";
247 <td width='10%' class='base' align='right'> $Lang::tr{'day'}: </td>
252 for ($day = 1; $day <= 31; $day++)
255 if ($day == $cgiparams{'DAY'}) {
256 print "selected='selected' "; }
257 print "value='$day'>$day</option>\n";
260 if( $cgiparams{'pienumber'} != 0){$pienumber=$cgiparams{'pienumber'};}
261 if( $cgiparams{'otherspie'} != 0){$otherspie=$cgiparams{'otherspie'};}
262 if( $cgiparams{'showpie'} != 0){$showpie=$cgiparams{'showpie'};}
263 if( $cgiparams{'sortcolumn'} != 0){$sortcolumn=$cgiparams{'sortcolumn'};}
267 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='<<' /></td>
268 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='>>' /></td>
269 <td width='20%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
272 <td colspan='3' align='left' valign="left">$Lang::tr{'Number of Countries for the pie chart'}:</td>
273 <td colspan='3' align='left' valign="center"><input type='text' name='pienumber' value='$pienumber' size='4'></td>
274 <td align='right'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
283 &Header
::openbox
('100%', 'left', $Lang::tr
{'firewall graph country'});
284 print "<p><b>$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines</b></p>";
286 my $red_interface = &General
::get_red_interface
();
290 if ($pienumber == -1 || $pienumber > $lines || $sortcolumn == 2) { $pienumber = $lines; };
295 # If ipv6 uses bridge, use PHYSIN for iface, otherwise IN
296 if (/^... (..) (..:..:..) [\w\-]+ kernel:(.*)(PHYSIN=.*)$/) {}
297 elsif (/^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/) {}
300 if ($packet =~ /PHYSIN=(\w+)/) { $iface = $1; } elsif ($packet =~ /IN=(\w+)/) { $iface = $1; }
301 if ( $1 =~ /2./ ) { $iface=''; }
303 # Find ipv4 and ipv6 addresses
304 if ($packet =~ /SRC\=(([\d]{1,3})(\.([\d]{1,3})){3})/) { $srcaddr = $1; }
305 elsif ($packet =~ /SRC\=(([0-9a-fA-F]{0,4})(\:([0-9a-fA-F]{0,4})){2,7})/) { $srcaddr = $1; }
307 if($iface eq $red_interface) {
311 my $ccode = &GeoIP
::lookup
($srcaddr);
315 $tabjc{$ccode} = $tabjc{$ccode} + 1;
316 if(($tabjc{$ccode} == 1) && ($lines < $pienumber)) { $lines = $lines + 1; }
321 # Traffic not from red
323 $tabjc{$iface} = $tabjc{$iface} + 1 ;
324 if(($tabjc{$iface} == 1) && ($lines < $pienumber)) { $lines = $lines + 1; }
328 # What to do with empty iface lines?
329 # This probably is traffic from ipfire itself (IN= OUT=XY)?
336 my @keytabjc = keys %tabjc;
342 if( $cgiparams{'linejc'} eq 'all' ){ $nblinejc = $linesjc; $go=1; }
343 if( ($cgiparams{'linejc'} != 0) && ($cgiparams{'linejc'} ne 'all') ){ $nblinejc = $cgiparams{'linejc'}; $go=1;}
344 if( $go != 1){ $nblinejc = 1000; }
351 if ($sortcolumn == 1) {
352 @tabjc2 = sort { $b <=> $a } values (%tabjc);
355 @tabjc2 = sort { $a <=> $b } keys (%tabjc);
360 ##############################################
361 #pie chart generation
363 use GD
::Graph
::colour
;
364 #ips sort by hits number
367 if ($sortcolumn == 1) {
368 for ($v=0;$v<$pienumber;$v++){
369 findkey
($tabjc2[$v]);
373 foreach $v (@tabjc2) {
375 $value[$indice] = $tabjc{$v};
388 if($cgiparams{'otherspie'} == 2 ){}
391 for($o=0;$o<$pienumber;$o++){
392 $numothers = $numothers + $numb[$o];
394 $numothers = $linesjc - $numothers;
395 if ($numothers > 0) {
396 $ips[$pienumber]="$Lang::tr{'otherip'}";
397 $numb[$pienumber] = $numothers;
401 my @data = (\
@ips,\
@numb);
402 use GD
::Graph
::colour
qw( :files );
406 my %mainsettings = ();
407 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
408 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
410 if ($showpie != 2 && $pienumber <= 50 && $pienumber != 0) {
411 my $mygraph = GD
::Graph
::pie
->new(500, 350);
416 ) or warn $mygraph->error;
418 $mygraph->set_value_font(GD
::gdMediumBoldFont
);
419 $mygraph->set( dclrs
=> [ "$color{'color1'}" , "$color{'color2'}" , "$color{'color3'}" , "$color{'color4'}" , "$color{'color5'}" , "$color{'color6'}" , "$color{'color7'}" , "$color{'color8'}" , "$color{'color9'}" , "$color{'color10'}" ] );
420 my $myimage = $mygraph->plot(\
@data) or die $mygraph->error;
422 my @filenames = glob("/srv/web/ipfire/html/graphs/fwlog-country*.png");
424 my $imagerandom = rand(1000000);
425 my $imagename = "/srv/web/ipfire/html/graphs/fwlog-country$imagerandom.png";
426 open(FILE
,">$imagename");
427 print FILE
$myimage->png;
429 #####################################################
430 print "<div style='text-align:center;'>";
431 print "<img src='/graphs/fwlog-country$imagerandom.png'>";
435 <table width='100%' class='tbl'>
437 <th width='10%' align='center' class='boldbase'></th>
438 <th width='30%' align='center' class='boldbase'><b>$Lang::tr{'country'}</b></th>
439 <th width='30%' align='center' class='boldbase'><b>Count</b></th>
440 <th width='30%' align='center' class='boldbase'><b>$Lang::tr{'percentage'}</b></th>
452 for($s=0;$s<$lines;$s++)
455 $percent = $value[$s] * 100 / $linesjc;
456 $percent = sprintf("%.f", $percent);
457 $total = $total + $value[$s];
458 # colors are numbered 1 to 10
459 my $colorIndex = ($color % 10) + 1;
460 $col="bgcolor='$color{\"color$colorIndex\"}'";
464 print "<td align='center' $col>";
466 # Dont show details button for "unknown" location.
467 if ($key[$s] ne 'unknown') {
468 print"<form method='post' action='showrequestfromcountry.dat'>";
469 print"<input type='hidden' name='MONTH' value='$cgiparams{'MONTH'}'>";
470 print"<input type='hidden' name='DAY' value='$cgiparams{'DAY'}'>";
471 print"<input type='hidden' name='country' value='$key[$s]'>";
472 print"<input type='submit' value='$Lang::tr{'details'}'></form>";
474 elsif ($key[$s] eq 'unknown') {
477 # Looks dangerous to use hardcoded interface names here. Probably needs fixing.
478 if ($key[$s] eq 'blue0' || $key[$s] eq 'green0' || $key[$s] eq 'orange0' ) {
479 print "<td align='center' $col>$key[$s]</td>";
482 my $fcode = lc($key[$s]);
484 # Get flag icon for of the country.
485 my $flag_icon = &GeoIP
::get_flag_icon
($fcode);
488 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>";
490 print "<td align='center' $col>$key[$s]</td>";
493 print "<td align='center' $col>$value[$s]</td>";
494 print "<td align='center' $col>$percent</td>";
498 if($cgiparams{'otherspie'} == 2 ){}
500 # colors are numbered 1 to 10
501 my $colorIndex = ($color % 10) + 1;
502 $col="bgcolor='$color{\"color$colorIndex\"}'";
505 if ( $linesjc ne "0")
508 $dif = $linesjc - $total;
509 $percent = $dif * 100 / $linesjc;
510 $percent = sprintf("%.f", $percent);
512 <td align='center' $col></TD>
513 <td align='center' $col>$Lang::tr{'other countries'}</td>
514 <td align='center' $col>$dif</TD>
515 <td align='center' $col>$percent</TD>
527 &Header
::closebigbox
();
528 &Header
::closepage
();
532 foreach $v (@keytabjc) {
533 if ($tabjc{$v} eq $_[0]) {
535 $key[$indice] = "$v";
536 $value[$indice] = $_[0];
543 #Automatic Updates is disabled