]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blame - html/cgi-bin/logs.cgi/firewalllogport.dat
Merge remote-tracking branch 'alfh/feature_vnstat_1.11' into beyond-next
[people/teissler/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
8c384f97
CS
16#use warnings;
17#use CGI::Carp 'fatalsToBrowser';
76dd79d1
MT
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
c8ee899d 225&Header::openbox('100%', 'left', "$Lang::tr{'settings'}");
76dd79d1
MT
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>
c8ee899d
AM
270<td width='20%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
271</tr>
272<tr>
273 <td colspan='3' align='left' valign="center">$Lang::tr{'Number of Ports for the pie chart'}:</td>
274 <td colspan='3' align='left' valign="center"><input type='text' name='pienumber' value='$pienumber' size='4'></td>
275 <td align='right'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
76dd79d1
MT
276</tr>
277</table>
c8ee899d 278</form>
76dd79d1
MT
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
4b4aec3a 327my $color=10;
76dd79d1
MT
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 );
4b4aec3a
CS
377
378my %color = ();
c94c9a32
CS
379my %mainsettings = ();
380&General::readhash("${General::swroot}/main/settings", \%mainsettings);
4b4aec3a 381&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
76dd79d1
MT
382
383if ($showpie != 2 && $pienumber <= 50 && $pienumber != 0) {
6bc302d1
CS
384 my $mygraph = GD::Graph::pie->new(500, 350);
385 $mygraph->set(
386 'title' => '',
387 'pie_height' => 50,
388 'start_angle' => 89
389 ) or warn $mygraph->error;
390
391 $mygraph->set_value_font(GD::gdMediumBoldFont);
4b4aec3a 392 $mygraph->set( dclrs => [ "$color{'color1'}" , "$color{'color2'}" , "$color{'color3'}" , "$color{'color4'}" , "$color{'color5'}" , "$color{'color6'}" , "$color{'color7'}" , "$color{'color8'}" , "$color{'color9'}" , "$color{'color10'}" ] );
6bc302d1
CS
393 my $myimage = $mygraph->plot(\@data) or die $mygraph->error;
394
395 my @filenames = glob("/srv/web/ipfire/html/graphs/fwlog-port*.png");
396 unlink(@filenames);
397 my $imagerandom = rand(1000000);
398 my $imagename = "/srv/web/ipfire/html/graphs/fwlog-port$imagerandom.png";
399 open(FILE,">$imagename");
400 print FILE $myimage->png;
401 close(FILE);
402 #####################################################
326a764d 403 print "<div style='text-align:center'>";
6bc302d1 404 print "<img src='/graphs/fwlog-port$imagerandom.png'>";
326a764d 405 print "</div>";
76dd79d1
MT
406}
407
408print <<END
326a764d 409<table width='100%' class='tbl'>
b740cdd7
AH
410<tr>
411<th width='10%' align='center' class='boldbase'></th>
5ee7500e 412<th width='33%' align='center' class='boldbase'><b>$Lang::tr{'port'}</b></th>
326a764d 413<th width='33%' align='center' class='boldbase'><b>$Lang::tr{'count'}</b></th>
5ee7500e 414<th width='33%' align='center' class='boldbase'><b>$Lang::tr{'percentage'}</b></th>
b740cdd7 415</tr>
76dd79d1
MT
416END
417;
418
419my $total=0;
420my $show=0;
76dd79d1
MT
421
422my $s;
423my $percent;
fda1f52f
AM
424my $col="";
425
76dd79d1
MT
426for($s=0;$s<$lines;$s++)
427{
428 $show++;
429 $percent = $value[$s] * 100 / $linesjc;
9fb25b1c 430 $percent = sprintf("%.f", $percent);
76dd79d1 431 $total = $total + $value[$s];
c1018273
AH
432 my $colorIndex = $color % 10;
433 if($colorIndex == 0) {
434 $colorIndex = 10;
435 }
436 $col="bgcolor='$color{\"color$colorIndex\"}'";
437 print "<tr>";
4b4aec3a
CS
438
439 $color++;
326a764d 440 print "<td align='center' $col><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='$Lang::tr{'details'}'></form></td>";
b740cdd7
AH
441 print "<td align='center' $col>$key[$s]</td>";
442 print "<td align='center' $col>$value[$s]</td>";
443 print "<td align='center' $col>$percent</td>";
444 print "</tr>";
76dd79d1
MT
445}
446
447if($cgiparams{'otherspie'} == 2 ){}
448else{
c1018273
AH
449 my $colorIndex = $color % 10;
450 if($colorIndex == 0) {
451 $colorIndex = 10;
452 }
453 $col="bgcolor='$color{\"color$colorIndex\"}'";
454 print "<tr>";
76dd79d1 455
cf29614f
CS
456if ( $linesjc ne "0")
457{
76dd79d1
MT
458my $dif;
459$dif = $linesjc - $total;
460$percent = $dif * 100 / $linesjc;
9fb25b1c 461$percent = sprintf("%.f", $percent);
76dd79d1 462print <<END
b740cdd7
AH
463<td align='center' $col></td>
464<td align='center' $col>$Lang::tr{'otherport'}</td>
465<td align='center' $col>$dif</td>
466<td align='center' $col>$percent</td>
467</tr>
76dd79d1
MT
468END
469;
470}
cf29614f 471}
76dd79d1 472print <<END
b740cdd7 473</table>
76dd79d1
MT
474END
475;
476
477&Header::closebox();
478&Header::closebigbox();
479&Header::closepage();
480
481sub findkey {
c1018273 482 my $v;
76dd79d1
MT
483 foreach $v (@keytabjc) {
484 if ($tabjc{$v} eq $_[0]) {
485 delete $tabjc{$v};
486 $key[$indice] = "$v";
487 $value[$indice] = $_[0];
488 $indice++;
489 last;
490 }
491 }
492}
c1018273 493
76dd79d1 494sub checkversion {
c1018273
AH
495 #Automatic Updates is disabled
496 return "0","0";
497}
498