"Update Booster" fertiggestellt und getestet.
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / logs.cgi / firewalllogport.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
15 # enable only the following on debugging purpose
16 #use warnings;
17 #use CGI::Carp 'fatalsToBrowser';
18
19 require '/var/ipfire/general-functions.pl';
20 require "${General::swroot}/lang.pl";
21 require "${General::swroot}/header.pl";
22
23 use POSIX();
24
25 #workaround to suppress a warning when a variable is used only once
26 my @dummy = ( ${Header::table2colour} );
27 undef (@dummy);
28
29 my %cgiparams=();
30 my %settings=();
31 my $pienumber;
32 my $otherspie;
33 my $showpie;
34 my $sortcolumn;
35 my $errormessage = '';
36
37 $cgiparams{'pienumber'} = 10;
38 $cgiparams{'otherspie'} = 1;
39 $cgiparams{'showpie'} = 1;
40 $cgiparams{'sortcolumn'} = 1;
41
42 my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
43 'Sep', 'Oct', 'Nov', 'Dec' );
44 my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
45 $Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
46 $Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
47 $Lang::tr{'december'} );
48
49 my @now = localtime();
50 my $dow = $now[6];
51 my $doy = $now[7];
52 my $tdoy = $now[7];
53 my $year = $now[5]+1900;
54
55 $cgiparams{'DAY'} = $now[3];
56 $cgiparams{'MONTH'} = $now[4];
57 $cgiparams{'ACTION'} = '';
58
59 &General::readhash("${General::swroot}/fwlogs/portsettings", \%settings);
60 if ($settings{'pienumber'} != 0) { $cgiparams{'pienumber'} = $settings{'pienumber'} };
61 if ($settings{'otherspie'} != 0) { $cgiparams{'otherspie'} = $settings{'otherspie'} };
62 if ($settings{'showpie'} != 0) { $cgiparams{'showpie'} = $settings{'showpie'} };
63 if ($settings{'sortcolumn'} != 0) { $cgiparams{'sortcolumn'} = $settings{'sortcolumn'} };
64
65 &Header::getcgihash(\%cgiparams);
66 if ($cgiparams{'pienumber'} != 0) { $settings{'pienumber'} = $cgiparams{'pienumber'} };
67 if ($cgiparams{'otherspie'} != 0) { $settings{'otherspie'} = $cgiparams{'otherspie'} };
68 if ($cgiparams{'showpie'} != 0) { $settings{'showpie'} = $cgiparams{'showpie'} };
69 if ($cgiparams{'sortcolumn'} != 0) { $settings{'sortcolumn'} = $cgiparams{'sortcolumn'} };
70
71 if ($cgiparams{'ACTION'} eq $Lang::tr{'save'})
72 {
73 &General::writehash("${General::swroot}/fwlogs/portsettings", \%settings);
74 }
75
76 my $start = -1;
77 if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
78 {
79 my @temp = split(',',$ENV{'QUERY_STRING'});
80 $start = $temp[0];
81 $cgiparams{'MONTH'} = $temp[1];
82 $cgiparams{'DAY'} = $temp[2];
83 }
84
85 if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
86 !($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)$/))
87 {
88 $cgiparams{'DAY'} = $now[3];
89 $cgiparams{'MONTH'} = $now[4];
90 }
91 elsif($cgiparams{'ACTION'} eq '>>')
92 {
93 my @temp_then=();
94 my @temp_now = localtime(time);
95 $temp_now[4] = $cgiparams{'MONTH'};
96 $temp_now[3] = $cgiparams{'DAY'};
97 @temp_then = localtime(POSIX::mktime(@temp_now) + 86400);
98 ## Retrieve the same time on the next day -
99 ## 86400 seconds in a day
100 $cgiparams{'MONTH'} = $temp_then[4];
101 $cgiparams{'DAY'} = $temp_then[3];
102 }
103 elsif($cgiparams{'ACTION'} eq '<<')
104 {
105 my @temp_then=();
106 my @temp_now = localtime(time);
107 $temp_now[4] = $cgiparams{'MONTH'};
108 $temp_now[3] = $cgiparams{'DAY'};
109 @temp_then = localtime(POSIX::mktime(@temp_now) - 86400);
110 ## Retrieve the same time on the previous day -
111 ## 86400 seconds in a day
112 $cgiparams{'MONTH'} = $temp_then[4];
113 $cgiparams{'DAY'} = $temp_then[3];
114 }
115
116 if (($cgiparams{'DAY'} ne $now[3]) || ($cgiparams{'MONTH'} ne $now[4]))
117 {
118 my @then = ();
119 if ( ( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
120 ( $cgiparams{'MONTH'} > $now[4] ) ) {
121 @then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 ));
122 } else {
123 @then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 ));
124 }
125 $tdoy = $then[7];
126 my $lastleap=($year-1)%4;
127 if ($tdoy>$doy) {
128 if ($lastleap == 0 && $tdoy < 60) {
129 $doy=$tdoy+366;
130 } else {
131 $doy=$doy+365;
132 }
133 }
134 }
135
136 my $datediff=0;
137 my $dowd=0;
138 my $multifile=0;
139 if ($tdoy ne $doy) {
140 $datediff=int(($doy-$tdoy)/7);
141 $dowd=($doy-$tdoy)%7;
142 if (($dow-$dowd)<1) {
143 $datediff=$datediff+1;
144 }
145 if (($dow-$dowd)==0) {
146 $multifile=1;
147 }
148 }
149
150 my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
151 my $longmonthstr = $longmonths[$cgiparams{'MONTH'}];
152 my $day = $cgiparams{'DAY'};
153 my $daystr='';
154 if ($day <= 9) {
155 $daystr = " $day"; }
156 else {
157 $daystr = $day;
158 }
159
160 my $skip=0;
161 my $filestr='';
162 if ($datediff==0) {
163 $filestr="/var/log/messages";
164 } else {
165 $filestr="/var/log/messages.$datediff";
166 $filestr = "$filestr.gz" if -f "$filestr.gz";
167 }
168
169 if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
170 $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
171 $skip=1;
172 # Note: This is in case the log does not exist for that date
173 }
174 my $lines = 0;
175 my @log=();
176
177 if (!$skip)
178 {
179 while (<FILE>)
180 {
181 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
182 $log[$lines] = $_;
183 $lines++;
184 }
185 }
186 close (FILE);
187 }
188
189 $skip=0;
190 if ($multifile) {
191 $datediff=$datediff-1;
192 if ($datediff==0) {
193 $filestr="/var/log/messages";
194 } else {
195 $filestr="/var/log/messages.$datediff";
196 $filestr = "$filestr.gz" if -f "$filestr.gz";
197 }
198 if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
199 $errormessage="$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
200 $skip=1;
201 }
202 if (!$skip) {
203 while (<FILE>) {
204 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
205 $log[$lines] = $_;
206 $lines++;
207 }
208 }
209 close (FILE);
210 }
211 }
212
213 my $MODNAME="fwlogs";
214
215 &Header::showhttpheaders();
216 &Header::openpage($Lang::tr{'firewall log'}, 1, '');
217 &Header::openbigbox('100%', 'left', '', $errormessage);
218
219 if ($errormessage) {
220 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
221 print "<font class='base'>$errormessage&nbsp;</font>\n";
222 &Header::closebox();
223 }
224
225 &Header::openbox('100%', 'left', "$Lang::tr{'settings'}:");
226
227 print <<END
228 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
229 <table width='100%'>
230 <tr>
231 <td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
232 <td width='10%'>
233 <select name='MONTH'>
234 END
235 ;
236 my $month;
237 for ($month = 0; $month < 12; $month++)
238 {
239 print "\t<option ";
240 if ($month == $cgiparams{'MONTH'}) {
241 print "selected='selected' "; }
242 print "value='$month'>$longmonths[$month]</option>\n";
243 }
244 print <<END
245 </select>
246 </td>
247 <td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
248 <td width='40%'>
249 <select name='DAY'>
250 END
251 ;
252 for ($day = 1; $day <= 31; $day++)
253 {
254 print "\t<option ";
255 if ($day == $cgiparams{'DAY'}) {
256 print "selected='selected' "; }
257 print "value='$day'>$day</option>\n";
258 }
259
260 if( $cgiparams{'pienumber'} != 0){$pienumber=$cgiparams{'pienumber'};}
261 if( $cgiparams{'otherspie'} != 0){$otherspie=$cgiparams{'otherspie'};}
262 if( $cgiparams{'showpie'} != 0){$showpie=$cgiparams{'showpie'};}
263 if( $cgiparams{'sortcolumn'} != 0){$sortcolumn=$cgiparams{'sortcolumn'};}
264
265 print <<END
266 </select>
267 </td>
268 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
269 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
270 <td width='20%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
271 </tr>
272 </table>
273 <table width='100%'>
274 <tr><td width='20%' align='right' valign="center">$Lang::tr{'Number of Ports for the pie chart'}:</td>
275 <td width='10%' align='left' valign="center"><input type='text' name='pienumber' value='$pienumber' size='4'></td>
276 <td width='20%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td></tr>
277 </table>
278 </form>
279 END
280 ;
281
282 &Header::closebox();
283
284 &Header::openbox('100%', 'left', 'Firewall Logs');
285 print "<p><b>$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines</b></p>";
286
287 my $linesjc = 0;
288 my %tabjc;
289
290 if ($pienumber == -1 || $pienumber > $lines || $sortcolumn == 2) { $pienumber = $lines; };
291 $lines = 0;
292 foreach $_ (@log)
293 {
294 if($_ =~ /DPT\=([\d\.]+)/){
295 $tabjc{$1} = $tabjc{$1} + 1 ;
296 if(($tabjc{$1} == 1) && ($lines < $pienumber)) { $lines = $lines + 1; }
297 $linesjc++;
298 }
299 }
300
301 $pienumber = $lines;
302
303 my @keytabjc = keys %tabjc;
304
305 my @slice;
306 my $go;
307 my $nblinejc;
308
309 if( $cgiparams{'linejc'} eq 'all' ){ $nblinejc = $linesjc; $go=1; }
310 if( ($cgiparams{'linejc'} != 0) && ($cgiparams{'linejc'} ne 'all') ){ $nblinejc = $cgiparams{'linejc'}; $go=1;}
311 if( $go != 1){ $nblinejc = 1000; }
312
313 my @key;
314 my @value;
315 my $indice=0;
316 my @tabjc2;
317
318 if ($sortcolumn == 1)
319 {
320 @tabjc2 = sort { $b <=> $a } values (%tabjc);
321 }
322 else
323 {
324 @tabjc2 = sort { $a <=> $b } keys (%tabjc);
325 }
326
327 my $colour=1;
328
329
330
331 ##############################################
332 #pie chart generation
333 use GD::Graph::pie;
334 use GD::Graph::colour;
335 #ports sort by hits number
336 #(port - number_of_hit) sorted by number_of_hit
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 @ports;
355 my @numb;
356
357 @ports = @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 $ports[$pienumber]="$Lang::tr{'otherport'}";
371 $numb[$pienumber] = $numothers;
372 }
373 }
374
375 my @data = (\@ports,\@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-port*.png");
392 unlink(@filenames);
393 my $imagerandom = rand(1000000);
394 my $imagename = "/srv/web/ipfire/html/graphs/fwlog-port$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-port$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='33%' ALIGN='CENTER' CLASS='boldbase'><B>Port</B></TD>
409 <TD WIDTH='33%' ALIGN='CENTER' CLASS='boldbase'><B>Count</B></TD>
410 <TD WIDTH='33%' ALIGN='CENTER' CLASS='boldbase'><B>Percent</B></TD>
411 </TR>
412 END
413 ;
414
415 my $total=0;
416 my $show=0;
417 my @colour;
418 my $colour1;
419 my $colour2;
420 my $colour3;
421 my $colour4;
422 my $colour5;
423 my $colour6;
424 my $colour7;
425 my $colour8;
426 my $colour9;
427 my $colour10;
428
429 @colour = GD::Graph::colour::_rgb("colour1");
430 $colour1 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
431 @colour = GD::Graph::colour::_rgb("colour2");
432 $colour2 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
433 @colour = GD::Graph::colour::_rgb("colour3");
434 $colour3 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
435 @colour = GD::Graph::colour::_rgb("colour4");
436 $colour4 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
437 @colour = GD::Graph::colour::_rgb("colour5");
438 $colour5 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
439 @colour = GD::Graph::colour::_rgb("colour6");
440 $colour6 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
441 @colour = GD::Graph::colour::_rgb("colour7");
442 $colour7 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
443 @colour = GD::Graph::colour::_rgb("colour8");
444 $colour8 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
445 @colour = GD::Graph::colour::_rgb("colour9");
446 $colour9 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
447 @colour = GD::Graph::colour::_rgb("colour10");
448 $colour10 = GD::Graph::colour::rgb2hex($colour[0], $colour[1], $colour[2]);
449
450 my $s;
451 my $percent;
452 for($s=0;$s<$lines;$s++)
453 {
454 $show++;
455 $percent = $value[$s] * 100 / $linesjc;
456 $percent = sprintf("%.f", $percent);
457 $total = $total + $value[$s];
458 if ( ($colour % 10) == 1 ){print "<TR BGCOLOR='$colour1'>\n";}
459 if ( ($colour % 10) == 2 ){print "<TR BGCOLOR='$colour2'>\n";}
460 if ( ($colour % 10) == 3 ){print "<TR BGCOLOR='$colour3'>\n";}
461 if ( ($colour % 10) == 4 ){print "<TR BGCOLOR='$colour4'>\n";}
462 if ( ($colour % 10) == 5 ){print "<TR BGCOLOR='$colour5'>\n";}
463 if ( ($colour % 10) == 6 ){print "<TR BGCOLOR='$colour6'>\n";}
464 if ( ($colour % 10) == 7 ){print "<TR BGCOLOR='$colour7'>\n";}
465 if ( ($colour % 10) == 8 ){print "<TR BGCOLOR='$colour8'>\n";}
466 if ( ($colour % 10) == 9 ){print "<TR BGCOLOR='$colour9'>\n";}
467 if ( ($colour % 10) == 0 ){print "<TR BGCOLOR='$colour10'>\n";}
468
469 $colour++;
470 print "<TD ALIGN='CENTER'><form method='post' action='showrequestfromport.dat'><input type='hidden' name='MONTH' value='$cgiparams{'MONTH'}'> <input type='hidden' name='DAY' value='$cgiparams{'DAY'}'> <input type='hidden' name='port' value='$key[$s]'> <input type='submit' value='details'></form></TD>";
471 print "<TD ALIGN='CENTER'>$key[$s]</TD>";
472 print "<TD ALIGN='CENTER'>$value[$s]</TD>";
473 print "<TD ALIGN='CENTER'>$percent</TD>";
474 print "</TR>";
475 }
476
477 if($cgiparams{'otherspie'} == 2 ){}
478 else{
479 if ( ($colour % 10) == 1 ){print "<TR BGCOLOR='$colour1'>\n";}
480 if ( ($colour % 10) == 2 ){print "<TR BGCOLOR='$colour2'>\n";}
481 if ( ($colour % 10) == 3 ){print "<TR BGCOLOR='$colour3'>\n";}
482 if ( ($colour % 10) == 4 ){print "<TR BGCOLOR='$colour4'>\n";}
483 if ( ($colour % 10) == 5 ){print "<TR BGCOLOR='$colour5'>\n";}
484 if ( ($colour % 10) == 6 ){print "<TR BGCOLOR='$colour6'>\n";}
485 if ( ($colour % 10) == 7 ){print "<TR BGCOLOR='$colour7'>\n";}
486 if ( ($colour % 10) == 8 ){print "<TR BGCOLOR='$colour8'>\n";}
487 if ( ($colour % 10) == 9 ){print "<TR BGCOLOR='$colour9'>\n";}
488 if ( ($colour % 10) == 0 ){print "<TR BGCOLOR='$colour10'>\n";}
489
490 my $dif;
491 $dif = $linesjc - $total;
492 $percent = $dif * 100 / $linesjc;
493 $percent = sprintf("%.f", $percent);
494 print <<END
495 <TD ALIGN='CENTER'></TD>
496 <TD ALIGN='CENTER'>$Lang::tr{'otherport'}</TD>
497 <TD ALIGN='CENTER'>$dif</TD>
498 <TD ALIGN='CENTER'>$percent</TD>
499 </TR>
500 END
501 ;
502 }
503 print <<END
504 </TABLE>
505 END
506 ;
507
508 &Header::closebox();
509 &Header::closebigbox();
510 &Header::closepage();
511
512 sub findkey {
513 foreach $v (@keytabjc) {
514 if ($tabjc{$v} eq $_[0]) {
515 delete $tabjc{$v};
516 $key[$indice] = "$v";
517 $value[$indice] = $_[0];
518 $indice++;
519 last;
520 }
521 }
522 }
523 sub checkversion {
524 #Automatic Updates is disabled
525 return "0","0";
526 }
527