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