]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/logs.cgi/log.dat
BUG12156: GUI cosmetic to show woi logs cleaner
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / logs.cgi / log.dat
CommitLineData
cd1a2927
MT
1#!/usr/bin/perl
2#
3# SmoothWall CGIs
4#
5# This code is distributed under the terms of the GPL
6#
7# (c) The SmoothWall Team
8#
9# $Id: log.dat,v 1.6.2.22 2006/01/03 12:47:17 franck78 Exp $
10#
11
12use strict;
13
14# enable only the following on debugging purpose
5595bc03
CS
15use warnings;
16use CGI::Carp 'fatalsToBrowser';
cd1a2927 17
986e08d9 18require '/var/ipfire/general-functions.pl';
cd1a2927
MT
19require "${General::swroot}/lang.pl";
20require "${General::swroot}/header.pl";
21
f2fdd0c1
CS
22my %color = ();
23my %mainsettings = ();
24&General::readhash("${General::swroot}/main/settings", \%mainsettings);
25&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
26
cd1a2927
MT
27use POSIX();
28
cd1a2927
MT
29my %cgiparams=();
30my %logsettings=();
31my $errormessage = '';
32
33my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
34 'Sep', 'Oct', 'Nov', 'Dec' );
35my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
36 $Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
37 $Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
38 $Lang::tr{'december'} );
39
40my @now = localtime();
41my $dow = $now[6];
42my $doy = $now[7];
43my $tdoy = $now[7];
44my $year = $now[5]+1900;
45
46$cgiparams{'DAY'} = $now[3];
47$cgiparams{'MONTH'} = $now[4];
48$cgiparams{'ACTION'} = '';
97fe1741 49$cgiparams{'SECTION'} = 'ipfire';
cd1a2927
MT
50
51my %sections = (
5e818d6a 52 'auth' => '(\w+\(pam_unix\)\[.*\]: )',
4f19781d 53 'wio' => '(wio:|wio\[.*\])',
6d31cfdd 54 'captive' => '(Captive:)',
4f57d698 55 'clamav' => '(clamd\[.*\]: |freshclam\[.*\]: )',
5362088b 56 'collectd' => '(collectd\[.*\]: )',
5595bc03 57 'cron' => '(fcron\[.*\]: )',
5e818d6a
MF
58 'ddns' => '(ddns\[\d+\]:)',
59 'dhcp' => '(dhcpd: )',
60 'dma' => '(dma\[.*\]: )',
5e818d6a
MF
61 'guardian' => '(guardian\[.*\]: )',
62 'ipfire' => '(ipfire: )',
0e14ef8a 63 'ipsec' => '(ipsec_[\w_]+: |pluto\[.*\]: |charon: |vpnwatch: )',
5e818d6a
MF
64 'kernel' => '(kernel: (?!DROP_))',
65 'ntp' => '(ntpd(?:ate)?\[.*\]: )',
5508f18c 66 'oinkmaster' => '(oinkmaster\[.*\]: )',
0f0db884 67 'openvpn' => '(openvpnserver\[.*\]: |.*n2n\[.*\]: )',
5e818d6a
MF
68 'pakfire' => '(pakfire:)',
69 'red' => '(red:|pppd\[.*\]: |chat\[.*\]|pppoe\[.*\]|pptp\[.*\]|pppoa\[.*\]|pppoa3\[.*\]|pppoeci\[.*\]|ipppd|ipppd\[.*\]|kernel: ippp\d|kernel: isdn.*|ibod\[.*\]|dhcpcd\[.*\]|modem_run\[.*\])',
830dfc97 70 'suricata' => '(suricata: )',
5e818d6a
MF
71 'squid' => '(squid\[.*\]: |squid: )',
72 'ssh' => '(sshd(?:\(.*\))?\[.*\]: )',
d659de88 73 'unbound' => '(unbound: \[.*:.*\])(.*:.*$)',
5e818d6a
MF
74 'urlfilter bl' => '(installpackage\[urlfilter\]: )',
75 'wireless' => '(hostapd:|kernel: ath.*:|kernel: wifi[0-9]:)'
cd1a2927
MT
76 );
77
78# Translations for the %sections array.
79my %trsections = (
5e818d6a 80 'auth' => "$Lang::tr{'loginlogout'}",
dec52693 81 'wio' => 'Who Is Online?',
e26a5c48 82 'captive' => $Lang::tr{'Captive'},
5e818d6a
MF
83 'clamav' => 'ClamAV',
84 'collectd' => 'Collectd',
85 'cron' => 'Cron',
9494c611 86 'ddns' => "$Lang::tr{'dynamic dns'}",
cd1a2927 87 'dhcp' => "$Lang::tr{'dhcp server'}",
5e818d6a 88 'dma' => 'Mail',
5e818d6a
MF
89 'guardian' => "$Lang::tr{'guardian'}",
90 'ipfire' => 'IPFire',
cd1a2927 91 'ipsec' => 'IPSec',
5e818d6a
MF
92 'kernel' => "$Lang::tr{'kernel'}",
93 'ntp' => 'NTP',
c77bd492 94 'oinkmaster' => 'Oinkmaster',
6e13d0a5 95 'openvpn' => 'OpenVPN',
c506cad0 96 'pakfire' => 'Pakfire',
5e818d6a 97 'red' => 'RED',
9f9651e0 98 'suricata' => "$Lang::tr{'intrusion detection'}",
5e818d6a
MF
99 'squid' => "$Lang::tr{'web proxy'}",
100 'ssh' => 'SSH',
d659de88 101 'unbound' => 'DNS: Unbound',
5e818d6a
MF
102 'urlfilter bl' => 'URLFilter Blacklist',
103 'wireless' => 'Wireless'
cd1a2927
MT
104 );
105
106
107&Header::getcgihash(\%cgiparams);
108$logsettings{'LOGVIEW_REVERSE'} = 'off';
109&General::readhash("${General::swroot}/logging/settings", \%logsettings);
110${Header::viewsize} = defined ($logsettings{'LOGVIEW_VIEWSIZE'}) ? $logsettings{'LOGVIEW_VIEWSIZE'} : 150;
111
112my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ? 0x7FFFF000 : 0; #index of firts line number to display
113
114if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
115{
116 my @temp = split(',',$ENV{'QUERY_STRING'});
117 $start = $temp[0];
118 $cgiparams{'MONTH'} = $temp[1];
119 $cgiparams{'DAY'} = $temp[2];
120 $cgiparams{'SECTION'} = $temp[3];
121}
122
123if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
124 !($cgiparams{'DAY'} =~ /^(0|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)$/))
125{
126 $cgiparams{'DAY'} = $now[3];
127 $cgiparams{'MONTH'} = $now[4];
128}
129elsif($cgiparams{'ACTION'} eq '>>')
130{
131 my @temp_then=();
132 my @temp_now = localtime(time);
133 $temp_now[4] = $cgiparams{'MONTH'};
134 $temp_now[3] = $cgiparams{'DAY'};
135 if ($cgiparams{'DAY'}) {
136 @temp_then = localtime(POSIX::mktime(@temp_now) + 86400);
137 ## Retrieve the same time on the next day +
138 ## 86400 seconds in a day
139 } else {
140 $temp_now[3] = 1;
141 $temp_now[4] = ($temp_now[4]+1) %12;
142 @temp_then = localtime(POSIX::mktime(@temp_now) );
143 $temp_then[3] = 0;
144 }
145 $cgiparams{'MONTH'} = $temp_then[4];
146 $cgiparams{'DAY'} = $temp_then[3];
147}
148elsif($cgiparams{'ACTION'} eq '<<')
149{
150 my @temp_then=();
151 my @temp_now = localtime(time);
152 $temp_now[4] = $cgiparams{'MONTH'};
153 $temp_now[3] = $cgiparams{'DAY'};
154 if ($cgiparams{'DAY'}) {
155 @temp_then = localtime(POSIX::mktime(@temp_now) - 86400);
156 ## Retrieve the same time on the next day -
157 ## 86400 seconds in a day
158 } else {
159 $temp_now[3] = 1;
160 $temp_now[4] = ($temp_now[4]-1) %12;
161 @temp_then = localtime(POSIX::mktime(@temp_now) );
162 $temp_then[3] = 0;
163 }
164 $cgiparams{'MONTH'} = $temp_then[4];
165 $cgiparams{'DAY'} = $temp_then[3];
166}
167
168# Find in which file.gz is the log. Can be calculated because WEEKLY ROTATING of access.log
169my $gzindex;
170my $date = $cgiparams{'DAY'} == 0 ? '' : $cgiparams{'DAY'} <= 9 ? "0$cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
171
172{
173 my $xday;
9f663e66 174
cd1a2927
MT
175 # Calculate time. If future date, calculate for past year !!!
176 if (( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
177 ( $cgiparams{'MONTH'} > $now[4] ) ) {
178 $xday = POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 );
179 $date = "$longmonths[$cgiparams{'MONTH'}] $date, ". int($year-1);
180 } else {
181 $xday = POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 );
182 $date = "$longmonths[$cgiparams{'MONTH'}] $date, $year";
183 }
184
185 # calculate end of active week (saturday 23H59)
186 my @then = ();
187 @then = localtime(time());
0d08de33 188 my $sunday = POSIX::mktime( 0, 0, 0, $then[3], $then[4], $then[5]);
cd1a2927
MT
189 $sunday += (6-$then[6]) * 86400;
190
191 # Convert delta in second to full weeks
192 $gzindex = int (($sunday-$xday)/604800 );
193}
9f663e66 194
cd1a2927
MT
195my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
196my $daystr = $cgiparams{'DAY'} == 0 ? '..' : $cgiparams{'DAY'} <= 9 ? " $cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
197my $section = $sections{$cgiparams{'SECTION'}};
198
199my $lines = 0;
200my @log=();
201
202my $loop = 1;
203my $filestr = 0;
204my $lastdatetime; # for debug
205my $search_for_end = 0;
9f663e66 206
cd1a2927
MT
207while ($gzindex >=0 && $loop) {
208 # calculate file name
209 if ($gzindex == 0) {
210 $filestr = "/var/log/messages";
1986cc88 211 $filestr = "/var/log/mail" if (${section} =~ 'dma');
cd1a2927
MT
212 } else {
213 $filestr = "/var/log/messages.$gzindex";
1986cc88 214 $filestr = "/var/log/mail.$gzindex" if (${section} =~ 'dma');
cd1a2927
MT
215 $filestr = "$filestr.gz" if -f "$filestr.gz";
216 }
217 # now read file if existing
218 if (open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr))) {
219 #&General::log("reading $filestr");
220 READ:while (<FILE>) {
221 my $line = $_;
5595bc03 222 if ($line =~ /^${monthstr} ${daystr} ..:..:.. [\w\-]+ ${section}(.*)/) {
5e818d6a 223 # when standard viewing, just keep in memory the correct slice
cd1a2927
MT
224 # it starts a '$start' and size is $viewport
225 # If export, then keep all lines...
226 if ($cgiparams{'ACTION'} eq $Lang::tr{'export'}){
227 $log[$lines++] = "$line";
228 } else {
229 if ($lines++ < ($start + $Header::viewsize)) {
230 push(@log,"$line");
231 if (@log > $Header::viewsize) {
232 shift (@log);
233 }
5e818d6a 234 #} else { don't do this optimisation, need to count lines !
cd1a2927
MT
235 # $datetime = $maxtime; # we have read viewsize lines, stop main loop
236 # last READ; # exit read file
237 }
238 }
239 $search_for_end = 1; # we find the start of slice, can look for end now
240 } else {
241 if ($search_for_end == 1) {
242 #finish read files when date is over (test month equality only)
243 $line =~ /^(...) (..) ..:..:..*$/;
244 $loop = 0 if ( ($1 ne $monthstr) || ( ($daystr ne '..') && ($daystr ne $2) ) );
245 }
246 }
247 }
248 close (FILE);
249 }
250 $gzindex--; # will try next gz file eg 40,39,38,.... because it may have holes when ipcop stopped
251 # for a long time
252}# while
253
254# $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
255
256if ($cgiparams{'ACTION'} eq $Lang::tr{'export'})
257{
258 print "Content-type: text/plain\n\n";
78331e30 259 print "IPFire diagnostics\r\n";
cd1a2927
MT
260 print "$Lang::tr{'section'}: $cgiparams{'SECTION'}\n";
261 print "$Lang::tr{'date'}: $date\r\n\r\n";
262
263 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
264
265 foreach $_ (@log)
266 {
5595bc03 267 /^... (..) (..:..:..) [\w\-]+ ${section}(.*)$/;
cd1a2927
MT
268 my $day = $1;
269 $day =~ tr / /0/;
270 my $time = $cgiparams{'DAY'} ? "$2" : "$day/$2" ;
5595bc03 271 print "$time $3 $4\r\n";
cd1a2927
MT
272 }
273 exit 0;
274}
275
276&Header::showhttpheaders();
277
986e08d9 278&Header::openpage($Lang::tr{'system logs'}, 1, '');
cd1a2927
MT
279
280&Header::openbigbox('100%', 'left', '', $errormessage);
281
282if ($errormessage) {
283 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
284 print "<font class='base'>$errormessage&nbsp;</font>\n";
285 &Header::closebox();
286}
287
288&Header::openbox('100%', 'left', "$Lang::tr{'settings'}:");
289
290print <<END
291<form method='post' action='$ENV{'SCRIPT_NAME'}'>
292<table width='100%'>
293<tr>
294 <td width='10%' class='base'>$Lang::tr{'section'}:&nbsp;</td>
295 <td width='20%'>
296 <select name='SECTION'>
297END
298;
5e818d6a 299foreach $section (sort {$trsections{$a} cmp $trsections{$b}} keys %sections) {
cd1a2927
MT
300 print "\t<option ";
301 if ($section eq $cgiparams{'SECTION'}) {
302 print "selected='selected' "; }
303 print "value='$section'>$trsections{$section}</option>\n";
304}
305print <<END
306 </select>
307 </td>
308 <td width='10%' class='base' align='right'>$Lang::tr{'month'}:&nbsp;</td>
309 <td width='10%'>
310 <select name='MONTH'>
311END
312;
313for (my $month = 0; $month < 12; $month++)
314{
315 print "\t<option ";
316 if ($month == $cgiparams{'MONTH'}) {
317 print "selected='selected' "; }
318 print "value='$month'>$longmonths[$month]</option>\n";
319}
320print <<END
321 </select>
322 </td>
323 <td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
324 <td width='40%'>
325 <select name='DAY'>
326END
327;
328print "<option value='0'>$Lang::tr{'all'}</option>\n";
9f663e66 329for (my $day = 1; $day <= 31; $day++)
cd1a2927
MT
330{
331 print "\t<option ";
332 if ($day == $cgiparams{'DAY'}) {
333 print "selected='selected' "; }
334 print "value='$day'>$day</option>\n";
335}
336print <<END
337</select>
338</td>
339<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
340<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
341<td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
342<td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'export'}' /></td>
343</tr>
344</table>
345</form>
346END
347;
348
349&Header::closebox();
350
351&Header::openbox('100%', 'left', $Lang::tr{'log'});
352print "<p><b>$Lang::tr{'total hits for log section'} $cgiparams{'SECTION'} $date: $lines</b></p>";
353
354$start = $lines - ${Header::viewsize} if ($start >= $lines - ${Header::viewsize});
355$start = 0 if ($start < 0);
356
357my $prev;
358 if ($start == 0) {
359 $prev = -1;
360 } else {
361 $prev = $start - ${Header::viewsize};
362 $prev = 0 if ( $prev < 0);
363 }
9f663e66 364
cd1a2927
MT
365my $next;
366 if ($start == $lines - ${Header::viewsize}) {
367 $next = -1;
368 } else {
369 $next = $start + ${Header::viewsize};
370 $next = $lines - ${Header::viewsize} if ($next >= $lines - ${Header::viewsize});
371 }
372
373if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
374if ($lines != 0) { &oldernewer(); }
375
376print <<END
fda1f52f 377<table width='100%' class='tbl'>
cd1a2927 378<tr>
fda1f52f 379 <th width='10%' align='center' class='boldbase'><b>$Lang::tr{'time'}</b></th>
5e818d6a
MF
380 <th width='15%' align='center' class='boldbase'><b>$Lang::tr{'section'}</b></th>
381 <th width='75%'>&nbsp;</th>
cd1a2927
MT
382</tr>
383END
384;
385
386$lines = 0;
387#print '<tt>';
388foreach $_ (@log)
389{
5595bc03 390 /^... (..) (..:..:..) [\w\-]+ ${section}(.*)$/;
cd1a2927
MT
391 my $day = $1;
392 $day =~ tr / /0/;
393 my $time = $cgiparams{'DAY'} ? "$2" : "$day/$2" ;
394 my $sec = $3;
395 my $data = $4;
396 # correct the cut position, just when section=RED
397 if (($cgiparams{'SECTION'} eq 'red' ) && ($sec =~ /(kernel:)(.*)/)) {
398 $sec = 'kernel';
399 $data = $2.': '.$data;
400 }
401 my $d = substr ($data,0,80);
402 while (length($data)>80){ #very basic breaking of lines...
403 $data = substr ($data,80); #permit correct display in table cell
404 $d .= ' ' . substr ($data,0,80);
405 }
fda1f52f 406 my $col="";
cd1a2927
MT
407
408 if ($lines % 2) {
fda1f52f
AM
409 print "<tr>";
410 $col="bgcolor='$color{'color20'}'"; }
cd1a2927 411 else {
fda1f52f
AM
412 print "<tr>";
413 $col="bgcolor='$color{'color22'}'"; }
414 print "<td $col>$time<td $col>$sec<td $col>" .&Header::cleanhtml ("$d", 'y') . "</td></tr>\n";
cd1a2927
MT
415 $lines++;
416}
417#print '</tt>';
418print "</table>";
419
420&oldernewer();
421
422&Header::closebox();
423
424&Header::closebigbox();
425
426&Header::closepage();
427
428sub oldernewer
429{
430print <<END
431<table width='100%'>
432<tr>
433END
434;
435
436print "<td align='center' width='50%'>";
437if ($prev != -1) {
438 print "<a href='/cgi-bin/logs.cgi/log.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SECTION'}'>$Lang::tr{'older'}</a>"; }
439else {
440 print "$Lang::tr{'older'}"; }
441print "</td>\n";
442
443print "<td align='center' width='50%'>";
444if ($next >= 0) {
445 print "<a href='/cgi-bin/logs.cgi/log.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SECTION'}'>$Lang::tr{'newer'}</a>"; }
446else {
447 print "$Lang::tr{'newer'}"; }
448print "</td>\n";
449
450print <<END
451</tr>
452</table>
453END
454;
455}