Handle hostnames with upper-case letters
[webmin.git] / squid / calamaris.cgi
1 #!/usr/local/bin/perl
2 # calamaris.cgi
3 # Run calamaris on the squid logfile(s)
4
5 require './squid-lib.pl';
6 $access{'calamaris'} || &error($text{'calamaris_ecannot'});
7 &ui_print_header(undef, $text{'calamaris_title'}, "");
8
9 # is calamaris installed?
10 if (!&has_command($config{'calamaris'})) {
11         print &text('calamaris_eprog', "<tt>$config{'calamaris'}</tt>",
12                   "$gconfig{'webprefix'}/config.cgi?$module_name"),"<p>\n";
13         &ui_print_footer("", $text{'index_return'});
14         exit;
15         }
16
17 # Work out Calamaris version and args
18 if (`$config{'calamaris'} -V 2>&1` =~ /(revision:|Calamaris)\s+(\d\S+)/i) {
19         $ver = $2;
20         }
21 $args = $config{'cal_all'} ? " -a" : "";
22 if ($ver >= 2.5) {
23         if ($config{'cal_fmt'} eq 'w') {
24                 $args .= " -F html";
25                 }
26         else {
27                 $args .= " -F mail";
28                 }
29         }
30 else {
31         $args .= " -".$config{'cal_fmt'};
32         }
33 $args .= " $config{'cal_extra'}";
34
35 # are there any logfiles to analyse?
36 $ld = $config{'log_dir'};
37 opendir(DIR, $ld);
38 while($f = readdir(DIR)) {
39         local @st = stat("$ld/$f");
40         if ($f =~ /^access.log.*gz$/) {
41                 push(@files, [ "gunzip -c $ld/$f |", $st[9] ]);
42                 }
43         elsif ($f =~ /^access.log.*Z$/) {
44                 push(@files, [ "uncompress -c $ld/$f |", $st[9] ]);
45                 }
46         elsif ($f =~ /^access.log/) {
47                 push(@files, [ "$ld/$f", $st[9] ]);
48                 }
49         }
50 closedir(DIR);
51 if (!@files) {
52         print &text('calamaris_elogs', "<tt>$ld</tt>",
53                   "$gconfig{'webprefix'}/config.cgi?$module_name"),"<p>\n";
54         &ui_print_footer("", $text{'index_return'});
55         exit;
56         }
57
58 # run calamaris, parsing newest files and records first
59 $temp = &transname();
60 open(CAL, "| $config{'calamaris'} $args >$temp 2>/dev/null");
61 if ($config{'cal_max'}) {
62         # read only the last N lines
63         print &text('calamaris_last', $config{'cal_max'}),"<p>\n";
64         @files = sort { $b->[1] <=> $a->[1] } @files;
65         $lnum = 0;
66         foreach $f (@files) {
67                 $left = $config{'cal_max'} - $lnum;
68                 last if ($left <= 0);
69                 if ($f->[0] =~ /\|$/) {
70                         open(LOG, "$f->[0] tail -$left |");
71                         }
72                 else {
73                         open(LOG, "tail -$left $f->[0] |");
74                         }
75                 while(<LOG>) {
76                         print CAL $_;
77                         $lnum++;
78                         }
79                 close(LOG);
80                 }
81         }
82 else {
83         # read all the log files
84         foreach $f (@files) {
85                 open(LOG, $f->[0]);
86                 while(read(LOG, $buf, 1024) > 0) {
87                         print CAL $buf;
88                         }
89                 close(LOG);
90                 }
91         }
92 close(CAL);
93
94 # Put the calamaris output into a nice webmin like table.
95 $date = &make_date(time());
96 print &ui_table_start(&text('calamaris_gen', $date), undef, 2);
97
98 # Get the output
99 open(OUT, $temp);
100 if ($config{'cal_fmt'} eq 'm') {
101         $html = "<pre>";
102         while(<OUT>) {
103                 $html .= &html_escape($_);
104                 }
105         $html = "</pre>";
106         }
107 else {
108         while(<OUT>) {
109                 if (/<\s*\/head/i || /<\s*body/i) { $inbody = 1; }
110                 elsif (/<\s*\/body/i) { $inbody = 0; }
111                 elsif ($inbody) { $html .= $_; }
112                 }
113         }
114 close(OUT);
115 unlink($temp);
116
117 # Show it
118 print &ui_table_row(undef, $html, 2);
119 print &ui_table_end();
120
121 &ui_print_footer("", $text{'index_return'});
122