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