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