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