.
[corvix.git] / var / deb-package / cluster_raw_old / opt / cluster / lib / www / ganglia / cluster_view.php
blob7ee7adb5c0601259091ca5764b94dd8a3f7b3267
1 <?php
2 /* $Id: cluster_view.php 783 2007-05-16 22:28:31Z bernardli $ */
3 $tpl = new TemplatePower( template("cluster_view.tpl") );
4 $tpl->assignInclude("extra", template("cluster_extra.tpl"));
5 $tpl->prepare();
7 $tpl->assign("images","./templates/$template_name/images");
9 $cpu_num = !$showhosts ? $metrics["cpu_num"]['SUM'] : cluster_sum("cpu_num", $metrics);
10 $load_one_sum = !$showhosts ? $metrics["load_one"]['SUM'] : cluster_sum("load_one", $metrics);
11 $load_five_sum = !$showhosts ? $metrics["load_five"]['SUM'] : cluster_sum("load_five", $metrics);
12 $load_fifteen_sum = !$showhosts ? $metrics["load_fifteen"]['SUM'] : cluster_sum("load_fifteen", $metrics);
14 # Correct handling of *_report metrics
16 if (!$showhosts) {
17 if(array_key_exists($metricname, $metrics))
18 $units = $metrics[$metricname]['UNITS'];
20 else {
21 if(array_key_exists($metricname, $metrics[key($metrics)]))
22 $units = $metrics[key($metrics)][$metricname]['UNITS'];
25 $tpl->assign("num_nodes", intval($cluster['HOSTS_UP']));
26 if(isset($cluster['HOSTS_DOWN'])) {
27 $tpl->assign("num_dead_nodes", intval($cluster['HOSTS_DOWN']));
28 } else {
29 $tpl->assign("num_dead_nodes", 0);
31 $tpl->assign("cpu_num", $cpu_num);
32 $tpl->assign("localtime", date("Y-m-d H:i", $cluster['LOCALTIME']));
34 if (!$cpu_num) $cpu_num = 1;
35 $cluster_load15 = sprintf("%.0f", ((double) $load_fifteen_sum / $cpu_num) * 100);
36 $cluster_load5 = sprintf("%.0f", ((double) $load_five_sum / $cpu_num) * 100);
37 $cluster_load1 = sprintf("%.0f", ((double) $load_one_sum / $cpu_num) * 100);
38 $tpl->assign("cluster_load", "$cluster_load15%, $cluster_load5%, $cluster_load1%");
40 $cluster_url=rawurlencode($clustername);
43 $tpl->assign("cluster", $clustername);
45 # Summary graphs
47 $graph_args = "c=$cluster_url&amp;$get_metric_string&amp;st=$cluster[LOCALTIME]";
48 $tpl->assign("graph_args", $graph_args);
49 if (!isset($optional_graphs))
50 $optional_graphs = array();
51 foreach ($optional_graphs as $g) {
52 $tpl->newBlock('optional_graphs');
53 $tpl->assign('name',$g);
54 $tpl->assign('graph_args',$graph_args);
59 # Correctly handle *_report cases and blank (" ") units
61 if (isset($units)) {
62 if ($units == " ")
63 $units = "";
64 else
65 $units=$units ? "($units)" : "";
67 else {
68 $units = "";
70 $tpl->assign("metric","$metricname $units");
71 $tpl->assign("sort", $sort);
72 $tpl->assign("range", $range);
73 # Host columns menu defined in header.php
74 if ($showhosts)
75 $tpl->assign("cols_menu", $cols_menu);
76 $tpl->assign("checked$showhosts", "checked");
78 $sorted_hosts = array();
79 $down_hosts = array();
80 $percent_hosts = array();
81 if ($showhosts)
83 foreach ($hosts_up as $host => $val)
85 $cpus = $metrics[$host]["cpu_num"]['VAL'];
86 if (!$cpus) $cpus=1;
87 $load_one = $metrics[$host]["load_one"]['VAL'];
88 $load = ((float) $load_one)/$cpus;
89 $host_load[$host] = $load;
90 if(isset($percent_hosts[load_color($load)])) {
91 $percent_hosts[load_color($load)] += 1;
92 } else {
93 $percent_hosts[load_color($load)] = 1;
95 if ($metricname=="load_one")
96 $sorted_hosts[$host] = $load;
97 else if (isset($metrics[$host][$metricname]))
98 $sorted_hosts[$host] = $metrics[$host][$metricname]['VAL'];
99 else
100 $sorted_hosts[$host] = "";
103 foreach ($hosts_down as $host => $val)
105 $load = -1.0;
106 $down_hosts[$host] = $load;
107 if(isset($percent_hosts[load_color($load)])) {
108 $percent_hosts[load_color($load)] += 1;
109 } else {
110 $percent_hosts[load_color($load)] = 1;
114 # Show pie chart of loads
115 $pie_args = "title=" . rawurlencode("Cluster Load Percentages");
116 $pie_args .= "&amp;size=250x150";
117 foreach($load_colors as $name=>$color)
119 if (!array_key_exists($color, $percent_hosts))
120 continue;
121 $n = $percent_hosts[$color];
122 $name_url = rawurlencode($name);
123 $pie_args .= "&$name_url=$n,$color";
126 else
128 # Show pie chart of hosts up/down
129 $pie_args = "title=" . rawurlencode("Host Status");
130 $pie_args .= "&amp;size=250x150";
131 $up_color = $load_colors["25-50"];
132 $down_color = $load_colors["down"];
133 $pie_args .= "&amp;Up=$cluster[HOSTS_UP],$up_color";
134 $pie_args .= "&amp;Down=$cluster[HOSTS_DOWN],$down_color";
136 $tpl->assign("pie_args", $pie_args);
138 # No reason to go on if we have no up hosts.
139 if (!is_array($hosts_up) or !$showhosts) {
140 $tpl->printToScreen();
141 return;
144 switch ($sort)
146 case "descending":
147 arsort($sorted_hosts);
148 break;
149 case "by name":
150 uksort($sorted_hosts, "strnatcmp");
151 break;
152 default:
153 case "ascending":
154 asort($sorted_hosts);
155 break;
158 $sorted_hosts = array_merge($down_hosts, $sorted_hosts);
160 # First pass to find the max value in all graphs for this
161 # metric. The $start,$end variables comes from get_context.php,
162 # included in index.php.
163 list($min, $max) = find_limits($sorted_hosts, $metricname);
165 # Second pass to output the graphs or metrics.
166 $i = 1;
168 foreach ( $sorted_hosts as $host => $value ) {
169 #echo "$host: $value, ";
172 foreach ( $sorted_hosts as $host => $value )
174 $tpl->newBlock ("sorted_list");
175 $host_url = rawurlencode($host);
177 $host_link="\"?c=$cluster_url&amp;h=$host_url&amp;$get_metric_string\"";
178 $textval = "";
180 #echo "$host: $value, ";
182 if (isset($hosts_down[$host]) and $hosts_down[$host])
184 $last_heartbeat = $cluster['LOCALTIME'] - $hosts_down[$host]['REPORTED'];
185 $age = $last_heartbeat > 3600 ? uptime($last_heartbeat) : "${last_heartbeat}s";
187 $class = "down";
188 $textval = "down <br>&nbsp;<font size=-2>Last heartbeat $age ago</font>";
190 else
192 if(isset($metrics[$host][$metricname]))
193 $val = $metrics[$host][$metricname];
194 else
195 $val = NULL;
196 $class = "metric";
198 if ($val['TYPE']=="timestamp" or
199 (isset($always_timestamp[$metricname]) and
200 $always_timestamp[$metricname]))
202 $textval = date("r", $val['VAL']);
204 elseif ($val['TYPE']=="string" or $val['SLOPE']=="zero" or
205 (isset($always_constant[$metricname]) and
206 $always_constant[$metricname] or
207 ($max_graphs > 0 and $i > $max_graphs )))
209 $textval = "$val[VAL] $val[UNITS]";
211 else
213 $load_color = load_color($host_load[$host]);
214 $graphargs = (isset($reports[$metricname]) and
215 $reports[$metricname]) ?
216 "g=$metricname&amp;" : "m=$metricname&amp;";
217 $graphargs .= "z=small&amp;c=$cluster_url&amp;h=$host_url"
218 ."&amp;l=$load_color&amp;v=$val[VAL]&amp;x=$max&amp;n=$min"
219 ."&amp;r=$range&amp;st=$cluster[LOCALTIME]";
223 if ($textval)
225 $cell="<td class=$class>".
226 "<b><a href=$host_link>$host</a></b><br>".
227 "<i>$metricname:</i> <b>$textval</b></td>";
229 else
231 $cell="<td><a href=$host_link>".
232 "<img src=\"./graph.php?$graphargs\" ".
233 "alt=\"$host\" border=0></a></td>";
236 $tpl->assign("metric_image", $cell);
237 if (! ($i++ % $hostcols) )
238 $tpl->assign ("br", "</tr><tr>");
241 $tpl->printToScreen();