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