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