2 /* $Id: ganglia.php 809 2007-07-12 06:34:23Z bernardli $ */
4 # Parses ganglia XML tree.
6 # The arrays defined in the first part of this file to hold XML info.
8 # sacerdoti: These are now context-sensitive, and hold only as much
9 # information as we need to make the page.
14 # Gives time in seconds to retrieve and parse XML tree. With subtree-
15 # capable gmetad, should be very fast in all but the largest cluster configurations.
18 # 2key = "Source Name" / "NAME | AUTHORITY | HOSTS_UP ..." = Value.
21 # 1Key = "NAME | LOCALTIME | HOSTS_UP | HOSTS_DOWN" = Value.
24 # 2Key = "Cluster Name / Host Name" ... Value = Array of Host Attributes
26 # 2Key = "Cluster Name / Host Name" ... Value = Array of Host Attributes
27 $hosts_down = array();
29 # Context dependant structure.
32 # 1Key = "Component" (gmetad | gmond) = Version string
35 # The web frontend version, from conf.php.
36 #$version["webfrontend"] = "$majorversion.$minorversion.$microversion";
37 $version["webfrontend"] = "$ganglia_version";
40 $rrdtool_version = array();
41 exec(RRDTOOL
, $rrdtool_version);
42 $rrdtool_version = explode(" ", $rrdtool_version[0]);
43 $rrdtool_version = $rrdtool_version[1];
44 $version["rrdtool"] = "$rrdtool_version";
46 # The name of our local grid.
50 # Returns true if the host is alive. Works for both old and new gmond sources.
51 function host_alive($host, $cluster)
55 if ($host['TN'] and $host['TMAX']) {
56 if ($host['TN'] > $host['TMAX'] * 4)
60 else { # The old method.
61 if (abs($cluster["LOCALTIME"] - $host['REPORTED']) > (4*$TTL))
68 # Called with <GANGLIA_XML> attributes.
69 function preamble($ganglia)
73 $component = $ganglia['SOURCE'];
74 $version[$component] = $ganglia['VERSION'];
78 function start_meta ($parser, $tagname, $attrs)
80 global $metrics, $grid, $self;
81 static $sourcename, $metricname;
91 # Our grid will be first.
92 if (!$sourcename) $self = $attrs['NAME'];
94 $sourcename = $attrs['NAME'];
95 $grid[$sourcename] = $attrs;
97 # Identify a grid from a cluster.
98 $grid[$sourcename][$tagname] = 1;
102 $metricname = $attrs['NAME'];
103 $metrics[$sourcename][$metricname] = $attrs;
107 $grid[$sourcename]['HOSTS_UP'] = $attrs['UP'];
108 $grid[$sourcename]['HOSTS_DOWN'] = $attrs['DOWN'];
117 function start_cluster ($parser, $tagname, $attrs)
119 global $metrics, $cluster, $self, $grid, $hosts_up, $hosts_down;
128 $self = $attrs['NAME'];
137 $hostname = $attrs['NAME'];
139 if (host_alive($attrs, $cluster))
141 isset($cluster['HOSTS_UP']) or $cluster['HOSTS_UP'] = 0;
142 $cluster['HOSTS_UP']++
;
143 $hosts_up[$hostname] = $attrs;
147 isset($cluster['HOSTS_DOWN']) or $cluster['HOSTS_DOWN'] = 0;
148 $cluster['HOSTS_DOWN']++
;
149 $hosts_down[$hostname] = $attrs;
154 $metricname = $attrs['NAME'];
155 $metrics[$hostname][$metricname] = $attrs;
164 function start_cluster_summary ($parser, $tagname, $attrs)
166 global $metrics, $cluster, $self, $grid;
174 $self = $attrs['NAME'];
181 $cluster['HOSTS_UP'] = $attrs['UP'];
182 $cluster['HOSTS_DOWN'] = $attrs['DOWN'];
186 $metrics[$attrs['NAME']] = $attrs;
195 function start_host ($parser, $tagname, $attrs)
197 global $metrics, $cluster, $hosts_up, $hosts_down, $self, $grid;
205 $self = $attrs['NAME'];
213 if (host_alive($attrs, $cluster))
216 $hosts_down = $attrs;
220 $metrics[$attrs['NAME']] = $attrs;
229 function end_all ($parser, $tagname)
237 global $error, $parsetime, $clustername, $hostname, $context;
239 global $ganglia_ip, $ganglia_port;
241 # Parameters are optionalshow
244 $port = $ganglia_port;
249 switch( func_num_args() )
252 $port = func_get_arg(1);
254 $ip = func_get_arg(0);
257 $parser = xml_parser_create();
264 xml_set_element_handler($parser, "start_meta", "end_all");
265 $request = "/?filter=summary";
269 xml_set_element_handler($parser, "start_cluster", "end_all");
270 $request = "/$clustername";
272 case "cluster-summary":
273 xml_set_element_handler($parser, "start_cluster_summary", "end_all");
274 $request = "/$clustername?filter=summary";
278 xml_set_element_handler($parser, "start_host", "end_all");
279 $request = "/$clustername/$hostname";
283 $fp = fsockopen( $ip, $port, $errno, $errstr, $timeout);
286 $error = "fsockopen error: $errstr";
292 # We are connecting to a gmond. Non-interactive.
293 xml_set_element_handler($parser, "start_cluster", "end_all");
298 $rc = fputs($fp, $request);
301 $error = "Could not sent request to gmetad: $errstr";
306 $start = gettimeofday();
310 $data = fread($fp, 16384);
311 if (!xml_parse($parser, $data, feof($fp)))
313 $error = sprintf("XML error: %s at %d",
314 xml_error_string(xml_get_error_code($parser)),
315 xml_get_current_line_number($parser));
322 $end = gettimeofday();
323 $parsetime = ($end['sec'] +
$end['usec']/1e6
) - ($start['sec'] +
$start['usec']/1e6
);