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