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