Handle hostnames with upper-case letters
[webmin.git] / syslog-ng / view_log.cgi
1 #!/usr/local/bin/perl
2 # Show a log file
3
4 require './syslog-ng-lib.pl';
5 &ReadParse();
6 &foreign_require("proc", "proc-lib.pl");
7
8 # Work out the file
9 $conf = &get_config();
10 if ($in{'dest'}) {
11         # From a destination
12         @dests = &find("destination", $conf);
13         ($dest) = grep { $_->{'value'} eq $in{'dest'} } @dests;
14         $dest || &error($text{'destination_egone'});
15         $file = &find_value("file", $dest->{'members'});
16         }
17 elsif ($in{'omod'}) {
18         # From another module
19         @others = &get_other_module_logs($in{'omod'});
20         ($other) = grep { $_->{'mindex'} == $in{'oidx'} } @others;
21         if ($other->{'file'}) {
22                 $file = $other->{'file'};
23                 }
24         else {
25                 $cmd = $other->{'cmd'};
26                 }
27         }
28
29 print "Refresh: $config{'refresh'}\r\n"
30         if ($config{'refresh'});
31 &ui_print_header("<tt>".($file || $cmd)."</tt>", $text{'view_title'}, "");
32
33 $lines = $in{'lines'} ? int($in{'lines'}) : $config{'lines'};
34 $filter = $in{'filter'} ? quotemeta($in{'filter'}) : "";
35
36 &filter_form();
37
38 $| = 1;
39 print "<pre>";
40 local $tailcmd = $config{'tail_cmd'} || "tail -n LINES";
41 $tailcmd =~ s/LINES/$lines/g;
42 if ($filter ne "") {
43         # Are we supposed to filter anything? Then use grep.
44         local @cats;
45         if ($cmd) {
46                 push(@cats, $cmd);
47                 }
48         elsif ($config{'compressed'}) {
49                 # All compressed versions
50                 foreach $l (&all_log_files($file)) {
51                         $c = &catter_command($l);
52                         push(@cats, $c) if ($c);
53                         }
54                 }
55         else {
56                 # Just the one log
57                 @cats = ( "cat ".quotemeta($file) );
58                 }
59         $cat = "(".join(" ; ", @cats).")";
60         $got = &foreign_call("proc", "safe_process_exec",
61                 "$cat | grep -i $filter | $tailcmd",
62                 0, 0, STDOUT, undef, 1, 0, undef, 1);
63         }
64 else {
65         # Not filtering .. so cat the most recent non-empty file
66         if ($cmd) {
67                 # Getting output from a command
68                 $fullcmd = $cmd." | ".$tailcmd;
69                 }
70         elsif ($config{'compressed'}) {
71                 # Find the first non-empty file, newest first
72                 $catter = "cat ".quotemeta($file);
73                 if (!-s $file) {
74                         foreach $l (&all_log_files($file)) {
75                                 next if (!-s $l);
76                                 $c = &catter_command($l);
77                                 if ($c) {
78                                         $catter = $c;
79                                         last;
80                                         }
81                                 }
82                         }
83                 $fullcmd = $catter." | ".$tailcmd;
84                 }
85         else {
86                 # Just run tail on the file
87                 $fullcmd = $tailcmd." ".quotemeta($file);
88                 }
89         $got = &foreign_call("proc", "safe_process_exec",
90                 $fullcmd, 0, 0, STDOUT, undef, 1, 0, undef, 1);
91         }
92 print "<i>$text{'view_empty'}</i>\n" if (!$got);
93 print "</pre>\n";
94 &filter_form();
95
96 &ui_print_footer("list_destinations.cgi", $text{'destinations_return'},
97                  "", $text{'index_return'});
98
99 sub filter_form
100 {
101 print "<form action=view_log.cgi style='margin-left:1em'>\n";
102 print &ui_hidden("dest", $in{'dest'}),"\n";
103 print &ui_hidden("oidx", $in{'oidx'}),"\n";
104 print &ui_hidden("omod", $in{'omod'}),"\n";
105
106 print &text('view_header', &ui_textbox("lines", $lines, 3),
107             "<tt>".&html_escape($log->{'file'})."</tt>"),"\n";
108 print "&nbsp;&nbsp;\n";
109 print &text('view_filter', &ui_textbox("filter", $in{'filter'}, 25)),"\n";
110 print "&nbsp;&nbsp;\n";
111 print "<input type=submit value='$text{'view_refresh'}'></form>\n";
112 }
113