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