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