Handle hostnames with upper-case letters
[webmin.git] / status / index.cgi
1 #!/usr/local/bin/perl
2 # index.cgi
3 # List all services currently being monitored
4
5 $trust_unknown_referers = 1;
6 require './status-lib.pl';
7 print "Refresh: $config{'refresh'}\r\n"
8         if ($config{'refresh'});
9 &ui_print_header(undef, $text{'index_title'}, "", undef, 1, 1);
10
11 # If we are using SNMP for notification, make sure the Perl module is installed
12 if ($config{'snmp_server'}) {
13         eval "use Net::SNMP";
14         if ($@) {
15                 eval "use SNMP_Session";
16                 }
17         if ($@) {
18                 &ui_print_endpage(
19                     &ui_config_link('index_esnmp',
20                         [ "<tt>Net::SNMP</tt>",
21                           "../cpan/download.cgi?source=3&cpan=Net::SNMP&mode=2&return=/$module_name/&returndesc=".&urlize($text{'index_return'}),
22                           undef ]));
23                 }
24         }
25
26 @serv = &list_services();
27 $mid = int((@serv-1) / 2);
28 $oldstatus = &read_file($oldstatus_file, \%oldstatus);
29
30 if (@serv) {
31         &show_button();
32         if ($config{'sort_mode'} == 1) {
33                 @serv = sort { $a->{'desc'} cmp $b->{'desc'} } @serv;
34                 }
35         elsif ($config{'sort_mode'} == 2) {
36                 @serv = sort { $a->{'remote'} cmp $b->{'remote'} } @serv;
37                 }
38         elsif ($config{'sort_mode'} == 3) {
39                 @serv = sort { $oldstatus{$a->{'id'}} <=> $oldstatus{$b->{'id'}} } @serv;
40                 }
41         if (!$config{'index_status'} && $oldstatus) {
42                 local @st = stat("$module_config_directory/oldstatus");
43                 local $t = localtime($st[9]);
44                 print &text('index_oldtime', $t),"<br>\n";
45                 }
46
47         # Show table of defined monitors
48         @links = ( );
49         if ($access{'edit'}) {
50                 print &ui_form_start("delete_mons.cgi", "post");
51                 push(@links, &select_all_link("d", 1),
52                              &select_invert_link("d", 1) );
53                 }
54         print &ui_links_row(\@links);
55         if ($config{'columns'} == 2) {
56                 print "<table width=100%><tr>\n";
57                 print "<td width=50% valign=top>\n";
58                 &service_table(@serv[0 .. $mid]);
59                 print "</td> <td width=50% valign=top>\n";
60                 &service_table(@serv[$mid+1 .. $#serv]) if (@serv > 1);
61                 print "</td></tr></table>\n";
62                 }
63         else {
64                 &service_table(@serv);
65                 }
66         print &ui_links_row(\@links);
67         if ($access{'edit'}) {
68                 print &ui_form_end([ [ "delete", $text{'index_delete'} ],
69                                      [ "refresh", $text{'index_refsel'} ] ]);
70                 }
71         }
72 else {
73         print "<b>$text{'index_none'}</b><p>\n";
74         }
75 &show_button();
76
77 print &ui_hr();
78 print &ui_buttons_start();
79 if ($access{'sched'}) {
80         # Open scheduled monitoring form
81         print &ui_buttons_row("edit_sched.cgi",
82                               $text{'index_sched'},
83                               $text{'index_scheddesc'});
84         }
85 if ($access{'edit'}) {
86         # Email templates button
87         print &ui_buttons_row("list_tmpls.cgi",
88                               $text{'index_tmpls'},
89                               $text{'index_tmplsdesc'});
90         }
91 if (!$config{'index_status'}) {
92         # Refresh now
93         print &ui_buttons_row("refresh.cgi",
94                               $text{'index_refresh'},
95                               $text{'index_refreshdesc'});
96         }
97 print "</tr></table>\n";
98
99 &remote_finished();
100 &ui_print_footer("/", $text{'index'});
101
102 sub service_table
103 {
104 # Table header
105 local @tds = $access{'edit'} ? ( "width=5" ) : ( );
106 print &ui_columns_start([
107         $access{'edit'} ? ( "" ) : ( ),
108         $text{'index_desc'},
109         $text{'index_host'},
110         $config{'index_status'} ? ( $text{'index_up'} ) :
111          $oldstatus ? ( $text{'index_last'} ) : ( ),
112         ], 100, 0, \@tds);
113
114 # One row per monitor
115 foreach $s (@_) {
116         local @cols;
117         local $esc = &html_escape($s->{'desc'});
118         $esc = "<i>$esc</i>" if ($s->{'nosched'} == 1);
119         if ($access{'edit'}) {
120                 push(@cols, "<a href='edit_mon.cgi?id=$s->{'id'}'>$esc</a>");
121                 }
122         else {
123                 push(@cols, $esc);
124                 }
125         push(@cols, &nice_remotes($s));
126
127         # Work out and show all the up icons
128         local @ups;
129         if ($config{'index_status'}) {
130                 # Showing the current status
131                 @stats = &service_status($s, 1);
132                 @ups = map { $_->{'up'} } @stats;
133                 @remotes = map { $_->{'remote'} } @stats;
134                 }
135         elsif ($oldstatus) {
136                 # Getting status from last check
137                 $stat = &expand_oldstatus($oldstatus{$s->{'id'}});
138                 @remotes = &expand_remotes($s);
139                 @ups = map { defined($stat->{$_}) ? ( $stat->{$_} ) : ( ) }
140                            @remotes;
141                 }
142         if (!@ups) {
143                 push(@cols, "");
144                 }
145         else {
146                 local @icons;
147                 for(my $i=0; $i<@ups; $i++) {
148                         $up = $ups[$i];
149                         $h = $remotes[$i];
150                         $h = $text{'index_local'} if ($h eq '*');
151                         push(@icons, "<img src=images/".
152                               ($up == 1 ? "up.gif" :
153                               $up == -1 ? "not.gif" :
154                               $up == -2 ? "webmin.gif" :
155                               $up == -3 ? "timed.gif" :
156                               $up == -4 ? "skip.gif" :
157                                           "down.gif").
158                               " title='".&html_escape($h)."'>");
159                         }
160                 push(@cols, join("", @icons));
161                 }
162         if ($access{'edit'}) {
163                 print &ui_checked_columns_row(\@cols, \@tds, "d", $s->{'id'});
164                 }
165         else {
166                 print &ui_columns_row(\@cols, \@tds);
167                 }
168         }
169 print &ui_columns_end();
170 }
171
172 sub show_button
173 {
174 if ($access{'edit'}) {
175         print "<form action=edit_mon.cgi>\n";
176         print "<input type=submit value='$text{'index_add'}'> ",
177               "<select name=type>\n";
178         foreach $h (sort { $a->[1] cmp $b->[1] } &list_handlers()) {
179                 printf "<option value=%s>%s\n",
180                         $h->[0], $h->[1] || $h->[0];
181                 }
182         print "</select></form>\n";
183         }
184 }
185