"Update Booster" fertiggestellt und getestet.
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / logs.cgi / firewalllogip.dat
CommitLineData
76dd79d1
MT
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#
6bc302d1
CS
10# Modified for IPFire by Christian Schmidt
11# and Michael Tremer (www.ipfire.org)
76dd79d1
MT
12
13use strict;
14use Geo::IP::PurePerl;
15use Getopt::Std;
16
17# enable only the following on debugging purpose
18#use warnings;
19#use CGI::Carp 'fatalsToBrowser';
20
21require '/var/ipfire/general-functions.pl';
22require "${General::swroot}/lang.pl";
23require "${General::swroot}/header.pl";
24
25use POSIX();
26
27#workaround to suppress a warning when a variable is used only once
28my @dummy = ( ${Header::table2colour} );
29undef (@dummy);
30
31my %cgiparams=();
32my %settings=();
33my $pienumber;
34my $otherspie;
35my $showpie;
36my $sortcolumn;
37my $errormessage = '';
38
39$cgiparams{'pienumber'} = 10;
40$cgiparams{'otherspie'} = 1;
41$cgiparams{'showpie'} = 1;
42$cgiparams{'sortcolumn'} = 1;
43
44my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
6bc302d1 45 'Sep', 'Oct', 'Nov', 'Dec' );
76dd79d1 46my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
6bc302d1
CS
47 $Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
48 $Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
49 $Lang::tr{'december'} );
76dd79d1
MT
50
51my @now = localtime();
52my $dow = $now[6];
53my $doy = $now[7];
54my $tdoy = $now[7];
55my $year = $now[5]+1900;
56
57$cgiparams{'DAY'} = $now[3];
58$cgiparams{'MONTH'} = $now[4];
59$cgiparams{'ACTION'} = '';
60
61&General::readhash("${General::swroot}/fwlogs/ipsettings", \%settings);
62if ($settings{'pienumber'} != 0) { $cgiparams{'pienumber'} = $settings{'pienumber'} };
63if ($settings{'otherspie'} != 0) { $cgiparams{'otherspie'} = $settings{'otherspie'} };
64if ($settings{'showpie'} != 0) { $cgiparams{'showpie'} = $settings{'showpie'} };
65if ($settings{'sortcolumn'} != 0) { $cgiparams{'sortcolumn'} = $settings{'sortcolumn'} };
66
67&Header::getcgihash(\%cgiparams);
68if ($cgiparams{'pienumber'} != 0) { $settings{'pienumber'} = $cgiparams{'pienumber'} };
69if ($cgiparams{'otherspie'} != 0) { $settings{'otherspie'} = $cgiparams{'otherspie'} };
70if ($cgiparams{'showpie'} != 0) { $settings{'showpie'} = $cgiparams{'showpie'} };
71if ($cgiparams{'sortcolumn'} != 0) { $settings{'sortcolumn'} = $cgiparams{'sortcolumn'} };
72
73if ($cgiparams{'ACTION'} eq $Lang::tr{'save'})
74{
75 &General::writehash("${General::swroot}/fwlogs/ipsettings", \%settings);
76}
77
78my $start = -1;
79if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
80{
6bc302d1
CS
81 my @temp = split(',',$ENV{'QUERY_STRING'});
82 $start = $temp[0];
83 $cgiparams{'MONTH'} = $temp[1];
84 $cgiparams{'DAY'} = $temp[2];
76dd79d1
MT
85}
86
87if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
88 !($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)$/))
89{
6bc302d1
CS
90 $cgiparams{'DAY'} = $now[3];
91 $cgiparams{'MONTH'} = $now[4];
76dd79d1
MT
92}
93elsif($cgiparams{'ACTION'} eq '>>')
94{
95 my @temp_then=();
96 my @temp_now = localtime(time);
97 $temp_now[4] = $cgiparams{'MONTH'};
98 $temp_now[3] = $cgiparams{'DAY'};
99 @temp_then = localtime(POSIX::mktime(@temp_now) + 86400);
100 ## Retrieve the same time on the next day -
101 ## 86400 seconds in a day
102 $cgiparams{'MONTH'} = $temp_then[4];
103 $cgiparams{'DAY'} = $temp_then[3];
104}
105elsif($cgiparams{'ACTION'} eq '<<')
106{
107 my @temp_then=();
108 my @temp_now = localtime(time);
109 $temp_now[4] = $cgiparams{'MONTH'};
110 $temp_now[3] = $cgiparams{'DAY'};
111 @temp_then = localtime(POSIX::mktime(@temp_now) - 86400);
112 ## Retrieve the same time on the previous day -
113 ## 86400 seconds in a day
114 $cgiparams{'MONTH'} = $temp_then[4];
115 $cgiparams{'DAY'} = $temp_then[3];
116}
117
118if (($cgiparams{'DAY'} ne $now[3]) || ($cgiparams{'MONTH'} ne $now[4]))
119{
120 my @then = ();
121 if ( ( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
122 ( $cgiparams{'MONTH'} > $now[4] ) ) {
123 @then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 ));
124 } else {
125 @then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 ));
126 }
127 $tdoy = $then[7];
128 my $lastleap=($year-1)%4;
129 if ($tdoy>$doy) {
130 if ($lastleap == 0 && $tdoy < 60) {
131 $doy=$tdoy+366;
132 } else {
133 $doy=$doy+365;
134 }
135 }
136}
137
138my $datediff=0;
139my $dowd=0;
140my $multifile=0;
141if ($tdoy ne $doy) {
142 $datediff=int(($doy-$tdoy)/7);
143 $dowd=($doy-$tdoy)%7;
144 if (($dow-$dowd)<1) {
145 $datediff=$datediff+1;
146 }
147 if (($dow-$dowd)==0) {
148 $multifile=1;
149 }
150}
151
152my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
153my $longmonthstr = $longmonths[$cgiparams{'MONTH'}];
154my $day = $cgiparams{'DAY'};
155my $daystr='';
156if ($day <= 9) {
6bc302d1 157 $daystr = " $day"; }
76dd79d1 158else {
6bc302d1 159 $daystr = $day;
76dd79d1
MT
160}
161
162my $skip=0;
163my $filestr='';
164if ($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
171if (!(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}
176my $lines = 0;
177my @log=();
178
179if (!$skip)
180{
181 while (<FILE>)
182 {
6bc302d1
CS
183 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
184 $log[$lines] = $_;
185 $lines++;
186 }
187 }
188 close (FILE);
76dd79d1
MT
189}
190
191$skip=0;
192if ($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) {
6bc302d1
CS
205 while (<FILE>) {
206 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
207 $log[$lines] = $_;
208 $lines++;
209 }
210 }
211 close (FILE);
212 }
76dd79d1
MT
213}
214
215my $MODNAME="fwlogs";
216
217&Header::showhttpheaders();
218&Header::openpage($Lang::tr{'firewall log'}, 1, '');
219&Header::openbigbox('100%', 'left', '', $errormessage);
220
221
222if ($errormessage) {
6bc302d1
CS
223 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
224 print "<font class='base'>$errormessage&nbsp;</font>\n";
225 &Header::closebox();
76dd79d1
MT
226}
227
228&Header::openbox('100%', 'left', "$Lang::tr{'settings'}:");
229
230print <<END
231<form method='post' action='$ENV{'SCRIPT_NAME'}'>
232<table width='100%'>
233<tr>
6bc302d1
CS
234 <td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
235 <td width='10%'>
236 <select name='MONTH'>
76dd79d1
MT
237END
238;
239my $month;
240for ($month = 0; $month < 12; $month++)
241{
6bc302d1
CS
242 print "\t<option ";
243 if ($month == $cgiparams{'MONTH'}) {
244 print "selected='selected' "; }
245 print "value='$month'>$longmonths[$month]</option>\n";
76dd79d1
MT
246}
247print <<END
6bc302d1
CS
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'>
76dd79d1
MT
253END
254;
255for ($day = 1; $day <= 31; $day++)
256{
6bc302d1
CS
257 print "\t<option ";
258 if ($day == $cgiparams{'DAY'}) {
259 print "selected='selected' "; }
260 print "value='$day'>$day</option>\n";
76dd79d1
MT
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'};}
267
268print <<END
269</select>
270</td>
271<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
272<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
273<td width='20%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
274</tr>
275</table>
276<table width='100%'>
277<tr><td width='20%' align='right' valign="center">$Lang::tr{'Number of IPs for the pie chart'}:</td>
6bc302d1
CS
278 <td width='10%' align='left' valign="center"><input type='text' name='pienumber' value='$pienumber' size='4'></td>
279 <td width='20%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td></tr>
76dd79d1
MT
280 </table>
281 </form>
282END
283;
284
285&Header::closebox();
286
287&Header::openbox('100%', 'left', 'Firewall Logs');
288print "<p><b>$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines</b></p>";
289
290my $linesjc = 0;
291my %tabjc;
292
293if ($pienumber == -1 || $pienumber > $lines || $sortcolumn == 2) { $pienumber = $lines; };
294$lines = 0;
295foreach $_ (@log)
296{
297 if($_ =~ /SRC\=([\d\.]+)/){
298 $tabjc{$1} = $tabjc{$1} + 1 ;
299 if(($tabjc{$1} == 1) && ($lines < $pienumber)) { $lines = $lines + 1; }
300 $linesjc++;
301 }
302}
303
304$pienumber = $lines;
305
306my @keytabjc = keys %tabjc;
307
308my @slice;
309my $go;
310my $nblinejc;
311
312if( $cgiparams{'linejc'} eq 'all' ){ $nblinejc = $linesjc; $go=1; }
313if( ($cgiparams{'linejc'} != 0) && ($cgiparams{'linejc'} ne 'all') ){ $nblinejc = $cgiparams{'linejc'}; $go=1;}
314if( $go != 1){ $nblinejc = 1000; }
315
316my @key;
317my @value;
318my $indice=0;
319my @tabjc2;
320
321if ($sortcolumn == 1)
322{
6bc302d1 323 @tabjc2 = sort { $b <=> $a } values (%tabjc);
76dd79d1
MT
324}
325else
326{
6bc302d1 327 @tabjc2 = sort { $a <=> $b } keys (%tabjc);
76dd79d1
MT
328}
329
330my $colour=1;
331
332##############################################
333#pie chart generation
334use GD::Graph::pie;
335use GD::Graph::colour;
336#ips sort by hits number
337my $v;
338
339if ($sortcolumn == 1)
340{
6bc302d1
CS
341 for ($v=0;$v<$pienumber;$v++){
342 findkey($tabjc2[$v]);
343 }
76dd79d1
MT
344}
345else
346{
6bc302d1
CS
347 foreach $v (@tabjc2) {
348 $key[$indice] = $v;
349 $value[$indice] = $tabjc{$v};
350 $indice++;
351 }
76dd79d1
MT
352}
353
354my @ips;
355my @numb;
356
357@ips = @key;
358@numb = @value;
359
360my $o;
361
362if($cgiparams{'otherspie'} == 2 ){}
363else{
6bc302d1
CS
364 my $numothers;
365 for($o=0;$o<$pienumber;$o++){
366 $numothers = $numothers + $numb[$o];
367 }
368 $numothers = $linesjc - $numothers;
369 if ($numothers > 0) {
370 $ips[$pienumber]="$Lang::tr{'otherip'}";
371 $numb[$pienumber] = $numothers;
372 }
76dd79d1
MT
373}
374
375my @data = (\@ips,\@numb);
376use GD::Graph::colour qw( :files );
9c100957 377GD::Graph::colour::read_rgb( "/srv/web/ipfire/html/themes/ipfire/include/colours.txt" );
76dd79d1
MT
378
379if ($showpie != 2 && $pienumber <= 50 && $pienumber != 0) {
6bc302d1
CS
380 my $mygraph = GD::Graph::pie->new(500, 350);
381 $mygraph->set(
382 'title' => '',
383 'pie_height' => 50,
384 'start_angle' => 89
385 ) or warn $mygraph->error;
386
387 $mygraph->set_value_font(GD::gdMediumBoldFont);
388 $mygraph->set( dclrs => [ "colour1" , "colour2" , "colour3" , "colour4" , "colour5" , "colour6" , "colour7" , "colour8" , "colour9" , "colour10" ] );
389 my $myimage = $mygraph->plot(\@data) or die $mygraph->error;
390
391 my @filenames = glob("/srv/web/ipfire/html/graphs/fwlog-ip*.png");
392 unlink(@filenames);
393 my $imagerandom = rand(1000000);
394 my $imagename = "/srv/web/ipfire/html/graphs/fwlog-ip$imagerandom.png";
395 open(FILE,">$imagename");
396 print FILE $myimage->png;
397 close(FILE);
398 #####################################################
399 print "<table align='center'><tr><td>";
400 print "<img src='/graphs/fwlog-ip$imagerandom.png'>";
401 print "</td></tr></table>";
76dd79d1
MT
402}
403
404print <<END
405<TABLE WIDTH='100%'>
406<TR>
407<TD WIDTH='10%' ALIGN='CENTER' CLASS='boldbase'></TD>
408<TD WIDTH='30%' ALIGN='CENTER' CLASS='boldbase'><B>IP</B></TD>
409<TD WIDTH='10%' ALIGN='CENTER' CLASS='boldbase'><B>Flag</B></TD>
410<TD WIDTH='10%' ALIGN='CENTER' CLASS='boldbase'><B>Count</B></TD>
411<TD WIDTH='30%' ALIGN='CENTER' CLASS='boldbase'><B>Percent</B></TD>
412</TR>
413END
414;
415
416my $total=0;
417my $show=0;
418my @colour;
419my $colour1;
420my $colour2;
421my $colour3;
422my $colour4;
423my $colour5;
424my $colour6;
425my $colour7;
426my $colour8;
427my $colour9;
428my $colour10;
429
430@colour = GD::Graph::colour::_rgb("colour1");
431$colour1 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
432@colour = GD::Graph::colour::_rgb("colour2");
433$colour2 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
434@colour = GD::Graph::colour::_rgb("colour3");
435$colour3 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
436@colour = GD::Graph::colour::_rgb("colour4");
437$colour4 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
438@colour = GD::Graph::colour::_rgb("colour5");
439$colour5 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
440@colour = GD::Graph::colour::_rgb("colour6");
441$colour6 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
442@colour = GD::Graph::colour::_rgb("colour7");
443$colour7 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
444@colour = GD::Graph::colour::_rgb("colour8");
445$colour8 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
446@colour = GD::Graph::colour::_rgb("colour9");
447$colour9 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
448@colour = GD::Graph::colour::_rgb("colour10");
449$colour10 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
450
451my $s;
452my $percent;
453for($s=0;$s<$lines;$s++)
454{
455 $show++;
456 $percent = $value[$s] * 100 / $linesjc;
9fb25b1c 457 $percent = sprintf("%.f", $percent);
76dd79d1
MT
458 $total = $total + $value[$s];
459 if ( ($colour % 10) == 1 ){print "<TR BGCOLOR='$colour1'>\n";}
460 if ( ($colour % 10) == 2 ){print "<TR BGCOLOR='$colour2'>\n";}
461 if ( ($colour % 10) == 3 ){print "<TR BGCOLOR='$colour3'>\n";}
462 if ( ($colour % 10) == 4 ){print "<TR BGCOLOR='$colour4'>\n";}
463 if ( ($colour % 10) == 5 ){print "<TR BGCOLOR='$colour5'>\n";}
464 if ( ($colour % 10) == 6 ){print "<TR BGCOLOR='$colour6'>\n";}
465 if ( ($colour % 10) == 7 ){print "<TR BGCOLOR='$colour7'>\n";}
466 if ( ($colour % 10) == 8 ){print "<TR BGCOLOR='$colour8'>\n";}
467 if ( ($colour % 10) == 9 ){print "<TR BGCOLOR='$colour9'>\n";}
468 if ( ($colour % 10) == 0 ){print "<TR BGCOLOR='$colour10'>\n";}
469
470 my $gi = Geo::IP::PurePerl->new();
471 my $ccode = $gi->country_code_by_name($key[$s]);
472 my $fcode = lc($ccode);
473
474 $colour++;
475 print "<TD ALIGN='CENTER'><form method='post' action='showrequestfromip.dat'><input type='hidden' name='MONTH' value='$cgiparams{'MONTH'}'> <input type='hidden' name='DAY' value='$cgiparams{'DAY'}'> <input type='hidden' name='ip' value='$key[$s]'> <input type='submit' value='details'></form></TD>";
476 print "<TD ALIGN='CENTER'><a href='/cgi-bin/ipinfo.cgi?ip=$key[$s]'>$key[$s]</a></TD>";
477 print "<TD ALIGN='CENTER'><a href='/cgi-bin/country.cgi#$fcode'><img src='/images/flags/$fcode.png' border='0' align='absmiddle' alt='$ccode'></a></TD>";
478 print "<TD ALIGN='CENTER'>$value[$s]</TD>";
479 print "<TD ALIGN='CENTER'>$percent</TD>";
480 print "</TR>";
481}
482
483if($cgiparams{'otherspie'} == 2 ){}
484else{
485if ( ($colour % 10) == 1 ){print "<TR BGCOLOR='$colour1'>\n";}
486if ( ($colour % 10) == 2 ){print "<TR BGCOLOR='$colour2'>\n";}
487if ( ($colour % 10) == 3 ){print "<TR BGCOLOR='$colour3'>\n";}
488if ( ($colour % 10) == 4 ){print "<TR BGCOLOR='$colour4'>\n";}
489if ( ($colour % 10) == 5 ){print "<TR BGCOLOR='$colour5'>\n";}
490if ( ($colour % 10) == 6 ){print "<TR BGCOLOR='$colour6'>\n";}
491if ( ($colour % 10) == 7 ){print "<TR BGCOLOR='$colour7'>\n";}
492if ( ($colour % 10) == 8 ){print "<TR BGCOLOR='$colour8'>\n";}
493if ( ($colour % 10) == 9 ){print "<TR BGCOLOR='$colour9'>\n";}
494if ( ($colour % 10) == 0 ){print "<TR BGCOLOR='$colour10'>\n";}
495
496my $dif;
497$dif = $linesjc - $total;
498$percent = $dif * 100 / $linesjc;
9fb25b1c 499$percent = sprintf("%.f", $percent);
76dd79d1
MT
500print <<END
501<TD ALIGN='CENTER'></TD>
502<TD ALIGN='CENTER'>$Lang::tr{'otherip'}</TD>
503<TD ALIGN='CENTER'></TD>
504<TD ALIGN='CENTER'>$dif</TD>
505<TD ALIGN='CENTER'>$percent</TD>
506</TR>
507END
508;
509}
510print <<END
511</TABLE>
512END
513;
514
515&Header::closebox();
516
517&Header::closebigbox();
518
519&Header::closepage();
520
521sub findkey {
522 my $v;
523 foreach $v (@keytabjc) {
524 if ($tabjc{$v} eq $_[0]) {
525 delete $tabjc{$v};
526 $key[$indice] = "$v";
527 $value[$indice] = $_[0];
528 $indice++;
529 last;
530 }
531 }
532}sub checkversion {
6bc302d1
CS
533 #Automatic Updates is disabled
534 return "0","0";
535 }