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