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