]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/logs.cgi/log.dat
pakfire cgi not refreshes after upgrades
[people/pmueller/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
29#workaround to suppress a warning when a variable is used only once
30my @dummy = ( ${Header::table2colour} );
31undef (@dummy);
32
33my %cgiparams=();
34my %logsettings=();
35my $errormessage = '';
36
37my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
38 'Sep', 'Oct', 'Nov', 'Dec' );
39my @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
44my @now = localtime();
45my $dow = $now[6];
46my $doy = $now[7];
47my $tdoy = $now[7];
48my $year = $now[5]+1900;
49
50$cgiparams{'DAY'} = $now[3];
51$cgiparams{'MONTH'} = $now[4];
52$cgiparams{'ACTION'} = '';
5595bc03 53$cgiparams{'SECTION'} = 'kernel';
cd1a2927
MT
54
55my %sections = (
5595bc03
CS
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)\[.*\]: '
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',
76 'ntp' => 'NTP',
77 'ssh' => 'SSH',
78 'auth' => "$Lang::tr{'loginlogout'}",
79 'kernel' => "$Lang::tr{'kernel'}",
80 'ipsec' => 'IPSec',
6e13d0a5 81 'openvpn' => 'OpenVPN',
cd1a2927
MT
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
90my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ? 0x7FFFF000 : 0; #index of firts line number to display
91
92if ($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
101if (!($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}
107elsif($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}
126elsif($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
147my $gzindex;
148my $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
173my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
174my $daystr = $cgiparams{'DAY'} == 0 ? '..' : $cgiparams{'DAY'} <= 9 ? " $cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
175my $section = $sections{$cgiparams{'SECTION'}};
176
177my $lines = 0;
178my @log=();
179
180my $loop = 1;
181my $filestr = 0;
182my $lastdatetime; # for debug
183my $search_for_end = 0;
184
185while ($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 = $_;
5595bc03 198 if ($line =~ /^${monthstr} ${daystr} ..:..:.. [\w\-]+ ${section}(.*)/) {
cd1a2927
MT
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
232if ($cgiparams{'ACTION'} eq $Lang::tr{'export'})
233{
234 print "Content-type: text/plain\n\n";
78331e30 235 print "IPFire diagnostics\r\n";
cd1a2927
MT
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 {
5595bc03 243 /^... (..) (..:..:..) [\w\-]+ ${section}(.*)$/;
cd1a2927
MT
244 my $day = $1;
245 $day =~ tr / /0/;
246 my $time = $cgiparams{'DAY'} ? "$2" : "$day/$2" ;
5595bc03 247 print "$time $3 $4\r\n";
cd1a2927
MT
248 }
249 exit 0;
250}
251
252&Header::showhttpheaders();
253
986e08d9 254&Header::openpage($Lang::tr{'system logs'}, 1, '');
cd1a2927
MT
255
256&Header::openbigbox('100%', 'left', '', $errormessage);
257
258if ($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
266print <<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'>
273END
274;
275foreach $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}
281print <<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'>
287END
288;
289for (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}
296print <<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'>
302END
303;
304print "<option value='0'>$Lang::tr{'all'}</option>\n";
305for (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}
312print <<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>
322END
323;
324
325&Header::closebox();
326
327&Header::openbox('100%', 'left', $Lang::tr{'log'});
328print "<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
333my $prev;
334 if ($start == 0) {
335 $prev = -1;
336 } else {
337 $prev = $start - ${Header::viewsize};
338 $prev = 0 if ( $prev < 0);
339 }
340
341my $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
349if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
350if ($lines != 0) { &oldernewer(); }
351
352print <<END
353<table width='100%'>
354<tr>
355 <td width='10%' align='center' class='boldbase'><b>$Lang::tr{'time'}</b></td>
6e13d0a5
MT
356 <td width='10%' align='center' class='boldbase'><b>$Lang::tr{'section'}</b></td>
357 <td width='80%'>&nbsp;</td>
cd1a2927
MT
358</tr>
359END
360;
361
362$lines = 0;
363#print '<tt>';
364foreach $_ (@log)
365{
5595bc03 366 /^... (..) (..:..:..) [\w\-]+ ${section}(.*)$/;
cd1a2927
MT
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) {
f2fdd0c1 385 print "<tr bgcolor='$color{'color20'}'>"; }
cd1a2927 386 else {
f2fdd0c1 387 print "<tr bgcolor='$color{'color22'}'>"; }
cd1a2927
MT
388 print "<td>$time<td>$sec<td>" .&Header::cleanhtml ("$d", 'y') . "</td></tr>\n";
389 $lines++;
390}
391#print '</tt>';
392print "</table>";
393
394&oldernewer();
395
396&Header::closebox();
397
398&Header::closebigbox();
399
400&Header::closepage();
401
402sub oldernewer
403{
404print <<END
405<table width='100%'>
406<tr>
407END
408;
409
410print "<td align='center' width='50%'>";
411if ($prev != -1) {
412 print "<a href='/cgi-bin/logs.cgi/log.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SECTION'}'>$Lang::tr{'older'}</a>"; }
413else {
414 print "$Lang::tr{'older'}"; }
415print "</td>\n";
416
417print "<td align='center' width='50%'>";
418if ($next >= 0) {
419 print "<a href='/cgi-bin/logs.cgi/log.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SECTION'}'>$Lang::tr{'newer'}</a>"; }
420else {
421 print "$Lang::tr{'newer'}"; }
422print "</td>\n";
423
424print <<END
425</tr>
426</table>
427END
428;
429}