Gro├čes Update:
[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
15#use warnings;
16#use CGI::Carp 'fatalsToBrowser';
17
18require 'CONFIG_ROOT/general-functions.pl';
19require "${General::swroot}/lang.pl";
20require "${General::swroot}/header.pl";
21
22use POSIX();
23
24#workaround to suppress a warning when a variable is used only once
25my @dummy = ( ${Header::table2colour} );
26undef (@dummy);
27
28my %cgiparams=();
29my %logsettings=();
30my $errormessage = '';
31
32my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
33 'Sep', 'Oct', 'Nov', 'Dec' );
34my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
35 $Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
36 $Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
37 $Lang::tr{'december'} );
38
39my @now = localtime();
40my $dow = $now[6];
41my $doy = $now[7];
42my $tdoy = $now[7];
43my $year = $now[5]+1900;
44
45$cgiparams{'DAY'} = $now[3];
46$cgiparams{'MONTH'} = $now[4];
47$cgiparams{'ACTION'} = '';
78331e30 48$cgiparams{'SECTION'} = 'ipfire';
cd1a2927
MT
49
50my %sections = (
78331e30 51 'ipfire' => '(ipfire)',
cd1a2927
MT
52 'red' => '(red.*|kernel: usb.*|pppd\[.*\]|chat\[.*\]|pppoe\[.*\]|pptp\[.*\]|pppoa\[.*\]|pppoa3\[.*\]|pppoeci\[.*\]|ipppd|ipppd\[.*\]|kernel: ippp\d|kernel: isdn.*|ibod\[.*\]|kernel: eth.*|dhcpcd\[.*\]|modem_run\[.*\])',
53 'dns' => '(dnsmasq\[.*\])',
54 'dhcp' => '(dhcpd)',
55 'cron' => '(fcron\[.*\])',
56 'ntp' => '(ntpd(?:ate)?\[.*\])',
57 'ssh' => '(sshd(?:\(.*\))?\[.*\])',
58 'auth' => '(\w+\(pam_unix\)\[.*\])',
59 'kernel' => '(kernel)',
60 'ipsec' => '(ipsec_[\w_]+|pluto\[.*\])',
61 'snort' => '(snort)',
62 'installpackage' => '(installpackage\[.*\])'
63 );
64
65# Translations for the %sections array.
66my %trsections = (
78331e30 67 'ipfire' => 'IPFire',
cd1a2927
MT
68 'red' => 'RED',
69 'dns' => 'DNS',
70 'dhcp' => "$Lang::tr{'dhcp server'}",
71 'cron' => 'Cron',
72 'ntp' => 'NTP',
73 'ssh' => 'SSH',
74 'auth' => "$Lang::tr{'loginlogout'}",
75 'kernel' => "$Lang::tr{'kernel'}",
76 'ipsec' => 'IPSec',
77 'snort' => 'Snort',
78 'installpackage' => "$Lang::tr{'update transcript'}"
79 );
80
81
82&Header::getcgihash(\%cgiparams);
83$logsettings{'LOGVIEW_REVERSE'} = 'off';
84&General::readhash("${General::swroot}/logging/settings", \%logsettings);
85${Header::viewsize} = defined ($logsettings{'LOGVIEW_VIEWSIZE'}) ? $logsettings{'LOGVIEW_VIEWSIZE'} : 150;
86
87my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ? 0x7FFFF000 : 0; #index of firts line number to display
88
89if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
90{
91 my @temp = split(',',$ENV{'QUERY_STRING'});
92 $start = $temp[0];
93 $cgiparams{'MONTH'} = $temp[1];
94 $cgiparams{'DAY'} = $temp[2];
95 $cgiparams{'SECTION'} = $temp[3];
96}
97
98if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
99 !($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)$/))
100{
101 $cgiparams{'DAY'} = $now[3];
102 $cgiparams{'MONTH'} = $now[4];
103}
104elsif($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 if ($cgiparams{'DAY'}) {
111 @temp_then = localtime(POSIX::mktime(@temp_now) + 86400);
112 ## Retrieve the same time on the next day +
113 ## 86400 seconds in a day
114 } else {
115 $temp_now[3] = 1;
116 $temp_now[4] = ($temp_now[4]+1) %12;
117 @temp_then = localtime(POSIX::mktime(@temp_now) );
118 $temp_then[3] = 0;
119 }
120 $cgiparams{'MONTH'} = $temp_then[4];
121 $cgiparams{'DAY'} = $temp_then[3];
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}
142
143# Find in which file.gz is the log. Can be calculated because WEEKLY ROTATING of access.log
144my $gzindex;
145my $date = $cgiparams{'DAY'} == 0 ? '' : $cgiparams{'DAY'} <= 9 ? "0$cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
146
147{
148 my $xday;
149
150 # Calculate time. If future date, calculate for past year !!!
151 if (( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
152 ( $cgiparams{'MONTH'} > $now[4] ) ) {
153 $xday = POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 );
154 $date = "$longmonths[$cgiparams{'MONTH'}] $date, ". int($year-1);
155 } else {
156 $xday = POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 );
157 $date = "$longmonths[$cgiparams{'MONTH'}] $date, $year";
158 }
159
160 # calculate end of active week (saturday 23H59)
161 my @then = ();
162 @then = localtime(time());
163 my $sunday = POSIX::mktime( 0, 0, 0, @then[3], @then[4], @then[5]);
164 $sunday += (6-$then[6]) * 86400;
165
166 # Convert delta in second to full weeks
167 $gzindex = int (($sunday-$xday)/604800 );
168}
169
170my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
171my $daystr = $cgiparams{'DAY'} == 0 ? '..' : $cgiparams{'DAY'} <= 9 ? " $cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
172my $section = $sections{$cgiparams{'SECTION'}};
173
174my $lines = 0;
175my @log=();
176
177my $loop = 1;
178my $filestr = 0;
179my $lastdatetime; # for debug
180my $search_for_end = 0;
181
182while ($gzindex >=0 && $loop) {
183 # calculate file name
184 if ($gzindex == 0) {
185 $filestr = "/var/log/messages";
186 } else {
187 $filestr = "/var/log/messages.$gzindex";
188 $filestr = "$filestr.gz" if -f "$filestr.gz";
189 }
190 # now read file if existing
191 if (open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr))) {
192 #&General::log("reading $filestr");
193 READ:while (<FILE>) {
194 my $line = $_;
195 if ($line =~ /^${monthstr} ${daystr} ..:..:.. [\w\-]+ ${section}: (.*)/) {
196 # when standart viewing, just keep in memory the correct slice
197 # it starts a '$start' and size is $viewport
198 # If export, then keep all lines...
199 if ($cgiparams{'ACTION'} eq $Lang::tr{'export'}){
200 $log[$lines++] = "$line";
201 } else {
202 if ($lines++ < ($start + $Header::viewsize)) {
203 push(@log,"$line");
204 if (@log > $Header::viewsize) {
205 shift (@log);
206 }
207 #} else { dont do this optimisation, need to count lines !
208 # $datetime = $maxtime; # we have read viewsize lines, stop main loop
209 # last READ; # exit read file
210 }
211 }
212 $search_for_end = 1; # we find the start of slice, can look for end now
213 } else {
214 if ($search_for_end == 1) {
215 #finish read files when date is over (test month equality only)
216 $line =~ /^(...) (..) ..:..:..*$/;
217 $loop = 0 if ( ($1 ne $monthstr) || ( ($daystr ne '..') && ($daystr ne $2) ) );
218 }
219 }
220 }
221 close (FILE);
222 }
223 $gzindex--; # will try next gz file eg 40,39,38,.... because it may have holes when ipcop stopped
224 # for a long time
225}# while
226
227# $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
228
229if ($cgiparams{'ACTION'} eq $Lang::tr{'export'})
230{
231 print "Content-type: text/plain\n\n";
78331e30 232 print "IPFire diagnostics\r\n";
cd1a2927
MT
233 print "$Lang::tr{'section'}: $cgiparams{'SECTION'}\n";
234 print "$Lang::tr{'date'}: $date\r\n\r\n";
235
236 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
237
238 foreach $_ (@log)
239 {
240 /^... (..) (..:..:..) [\w\-]+ ${section}: (.*)$/;
241 my $day = $1;
242 $day =~ tr / /0/;
243 my $time = $cgiparams{'DAY'} ? "$2" : "$day/$2" ;
244 print "$time $3 $4\r\n";
245 }
246 exit 0;
247}
248
249&Header::showhttpheaders();
250
251&Header::openpage($Lang::tr{'firewall log'}, 1, '');
252
253&Header::openbigbox('100%', 'left', '', $errormessage);
254
255if ($errormessage) {
256 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
257 print "<font class='base'>$errormessage&nbsp;</font>\n";
258 &Header::closebox();
259}
260
261&Header::openbox('100%', 'left', "$Lang::tr{'settings'}:");
262
263print <<END
264<form method='post' action='$ENV{'SCRIPT_NAME'}'>
265<table width='100%'>
266<tr>
267 <td width='10%' class='base'>$Lang::tr{'section'}:&nbsp;</td>
268 <td width='20%'>
269 <select name='SECTION'>
270END
271;
272foreach $section (keys %sections) {
273 print "\t<option ";
274 if ($section eq $cgiparams{'SECTION'}) {
275 print "selected='selected' "; }
276 print "value='$section'>$trsections{$section}</option>\n";
277}
278print <<END
279 </select>
280 </td>
281 <td width='10%' class='base' align='right'>$Lang::tr{'month'}:&nbsp;</td>
282 <td width='10%'>
283 <select name='MONTH'>
284END
285;
286for (my $month = 0; $month < 12; $month++)
287{
288 print "\t<option ";
289 if ($month == $cgiparams{'MONTH'}) {
290 print "selected='selected' "; }
291 print "value='$month'>$longmonths[$month]</option>\n";
292}
293print <<END
294 </select>
295 </td>
296 <td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
297 <td width='40%'>
298 <select name='DAY'>
299END
300;
301print "<option value='0'>$Lang::tr{'all'}</option>\n";
302for (my $day = 1; $day <= 31; $day++)
303{
304 print "\t<option ";
305 if ($day == $cgiparams{'DAY'}) {
306 print "selected='selected' "; }
307 print "value='$day'>$day</option>\n";
308}
309print <<END
310</select>
311</td>
312<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
313<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
314<td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
315<td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'export'}' /></td>
316</tr>
317</table>
318</form>
319END
320;
321
322&Header::closebox();
323
324&Header::openbox('100%', 'left', $Lang::tr{'log'});
325print "<p><b>$Lang::tr{'total hits for log section'} $cgiparams{'SECTION'} $date: $lines</b></p>";
326
327$start = $lines - ${Header::viewsize} if ($start >= $lines - ${Header::viewsize});
328$start = 0 if ($start < 0);
329
330my $prev;
331 if ($start == 0) {
332 $prev = -1;
333 } else {
334 $prev = $start - ${Header::viewsize};
335 $prev = 0 if ( $prev < 0);
336 }
337
338my $next;
339 if ($start == $lines - ${Header::viewsize}) {
340 $next = -1;
341 } else {
342 $next = $start + ${Header::viewsize};
343 $next = $lines - ${Header::viewsize} if ($next >= $lines - ${Header::viewsize});
344 }
345
346if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
347if ($lines != 0) { &oldernewer(); }
348
349print <<END
350<table width='100%'>
351<tr>
352 <td width='10%' align='center' class='boldbase'><b>$Lang::tr{'time'}</b></td>
353 <td width='15%' align='center' class='boldbase'><b>$Lang::tr{'section'}</b></td>
354 <td width='75%'>&nbsp;</td>
355</tr>
356END
357;
358
359$lines = 0;
360#print '<tt>';
361foreach $_ (@log)
362{
363 /^... (..) (..:..:..) [\w\-]+ ${section}: (.*)$/;
364 my $day = $1;
365 $day =~ tr / /0/;
366 my $time = $cgiparams{'DAY'} ? "$2" : "$day/$2" ;
367 my $sec = $3;
368 my $data = $4;
369 # correct the cut position, just when section=RED
370 if (($cgiparams{'SECTION'} eq 'red' ) && ($sec =~ /(kernel:)(.*)/)) {
371 $sec = 'kernel';
372 $data = $2.': '.$data;
373 }
374 my $d = substr ($data,0,80);
375 while (length($data)>80){ #very basic breaking of lines...
376 $data = substr ($data,80); #permit correct display in table cell
377 $d .= ' ' . substr ($data,0,80);
378 }
379
380
381 if ($lines % 2) {
382 print "<tr bgcolor='${Header::table1colour}'>"; }
383 else {
384 print "<tr bgcolor='${Header::table2colour}'>"; }
385 print "<td>$time<td>$sec<td>" .&Header::cleanhtml ("$d", 'y') . "</td></tr>\n";
386 $lines++;
387}
388#print '</tt>';
389print "</table>";
390
391&oldernewer();
392
393&Header::closebox();
394
395&Header::closebigbox();
396
397&Header::closepage();
398
399sub oldernewer
400{
401print <<END
402<table width='100%'>
403<tr>
404END
405;
406
407print "<td align='center' width='50%'>";
408if ($prev != -1) {
409 print "<a href='/cgi-bin/logs.cgi/log.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SECTION'}'>$Lang::tr{'older'}</a>"; }
410else {
411 print "$Lang::tr{'older'}"; }
412print "</td>\n";
413
414print "<td align='center' width='50%'>";
415if ($next >= 0) {
416 print "<a href='/cgi-bin/logs.cgi/log.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SECTION'}'>$Lang::tr{'newer'}</a>"; }
417else {
418 print "$Lang::tr{'newer'}"; }
419print "</td>\n";
420
421print <<END
422</tr>
423</table>
424END
425;
426}