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