]> git.ipfire.org Git - ipfire-2.x.git/blob - 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
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/ipfire/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 'apcupsd' => '(apcupsd\[.*?\]: )',
53 'auth' => '(\w+\(pam_unix\)\[.*\]: )',
54 'wio' => '(wio:|wio\[.*\])',
55 'captive' => '(Captive:)',
56 'clamav' => '(clamd\[.*\]: |freshclam\[.*\]: )',
57 'collectd' => '(collectd\[.*\]: )',
58 'cron' => '(fcron\[.*\]: )',
59 'ddns' => '(ddns\[\d+\]:)',
60 'dhcp' => '(dhcpd: )',
61 'dma' => '(dma: |dma\[.*\]: |postfix/\w*\[\d*\]: )',
62 'guardian' => '(guardian\[.*\]: )',
63 'haproxy' => '(haproxy\[.*?\]: )',
64 'ipblocklist' => '(ipblocklist: )',
65 'ipfire' => '(ipfire: )',
66 'ipsec' => '(ipsec_[\w_]+: |pluto\[.*\]: |charon: |vpnwatch: )',
67 'kernel' => '(kernel: (?!DROP_))',
68 'monit' => '(monit\[.*?\]: )',
69 'ntp' => '(ntpd(?:ate)?\[.*\]: )',
70 'nut' => '(upsmon\[.*?\]: )',
71 'oinkmaster' => '(oinkmaster\[.*\]: )',
72 'openvpn' => '(openvpnserver\[.*\]: |.*n2n\[.*\]: )',
73 'pakfire' => '(pakfire:)',
74 'red' => '(red:|pppd\[.*\]: |chat\[.*\]|pppoe\[.*\]|pptp\[.*\]|pppoa\[.*\]|pppoa3\[.*\]|pppoeci\[.*\]|ipppd|ipppd\[.*\]|kernel: ippp\d|kernel: isdn.*|ibod\[.*\]|dhcpcd\[.*\]|modem_run\[.*\])',
75 'samba' => '(nmbd|smbd|winbind)\[\d+\]:',
76 'suricata' => '(suricata: )',
77 'squid' => '(squid\[.*\]: |squid: )',
78 'ssh' => '(sshd(?:\(.*\))?\[.*\]: )',
79 'unbound' => '(unbound: \[.*?\])(.*:.*$)',
80 'urlfilter bl' => '(installpackage\[urlfilter\]: )',
81 'wireless' => '(hostapd:|kernel: ath.*:|kernel: wifi[0-9]:)'
82 );
83
84 # Translations for the %sections array.
85 my %trsections = (
86 'apcupsd' => 'APCUPS',
87 'auth' => "$Lang::tr{'loginlogout'}",
88 'wio' => 'Who Is Online?',
89 'captive' => $Lang::tr{'Captive'},
90 'clamav' => 'ClamAV',
91 'collectd' => 'Collectd',
92 'cron' => 'Cron',
93 'ddns' => "$Lang::tr{'dynamic dns'}",
94 'dhcp' => "$Lang::tr{'dhcp server'}",
95 'dma' => 'Mail',
96 'guardian' => "$Lang::tr{'guardian'}",
97 'haproxy' => 'HAProxy',
98 'ipblocklist' => "$Lang::tr{'ipblocklist'}",
99 'ipfire' => 'IPFire',
100 'ipsec' => 'IPSec',
101 'kernel' => "$Lang::tr{'kernel'}",
102 'monit' => 'Monit',
103 'ntp' => 'NTP',
104 'nut' => 'NUT',
105 'oinkmaster' => 'Oinkmaster',
106 'openvpn' => 'OpenVPN',
107 'pakfire' => 'Pakfire',
108 'red' => 'RED',
109 'samba' => $Lang::tr{'samba'},
110 'suricata' => "$Lang::tr{'intrusion detection'}",
111 'squid' => "$Lang::tr{'web proxy'}",
112 'ssh' => 'SSH',
113 'unbound' => 'DNS: Unbound',
114 'urlfilter bl' => 'URLFilter Blacklist',
115 'wireless' => 'Wireless'
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
124 my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ? 0x7FFFF000 : 0; #index of firts line number to display
125
126 if ($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
135 if (!($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 }
141 elsif($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 }
160 elsif($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
181 my $gzindex;
182 my $date = $cgiparams{'DAY'} == 0 ? '' : $cgiparams{'DAY'} <= 9 ? "0$cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
183
184 {
185 my $xday;
186
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());
200 my $sunday = POSIX::mktime( 0, 0, 0, $then[3], $then[4], $then[5]);
201 $sunday += (6-$then[6]) * 86400;
202
203 # Convert delta in second to full weeks
204 $gzindex = int (($sunday-$xday)/604800 );
205 }
206
207 my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
208 my $daystr = $cgiparams{'DAY'} == 0 ? '..' : $cgiparams{'DAY'} <= 9 ? " $cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
209 my $section = $sections{$cgiparams{'SECTION'}};
210
211 my $lines = 0;
212 my @log=();
213
214 my $loop = 1;
215 my $filestr = 0;
216 my $lastdatetime; # for debug
217 my $search_for_end = 0;
218
219 while ($gzindex >=0 && $loop) {
220 # calculate file name
221 if ($gzindex == 0) {
222 $filestr = "/var/log/messages";
223 $filestr = "/var/log/mail" if (${section} =~ 'dma');
224 } else {
225 $filestr = "/var/log/messages.$gzindex";
226 $filestr = "/var/log/mail.$gzindex" if (${section} =~ 'dma');
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 = $_;
234 if ($line =~ /^${monthstr} ${daystr} ..:..:.. [\w\-]+ ${section}(.*)/) {
235 # when standard viewing, just keep in memory the correct slice
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 }
246 #} else { don't do this optimisation, need to count lines !
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
268 if ($cgiparams{'ACTION'} eq $Lang::tr{'export'})
269 {
270 print "Content-type: text/plain\n\n";
271 print "IPFire diagnostics\r\n";
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 {
279 /^... (..) (..:..:..) [\w\-]+ ${section}(.*)$/;
280 my $day = $1;
281 $day =~ tr / /0/;
282 my $time = $cgiparams{'DAY'} ? "$2" : "$day/$2" ;
283 print "$time $3 $4\r\n";
284 }
285 exit 0;
286 }
287
288 &Header::showhttpheaders();
289
290 &Header::openpage($Lang::tr{'system logs'}, 1, '');
291
292 &Header::openbigbox('100%', 'left', '', $errormessage);
293
294 if ($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
302 print <<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'>
309 END
310 ;
311 foreach $section (sort {$trsections{$a} cmp $trsections{$b}} keys %sections) {
312 print "\t<option ";
313 if ($section eq $cgiparams{'SECTION'}) {
314 print "selected='selected' "; }
315 print "value='$section'>$trsections{$section}</option>\n";
316 }
317 print <<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'>
323 END
324 ;
325 for (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 }
332 print <<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'>
338 END
339 ;
340 print "<option value='0'>$Lang::tr{'all'}</option>\n";
341 for (my $day = 1; $day <= 31; $day++)
342 {
343 print "\t<option ";
344 if ($day == $cgiparams{'DAY'}) {
345 print "selected='selected' "; }
346 print "value='$day'>$day</option>\n";
347 }
348 print <<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>
358 END
359 ;
360
361 &Header::closebox();
362
363 &Header::openbox('100%', 'left', $Lang::tr{'log'});
364 print "<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
369 my $prev;
370 if ($start == 0) {
371 $prev = -1;
372 } else {
373 $prev = $start - ${Header::viewsize};
374 $prev = 0 if ( $prev < 0);
375 }
376
377 my $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
385 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
386 if ($lines != 0) { &oldernewer(); }
387
388 print <<END
389 <table width='100%' class='tbl'>
390 <tr>
391 <th width='10%' align='center' class='boldbase'><b>$Lang::tr{'time'}</b></th>
392 <th width='15%' align='center' class='boldbase'><b>$Lang::tr{'section'}</b></th>
393 <th width='75%'>&nbsp;</th>
394 </tr>
395 END
396 ;
397
398 $lines = 0;
399 #print '<tt>';
400 foreach $_ (@log)
401 {
402 /^... (..) (..:..:..) [\w\-]+ ${section}(.*)$/;
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 }
418 my $col="";
419
420 if ($lines % 2) {
421 print "<tr>";
422 $col="bgcolor='$color{'color20'}'"; }
423 else {
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";
427 $lines++;
428 }
429 #print '</tt>';
430 print "</table>";
431
432 &oldernewer();
433
434 &Header::closebox();
435
436 &Header::closebigbox();
437
438 &Header::closepage();
439
440 sub oldernewer
441 {
442 print <<END
443 <table width='100%'>
444 <tr>
445 END
446 ;
447
448 print "<td align='center' width='50%'>";
449 if ($prev != -1) {
450 print "<a href='/cgi-bin/logs.cgi/log.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SECTION'}'>$Lang::tr{'older'}</a>"; }
451 else {
452 print "$Lang::tr{'older'}"; }
453 print "</td>\n";
454
455 print "<td align='center' width='50%'>";
456 if ($next >= 0) {
457 print "<a href='/cgi-bin/logs.cgi/log.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SECTION'}'>$Lang::tr{'newer'}</a>"; }
458 else {
459 print "$Lang::tr{'newer'}"; }
460 print "</td>\n";
461
462 print <<END
463 </tr>
464 </table>
465 END
466 ;
467 }