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