]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/logs.cgi/firewalllogcountry.dat
af142790870b24eebcb8edc80cbce4415149f195
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>";
288 my $gi = Geo
::IP
::PurePerl
->new();
290 if ($pienumber == -1 || $pienumber > $lines || $sortcolumn == 2) { $pienumber = $lines; };
294 /^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/;
296 $packet =~ /IN=(\w+)/; my $iface=$1; if ( $1 =~ /2./ ){ $iface="";}
297 $packet =~ /SRC=([\d\.]+)/; my $srcaddr=$1;
299 if($iface eq 'red0') {
301 my $ccode = $gi->country_code_by_name($srcaddr);
305 $tabjc{$ccode} = $tabjc{$ccode} + 1 ;
306 if(($tabjc{$ccode} == 1) && ($lines < $pienumber)) { $lines = $lines + 1; }
312 $tabjc{$iface} = $tabjc{$iface} + 1 ;
313 if(($tabjc{$iface} == 1) && ($lines < $pienumber)) { $lines = $lines + 1; }
321 my @keytabjc = keys %tabjc;
327 if( $cgiparams{'linejc'} eq 'all' ){ $nblinejc = $linesjc; $go=1; }
328 if( ($cgiparams{'linejc'} != 0) && ($cgiparams{'linejc'} ne 'all') ){ $nblinejc = $cgiparams{'linejc'}; $go=1;}
329 if( $go != 1){ $nblinejc = 1000; }
336 if ($sortcolumn == 1)
338 @tabjc2 = sort { $b <=> $a } values (%tabjc);
342 @tabjc2 = sort { $a <=> $b } keys (%tabjc);
347 ##############################################
348 #pie chart generation
350 use GD
::Graph
::colour
;
351 #ips sort by hits number
354 if ($sortcolumn == 1)
356 for ($v=0;$v<$pienumber;$v++){
357 findkey
($tabjc2[$v]);
362 foreach $v (@tabjc2) {
364 $value[$indice] = $tabjc{$v};
377 if($cgiparams{'otherspie'} == 2 ){}
380 for($o=0;$o<$pienumber;$o++){
381 $numothers = $numothers + $numb[$o];
383 $numothers = $linesjc - $numothers;
384 if ($numothers > 0) {
385 $ips[$pienumber]="$Lang::tr{'otherip'}";
386 $numb[$pienumber] = $numothers;
390 my @data = (\
@ips,\
@numb);
391 use GD
::Graph
::colour
qw( :files );
395 my %mainsettings = ();
396 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
397 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
399 if ($showpie != 2 && $pienumber <= 50 && $pienumber != 0) {
400 my $mygraph = GD
::Graph
::pie
->new(500, 350);
405 ) or warn $mygraph->error;
407 $mygraph->set_value_font(GD
::gdMediumBoldFont
);
408 $mygraph->set( dclrs
=> [ "$color{'color1'}" , "$color{'color2'}" , "$color{'color3'}" , "$color{'color4'}" , "$color{'color5'}" , "$color{'color6'}" , "$color{'color7'}" , "$color{'color8'}" , "$color{'color9'}" , "$color{'color10'}" ] );
409 my $myimage = $mygraph->plot(\
@data) or die $mygraph->error;
411 my @filenames = glob("/srv/web/ipfire/html/graphs/fwlog-country*.png");
413 my $imagerandom = rand(1000000);
414 my $imagename = "/srv/web/ipfire/html/graphs/fwlog-country$imagerandom.png";
415 open(FILE
,">$imagename");
416 print FILE
$myimage->png;
418 #####################################################
419 print "<div style='text-align:center;'>";
420 print "<img src='/graphs/fwlog-country$imagerandom.png'>";
425 <table width='100%' class='tbl'>
427 <th width='10%' align='center' class='boldbase'></th>
428 <th width='30%' align='center' class='boldbase'><b>$Lang::tr{'country'}</b></th>
429 <th width='30%' align='center' class='boldbase'><b>Count</b></th>
430 <th width='30%' align='center' class='boldbase'><b>$Lang::tr{'percentage'}</b></th>
442 for($s=0;$s<$lines;$s++)
445 $percent = $value[$s] * 100 / $linesjc;
446 $percent = sprintf("%.f", $percent);
447 $total = $total + $value[$s];
448 my $colorIndex = $color % 10;
449 if($colorIndex == 0) {
452 $col="bgcolor='$color{\"color$colorIndex\"}'";
456 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>";
457 if($key[$s] eq 'blue0' || $key[$s] eq 'green0' || $key[$s] eq 'orange0') {
458 print "<td align='center' $col>$key[$s]</td>";
461 if($key[$s] ne 'unknown' ) {
462 my $fcode = lc($key[$s]);
463 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>";}
465 print "<td align='center' $col>$key[$s]</td>";
468 print "<td align='center' $col>$value[$s]</td>";
469 print "<td align='center' $col>$percent</td>";
473 if($cgiparams{'otherspie'} == 2 ){}
475 my $colorIndex = $color % 10;
476 if($colorIndex == 0) {
479 $col="bgcolor='$color{\"color$colorIndex\"}'";
482 if ( $linesjc ne "0")
485 $dif = $linesjc - $total;
486 $percent = $dif * 100 / $linesjc;
487 $percent = sprintf("%.f", $percent);
489 <td align='center' $col></TD>
490 <td align='center' $col>$Lang::tr{'other countries'}</td>
491 <td align='center' $col>$dif</TD>
492 <td align='center' $col>$percent</TD>
504 &Header
::closebigbox
();
505 &Header
::closepage
();
509 foreach $v (@keytabjc) {
510 if ($tabjc{$v} eq $_[0]) {
512 $key[$indice] = "$v";
513 $value[$indice] = $_[0];
520 #Automatic Updates is disabled