]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/logs.cgi/firewalllogcountry.dat
logs.cgi/firewalllog.dat: Use location-functions.pl
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / logs.cgi / firewalllogcountry.dat
CommitLineData
e4aac473
AH
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
43638be5 11# and Michael Tremer (www.ipfire.org)
e4aac473
AH
12
13use strict;
e4aac473
AH
14use Getopt::Std;
15
16# enable only the following on debugging purpose
17#use warnings;
18#use CGI::Carp 'fatalsToBrowser';
19
20require '/var/ipfire/general-functions.pl';
3ea01fe0 21require "${General::swroot}/geoip-functions.pl";
e4aac473
AH
22require "${General::swroot}/lang.pl";
23require "${General::swroot}/header.pl";
24
50494dfd
SS
25# Libloc database handle.
26my $libloc_db_handle = &GeoIP::init();
27
e4aac473
AH
28use POSIX();
29
30my %cgiparams=();
31my %settings=();
32my $pienumber;
33my $otherspie;
34my $showpie;
35my $sortcolumn;
36my $errormessage = '';
37
38$cgiparams{'pienumber'} = 10;
39$cgiparams{'otherspie'} = 1;
40$cgiparams{'showpie'} = 1;
41$cgiparams{'sortcolumn'} = 1;
42
43my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
43638be5 44 'Sep', 'Oct', 'Nov', 'Dec' );
e4aac473 45my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
43638be5
ME
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'} );
e4aac473
AH
49
50my @now = localtime();
51my $dow = $now[6];
52my $doy = $now[7];
53my $tdoy = $now[7];
54my $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);
61if ($settings{'pienumber'} != 0) { $cgiparams{'pienumber'} = $settings{'pienumber'} };
62if ($settings{'otherspie'} != 0) { $cgiparams{'otherspie'} = $settings{'otherspie'} };
63if ($settings{'showpie'} != 0) { $cgiparams{'showpie'} = $settings{'showpie'} };
64if ($settings{'sortcolumn'} != 0) { $cgiparams{'sortcolumn'} = $settings{'sortcolumn'} };
65
66&Header::getcgihash(\%cgiparams);
67if ($cgiparams{'pienumber'} != 0) { $settings{'pienumber'} = $cgiparams{'pienumber'} };
68if ($cgiparams{'otherspie'} != 0) { $settings{'otherspie'} = $cgiparams{'otherspie'} };
69if ($cgiparams{'showpie'} != 0) { $settings{'showpie'} = $cgiparams{'showpie'} };
70if ($cgiparams{'sortcolumn'} != 0) { $settings{'sortcolumn'} = $cgiparams{'sortcolumn'} };
71
72if ($cgiparams{'ACTION'} eq $Lang::tr{'save'})
73{
43638be5 74 &General::writehash("${General::swroot}/fwlogs/ipsettings", \%settings);
e4aac473
AH
75}
76
77my $start = -1;
78if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
79{
43638be5
ME
80 my @temp = split(',',$ENV{'QUERY_STRING'});
81 $start = $temp[0];
82 $cgiparams{'MONTH'} = $temp[1];
83 $cgiparams{'DAY'} = $temp[2];
e4aac473
AH
84}
85
86if (!($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{
43638be5
ME
89 $cgiparams{'DAY'} = $now[3];
90 $cgiparams{'MONTH'} = $now[4];
e4aac473
AH
91}
92elsif($cgiparams{'ACTION'} eq '>>')
93{
43638be5
ME
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];
e4aac473
AH
103}
104elsif($cgiparams{'ACTION'} eq '<<')
105{
43638be5
ME
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];
e4aac473
AH
115}
116
117if (($cgiparams{'DAY'} ne $now[3]) || ($cgiparams{'MONTH'} ne $now[4]))
118{
43638be5
ME
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 }
e4aac473
AH
135}
136
137my $datediff=0;
138my $dowd=0;
139my $multifile=0;
140if ($tdoy ne $doy) {
43638be5
ME
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 }
e4aac473
AH
149}
150
151my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
152my $longmonthstr = $longmonths[$cgiparams{'MONTH'}];
153my $day = $cgiparams{'DAY'};
154my $daystr='';
155if ($day <= 9) {
43638be5
ME
156 $daystr = " $day";
157}
e4aac473 158else {
43638be5 159 $daystr = $day;
e4aac473
AH
160}
161
162my $skip=0;
163my $filestr='';
164if ($datediff==0) {
43638be5 165 $filestr="/var/log/messages";
e4aac473 166} else {
43638be5
ME
167 $filestr="/var/log/messages.$datediff";
168 $filestr = "$filestr.gz" if -f "$filestr.gz";
e4aac473
AH
169}
170
171if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
43638be5
ME
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
e4aac473
AH
175}
176my $lines = 0;
177my @log=();
178
179if (!$skip)
180{
43638be5
ME
181 while (<FILE>)
182 {
183 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
184 $log[$lines] = $_;
185 $lines++;
186 }
187 }
188 close (FILE);
e4aac473
AH
189}
190
191$skip=0;
192if ($multifile) {
43638be5
ME
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 }
e4aac473
AH
213}
214
215my $MODNAME="fwlogs";
216
217&Header::showhttpheaders();
1bea8be2 218&Header::openpage($Lang::tr{'firewall log country'}, 1, '');
e4aac473
AH
219&Header::openbigbox('100%', 'left', '', $errormessage);
220
221
222if ($errormessage) {
43638be5
ME
223 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
224 print "<font class='base'>$errormessage&nbsp;</font>\n";
225 &Header::closebox();
e4aac473
AH
226}
227
f4248975 228&Header::openbox('100%', 'left', "$Lang::tr{'settings'}");
e4aac473
AH
229
230print <<END
231<form method='post' action='$ENV{'SCRIPT_NAME'}'>
232<table width='100%'>
233<tr>
43638be5
ME
234 <td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
235 <td width='10%'>
236 <select name='MONTH'>
e4aac473
AH
237END
238;
239my $month;
240for ($month = 0; $month < 12; $month++)
241{
43638be5
ME
242 print "\t<option ";
243 if ($month == $cgiparams{'MONTH'}) {
244 print "selected='selected' "; }
245 print "value='$month'>$longmonths[$month]</option>\n";
e4aac473
AH
246}
247print <<END
43638be5
ME
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'>
e4aac473
AH
253END
254;
255for ($day = 1; $day <= 31; $day++)
256{
43638be5
ME
257 print "\t<option ";
258 if ($day == $cgiparams{'DAY'}) {
259 print "selected='selected' "; }
260 print "value='$day'>$day</option>\n";
e4aac473
AH
261}
262
263if( $cgiparams{'pienumber'} != 0){$pienumber=$cgiparams{'pienumber'};}
264if( $cgiparams{'otherspie'} != 0){$otherspie=$cgiparams{'otherspie'};}
265if( $cgiparams{'showpie'} != 0){$showpie=$cgiparams{'showpie'};}
266if( $cgiparams{'sortcolumn'} != 0){$sortcolumn=$cgiparams{'sortcolumn'};}
e4aac473
AH
267print <<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>
f4248975
AH
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>
e4aac473
AH
278</tr>
279</table>
f4248975 280</form>
e4aac473
AH
281END
282;
283
284&Header::closebox();
285
1bea8be2 286&Header::openbox('100%', 'left', $Lang::tr{'firewall graph country'});
e4aac473
AH
287print "<p><b>$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines</b></p>";
288
4fca9c99 289my $red_interface = &General::get_red_interface();
e4aac473
AH
290my $linesjc = 0;
291my %tabjc;
e4aac473
AH
292
293if ($pienumber == -1 || $pienumber > $lines || $sortcolumn == 2) { $pienumber = $lines; };
294$lines = 0;
4fca9c99 295
e4aac473
AH
296foreach $_ (@log)
297{
43638be5
ME
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
50494dfd 314 my $ccode = &GeoIP::lookup_country_code($libloc_db_handle, $srcaddr);
43638be5
ME
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 }
e4aac473
AH
335}
336
337$pienumber = $lines;
338
339my @keytabjc = keys %tabjc;
340
341my @slice;
342my $go;
343my $nblinejc;
344
345if( $cgiparams{'linejc'} eq 'all' ){ $nblinejc = $linesjc; $go=1; }
346if( ($cgiparams{'linejc'} != 0) && ($cgiparams{'linejc'} ne 'all') ){ $nblinejc = $cgiparams{'linejc'}; $go=1;}
347if( $go != 1){ $nblinejc = 1000; }
348
349my @key;
350my @value;
351my $indice=0;
352my @tabjc2;
353
43638be5
ME
354if ($sortcolumn == 1) {
355 @tabjc2 = sort { $b <=> $a } values (%tabjc);
e4aac473 356}
43638be5
ME
357else {
358 @tabjc2 = sort { $a <=> $b } keys (%tabjc);
e4aac473
AH
359}
360
361my $colour=1;
362
363##############################################
364#pie chart generation
365use GD::Graph::pie;
366use GD::Graph::colour;
367#ips sort by hits number
368my $v;
369
43638be5
ME
370if ($sortcolumn == 1) {
371 for ($v=0;$v<$pienumber;$v++){
372 findkey($tabjc2[$v]);
373 }
e4aac473 374}
43638be5
ME
375else {
376 foreach $v (@tabjc2) {
377 $key[$indice] = $v;
378 $value[$indice] = $tabjc{$v};
379 $indice++;
380 }
e4aac473
AH
381}
382
383my @ips;
384my @numb;
385
386@ips = @key;
387@numb = @value;
388
389my $o;
390
391if($cgiparams{'otherspie'} == 2 ){}
392else{
43638be5
ME
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 }
e4aac473
AH
402}
403
404my @data = (\@ips,\@numb);
405use GD::Graph::colour qw( :files );
406
407my $color=0;
408my %color = ();
409my %mainsettings = ();
410&General::readhash("${General::swroot}/main/settings", \%mainsettings);
411&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
412
413if ($showpie != 2 && $pienumber <= 50 && $pienumber != 0) {
43638be5
ME
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>";
e4aac473 436}
e4aac473 437print <<END
f4248975
AH
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>
e4aac473
AH
445END
446;
447
448my $total=0;
449my $show=0;
450
451my $s;
452my $percent;
f4248975 453my $col="";
e4aac473
AH
454
455for($s=0;$s<$lines;$s++)
456{
43638be5
ME
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]'>";
1bea8be2 475 print"<input type='submit' value='$Lang::tr{'details'}'></form>";
43638be5
ME
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' ) {
3ea01fe0 482 print "<td align='center' $col>$key[$s]</td>";
43638be5
ME
483 }
484 else {
485 my $fcode = lc($key[$s]);
486
487 # Get flag icon for of the country.
488 my $flag_icon = &GeoIP::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>";
f4248975 499}
e4aac473
AH
500
501if($cgiparams{'otherspie'} == 2 ){}
502else{
43638be5
ME
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>
e4aac473 520END
43638be5
ME
521 ;
522 }
e4aac473
AH
523}
524print <<END
525</TABLE>
526END
527;
528
529&Header::closebox();
530&Header::closebigbox();
531&Header::closepage();
532
533sub findkey {
43638be5
ME
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 }
e4aac473
AH
544}
545sub checkversion {
43638be5
ME
546 #Automatic Updates is disabled
547 return "0","0";
e4aac473
AH
548}
549