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