]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/logs.cgi/log.dat
Added syslog filter for hostap messages
[people/pmueller/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 #workaround to suppress a warning when a variable is used only once
30 my @dummy = ( ${Header::table2colour} );
31 undef (@dummy);
32
33 my %cgiparams=();
34 my %logsettings=();
35 my $errormessage = '';
36
37 my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
38 'Sep', 'Oct', 'Nov', 'Dec' );
39 my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
40 $Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
41 $Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
42 $Lang::tr{'december'} );
43
44 my @now = localtime();
45 my $dow = $now[6];
46 my $doy = $now[7];
47 my $tdoy = $now[7];
48 my $year = $now[5]+1900;
49
50 $cgiparams{'DAY'} = $now[3];
51 $cgiparams{'MONTH'} = $now[4];
52 $cgiparams{'ACTION'} = '';
53 $cgiparams{'SECTION'} = 'ipfire';
54
55 my %sections = (
56 'ipfire' => '(ipfire: )',
57 'red' => '(red:|pppd\[.*\]: |chat\[.*\]|pppoe\[.*\]|pptp\[.*\]|pppoa\[.*\]|pppoa3\[.*\]|pppoeci\[.*\]|ipppd|ipppd\[.*\]|kernel: ippp\d|kernel: isdn.*|ibod\[.*\]|dhcpcd\[.*\]|modem_run\[.*\])',
58 'dns' => '(dnsmasq\[.*\]: )',
59 'dhcp' => '(dhcpd: )',
60 'clamav' => '(clamd\[.*\]: |freshclam\[.*\]: )',
61 'collectd' => '(collectd\[.*\]: )',
62 'cron' => '(fcron\[.*\]: )',
63 'ntp' => '(ntpd(?:ate)?\[.*\]: )',
64 'ssh' => '(sshd(?:\(.*\))?\[.*\]: )',
65 'auth' => '(\w+\(pam_unix\)\[.*\]: )',
66 'kernel' => '(kernel: (?!DROP_))',
67 'ipsec' => '(ipsec_[\w_]+: |pluto\[.*\]: |vpnwatch: )',
68 'openvpn' => '(openvpnserver)\[.*\]: ',
69 'pakfire' => '(pakfire:) ',
70 'wireless' => '(hostapd:) '
71 );
72
73 # Translations for the %sections array.
74 my %trsections = (
75 'ipfire' => 'IPFire',
76 'red' => 'RED',
77 'dns' => 'DNS',
78 'dhcp' => "$Lang::tr{'dhcp server'}",
79 'cron' => 'Cron',
80 'collectd' => 'Collectd',
81 'clamav' => 'ClamAV',
82 'ntp' => 'NTP',
83 'ssh' => 'SSH',
84 'auth' => "$Lang::tr{'loginlogout'}",
85 'kernel' => "$Lang::tr{'kernel'}",
86 'ipsec' => 'IPSec',
87 'openvpn' => 'OpenVPN',
88 'pakfire' => 'Pakfire',
89 'wireless' => 'Wireless'
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
98 my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ? 0x7FFFF000 : 0; #index of firts line number to display
99
100 if ($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
109 if (!($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 }
115 elsif($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 }
134 elsif($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
155 my $gzindex;
156 my $date = $cgiparams{'DAY'} == 0 ? '' : $cgiparams{'DAY'} <= 9 ? "0$cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
157
158 {
159 my $xday;
160
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());
174 my $sunday = POSIX::mktime( 0, 0, 0, @then[3], @then[4], @then[5]);
175 $sunday += (6-$then[6]) * 86400;
176
177 # Convert delta in second to full weeks
178 $gzindex = int (($sunday-$xday)/604800 );
179 }
180
181 my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
182 my $daystr = $cgiparams{'DAY'} == 0 ? '..' : $cgiparams{'DAY'} <= 9 ? " $cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
183 my $section = $sections{$cgiparams{'SECTION'}};
184
185 my $lines = 0;
186 my @log=();
187
188 my $loop = 1;
189 my $filestr = 0;
190 my $lastdatetime; # for debug
191 my $search_for_end = 0;
192
193 while ($gzindex >=0 && $loop) {
194 # calculate file name
195 if ($gzindex == 0) {
196 $filestr = "/var/log/messages";
197 } else {
198 $filestr = "/var/log/messages.$gzindex";
199 $filestr = "$filestr.gz" if -f "$filestr.gz";
200 }
201 # now read file if existing
202 if (open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr))) {
203 #&General::log("reading $filestr");
204 READ:while (<FILE>) {
205 my $line = $_;
206 if ($line =~ /^${monthstr} ${daystr} ..:..:.. [\w\-]+ ${section}(.*)/) {
207 # when standart viewing, just keep in memory the correct slice
208 # it starts a '$start' and size is $viewport
209 # If export, then keep all lines...
210 if ($cgiparams{'ACTION'} eq $Lang::tr{'export'}){
211 $log[$lines++] = "$line";
212 } else {
213 if ($lines++ < ($start + $Header::viewsize)) {
214 push(@log,"$line");
215 if (@log > $Header::viewsize) {
216 shift (@log);
217 }
218 #} else { dont do this optimisation, need to count lines !
219 # $datetime = $maxtime; # we have read viewsize lines, stop main loop
220 # last READ; # exit read file
221 }
222 }
223 $search_for_end = 1; # we find the start of slice, can look for end now
224 } else {
225 if ($search_for_end == 1) {
226 #finish read files when date is over (test month equality only)
227 $line =~ /^(...) (..) ..:..:..*$/;
228 $loop = 0 if ( ($1 ne $monthstr) || ( ($daystr ne '..') && ($daystr ne $2) ) );
229 }
230 }
231 }
232 close (FILE);
233 }
234 $gzindex--; # will try next gz file eg 40,39,38,.... because it may have holes when ipcop stopped
235 # for a long time
236 }# while
237
238 # $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
239
240 if ($cgiparams{'ACTION'} eq $Lang::tr{'export'})
241 {
242 print "Content-type: text/plain\n\n";
243 print "IPFire diagnostics\r\n";
244 print "$Lang::tr{'section'}: $cgiparams{'SECTION'}\n";
245 print "$Lang::tr{'date'}: $date\r\n\r\n";
246
247 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
248
249 foreach $_ (@log)
250 {
251 /^... (..) (..:..:..) [\w\-]+ ${section}(.*)$/;
252 my $day = $1;
253 $day =~ tr / /0/;
254 my $time = $cgiparams{'DAY'} ? "$2" : "$day/$2" ;
255 print "$time $3 $4\r\n";
256 }
257 exit 0;
258 }
259
260 &Header::showhttpheaders();
261
262 &Header::openpage($Lang::tr{'system logs'}, 1, '');
263
264 &Header::openbigbox('100%', 'left', '', $errormessage);
265
266 if ($errormessage) {
267 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
268 print "<font class='base'>$errormessage&nbsp;</font>\n";
269 &Header::closebox();
270 }
271
272 &Header::openbox('100%', 'left', "$Lang::tr{'settings'}:");
273
274 print <<END
275 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
276 <table width='100%'>
277 <tr>
278 <td width='10%' class='base'>$Lang::tr{'section'}:&nbsp;</td>
279 <td width='20%'>
280 <select name='SECTION'>
281 END
282 ;
283 foreach $section (keys %sections) {
284 print "\t<option ";
285 if ($section eq $cgiparams{'SECTION'}) {
286 print "selected='selected' "; }
287 print "value='$section'>$trsections{$section}</option>\n";
288 }
289 print <<END
290 </select>
291 </td>
292 <td width='10%' class='base' align='right'>$Lang::tr{'month'}:&nbsp;</td>
293 <td width='10%'>
294 <select name='MONTH'>
295 END
296 ;
297 for (my $month = 0; $month < 12; $month++)
298 {
299 print "\t<option ";
300 if ($month == $cgiparams{'MONTH'}) {
301 print "selected='selected' "; }
302 print "value='$month'>$longmonths[$month]</option>\n";
303 }
304 print <<END
305 </select>
306 </td>
307 <td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
308 <td width='40%'>
309 <select name='DAY'>
310 END
311 ;
312 print "<option value='0'>$Lang::tr{'all'}</option>\n";
313 for (my $day = 1; $day <= 31; $day++)
314 {
315 print "\t<option ";
316 if ($day == $cgiparams{'DAY'}) {
317 print "selected='selected' "; }
318 print "value='$day'>$day</option>\n";
319 }
320 print <<END
321 </select>
322 </td>
323 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
324 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
325 <td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
326 <td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'export'}' /></td>
327 </tr>
328 </table>
329 </form>
330 END
331 ;
332
333 &Header::closebox();
334
335 &Header::openbox('100%', 'left', $Lang::tr{'log'});
336 print "<p><b>$Lang::tr{'total hits for log section'} $cgiparams{'SECTION'} $date: $lines</b></p>";
337
338 $start = $lines - ${Header::viewsize} if ($start >= $lines - ${Header::viewsize});
339 $start = 0 if ($start < 0);
340
341 my $prev;
342 if ($start == 0) {
343 $prev = -1;
344 } else {
345 $prev = $start - ${Header::viewsize};
346 $prev = 0 if ( $prev < 0);
347 }
348
349 my $next;
350 if ($start == $lines - ${Header::viewsize}) {
351 $next = -1;
352 } else {
353 $next = $start + ${Header::viewsize};
354 $next = $lines - ${Header::viewsize} if ($next >= $lines - ${Header::viewsize});
355 }
356
357 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
358 if ($lines != 0) { &oldernewer(); }
359
360 print <<END
361 <table width='100%'>
362 <tr>
363 <td width='10%' align='center' class='boldbase'><b>$Lang::tr{'time'}</b></td>
364 <td width='10%' align='center' class='boldbase'><b>$Lang::tr{'section'}</b></td>
365 <td width='80%'>&nbsp;</td>
366 </tr>
367 END
368 ;
369
370 $lines = 0;
371 #print '<tt>';
372 foreach $_ (@log)
373 {
374 /^... (..) (..:..:..) [\w\-]+ ${section}(.*)$/;
375 my $day = $1;
376 $day =~ tr / /0/;
377 my $time = $cgiparams{'DAY'} ? "$2" : "$day/$2" ;
378 my $sec = $3;
379 my $data = $4;
380 # correct the cut position, just when section=RED
381 if (($cgiparams{'SECTION'} eq 'red' ) && ($sec =~ /(kernel:)(.*)/)) {
382 $sec = 'kernel';
383 $data = $2.': '.$data;
384 }
385 my $d = substr ($data,0,80);
386 while (length($data)>80){ #very basic breaking of lines...
387 $data = substr ($data,80); #permit correct display in table cell
388 $d .= ' ' . substr ($data,0,80);
389 }
390
391
392 if ($lines % 2) {
393 print "<tr bgcolor='$color{'color20'}'>"; }
394 else {
395 print "<tr bgcolor='$color{'color22'}'>"; }
396 print "<td>$time<td>$sec<td>" .&Header::cleanhtml ("$d", 'y') . "</td></tr>\n";
397 $lines++;
398 }
399 #print '</tt>';
400 print "</table>";
401
402 &oldernewer();
403
404 &Header::closebox();
405
406 &Header::closebigbox();
407
408 &Header::closepage();
409
410 sub oldernewer
411 {
412 print <<END
413 <table width='100%'>
414 <tr>
415 END
416 ;
417
418 print "<td align='center' width='50%'>";
419 if ($prev != -1) {
420 print "<a href='/cgi-bin/logs.cgi/log.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SECTION'}'>$Lang::tr{'older'}</a>"; }
421 else {
422 print "$Lang::tr{'older'}"; }
423 print "</td>\n";
424
425 print "<td align='center' width='50%'>";
426 if ($next >= 0) {
427 print "<a href='/cgi-bin/logs.cgi/log.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SECTION'}'>$Lang::tr{'newer'}</a>"; }
428 else {
429 print "$Lang::tr{'newer'}"; }
430 print "</td>\n";
431
432 print <<END
433 </tr>
434 </table>
435 END
436 ;
437 }