]>
git.ipfire.org Git - 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)
14 use Geo
::IP
::PurePerl
;
17 # enable only the following on debugging purpose
19 #use CGI::Carp 'fatalsToBrowser';
21 require '/var/ipfire/general-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'};
161 $filestr="/var/log/messages";
163 $filestr="/var/log/messages.$datediff";
164 $filestr = "$filestr.gz" if -f
"$filestr.gz";
167 if (!(open (FILE
,($filestr =~ /.gz$/ ?
"gzip -dc $filestr |" : $filestr)))) {
168 $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
170 # Note: This is in case the log does not exist for that date
179 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
189 $datediff=$datediff-1;
191 $filestr="/var/log/messages";
193 $filestr="/var/log/messages.$datediff";
194 $filestr = "$filestr.gz" if -f
"$filestr.gz";
196 if (!(open (FILE
,($filestr =~ /.gz$/ ?
"gzip -dc $filestr |" : $filestr)))) {
197 $errormessage="$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
202 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
211 my $MODNAME="fwlogs";
213 &Header
::showhttpheaders
();
214 &Header
::openpage
($Lang::tr
{'firewall log'}, 1, '');
215 &Header
::openbigbox
('100%', 'left', '', $errormessage);
219 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
220 print "<font class='base'>$errormessage </font>\n";
224 &Header
::openbox
('100%', 'left', "$Lang::tr{'settings'}");
227 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
230 <td width='10%' class='base'>$Lang::tr{'month'}: </td>
232 <select name='MONTH'>
236 for ($month = 0; $month < 12; $month++)
239 if ($month == $cgiparams{'MONTH'}) {
240 print "selected='selected' "; }
241 print "value='$month'>$longmonths[$month]</option>\n";
246 <td width='10%' class='base' align='right'> $Lang::tr{'day'}: </td>
251 for ($day = 1; $day <= 31; $day++)
254 if ($day == $cgiparams{'DAY'}) {
255 print "selected='selected' "; }
256 print "value='$day'>$day</option>\n";
259 if( $cgiparams{'pienumber'} != 0){$pienumber=$cgiparams{'pienumber'};}
260 if( $cgiparams{'otherspie'} != 0){$otherspie=$cgiparams{'otherspie'};}
261 if( $cgiparams{'showpie'} != 0){$showpie=$cgiparams{'showpie'};}
262 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', 'Firewall Logs');
284 print "<p><b>$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines</b></p>";
286 my $red_interface = &General
::get_red_interface
();
289 my $gi = Geo
::IP
::PurePerl
->new();
291 if ($pienumber == -1 || $pienumber > $lines || $sortcolumn == 2) { $pienumber = $lines; };
296 /^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/;
298 $packet =~ /IN=(\w+)/; my $iface=$1; if ( $1 =~ /2./ ){ $iface="";}
299 $packet =~ /SRC=([\d\.]+)/; my $srcaddr=$1;
301 if($iface eq $red_interface) {
303 my $ccode = $gi->country_code_by_name($srcaddr);
307 $tabjc{$ccode} = $tabjc{$ccode} + 1 ;
308 if(($tabjc{$ccode} == 1) && ($lines < $pienumber)) { $lines = $lines + 1; }
314 $tabjc{$iface} = $tabjc{$iface} + 1 ;
315 if(($tabjc{$iface} == 1) && ($lines < $pienumber)) { $lines = $lines + 1; }
323 my @keytabjc = keys %tabjc;
329 if( $cgiparams{'linejc'} eq 'all' ){ $nblinejc = $linesjc; $go=1; }
330 if( ($cgiparams{'linejc'} != 0) && ($cgiparams{'linejc'} ne 'all') ){ $nblinejc = $cgiparams{'linejc'}; $go=1;}
331 if( $go != 1){ $nblinejc = 1000; }
338 if ($sortcolumn == 1)
340 @tabjc2 = sort { $b <=> $a } values (%tabjc);
344 @tabjc2 = sort { $a <=> $b } keys (%tabjc);
349 ##############################################
350 #pie chart generation
352 use GD
::Graph
::colour
;
353 #ips sort by hits number
356 if ($sortcolumn == 1)
358 for ($v=0;$v<$pienumber;$v++){
359 findkey
($tabjc2[$v]);
364 foreach $v (@tabjc2) {
366 $value[$indice] = $tabjc{$v};
379 if($cgiparams{'otherspie'} == 2 ){}
382 for($o=0;$o<$pienumber;$o++){
383 $numothers = $numothers + $numb[$o];
385 $numothers = $linesjc - $numothers;
386 if ($numothers > 0) {
387 $ips[$pienumber]="$Lang::tr{'otherip'}";
388 $numb[$pienumber] = $numothers;
392 my @data = (\
@ips,\
@numb);
393 use GD
::Graph
::colour
qw( :files );
397 my %mainsettings = ();
398 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
399 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
401 if ($showpie != 2 && $pienumber <= 50 && $pienumber != 0) {
402 my $mygraph = GD
::Graph
::pie
->new(500, 350);
407 ) or warn $mygraph->error;
409 $mygraph->set_value_font(GD
::gdMediumBoldFont
);
410 $mygraph->set( dclrs
=> [ "$color{'color1'}" , "$color{'color2'}" , "$color{'color3'}" , "$color{'color4'}" , "$color{'color5'}" , "$color{'color6'}" , "$color{'color7'}" , "$color{'color8'}" , "$color{'color9'}" , "$color{'color10'}" ] );
411 my $myimage = $mygraph->plot(\
@data) or die $mygraph->error;
413 my @filenames = glob("/srv/web/ipfire/html/graphs/fwlog-country*.png");
415 my $imagerandom = rand(1000000);
416 my $imagename = "/srv/web/ipfire/html/graphs/fwlog-country$imagerandom.png";
417 open(FILE
,">$imagename");
418 print FILE
$myimage->png;
420 #####################################################
421 print "<div style='text-align:center;'>";
422 print "<img src='/graphs/fwlog-country$imagerandom.png'>";
427 <table width='100%' class='tbl'>
429 <th width='10%' align='center' class='boldbase'></th>
430 <th width='30%' align='center' class='boldbase'><b>$Lang::tr{'country'}</b></th>
431 <th width='30%' align='center' class='boldbase'><b>Count</b></th>
432 <th width='30%' align='center' class='boldbase'><b>$Lang::tr{'percentage'}</b></th>
444 for($s=0;$s<$lines;$s++)
447 $percent = $value[$s] * 100 / $linesjc;
448 $percent = sprintf("%.f", $percent);
449 $total = $total + $value[$s];
450 my $colorIndex = $color % 10;
451 if($colorIndex == 0) {
454 $col="bgcolor='$color{\"color$colorIndex\"}'";
458 print "<td align='center' $col><form method='post' action='showrequestfromcountry.dat'><input type='hidden' name='MONTH' value='$cgiparams{'MONTH'}'> <input type='hidden' name='DAY' value='$cgiparams{'DAY'}'> <input type='hidden' name='country' value='$key[$s]'> <input type='submit' value='details'></form></td>";
459 if($key[$s] eq 'blue0' || $key[$s] eq 'green0' || $key[$s] eq 'orange0') {
460 print "<td align='center' $col>$key[$s]</td>";
463 if($key[$s] ne 'unknown' ) {
464 my $fcode = lc($key[$s]);
465 print "<td align='center' $col><a href='/cgi-bin/country.cgi#$fcode'><img src='/images/flags/$fcode.png' border='0' align='absmiddle' alt='$key[$s]' title='$key[$s]'></a></td>";}
467 print "<td align='center' $col>$key[$s]</td>";
470 print "<td align='center' $col>$value[$s]</td>";
471 print "<td align='center' $col>$percent</td>";
475 if($cgiparams{'otherspie'} == 2 ){}
477 my $colorIndex = $color % 10;
478 if($colorIndex == 0) {
481 $col="bgcolor='$color{\"color$colorIndex\"}'";
484 if ( $linesjc ne "0")
487 $dif = $linesjc - $total;
488 $percent = $dif * 100 / $linesjc;
489 $percent = sprintf("%.f", $percent);
491 <td align='center' $col></TD>
492 <td align='center' $col>$Lang::tr{'other countries'}</td>
493 <td align='center' $col>$dif</TD>
494 <td align='center' $col>$percent</TD>
506 &Header
::closebigbox
();
507 &Header
::closepage
();
511 foreach $v (@keytabjc) {
512 if ($tabjc{$v} eq $_[0]) {
514 $key[$indice] = "$v";
515 $value[$indice] = $_[0];
522 #Automatic Updates is disabled