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