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