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