.
[corvix.git] / var / deb-package / cluster_raw_old / opt / cluster / lib / www / ganglia / ganglia.php
blob4dcc144c386d8deb6511af67a48bd6fe9a901a33
1 <?php
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.
12 $error="";
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.
16 $parsetime = 0;
18 # 2key = "Source Name" / "NAME | AUTHORITY | HOSTS_UP ..." = Value.
19 $grid = array();
21 # 1Key = "NAME | LOCALTIME | HOSTS_UP | HOSTS_DOWN" = Value.
22 $cluster = array();
24 # 2Key = "Cluster Name / Host Name" ... Value = Array of Host Attributes
25 $hosts_up = array();
26 # 2Key = "Cluster Name / Host Name" ... Value = Array of Host Attributes
27 $hosts_down = array();
29 # Context dependant structure.
30 $metrics = array();
32 # 1Key = "Component" (gmetad | gmond) = Version string
33 $version = array();
35 # The web frontend version, from conf.php.
36 #$version["webfrontend"] = "$majorversion.$minorversion.$microversion";
37 $version["webfrontend"] = "$ganglia_version";
39 # Get rrdtool 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.
47 $self = " ";
50 # Returns true if the host is alive. Works for both old and new gmond sources.
51 function host_alive($host, $cluster)
53 $TTL = 60;
55 if ($host['TN'] and $host['TMAX']) {
56 if ($host['TN'] > $host['TMAX'] * 4)
57 return FALSE;
58 $host_up = FALSE;
60 else { # The old method.
61 if (abs($cluster["LOCALTIME"] - $host['REPORTED']) > (4*$TTL))
62 return FALSE;
64 return TRUE;
68 # Called with <GANGLIA_XML> attributes.
69 function preamble($ganglia)
71 global $version;
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;
83 switch ($tagname)
85 case "GANGLIA_XML":
86 preamble($attrs);
87 break;
89 case "GRID":
90 case "CLUSTER":
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;
99 break;
101 case "METRICS":
102 $metricname = $attrs['NAME'];
103 $metrics[$sourcename][$metricname] = $attrs;
104 break;
106 case "HOSTS":
107 $grid[$sourcename]['HOSTS_UP'] = $attrs['UP'];
108 $grid[$sourcename]['HOSTS_DOWN'] = $attrs['DOWN'];
109 break;
111 default:
112 break;
117 function start_cluster ($parser, $tagname, $attrs)
119 global $metrics, $cluster, $self, $grid, $hosts_up, $hosts_down;
120 static $hostname;
122 switch ($tagname)
124 case "GANGLIA_XML":
125 preamble($attrs);
126 break;
127 case "GRID":
128 $self = $attrs['NAME'];
129 $grid = $attrs;
130 break;
132 case "CLUSTER":
133 $cluster = $attrs;
134 break;
136 case "HOST":
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;
145 else
147 isset($cluster['HOSTS_DOWN']) or $cluster['HOSTS_DOWN'] = 0;
148 $cluster['HOSTS_DOWN']++;
149 $hosts_down[$hostname] = $attrs;
151 break;
153 case "METRIC":
154 $metricname = $attrs['NAME'];
155 $metrics[$hostname][$metricname] = $attrs;
156 break;
158 default:
159 break;
164 function start_cluster_summary ($parser, $tagname, $attrs)
166 global $metrics, $cluster, $self, $grid;
168 switch ($tagname)
170 case "GANGLIA_XML":
171 preamble($attrs);
172 break;
173 case "GRID":
174 $self = $attrs['NAME'];
175 $grid = $attrs;
176 case "CLUSTER":
177 $cluster = $attrs;
178 break;
180 case "HOSTS":
181 $cluster['HOSTS_UP'] = $attrs['UP'];
182 $cluster['HOSTS_DOWN'] = $attrs['DOWN'];
183 break;
185 case "METRICS":
186 $metrics[$attrs['NAME']] = $attrs;
187 break;
189 default:
190 break;
195 function start_host ($parser, $tagname, $attrs)
197 global $metrics, $cluster, $hosts_up, $hosts_down, $self, $grid;
199 switch ($tagname)
201 case "GANGLIA_XML":
202 preamble($attrs);
203 break;
204 case "GRID":
205 $self = $attrs['NAME'];
206 $grid = $attrs;
207 break;
208 case "CLUSTER":
209 $cluster = $attrs;
210 break;
212 case "HOST":
213 if (host_alive($attrs, $cluster))
214 $hosts_up = $attrs;
215 else
216 $hosts_down = $attrs;
217 break;
219 case "METRIC":
220 $metrics[$attrs['NAME']] = $attrs;
221 break;
223 default:
224 break;
229 function end_all ($parser, $tagname)
235 function Gmetad ()
237 global $error, $parsetime, $clustername, $hostname, $context;
238 # From conf.php:
239 global $ganglia_ip, $ganglia_port;
241 # Parameters are optionalshow
242 # Defaults...
243 $ip = $ganglia_ip;
244 $port = $ganglia_port;
245 $timeout = 3.0;
246 $errstr = "";
247 $errno = "";
249 switch( func_num_args() )
251 case 2:
252 $port = func_get_arg(1);
253 case 1:
254 $ip = func_get_arg(0);
257 $parser = xml_parser_create();
258 switch ($context)
260 case "meta":
261 case "control":
262 case "tree":
263 default:
264 xml_set_element_handler($parser, "start_meta", "end_all");
265 $request = "/?filter=summary";
266 break;
267 case "physical":
268 case "cluster":
269 xml_set_element_handler($parser, "start_cluster", "end_all");
270 $request = "/$clustername";
271 break;
272 case "cluster-summary":
273 xml_set_element_handler($parser, "start_cluster_summary", "end_all");
274 $request = "/$clustername?filter=summary";
275 break;
276 case "node":
277 case "host":
278 xml_set_element_handler($parser, "start_host", "end_all");
279 $request = "/$clustername/$hostname";
280 break;
283 $fp = fsockopen( $ip, $port, $errno, $errstr, $timeout);
284 if (!$fp)
286 $error = "fsockopen error: $errstr";
287 return FALSE;
290 if ($port == 8649)
292 # We are connecting to a gmond. Non-interactive.
293 xml_set_element_handler($parser, "start_cluster", "end_all");
295 else
297 $request .= "\n";
298 $rc = fputs($fp, $request);
299 if (!$rc)
301 $error = "Could not sent request to gmetad: $errstr";
302 return FALSE;
306 $start = gettimeofday();
308 while(!feof($fp))
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));
316 fclose($fp);
317 return FALSE;
320 fclose($fp);
322 $end = gettimeofday();
323 $parsetime = ($end['sec'] + $end['usec']/1e6) - ($start['sec'] + $start['usec']/1e6);
325 return TRUE;