]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blame - 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
CommitLineData
cd1a2927
MT
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
12use strict;
13
14# enable only the following on debugging purpose
5595bc03
CS
15use warnings;
16use CGI::Carp 'fatalsToBrowser';
cd1a2927 17
986e08d9 18require '/var/ipfire/general-functions.pl';
cd1a2927
MT
19require "${General::swroot}/lang.pl";
20require "${General::swroot}/header.pl";
21
f2fdd0c1
CS
22my %color = ();
23my %mainsettings = ();
24&General::readhash("${General::swroot}/main/settings", \%mainsettings);
25&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
26
cd1a2927
MT
27use POSIX();
28
cd1a2927
MT
29my %cgiparams=();
30my %logsettings=();
31my $errormessage = '';
32
33my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
34 'Sep', 'Oct', 'Nov', 'Dec' );
35my @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
40my @now = localtime();
41my $dow = $now[6];
42my $doy = $now[7];
43my $tdoy = $now[7];
44my $year = $now[5]+1900;
45
46$cgiparams{'DAY'} = $now[3];
47$cgiparams{'MONTH'} = $now[4];
48$cgiparams{'ACTION'} = '';
97fe1741 49$cgiparams{'SECTION'} = 'ipfire';
cd1a2927
MT
50
51my %sections = (
5595bc03
CS
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: )',
4f57d698 56 'clamav' => '(clamd\[.*\]: |freshclam\[.*\]: )',
5362088b 57 'collectd' => '(collectd\[.*\]: )',
5595bc03
CS
58 'cron' => '(fcron\[.*\]: )',
59 'ntp' => '(ntpd(?:ate)?\[.*\]: )',
60 'ssh' => '(sshd(?:\(.*\))?\[.*\]: )',
61 'auth' => '(\w+\(pam_unix\)\[.*\]: )',
97fe1741 62 'kernel' => '(kernel: (?!DROP_))',
0e14ef8a 63 'ipsec' => '(ipsec_[\w_]+: |pluto\[.*\]: |charon: |vpnwatch: )',
0f0db884 64 'openvpn' => '(openvpnserver\[.*\]: |.*n2n\[.*\]: )',
af714e3c 65 'pakfire' => '(pakfire:) ',
0eafc0ef 66 'wireless' => '(hostapd:|kernel: ath.*:|kernel: wifi[0-9]:) '
cd1a2927
MT
67 );
68
69# Translations for the %sections array.
70my %trsections = (
78331e30 71 'ipfire' => 'IPFire',
cd1a2927
MT
72 'red' => 'RED',
73 'dns' => 'DNS',
74 'dhcp' => "$Lang::tr{'dhcp server'}",
75 'cron' => 'Cron',
9f663e66 76 'collectd' => 'Collectd',
c506cad0 77 'clamav' => 'ClamAV',
cd1a2927
MT
78 'ntp' => 'NTP',
79 'ssh' => 'SSH',
80 'auth' => "$Lang::tr{'loginlogout'}",
81 'kernel' => "$Lang::tr{'kernel'}",
82 'ipsec' => 'IPSec',
6e13d0a5 83 'openvpn' => 'OpenVPN',
c506cad0 84 'pakfire' => 'Pakfire',
af714e3c 85 'wireless' => 'Wireless'
cd1a2927
MT
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
94my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ? 0x7FFFF000 : 0; #index of firts line number to display
95
96if ($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
105if (!($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}
111elsif($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}
130elsif($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
151my $gzindex;
152my $date = $cgiparams{'DAY'} == 0 ? '' : $cgiparams{'DAY'} <= 9 ? "0$cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
153
154{
155 my $xday;
9f663e66 156
cd1a2927
MT
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());
0d08de33 170 my $sunday = POSIX::mktime( 0, 0, 0, $then[3], $then[4], $then[5]);
cd1a2927
MT
171 $sunday += (6-$then[6]) * 86400;
172
173 # Convert delta in second to full weeks
174 $gzindex = int (($sunday-$xday)/604800 );
175}
9f663e66 176
cd1a2927
MT
177my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
178my $daystr = $cgiparams{'DAY'} == 0 ? '..' : $cgiparams{'DAY'} <= 9 ? " $cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
179my $section = $sections{$cgiparams{'SECTION'}};
180
181my $lines = 0;
182my @log=();
183
184my $loop = 1;
185my $filestr = 0;
186my $lastdatetime; # for debug
187my $search_for_end = 0;
9f663e66 188
cd1a2927
MT
189while ($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 = $_;
5595bc03 202 if ($line =~ /^${monthstr} ${daystr} ..:..:.. [\w\-]+ ${section}(.*)/) {
cd1a2927
MT
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
236if ($cgiparams{'ACTION'} eq $Lang::tr{'export'})
237{
238 print "Content-type: text/plain\n\n";
78331e30 239 print "IPFire diagnostics\r\n";
cd1a2927
MT
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 {
5595bc03 247 /^... (..) (..:..:..) [\w\-]+ ${section}(.*)$/;
cd1a2927
MT
248 my $day = $1;
249 $day =~ tr / /0/;
250 my $time = $cgiparams{'DAY'} ? "$2" : "$day/$2" ;
5595bc03 251 print "$time $3 $4\r\n";
cd1a2927
MT
252 }
253 exit 0;
254}
255
256&Header::showhttpheaders();
257
986e08d9 258&Header::openpage($Lang::tr{'system logs'}, 1, '');
cd1a2927
MT
259
260&Header::openbigbox('100%', 'left', '', $errormessage);
261
262if ($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
270print <<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'>
277END
278;
279foreach $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}
285print <<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'>
291END
292;
293for (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}
300print <<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'>
306END
307;
308print "<option value='0'>$Lang::tr{'all'}</option>\n";
9f663e66 309for (my $day = 1; $day <= 31; $day++)
cd1a2927
MT
310{
311 print "\t<option ";
312 if ($day == $cgiparams{'DAY'}) {
313 print "selected='selected' "; }
314 print "value='$day'>$day</option>\n";
315}
316print <<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>
326END
327;
328
329&Header::closebox();
330
331&Header::openbox('100%', 'left', $Lang::tr{'log'});
332print "<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
337my $prev;
338 if ($start == 0) {
339 $prev = -1;
340 } else {
341 $prev = $start - ${Header::viewsize};
342 $prev = 0 if ( $prev < 0);
343 }
9f663e66 344
cd1a2927
MT
345my $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
353if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
354if ($lines != 0) { &oldernewer(); }
355
356print <<END
fda1f52f 357<table width='100%' class='tbl'>
cd1a2927 358<tr>
fda1f52f
AM
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>
cd1a2927
MT
362</tr>
363END
364;
365
366$lines = 0;
367#print '<tt>';
368foreach $_ (@log)
369{
5595bc03 370 /^... (..) (..:..:..) [\w\-]+ ${section}(.*)$/;
cd1a2927
MT
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 }
fda1f52f 386 my $col="";
cd1a2927
MT
387
388 if ($lines % 2) {
fda1f52f
AM
389 print "<tr>";
390 $col="bgcolor='$color{'color20'}'"; }
cd1a2927 391 else {
fda1f52f
AM
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";
cd1a2927
MT
395 $lines++;
396}
397#print '</tt>';
398print "</table>";
399
400&oldernewer();
401
402&Header::closebox();
403
404&Header::closebigbox();
405
406&Header::closepage();
407
408sub oldernewer
409{
410print <<END
411<table width='100%'>
412<tr>
413END
414;
415
416print "<td align='center' width='50%'>";
417if ($prev != -1) {
418 print "<a href='/cgi-bin/logs.cgi/log.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SECTION'}'>$Lang::tr{'older'}</a>"; }
419else {
420 print "$Lang::tr{'older'}"; }
421print "</td>\n";
422
423print "<td align='center' width='50%'>";
424if ($next >= 0) {
425 print "<a href='/cgi-bin/logs.cgi/log.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SECTION'}'>$Lang::tr{'newer'}</a>"; }
426else {
427 print "$Lang::tr{'newer'}"; }
428print "</td>\n";
429
430print <<END
431</tr>
432</table>
433END
434;
435}