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