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