forked from ganglia/ganglia-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ganglia.php
439 lines (354 loc) · 12.4 KB
/
ganglia.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
#
# Parses ganglia XML tree.
#
# The arrays defined in the first part of this file to hold XML info.
#
# sacerdoti: These are now context-sensitive, and hold only as much
# information as we need to make the page.
#
$gweb_root = dirname(__FILE__);
include_once($gweb_root . "/version.php");
include_once("./global.php");
$error="";
# Gives time in seconds to retrieve and parse XML tree. With subtree-
# capable gmetad, should be very fast in all but the largest cluster configurations.
$parsetime = 0;
# 2key = "Source Name" / "NAME | AUTHORITY | HOSTS_UP ..." = Value.
$grid = array();
# 1Key = "NAME | LOCALTIME | HOSTS_UP | HOSTS_DOWN" = Value.
$cluster = array();
# 2Key = "Cluster Name / Host Name" ... Value = Array of Host Attributes
$hosts_up = array();
# 2Key = "Cluster Name / Host Name" ... Value = Array of Host Attributes
$hosts_down = array();
# Context dependant structure.
$metrics = array();
# 1Key = "Component" (gmetad | gmond) = Version string
$version = array();
# The web frontend version, from conf.php.
$version["webfrontend"] = $GLOBALS["ganglia_version"];
# Get rrdtool version
$rrdtool_version = array();
exec($conf['rrdtool'], $rrdtool_version);
$rrdtool_version = explode(" ", $rrdtool_version[0]);
$rrdtool_version = $rrdtool_version[1];
$version["rrdtool"] = "$rrdtool_version";
# The name of our local grid.
$self = " ";
$index_array = array();
# Returns true if the host is alive. Works for both old and new gmond sources.
function host_alive($host, $cluster) {
$TTL = 60;
if ($host['TN'] and $host['TMAX']) {
if ($host['TN'] > $host['TMAX'] * 4)
return FALSE;
$host_up = FALSE;
}
else { # The old method.
if (abs($cluster["LOCALTIME"] - $host['REPORTED']) > (4*$TTL))
return FALSE;
}
return TRUE;
}
# Called with <GANGLIA_XML> attributes.
function preamble($ganglia) {
global $version;
$component = $ganglia['SOURCE'];
$version[$component] = $ganglia['VERSION'];
}
function start_meta ($parser, $tagname, $attrs) {
global $metrics, $grid, $self, $debug;
static $sourcename, $metricname;
$parser; // PHPCS
if ($debug) print "<br/>DEBUG: parser start meta [$tagname]\n";
switch ($tagname)
{
case "GANGLIA_XML":
preamble($attrs);
break;
case "GRID":
case "CLUSTER":
if ($debug) print "<br/>DEBUG: parser start meta GRID|CLUSTER\n";
# Our grid will be first.
if (!$sourcename) $self = $attrs['NAME'];
$sourcename = $attrs['NAME'];
$grid[$sourcename] = $attrs;
# Identify a grid from a cluster.
$grid[$sourcename][$tagname] = 1;
break;
case "METRICS":
$metricname = rawurlencode($attrs['NAME']);
$metrics[$sourcename][$metricname] = $attrs;
break;
case "HOSTS":
$grid[$sourcename]['HOSTS_UP'] = $attrs['UP'];
$grid[$sourcename]['HOSTS_DOWN'] = $attrs['DOWN'];
break;
default:
break;
}
}
function start_cluster ($parser, $tagname, $attrs) {
global $metrics, $cluster, $self, $grid, $hosts_up, $hosts_down, $debug;
static $hostname;
$parser; // PHPCS
if ($debug) print "<br/>DEBUG: parser start cluster [$tagname]\n";
switch ($tagname)
{
case "GANGLIA_XML":
preamble($attrs);
break;
case "GRID":
$self = $attrs['NAME'];
$grid = $attrs;
break;
case "CLUSTER":
$cluster = $attrs;
break;
case "HOST":
$hostname = $attrs['NAME'];
if (host_alive($attrs, $cluster))
{
isset($cluster['HOSTS_UP']) or $cluster['HOSTS_UP'] = 0;
$cluster['HOSTS_UP']++;
$hosts_up[$hostname] = $attrs;
}
else
{
isset($cluster['HOSTS_DOWN']) or $cluster['HOSTS_DOWN'] = 0;
$cluster['HOSTS_DOWN']++;
$hosts_down[$hostname] = $attrs;
}
# Pseudo metrics - add useful HOST attributes like gmond_started & last_reported to the metrics list:
$metrics[$hostname]['gmond_started']['NAME'] = "GMOND_STARTED";
$metrics[$hostname]['gmond_started']['VAL'] = $attrs['GMOND_STARTED'];
$metrics[$hostname]['gmond_started']['TYPE'] = "timestamp";
$metrics[$hostname]['last_reported']['NAME'] = "REPORTED";
$metrics[$hostname]['last_reported']['VAL'] = uptime($cluster['LOCALTIME'] - $attrs['REPORTED']);
$metrics[$hostname]['last_reported']['TYPE'] = "string";
$metrics[$hostname]['last_reported_uptime_in_sec']['VAL'] = $cluster['LOCALTIME'] - $attrs['REPORTED'];
$metrics[$hostname]['last_reported_timestamp']['NAME'] = "REPORTED TIMESTAMP";
$metrics[$hostname]['last_reported_timestamp']['VAL'] = $attrs['REPORTED'];
$metrics[$hostname]['last_reported_timestamp']['TYPE'] = "uint32";
$metrics[$hostname]['ip_address']['NAME'] = "IP";
$metrics[$hostname]['ip_address']['VAL'] = $attrs['IP'];
$metrics[$hostname]['ip_address']['TYPE'] = "string";
$metrics[$hostname]['location']['NAME'] = "LOCATION";
$metrics[$hostname]['location']['VAL'] = $attrs['LOCATION'];
$metrics[$hostname]['location']['TYPE'] = "string";
break;
case "METRIC":
$metricname = rawurlencode($attrs['NAME']);
$metrics[$hostname][$metricname] = $attrs;
break;
default:
break;
}
}
function start_everything ($parser, $tagname, $attrs) {
global $index_array, $hosts, $metrics, $cluster, $self, $grid, $hosts_up, $hosts_down, $debug;
static $hostname, $cluster_name;
$parser; // PHPCS
if ($debug) print "<br/>DEBUG: parser start everything [$tagname]\n";
switch ($tagname)
{
case "GANGLIA_XML":
preamble($attrs);
break;
case "GRID":
$self = $attrs['NAME'];
$grid = $attrs;
break;
case "CLUSTER":
# $cluster = $attrs;
$cluster_name = $attrs['NAME'];
break;
case "HOST":
$hostname = $attrs['NAME'];
# For some reason this occasionally will end up marking live hosts not alive
# causing them to miss out from aggregate graphs
# if (host_alive($attrs, $cluster_name))
$index_array['cluster'][$hostname][] = $cluster_name;
case "METRIC":
$metricname = rawurlencode($attrs['NAME']);
if ( $metricname != $hostname )
$index_array['metrics'][$metricname][] = $hostname;
break;
default:
break;
}
}
function start_cluster_summary ($parser, $tagname, $attrs) {
global $metrics, $cluster, $self, $grid;
$parser; // PHPCS
switch ($tagname)
{
case "GANGLIA_XML":
preamble($attrs);
break;
case "GRID":
$self = $attrs['NAME'];
$grid = $attrs;
case "CLUSTER":
$cluster = $attrs;
break;
case "HOSTS":
$cluster['HOSTS_UP'] = $attrs['UP'];
$cluster['HOSTS_DOWN'] = $attrs['DOWN'];
break;
case "METRICS":
$metrics[$attrs['NAME']] = $attrs;
break;
default:
break;
}
}
function start_host ($parser, $tagname, $attrs) {
global $metrics, $cluster, $hosts_up, $hosts_down, $self, $grid;
static $metricname;
$parser; // PHPCS
switch ($tagname)
{
case "GANGLIA_XML":
preamble($attrs);
break;
case "GRID":
$self = $attrs['NAME'];
$grid = $attrs;
break;
case "CLUSTER":
$cluster = $attrs;
break;
case "HOST":
if (host_alive($attrs, $cluster))
$hosts_up = $attrs;
else
$hosts_down = $attrs;
break;
case "METRIC":
$metricname = rawurlencode($attrs['NAME']);
$metrics[$metricname] = $attrs;
break;
case "EXTRA_DATA":
break;
case "EXTRA_ELEMENT":
if ( isset($attrs['NAME']) && isset($attrs['VAL']) && ($attrs['NAME'] == "GROUP")) {
if ( isset($metrics[$metricname]['GROUP']) ) {
$group_array = array_merge( (array)$attrs['VAL'], $metrics[$metricname]['GROUP'] );
} else {
$group_array = (array)$attrs['VAL'];
}
$attribarray = array($attrs['NAME'] => $attrs['VAL']);
$metrics[$metricname] = array_merge($metrics[$metricname], $attribarray);
$metrics[$metricname]['GROUP'] = $group_array;
} else {
$attribarray = array($attrs['NAME'] => $attrs['VAL']);
$metrics[$metricname] = array_merge($metrics[$metricname], $attribarray);
}
break;
default:
break;
}
}
function end_all ($parser, $tagname) {
}
function Gmetad () {
global $conf, $error, $parsetime, $clustername, $hostname, $context, $debug;
if ($debug) print "<br/>\n";
# Parameters are optionalshow
# Defaults...
$ip = $conf['ganglia_ip'];
$port = $conf['ganglia_port'];
$timeout = 3.0;
$errstr = "";
$errno = "";
//TODO: all calls to this function (in get_ganglia.php) supply 2 args. Why do we make that optional?
switch( func_num_args() )
{
case 2:
$port = func_get_arg(1);
case 1:
$ip = func_get_arg(0);
}
if ($debug) print "<br/>DEBUG: Creating parser\n";
if ( in_array($context, $SKIP_GMETAD_CONTEXTS) ) {
return TRUE;
}
$parser = xml_parser_create();
$strip_extra = $conf['strip_extra'];
switch ($context)
{
case "meta":
case "control":
case "tree":
default:
xml_set_element_handler($parser, "start_meta", "end_all");
$request = "/?filter=summary";
break;
case "physical":
case "cluster":
xml_set_element_handler($parser, "start_cluster", "end_all");
$request = "/$clustername";
break;
case "index_array":
case "views":
xml_set_element_handler($parser, "start_everything", "end_all");
$request = "/";
break;
case "cluster-summary":
xml_set_element_handler($parser, "start_cluster_summary", "end_all");
$request = "/$clustername?filter=summary";
break;
case "node":
case "host":
xml_set_element_handler($parser, "start_host", "end_all");
$request = "/$clustername/$hostname";
$strip_extra = false;
break;
}
$fp = fsockopen( $ip, $port, $errno, $errstr, $timeout);
if (!$fp)
{
$error = "fsockopen error: $errstr";
if ($debug) print "<br/>DEBUG: $error\n";
return FALSE;
}
if ($port == 8649)
{
# We are connecting to a gmond. Non-interactive.
xml_set_element_handler($parser, "start_cluster", "end_all");
}
else
{
$request .= "\n";
$rc = fputs($fp, $request);
if (!$rc)
{
$error = "Could not sent request to gmetad: $errstr";
if ($debug) print "<br/>DEBUG: $error\n";
return FALSE;
}
}
$start = gettimeofday();
while(!feof($fp))
{
$data = fread($fp, 16384);
if($strip_extra) $data = preg_replace('/<EXTRA_DATA>.*?<\/EXTRA_DATA>/s', '', $data);
if (!xml_parse($parser, $data, feof($fp)))
{
$error = sprintf("XML error: %s at %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser));
if ($debug) print "<br/>DEBUG: $error\n";
fclose($fp);
return FALSE;
}
}
fclose($fp);
$end = gettimeofday();
$parsetime = ($end['sec'] + $end['usec']/1e6) - ($start['sec'] + $start['usec']/1e6);
if ($debug) print "<br/>DEBUG: theoretically completed gmetad parsing\n";
return TRUE;
}
?>