forked from vvuksan/ganglia-misc
/
cluster_view.php
357 lines (305 loc) · 12.6 KB
/
cluster_view.php
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
/* $Id: cluster_view.php 2535 2011-03-26 19:14:06Z vvuksan $ */
$tpl = new Dwoo_Template_File( template("cluster_view.tpl") );
$data = new Dwoo_Data();
$data->assign("extra", template("cluster_extra.tpl"));
$data->assign("images","./templates/${conf['template_name']}/images");
$cpu_num = !$showhosts ? $metrics["cpu_num"]['SUM'] : cluster_sum("cpu_num", $metrics);
$load_one_sum = !$showhosts ? $metrics["load_one"]['SUM'] : cluster_sum("load_one", $metrics);
$load_five_sum = !$showhosts ? $metrics["load_five"]['SUM'] : cluster_sum("load_five", $metrics);
$load_fifteen_sum = !$showhosts ? $metrics["load_fifteen"]['SUM'] : cluster_sum("load_fifteen", $metrics);
#
# Correct handling of *_report metrics
#
if (!$showhosts) {
if(array_key_exists($metricname, $metrics))
$units = $metrics[$metricname]['UNITS'];
}
else {
if(array_key_exists($metricname, $metrics[key($metrics)]))
if (isset($metrics[key($metrics)][$metricname]['UNITS']))
$units = $metrics[key($metrics)][$metricname]['UNITS'];
else
$units = '';
}
if(isset($cluster['HOSTS_UP'])) {
$data->assign("num_nodes", intval($cluster['HOSTS_UP']));
} else {
$data->assign("num_nodes", 0);
}
if(isset($cluster['HOSTS_DOWN'])) {
$data->assign("num_dead_nodes", intval($cluster['HOSTS_DOWN']));
} else {
$data->assign("num_dead_nodes", 0);
}
$data->assign("cpu_num", $cpu_num);
$data->assign("localtime", date("Y-m-d H:i", $cluster['LOCALTIME']));
if (!$cpu_num) $cpu_num = 1;
$cluster_load15 = sprintf("%.0f", ((double) $load_fifteen_sum / $cpu_num) * 100);
$cluster_load5 = sprintf("%.0f", ((double) $load_five_sum / $cpu_num) * 100);
$cluster_load1 = sprintf("%.0f", ((double) $load_one_sum / $cpu_num) * 100);
$data->assign("cluster_load", "$cluster_load15%, $cluster_load5%, $cluster_load1%");
$avg_cpu_num = find_avg($clustername, "", "cpu_num");
if ($avg_cpu_num == 0) $avg_cpu_num = 1;
$cluster_util = sprintf("%.0f", ((double) find_avg($clustername, "", "load_one") / $avg_cpu_num ) * 100);
$data->assign("cluster_util", "$cluster_util%");
$cluster_url=rawurlencode($clustername);
// If we want zoomable support on graphs we need to add correct zoomable class to every image
$additional_cluster_img_html_args = "";
$additional_host_img_html_args = "";
if ( isset($conf['zoom_support']) && $conf['zoom_support'] === true )
$additional_cluster_img_html_args = "class=cluster_zoomable";
$data->assign("additional_cluster_img_html_args", $additional_cluster_img_html_args);
$data->assign("cluster", $clustername);
$graph_args = "c=$cluster_url&$get_metric_string&st=$cluster[LOCALTIME]";
$optional_reports = "";
####################################################################################
# Let's find out what optional reports are included
# First we find out what the default (site-wide) reports are then look
# for host specific included or excluded reports
####################################################################################
$default_reports = array("included_reports" => array(), "excluded_reports" => array());
if ( is_file($conf['conf_dir'] . "/default.json") ) {
$default_reports = array_merge($default_reports,json_decode(file_get_contents($conf['conf_dir'] . "/default.json"), TRUE));
}
$cluster_file = $conf['conf_dir'] . "/cluster_" . $clustername . ".json";
$override_reports = array("included_reports" => array(), "excluded_reports" => array());
if ( is_file($cluster_file) ) {
$override_reports = array_merge($override_reports, json_decode(file_get_contents($cluster_file), TRUE));
}
# Merge arrays
$reports["included_reports"] = array_merge( $default_reports["included_reports"] , $override_reports["included_reports"]);
$reports["excluded_reports"] = array_merge($default_reports["excluded_reports"] , $override_reports["excluded_reports"]);
# Remove duplicates
$reports["included_reports"] = array_unique($reports["included_reports"]);
$reports["excluded_reports"] = array_unique($reports["excluded_reports"]);
foreach ( $reports["included_reports"] as $index => $report_name ) {
if ( ! in_array( $report_name, $reports["excluded_reports"] ) ) {
$optional_reports .= "<a name=metric_" . $report_name . ">
<A HREF=\"./graph_all_periods.php?$graph_args&g=" . $report_name . "&z=large&c=$cluster_url\">
<IMG BORDER=0 $additional_cluster_img_html_args ALT=\"$cluster_url\" SRC=\"./graph.php?$graph_args&g=" . $report_name ."&z=medium&c=$cluster_url\"></A>
";
}
}
$data->assign("optional_reports", $optional_reports);
#
# Summary graphs
#
$data->assign("graph_args", $graph_args);
if (!isset($conf['optional_graphs']))
$conf['optional_graphs'] = array();
$optional_graphs_data = array();
foreach ($conf['optional_graphs'] as $g) {
$optional_graphs_data[$g]['name'] = $g;
# $data->assign("name", $optional_graphs_data[$g]['name']);
$optional_graphs_data[$g]['graph_args'] = $graph_args;
}
$data->assign('optional_graphs_data', $optional_graphs_data);
#
# Correctly handle *_report cases and blank (" ") units
#
if (isset($units)) {
if ($units == " ")
$units = "";
else
$units=$units ? "($units)" : "";
}
else {
$units = "";
}
$data->assign("metric","$metricname $units");
$data->assign("metric_name","$metricname");
$data->assign("sort", $sort);
$data->assign("range", $range);
#$data->assign("checked$showhosts", "checked");
$showhosts_levels = array(
2 => array('checked'=>'', 'name'=>'Auto'),
1 => array('checked'=>'', 'name'=>'Same'),
0 => array('checked'=>'', 'name'=>'None'),
);
$showhosts_levels[$showhosts]['checked'] = 'checked';
$data->assign("showhosts_levels", $showhosts_levels);
$sorted_hosts = array();
$down_hosts = array();
$percent_hosts = array();
if ($showhosts)
{
foreach ($hosts_up as $host => $val)
{
// If host_regex is defined
if ( isset($user['host_regex']) && ! preg_match("/" .$user['host_regex'] . "/", $host ) )
continue;
if ( isset($metrics[$host]["cpu_num"]['VAL']) and $metrics[$host]["cpu_num"]['VAL'] != 0 ){
$cpus = $metrics[$host]["cpu_num"]['VAL'];
} else {
$cpus = 1;
}
if ( isset($metrics[$host]["load_one"]['VAL']) ){
$load_one = $metrics[$host]["load_one"]['VAL'];
} else {
$load_one = 0;
}
$load = ((float) $load_one)/$cpus;
$host_load[$host] = $load;
if(isset($percent_hosts[load_color($load)])) {
$percent_hosts[load_color($load)] += 1;
} else {
$percent_hosts[load_color($load)] = 1;
}
if ($metricname=="load_one")
$sorted_hosts[$host] = $load;
else if (isset($metrics[$host][$metricname]))
$sorted_hosts[$host] = $metrics[$host][$metricname]['VAL'];
else
$sorted_hosts[$host] = "";
}
foreach ($hosts_down as $host => $val)
{
$load = -1.0;
$down_hosts[$host] = $load;
if(isset($percent_hosts[load_color($load)])) {
$percent_hosts[load_color($load)] += 1;
} else {
$percent_hosts[load_color($load)] = 1;
}
}
# Show pie chart of loads
$pie_args = "title=" . rawurlencode("Cluster Load Percentages");
$pie_args .= "&size=250x150";
foreach($conf['load_colors'] as $name=>$color)
{
if (!array_key_exists($color, $percent_hosts))
continue;
$n = $percent_hosts[$color];
$name_url = rawurlencode($name);
$pie_args .= "&$name_url=$n,$color";
}
$data->assign("pie_args", $pie_args);
# Host columns menu defined in header.php
$data->assign("columns_size_dropdown", 1);
$data->assign("cols_menu", $cols_menu);
$data->assign("size_menu", $size_menu);
$data->assign("node_legend", 1);
}
else
{
# Show pie chart of hosts up/down
$pie_args = "title=" . rawurlencode("Host Status");
$pie_args .= "&size=250x150";
$up_color = $conf['load_colors']["25-50"];
$down_color = $conf['load_colors']["down"];
$pie_args .= "&Up=$cluster[HOSTS_UP],$up_color";
$pie_args .= "&Down=$cluster[HOSTS_DOWN],$down_color";
$data->assign("pie_args", $pie_args);
}
# No reason to go on if we have no up hosts.
if (!is_array($hosts_up) or !$showhosts) {
$dwoo->output($tpl, $data);
return;
}
switch ($sort)
{
case "descending":
arsort($sorted_hosts);
break;
case "by name":
uksort($sorted_hosts, "strnatcmp");
break;
default:
case "ascending":
asort($sorted_hosts);
break;
}
$sorted_hosts = array_merge($down_hosts, $sorted_hosts);
# First pass to find the max value in all graphs for this
# metric. The $start,$end variables comes from get_context.php,
# included in index.php.
list($min, $max) = find_limits($sorted_hosts, $metricname);
# Second pass to output the graphs or metrics.
$i = 1;
foreach ( $sorted_hosts as $host => $value )
{
$host_url = rawurlencode($host);
$host_link="\"?c=$cluster_url&h=$host_url&$get_metric_string\"";
$textval = "";
#echo "$host: $value, ";
if (isset($hosts_down[$host]) and $hosts_down[$host])
{
$last_heartbeat = $cluster['LOCALTIME'] - $hosts_down[$host]['REPORTED'];
$age = $last_heartbeat > 3600 ? uptime($last_heartbeat) : "${last_heartbeat}s";
$class = "down";
$textval = "down <br> <font size=\"-2\">Last heartbeat $age ago</font>";
}
else
{
if(isset($metrics[$host][$metricname]))
$val = $metrics[$host][$metricname];
else
$val = NULL;
$class = "metric";
if ($val['TYPE']=="timestamp" or
(isset($always_timestamp[$metricname]) and
$always_timestamp[$metricname]))
{
$textval = date("r", $val['VAL']);
}
elseif ($val['TYPE']=="string" or $val['SLOPE']=="zero" or
(isset($always_constant[$metricname]) and
$always_constant[$metricname] or
($conf['max_graphs'] > 0 and $i > $conf['max_graphs'] )))
{
$textval = "$val[VAL]";
if (isset($val['UNITS']))
$textval .= " $val[UNITS]";
}
}
$size = isset($clustergraphsize) ? $clustergraphsize : 'small';
if ($conf['hostcols'] == 0) # enforce small size in multi-host report
$size = 'small';
// set host zoom class based on the size of the graph shown
if ( isset($conf['zoom_support']) && $conf['zoom_support'] === true )
$additional_host_img_html_args = "class=host_${size}_zoomable";
$data->assign("additional_host_img_html_args", $additional_host_img_html_args);
$graphargs = "z=$size&c=$cluster_url&h=$host_url";
if (isset($host_load[$host])) {
$load_color = load_color($host_load[$host]);
$graphargs .= "&l=$load_color&v=$val[VAL]";
}
$graphargs .= "&r=$range&su=1&st=$cluster[LOCALTIME]";
if ($cs)
$graphargs .= "&cs=" . rawurlencode($cs);
if ($ce)
$graphargs .= "&ce=" . rawurlencode($ce);
if ($showhosts == 1)
$graphargs .= "&x=$max&n=$min";
if ($textval)
{
$cell="<td class=$class>".
"<b><a href=$host_link>$host</a></b><br>".
"<i>$metricname:</i> <b>$textval</b></td>";
}
else
{
$cell="<td><div><font style='font-size: 8px'>$host</font><br><a href=$host_link><img $additional_host_img_html_args src=\"./graph.php?";
$cell .= (isset($reports[$metricname]) and $reports[$metricname])
? "g=$metricname" : "m=$metricname";
$cell .= "&$graphargs\" alt=\"$host\" border=0></a></div></td>";
}
if ($conf['hostcols'] == 0) {
$pre = "<td><a href=$host_link><img src=\"./graph.php?g=";
$post = "&$graphargs\" $additional_host_img_html_args alt=\"$host\" border=0></a></td>";
$cell .= $pre . "load_report" . $post;
$cell .= $pre . "mem_report" . $post;
$cell .= $pre . "cpu_report" . $post;
$cell .= $pre . "network_report" . $post;
}
$sorted_list[$host]["metric_image"] = $cell;
if (! ($i++ % $conf['hostcols']) ) {
$sorted_list[$host]["br"] = "</tr><tr>";
} else {
$sorted_list[$host]["br"] = "";
}
}
$data->assign("sorted_list", $sorted_list);
$dwoo->output($tpl, $data);
?>