]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/logs.cgi/log.dat
logs: Add dynamic DNS logging section.
[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 'ddns' => '(ddns\[\d+\]:)',
55 'dns' => '(dnsmasq\[.*\]: )',
56 'dhcp' => '(dhcpd: )',
57 'clamav' => '(clamd\[.*\]: |freshclam\[.*\]: )',
58 'collectd' => '(collectd\[.*\]: )',
59 'cron' => '(fcron\[.*\]: )',
60 'ntp' => '(ntpd(?:ate)?\[.*\]: )',
61 'ssh' => '(sshd(?:\(.*\))?\[.*\]: )',
62 'auth' => '(\w+\(pam_unix\)\[.*\]: )',
63 'kernel' => '(kernel: (?!DROP_))',
64 'ipsec' => '(ipsec_[\w_]+: |pluto\[.*\]: |charon: |vpnwatch: )',
65 'openvpn' => '(openvpnserver\[.*\]: |.*n2n\[.*\]: )',
66 'pakfire' => '(pakfire:) ',
67 'wireless' => '(hostapd:|kernel: ath.*:|kernel: wifi[0-9]:) '
68 );
69
70 # Translations for the %sections array.
71 my %trsections = (
72 'ipfire' => 'IPFire',
73 'red' => 'RED',
74 'ddns' => $Lang::tr{'dynamic dns'},
75 'dns' => 'DNS',
76 'dhcp' => "$Lang::tr{'dhcp server'}",
77 'cron' => 'Cron',
78 'collectd' => 'Collectd',
79 'clamav' => 'ClamAV',
80 'ntp' => 'NTP',
81 'ssh' => 'SSH',
82 'auth' => "$Lang::tr{'loginlogout'}",
83 'kernel' => "$Lang::tr{'kernel'}",
84 'ipsec' => 'IPSec',
85 'openvpn' => 'OpenVPN',
86 'pakfire' => 'Pakfire',
87 'wireless' => 'Wireless'
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
96 my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ? 0x7FFFF000 : 0; #index of firts line number to display
97
98 if ($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
107 if (!($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 }
113 elsif($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 }
132 elsif($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
153 my $gzindex;
154 my $date = $cgiparams{'DAY'} == 0 ? '' : $cgiparams{'DAY'} <= 9 ? "0$cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
155
156 {
157 my $xday;
158
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 }
178
179 my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
180 my $daystr = $cgiparams{'DAY'} == 0 ? '..' : $cgiparams{'DAY'} <= 9 ? " $cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
181 my $section = $sections{$cgiparams{'SECTION'}};
182
183 my $lines = 0;
184 my @log=();
185
186 my $loop = 1;
187 my $filestr = 0;
188 my $lastdatetime; # for debug
189 my $search_for_end = 0;
190
191 while ($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 = $_;
204 if ($line =~ /^${monthstr} ${daystr} ..:..:.. [\w\-]+ ${section}(.*)/) {
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
238 if ($cgiparams{'ACTION'} eq $Lang::tr{'export'})
239 {
240 print "Content-type: text/plain\n\n";
241 print "IPFire diagnostics\r\n";
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 {
249 /^... (..) (..:..:..) [\w\-]+ ${section}(.*)$/;
250 my $day = $1;
251 $day =~ tr / /0/;
252 my $time = $cgiparams{'DAY'} ? "$2" : "$day/$2" ;
253 print "$time $3 $4\r\n";
254 }
255 exit 0;
256 }
257
258 &Header::showhttpheaders();
259
260 &Header::openpage($Lang::tr{'system logs'}, 1, '');
261
262 &Header::openbigbox('100%', 'left', '', $errormessage);
263
264 if ($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
272 print <<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'>
279 END
280 ;
281 foreach $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 }
287 print <<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'>
293 END
294 ;
295 for (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 }
302 print <<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'>
308 END
309 ;
310 print "<option value='0'>$Lang::tr{'all'}</option>\n";
311 for (my $day = 1; $day <= 31; $day++)
312 {
313 print "\t<option ";
314 if ($day == $cgiparams{'DAY'}) {
315 print "selected='selected' "; }
316 print "value='$day'>$day</option>\n";
317 }
318 print <<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>
328 END
329 ;
330
331 &Header::closebox();
332
333 &Header::openbox('100%', 'left', $Lang::tr{'log'});
334 print "<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
339 my $prev;
340 if ($start == 0) {
341 $prev = -1;
342 } else {
343 $prev = $start - ${Header::viewsize};
344 $prev = 0 if ( $prev < 0);
345 }
346
347 my $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
355 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
356 if ($lines != 0) { &oldernewer(); }
357
358 print <<END
359 <table width='100%' class='tbl'>
360 <tr>
361 <th width='10%' align='center' class='boldbase'><b>$Lang::tr{'time'}</b></th>
362 <th width='10%' align='center' class='boldbase'><b>$Lang::tr{'section'}</b></th>
363 <th width='80%'>&nbsp;</th>
364 </tr>
365 END
366 ;
367
368 $lines = 0;
369 #print '<tt>';
370 foreach $_ (@log)
371 {
372 /^... (..) (..:..:..) [\w\-]+ ${section}(.*)$/;
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 my $col="";
389
390 if ($lines % 2) {
391 print "<tr>";
392 $col="bgcolor='$color{'color20'}'"; }
393 else {
394 print "<tr>";
395 $col="bgcolor='$color{'color22'}'"; }
396 print "<td $col>$time<td $col>$sec<td $col>" .&Header::cleanhtml ("$d", 'y') . "</td></tr>\n";
397 $lines++;
398 }
399 #print '</tt>';
400 print "</table>";
401
402 &oldernewer();
403
404 &Header::closebox();
405
406 &Header::closebigbox();
407
408 &Header::closepage();
409
410 sub oldernewer
411 {
412 print <<END
413 <table width='100%'>
414 <tr>
415 END
416 ;
417
418 print "<td align='center' width='50%'>";
419 if ($prev != -1) {
420 print "<a href='/cgi-bin/logs.cgi/log.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SECTION'}'>$Lang::tr{'older'}</a>"; }
421 else {
422 print "$Lang::tr{'older'}"; }
423 print "</td>\n";
424
425 print "<td align='center' width='50%'>";
426 if ($next >= 0) {
427 print "<a href='/cgi-bin/logs.cgi/log.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{'SECTION'}'>$Lang::tr{'newer'}</a>"; }
428 else {
429 print "$Lang::tr{'newer'}"; }
430 print "</td>\n";
431
432 print <<END
433 </tr>
434 </table>
435 END
436 ;
437 }