"Update Booster" fertiggestellt und getestet.
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / logs.cgi / firewalllogip.dat
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
11 # and Michael Tremer (www.ipfire.org)
12
13 use strict;
14 use Geo::IP::PurePerl;
15 use Getopt::Std;
16
17 # enable only the following on debugging purpose
18 #use warnings;
19 #use CGI::Carp 'fatalsToBrowser';
20
21 require '/var/ipfire/general-functions.pl';
22 require "${General::swroot}/lang.pl";
23 require "${General::swroot}/header.pl";
24
25 use POSIX();
26
27 #workaround to suppress a warning when a variable is used only once
28 my @dummy = ( ${Header::table2colour} );
29 undef (@dummy);
30
31 my %cgiparams=();
32 my %settings=();
33 my $pienumber;
34 my $otherspie;
35 my $showpie;
36 my $sortcolumn;
37 my $errormessage = '';
38
39 $cgiparams{'pienumber'} = 10;
40 $cgiparams{'otherspie'} = 1;
41 $cgiparams{'showpie'} = 1;
42 $cgiparams{'sortcolumn'} = 1;
43
44 my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
45 'Sep', 'Oct', 'Nov', 'Dec' );
46 my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
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'} );
50
51 my @now = localtime();
52 my $dow = $now[6];
53 my $doy = $now[7];
54 my $tdoy = $now[7];
55 my $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);
62 if ($settings{'pienumber'} != 0) { $cgiparams{'pienumber'} = $settings{'pienumber'} };
63 if ($settings{'otherspie'} != 0) { $cgiparams{'otherspie'} = $settings{'otherspie'} };
64 if ($settings{'showpie'} != 0) { $cgiparams{'showpie'} = $settings{'showpie'} };
65 if ($settings{'sortcolumn'} != 0) { $cgiparams{'sortcolumn'} = $settings{'sortcolumn'} };
66
67 &Header::getcgihash(\%cgiparams);
68 if ($cgiparams{'pienumber'} != 0) { $settings{'pienumber'} = $cgiparams{'pienumber'} };
69 if ($cgiparams{'otherspie'} != 0) { $settings{'otherspie'} = $cgiparams{'otherspie'} };
70 if ($cgiparams{'showpie'} != 0) { $settings{'showpie'} = $cgiparams{'showpie'} };
71 if ($cgiparams{'sortcolumn'} != 0) { $settings{'sortcolumn'} = $cgiparams{'sortcolumn'} };
72
73 if ($cgiparams{'ACTION'} eq $Lang::tr{'save'})
74 {
75 &General::writehash("${General::swroot}/fwlogs/ipsettings", \%settings);
76 }
77
78 my $start = -1;
79 if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
80 {
81 my @temp = split(',',$ENV{'QUERY_STRING'});
82 $start = $temp[0];
83 $cgiparams{'MONTH'} = $temp[1];
84 $cgiparams{'DAY'} = $temp[2];
85 }
86
87 if (!($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 {
90 $cgiparams{'DAY'} = $now[3];
91 $cgiparams{'MONTH'} = $now[4];
92 }
93 elsif($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 }
105 elsif($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
118 if (($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
138 my $datediff=0;
139 my $dowd=0;
140 my $multifile=0;
141 if ($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
152 my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
153 my $longmonthstr = $longmonths[$cgiparams{'MONTH'}];
154 my $day = $cgiparams{'DAY'};
155 my $daystr='';
156 if ($day <= 9) {
157 $daystr = " $day"; }
158 else {
159 $daystr = $day;
160 }
161
162 my $skip=0;
163 my $filestr='';
164 if ($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
171 if (!(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 }
176 my $lines = 0;
177 my @log=();
178
179 if (!$skip)
180 {
181 while (<FILE>)
182 {
183 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
184 $log[$lines] = $_;
185 $lines++;
186 }
187 }
188 close (FILE);
189 }
190
191 $skip=0;
192 if ($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) {
205 while (<FILE>) {
206 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
207 $log[$lines] = $_;
208 $lines++;
209 }
210 }
211 close (FILE);
212 }
213 }
214
215 my $MODNAME="fwlogs";
216
217 &Header::showhttpheaders();
218 &Header::openpage($Lang::tr{'firewall log'}, 1, '');
219 &Header::openbigbox('100%', 'left', '', $errormessage);
220
221
222 if ($errormessage) {
223 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
224 print "<font class='base'>$errormessage&nbsp;</font>\n";
225 &Header::closebox();
226 }
227
228 &Header::openbox('100%', 'left', "$Lang::tr{'settings'}:");
229
230 print <<END
231 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
232 <table width='100%'>
233 <tr>
234 <td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
235 <td width='10%'>
236 <select name='MONTH'>
237 END
238 ;
239 my $month;
240 for ($month = 0; $month < 12; $month++)
241 {
242 print "\t<option ";
243 if ($month == $cgiparams{'MONTH'}) {
244 print "selected='selected' "; }
245 print "value='$month'>$longmonths[$month]</option>\n";
246 }
247 print <<END
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'>
253 END
254 ;
255 for ($day = 1; $day <= 31; $day++)
256 {
257 print "\t<option ";
258 if ($day == $cgiparams{'DAY'}) {
259 print "selected='selected' "; }
260 print "value='$day'>$day</option>\n";
261 }
262
263 if( $cgiparams{'pienumber'} != 0){$pienumber=$cgiparams{'pienumber'};}
264 if( $cgiparams{'otherspie'} != 0){$otherspie=$cgiparams{'otherspie'};}
265 if( $cgiparams{'showpie'} != 0){$showpie=$cgiparams{'showpie'};}
266 if( $cgiparams{'sortcolumn'} != 0){$sortcolumn=$cgiparams{'sortcolumn'};}
267
268 print <<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>
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>
280 </table>
281 </form>
282 END
283 ;
284
285 &Header::closebox();
286
287 &Header::openbox('100%', 'left', 'Firewall Logs');
288 print "<p><b>$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines</b></p>";
289
290 my $linesjc = 0;
291 my %tabjc;
292
293 if ($pienumber == -1 || $pienumber > $lines || $sortcolumn == 2) { $pienumber = $lines; };
294 $lines = 0;
295 foreach $_ (@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
306 my @keytabjc = keys %tabjc;
307
308 my @slice;
309 my $go;
310 my $nblinejc;
311
312 if( $cgiparams{'linejc'} eq 'all' ){ $nblinejc = $linesjc; $go=1; }
313 if( ($cgiparams{'linejc'} != 0) && ($cgiparams{'linejc'} ne 'all') ){ $nblinejc = $cgiparams{'linejc'}; $go=1;}
314 if( $go != 1){ $nblinejc = 1000; }
315
316 my @key;
317 my @value;
318 my $indice=0;
319 my @tabjc2;
320
321 if ($sortcolumn == 1)
322 {
323 @tabjc2 = sort { $b <=> $a } values (%tabjc);
324 }
325 else
326 {
327 @tabjc2 = sort { $a <=> $b } keys (%tabjc);
328 }
329
330 my $colour=1;
331
332 ##############################################
333 #pie chart generation
334 use GD::Graph::pie;
335 use GD::Graph::colour;
336 #ips sort by hits number
337 my $v;
338
339 if ($sortcolumn == 1)
340 {
341 for ($v=0;$v<$pienumber;$v++){
342 findkey($tabjc2[$v]);
343 }
344 }
345 else
346 {
347 foreach $v (@tabjc2) {
348 $key[$indice] = $v;
349 $value[$indice] = $tabjc{$v};
350 $indice++;
351 }
352 }
353
354 my @ips;
355 my @numb;
356
357 @ips = @key;
358 @numb = @value;
359
360 my $o;
361
362 if($cgiparams{'otherspie'} == 2 ){}
363 else{
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 }
373 }
374
375 my @data = (\@ips,\@numb);
376 use GD::Graph::colour qw( :files );
377 GD::Graph::colour::read_rgb( "/srv/web/ipfire/html/themes/ipfire/include/colours.txt" );
378
379 if ($showpie != 2 && $pienumber <= 50 && $pienumber != 0) {
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>";
402 }
403
404 print <<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>
413 END
414 ;
415
416 my $total=0;
417 my $show=0;
418 my @colour;
419 my $colour1;
420 my $colour2;
421 my $colour3;
422 my $colour4;
423 my $colour5;
424 my $colour6;
425 my $colour7;
426 my $colour8;
427 my $colour9;
428 my $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
451 my $s;
452 my $percent;
453 for($s=0;$s<$lines;$s++)
454 {
455 $show++;
456 $percent = $value[$s] * 100 / $linesjc;
457 $percent = sprintf("%.f", $percent);
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
483 if($cgiparams{'otherspie'} == 2 ){}
484 else{
485 if ( ($colour % 10) == 1 ){print "<TR BGCOLOR='$colour1'>\n";}
486 if ( ($colour % 10) == 2 ){print "<TR BGCOLOR='$colour2'>\n";}
487 if ( ($colour % 10) == 3 ){print "<TR BGCOLOR='$colour3'>\n";}
488 if ( ($colour % 10) == 4 ){print "<TR BGCOLOR='$colour4'>\n";}
489 if ( ($colour % 10) == 5 ){print "<TR BGCOLOR='$colour5'>\n";}
490 if ( ($colour % 10) == 6 ){print "<TR BGCOLOR='$colour6'>\n";}
491 if ( ($colour % 10) == 7 ){print "<TR BGCOLOR='$colour7'>\n";}
492 if ( ($colour % 10) == 8 ){print "<TR BGCOLOR='$colour8'>\n";}
493 if ( ($colour % 10) == 9 ){print "<TR BGCOLOR='$colour9'>\n";}
494 if ( ($colour % 10) == 0 ){print "<TR BGCOLOR='$colour10'>\n";}
495
496 my $dif;
497 $dif = $linesjc - $total;
498 $percent = $dif * 100 / $linesjc;
499 $percent = sprintf("%.f", $percent);
500 print <<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>
507 END
508 ;
509 }
510 print <<END
511 </TABLE>
512 END
513 ;
514
515 &Header::closebox();
516
517 &Header::closebigbox();
518
519 &Header::closepage();
520
521 sub 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 {
533 #Automatic Updates is disabled
534 return "0","0";
535 }