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