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