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