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