76d5d295968655cd3219910be280a537ffe7b0e7
[ipfire-2.x.git] / html / cgi-bin / logs.cgi / log.dat
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
12 use strict;
13
14 # enable only the following on debugging purpose
15 #use warnings;
16 #use CGI::Carp 'fatalsToBrowser';
17
18 require 'CONFIG_ROOT/general-functions.pl';
19 require "${General::swroot}/lang.pl";
20 require "${General::swroot}/header.pl";
21
22 use POSIX();
23
24 #workaround to suppress a warning when a variable is used only once
25 my @dummy = ( ${Header::table2colour} );
26 undef (@dummy);
27
28 my %cgiparams=();
29 my %logsettings=();
30 my $errormessage = '';
31
32 my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
33 'Sep', 'Oct', 'Nov', 'Dec' );
34 my @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
39 my @now = localtime();
40 my $dow = $now[6];
41 my $doy = $now[7];
42 my $tdoy = $now[7];
43 my $year = $now[5]+1900;
44
45 $cgiparams{'DAY'} = $now[3];
46 $cgiparams{'MONTH'} = $now[4];
47 $cgiparams{'ACTION'} = '';
48 $cgiparams{'SECTION'} = 'ipfire';
49
50 my %sections = (
51 'ipfire' => '(ipfire)',
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.
66 my %trsections = (
67 'ipfire' => 'IPFire',
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
87 my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ? 0x7FFFF000 : 0; #index of firts line number to display
88
89 if ($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
98 if (!($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 }
104 elsif($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 }
123 elsif($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
144 my $gzindex;
145 my $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
170 my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
171 my $daystr = $cgiparams{'DAY'} == 0 ? '..' : $cgiparams{'DAY'} <= 9 ? " $cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
172 my $section = $sections{$cgiparams{'SECTION'}};
173
174 my $lines = 0;
175 my @log=();
176
177 my $loop = 1;
178 my $filestr = 0;
179 my $lastdatetime; # for debug
180 my $search_for_end = 0;
181
182 while ($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
229 if ($cgiparams{'ACTION'} eq $Lang::tr{'export'})
230 {
231 print "Content-type: text/plain\n\n";
232 print "IPFire diagnostics\r\n";
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
255 if ($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
263 print <<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'>
270 END
271 ;
272 foreach $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 }
278 print <<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'>
284 END
285 ;
286 for (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 }
293 print <<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'>
299 END
300 ;
301 print "<option value='0'>$Lang::tr{'all'}</option>\n";
302 for (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 }
309 print <<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>
319 END
320 ;
321
322 &Header::closebox();
323
324 &Header::openbox('100%', 'left', $Lang::tr{'log'});
325 print "<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
330 my $prev;
331 if ($start == 0) {
332 $prev = -1;
333 } else {
334 $prev = $start - ${Header::viewsize};
335 $prev = 0 if ( $prev < 0);
336 }
337
338 my $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
346 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
347 if ($lines != 0) { &oldernewer(); }
348
349 print <<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>
356 END
357 ;
358
359 $lines = 0;
360 #print '<tt>';
361 foreach $_ (@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>';
389 print "</table>";
390
391 &oldernewer();
392
393 &Header::closebox();
394
395 &Header::closebigbox();
396
397 &Header::closepage();
398
399 sub oldernewer
400 {
401 print <<END
402 <table width='100%'>
403 <tr>
404 END
405 ;
406
407 print "<td align='center' width='50%'>";
408 if ($prev != -1) {
409 print "<a href='/cgi-bin/logs.cgi/log.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SECTION'}'>$Lang::tr{'older'}</a>"; }
410 else {
411 print "$Lang::tr{'older'}"; }
412 print "</td>\n";
413
414 print "<td align='center' width='50%'>";
415 if ($next >= 0) {
416 print "<a href='/cgi-bin/logs.cgi/log.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SECTION'}'>$Lang::tr{'newer'}</a>"; }
417 else {
418 print "$Lang::tr{'newer'}"; }
419 print "</td>\n";
420
421 print <<END
422 </tr>
423 </table>
424 END
425 ;
426 }