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