"Update Booster" fertiggestellt und getestet.
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / logs.cgi / firewalllogport.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;
14
15# enable only the following on debugging purpose
16#use warnings;
17#use CGI::Carp 'fatalsToBrowser';
18
19require '/var/ipfire/general-functions.pl';
20require "${General::swroot}/lang.pl";
21require "${General::swroot}/header.pl";
22
23use POSIX();
24
25#workaround to suppress a warning when a variable is used only once
26my @dummy = ( ${Header::table2colour} );
27undef (@dummy);
28
29my %cgiparams=();
30my %settings=();
31my $pienumber;
32my $otherspie;
33my $showpie;
34my $sortcolumn;
35my $errormessage = '';
36
37$cgiparams{'pienumber'} = 10;
38$cgiparams{'otherspie'} = 1;
39$cgiparams{'showpie'} = 1;
40$cgiparams{'sortcolumn'} = 1;
41
42my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
6bc302d1 43 'Sep', 'Oct', 'Nov', 'Dec' );
76dd79d1 44my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
6bc302d1
CS
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'} );
76dd79d1
MT
48
49my @now = localtime();
50my $dow = $now[6];
51my $doy = $now[7];
52my $tdoy = $now[7];
53my $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);
60if ($settings{'pienumber'} != 0) { $cgiparams{'pienumber'} = $settings{'pienumber'} };
61if ($settings{'otherspie'} != 0) { $cgiparams{'otherspie'} = $settings{'otherspie'} };
62if ($settings{'showpie'} != 0) { $cgiparams{'showpie'} = $settings{'showpie'} };
63if ($settings{'sortcolumn'} != 0) { $cgiparams{'sortcolumn'} = $settings{'sortcolumn'} };
64
65&Header::getcgihash(\%cgiparams);
66if ($cgiparams{'pienumber'} != 0) { $settings{'pienumber'} = $cgiparams{'pienumber'} };
67if ($cgiparams{'otherspie'} != 0) { $settings{'otherspie'} = $cgiparams{'otherspie'} };
68if ($cgiparams{'showpie'} != 0) { $settings{'showpie'} = $cgiparams{'showpie'} };
69if ($cgiparams{'sortcolumn'} != 0) { $settings{'sortcolumn'} = $cgiparams{'sortcolumn'} };
70
71if ($cgiparams{'ACTION'} eq $Lang::tr{'save'})
72{
73 &General::writehash("${General::swroot}/fwlogs/portsettings", \%settings);
74}
75
76my $start = -1;
77if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
78{
6bc302d1
CS
79 my @temp = split(',',$ENV{'QUERY_STRING'});
80 $start = $temp[0];
81 $cgiparams{'MONTH'} = $temp[1];
82 $cgiparams{'DAY'} = $temp[2];
76dd79d1
MT
83}
84
85if (!($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{
6bc302d1
CS
88 $cgiparams{'DAY'} = $now[3];
89 $cgiparams{'MONTH'} = $now[4];
76dd79d1
MT
90}
91elsif($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}
103elsif($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
116if (($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
136my $datediff=0;
137my $dowd=0;
138my $multifile=0;
139if ($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
150my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
151my $longmonthstr = $longmonths[$cgiparams{'MONTH'}];
152my $day = $cgiparams{'DAY'};
153my $daystr='';
154if ($day <= 9) {
6bc302d1 155 $daystr = " $day"; }
76dd79d1 156else {
6bc302d1 157 $daystr = $day;
76dd79d1
MT
158}
159
160my $skip=0;
161my $filestr='';
162if ($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
169if (!(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}
174my $lines = 0;
175my @log=();
176
177if (!$skip)
178{
179 while (<FILE>)
180 {
6bc302d1
CS
181 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
182 $log[$lines] = $_;
183 $lines++;
184 }
185 }
186 close (FILE);
76dd79d1
MT
187}
188
189$skip=0;
190if ($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) {
6bc302d1
CS
203 while (<FILE>) {
204 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
205 $log[$lines] = $_;
206 $lines++;
207 }
208 }
209 close (FILE);
210 }
76dd79d1
MT
211}
212
213my $MODNAME="fwlogs";
214
215&Header::showhttpheaders();
216&Header::openpage($Lang::tr{'firewall log'}, 1, '');
217&Header::openbigbox('100%', 'left', '', $errormessage);
218
219if ($errormessage) {
6bc302d1
CS
220 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
221 print "<font class='base'>$errormessage&nbsp;</font>\n";
222 &Header::closebox();
76dd79d1
MT
223}
224
225&Header::openbox('100%', 'left', "$Lang::tr{'settings'}:");
226
227print <<END
228<form method='post' action='$ENV{'SCRIPT_NAME'}'>
229<table width='100%'>
230<tr>
6bc302d1
CS
231 <td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
232 <td width='10%'>
233 <select name='MONTH'>
76dd79d1
MT
234END
235;
236my $month;
237for ($month = 0; $month < 12; $month++)
238{
6bc302d1
CS
239 print "\t<option ";
240 if ($month == $cgiparams{'MONTH'}) {
241 print "selected='selected' "; }
242 print "value='$month'>$longmonths[$month]</option>\n";
76dd79d1
MT
243}
244print <<END
6bc302d1
CS
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'>
76dd79d1
MT
250END
251;
252for ($day = 1; $day <= 31; $day++)
253{
6bc302d1
CS
254 print "\t<option ";
255 if ($day == $cgiparams{'DAY'}) {
256 print "selected='selected' "; }
257 print "value='$day'>$day</option>\n";
76dd79d1
MT
258}
259
260if( $cgiparams{'pienumber'} != 0){$pienumber=$cgiparams{'pienumber'};}
261if( $cgiparams{'otherspie'} != 0){$otherspie=$cgiparams{'otherspie'};}
262if( $cgiparams{'showpie'} != 0){$showpie=$cgiparams{'showpie'};}
263if( $cgiparams{'sortcolumn'} != 0){$sortcolumn=$cgiparams{'sortcolumn'};}
264
265print <<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>
279END
280;
281
282&Header::closebox();
283
284&Header::openbox('100%', 'left', 'Firewall Logs');
285print "<p><b>$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines</b></p>";
286
287my $linesjc = 0;
288my %tabjc;
289
290if ($pienumber == -1 || $pienumber > $lines || $sortcolumn == 2) { $pienumber = $lines; };
291$lines = 0;
292foreach $_ (@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
303my @keytabjc = keys %tabjc;
304
305my @slice;
306my $go;
307my $nblinejc;
308
309if( $cgiparams{'linejc'} eq 'all' ){ $nblinejc = $linesjc; $go=1; }
310if( ($cgiparams{'linejc'} != 0) && ($cgiparams{'linejc'} ne 'all') ){ $nblinejc = $cgiparams{'linejc'}; $go=1;}
311if( $go != 1){ $nblinejc = 1000; }
312
313my @key;
314my @value;
315my $indice=0;
316my @tabjc2;
317
318if ($sortcolumn == 1)
319{
6bc302d1 320 @tabjc2 = sort { $b <=> $a } values (%tabjc);
76dd79d1
MT
321}
322else
323{
6bc302d1 324 @tabjc2 = sort { $a <=> $b } keys (%tabjc);
76dd79d1
MT
325}
326
327my $colour=1;
328
329
330
331##############################################
332#pie chart generation
333use GD::Graph::pie;
334use GD::Graph::colour;
335#ports sort by hits number
336#(port - number_of_hit) sorted by number_of_hit
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 @ports;
355my @numb;
356
357@ports = @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 $ports[$pienumber]="$Lang::tr{'otherport'}";
371 $numb[$pienumber] = $numothers;
372 }
76dd79d1
MT
373}
374
375my @data = (\@ports,\@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-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>";
76dd79d1
MT
402}
403
404print <<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>
412END
413;
414
415my $total=0;
416my $show=0;
417my @colour;
418my $colour1;
419my $colour2;
420my $colour3;
421my $colour4;
422my $colour5;
423my $colour6;
424my $colour7;
425my $colour8;
426my $colour9;
427my $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
450my $s;
451my $percent;
452for($s=0;$s<$lines;$s++)
453{
454 $show++;
455 $percent = $value[$s] * 100 / $linesjc;
9fb25b1c 456 $percent = sprintf("%.f", $percent);
76dd79d1
MT
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
477if($cgiparams{'otherspie'} == 2 ){}
478else{
479if ( ($colour % 10) == 1 ){print "<TR BGCOLOR='$colour1'>\n";}
480if ( ($colour % 10) == 2 ){print "<TR BGCOLOR='$colour2'>\n";}
481if ( ($colour % 10) == 3 ){print "<TR BGCOLOR='$colour3'>\n";}
482if ( ($colour % 10) == 4 ){print "<TR BGCOLOR='$colour4'>\n";}
483if ( ($colour % 10) == 5 ){print "<TR BGCOLOR='$colour5'>\n";}
484if ( ($colour % 10) == 6 ){print "<TR BGCOLOR='$colour6'>\n";}
485if ( ($colour % 10) == 7 ){print "<TR BGCOLOR='$colour7'>\n";}
486if ( ($colour % 10) == 8 ){print "<TR BGCOLOR='$colour8'>\n";}
487if ( ($colour % 10) == 9 ){print "<TR BGCOLOR='$colour9'>\n";}
488if ( ($colour % 10) == 0 ){print "<TR BGCOLOR='$colour10'>\n";}
489
490my $dif;
491$dif = $linesjc - $total;
492$percent = $dif * 100 / $linesjc;
9fb25b1c 493$percent = sprintf("%.f", $percent);
76dd79d1
MT
494print <<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>
500END
501;
502}
503print <<END
504</TABLE>
505END
506;
507
508&Header::closebox();
509&Header::closebigbox();
510&Header::closepage();
511
512sub 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}
523sub checkversion {
6bc302d1
CS
524 #Automatic Updates is disabled
525 return "0","0";
526 }
527