1 | <?php |
---|
2 | class HTTPVariables { |
---|
3 | |
---|
4 | var $clustername, $metricname; |
---|
5 | var $restvars, $httpvars; |
---|
6 | |
---|
7 | function HTTPVariables( $vars ) { |
---|
8 | |
---|
9 | $this->restvars = array(); |
---|
10 | |
---|
11 | $this->clustername = $vars["c"] ? $vars["c"] : null; |
---|
12 | $this->metricname = $vars["m"] ? $vars["m"] : null; |
---|
13 | |
---|
14 | foreach( $vars as $httpvar => $httpval ) { |
---|
15 | |
---|
16 | if( $httpval ) { |
---|
17 | $this->restvars[$httpvar] = $httpval; |
---|
18 | } |
---|
19 | } |
---|
20 | } |
---|
21 | |
---|
22 | function getClusterName() { |
---|
23 | return $this->clustername; |
---|
24 | } |
---|
25 | |
---|
26 | function getMetricName() { |
---|
27 | return $this->metricname; |
---|
28 | } |
---|
29 | |
---|
30 | function getHttpVar( $var ) { |
---|
31 | if( isset( $this->restvars[$var] ) ) |
---|
32 | return $this->restvars[$var]; |
---|
33 | else |
---|
34 | return null; |
---|
35 | } |
---|
36 | } |
---|
37 | |
---|
38 | // Toga's conf |
---|
39 | // |
---|
40 | include_once "./conf.php"; |
---|
41 | |
---|
42 | global $GANGLIA_PATH, $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH, $DATA_SOURCE; |
---|
43 | |
---|
44 | include_once "$GANGLIA_PATH/conf.php"; |
---|
45 | include_once "$GANGLIA_PATH/functions.php"; |
---|
46 | include_once "$GANGLIA_PATH/ganglia.php"; |
---|
47 | |
---|
48 | global $HTTP_GET_VARS; |
---|
49 | $httpvars = new HTTPVariables( $HTTP_GET_VARS ); |
---|
50 | |
---|
51 | // Set cluster context so that Ganglia will |
---|
52 | // provide us with the correct metrics array |
---|
53 | // |
---|
54 | global $context, $clustername; |
---|
55 | $clustername = $httpvars->getClusterName(); |
---|
56 | $context = 'cluster'; |
---|
57 | |
---|
58 | include_once "$GANGLIA_PATH/get_ganglia.php"; |
---|
59 | |
---|
60 | // Ganglia's array of host metrics |
---|
61 | // |
---|
62 | global $metrics; |
---|
63 | |
---|
64 | // If php is compiled without globals |
---|
65 | // |
---|
66 | if ( !empty( $_GET ) ) { |
---|
67 | extract( $_GET ); |
---|
68 | } |
---|
69 | |
---|
70 | class DataSource { |
---|
71 | |
---|
72 | var $data, $ip, $port; |
---|
73 | |
---|
74 | function DataSource( $ip = '127.0.0.1', $port = 8649 ) { |
---|
75 | $this->ip = $ip; |
---|
76 | $this->port = $port; |
---|
77 | } |
---|
78 | |
---|
79 | function getData() { |
---|
80 | |
---|
81 | $errstr; |
---|
82 | $errno = 0; |
---|
83 | $timeout = 3; |
---|
84 | |
---|
85 | $fp = fsockopen( $this->ip, $this->port, &$errno, &$errstr, $timeout ); |
---|
86 | |
---|
87 | if( !$fp ) { |
---|
88 | echo 'Unable to connect to '.$this->ip.':'.$this->port; // printf( 'Unable to connect to [%s:%.0f]', $this->ip, $this->port ); |
---|
89 | return; |
---|
90 | } |
---|
91 | |
---|
92 | while ( !feof( $fp ) ) { |
---|
93 | |
---|
94 | $data .= fread( $fp, 16384 ); |
---|
95 | } |
---|
96 | |
---|
97 | fclose( $fp ); |
---|
98 | |
---|
99 | return $data; |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | class DataGatherer { |
---|
104 | |
---|
105 | var $xmlhandler, $data, $httpvars; |
---|
106 | |
---|
107 | function DataGatherer() { |
---|
108 | |
---|
109 | global $DATA_SOURCE; |
---|
110 | |
---|
111 | $ds_fields = explode( ':', $DATA_SOURCE ); |
---|
112 | $ds_ip = $ds_fields[0]; |
---|
113 | $ds_port = $ds_fields[1]; |
---|
114 | |
---|
115 | $this->source = new DataSource( $ds_ip, $ds_port ); |
---|
116 | |
---|
117 | $this->parser = xml_parser_create(); |
---|
118 | $this->httpvars = $httpvars; |
---|
119 | $this->xmlhandler = new TorqueXMLHandler(); |
---|
120 | xml_set_element_handler( $this->parser, array( &$this->xmlhandler, 'startElement' ), array( &$this->xmlhandler, 'stopElement' ) ); |
---|
121 | } |
---|
122 | |
---|
123 | function parseXML() { |
---|
124 | |
---|
125 | $src = &$this->source; |
---|
126 | $this->data = $src->getData(); |
---|
127 | |
---|
128 | if ( !xml_parse( &$this->parser, $this->data ) ) |
---|
129 | $error = sprintf( 'XML error: %s at %d', xml_error_string( xml_get_error_code( &$this->parser ) ), xml_get_current_line_number( &$this->parser ) ); |
---|
130 | } |
---|
131 | |
---|
132 | function printInfo() { |
---|
133 | $handler = $this->xmlhandler; |
---|
134 | $handler->printInfo(); |
---|
135 | } |
---|
136 | |
---|
137 | function getNodes() { |
---|
138 | $handler = $this->xmlhandler; |
---|
139 | return $handler->getNodes(); |
---|
140 | } |
---|
141 | |
---|
142 | function getJobs() { |
---|
143 | $handler = $this->xmlhandler; |
---|
144 | return $handler->getJobs(); |
---|
145 | } |
---|
146 | |
---|
147 | } |
---|
148 | |
---|
149 | class TorqueXMLHandler { |
---|
150 | |
---|
151 | var $clusters, $heartbeat, $nodes, $jobs; |
---|
152 | |
---|
153 | function TorqueXMLHandler() { |
---|
154 | $jobs = array(); |
---|
155 | $clusters = array(); |
---|
156 | $nodes = array(); |
---|
157 | $heartbeat = array(); |
---|
158 | } |
---|
159 | |
---|
160 | function startElement( $parser, $name, $attrs ) { |
---|
161 | |
---|
162 | $jobs = &$this->jobs; |
---|
163 | $nodes = &$this->nodes; |
---|
164 | |
---|
165 | if ( $attrs[TN] ) { |
---|
166 | |
---|
167 | // Ignore dead metrics. Detect and mask failures. |
---|
168 | if ( $attrs[TN] > $attrs[TMAX] * 4 ) |
---|
169 | return; |
---|
170 | } |
---|
171 | |
---|
172 | $jobid = null; |
---|
173 | |
---|
174 | // printf( '%s=%s', $attrs[NAME], $attrs[VAL] ); |
---|
175 | |
---|
176 | if( $name == 'CLUSTER' ) { |
---|
177 | |
---|
178 | $clustername = $attrs[VAL]; |
---|
179 | |
---|
180 | if( !isset( $clusters[$clustername] ) ) |
---|
181 | $clusters[$clustername] = array(); |
---|
182 | |
---|
183 | } else if( $name == 'HOST' ) { |
---|
184 | |
---|
185 | $hostname = $attrs[NAME]; |
---|
186 | $location = $attrs[LOCATION]; |
---|
187 | |
---|
188 | if( !isset( $this->nodes[$hostname] ) ) |
---|
189 | $this->nodes[$hostname] = new NodeImage( $hostname ); |
---|
190 | |
---|
191 | } else if( $name == 'METRIC' and strstr( $attrs[NAME], 'TOGA' ) ) { |
---|
192 | |
---|
193 | if( strstr( $attrs[NAME], 'TOGA-HEARTBEAT' ) ) { |
---|
194 | |
---|
195 | $heartbeat['time'] = $attrs[VAL]; |
---|
196 | //printf( "heartbeat %s\n", $heartbeat['time'] ); |
---|
197 | |
---|
198 | } else if( strstr( $attrs[NAME], 'TOGA-JOB' ) ) { |
---|
199 | |
---|
200 | sscanf( $attrs[NAME], 'TOGA-JOB-%d', $jobid ); |
---|
201 | |
---|
202 | //printf( "jobid %s\n", $jobid ); |
---|
203 | |
---|
204 | if( !isset( $jobs[$jobid] ) ) |
---|
205 | $jobs[$jobid] = array(); |
---|
206 | |
---|
207 | $fields = explode( ' ', $attrs[VAL] ); |
---|
208 | |
---|
209 | foreach( $fields as $f ) { |
---|
210 | $togavalues = explode( '=', $f ); |
---|
211 | |
---|
212 | $toganame = $togavalues[0]; |
---|
213 | $togavalue = $togavalues[1]; |
---|
214 | |
---|
215 | //printf( "\t%s\t= %s\n", $toganame, $togavalue ); |
---|
216 | |
---|
217 | if( $toganame == 'nodes' ) { |
---|
218 | |
---|
219 | if( !isset( $jobs[$jobid][$toganame] ) ) |
---|
220 | $jobs[$jobid][$toganame] = array(); |
---|
221 | |
---|
222 | $mynodes = explode( ';', $togavalue ); |
---|
223 | |
---|
224 | foreach( $mynodes as $node ) |
---|
225 | |
---|
226 | $jobs[$jobid][$toganame][] = $node; |
---|
227 | } else { |
---|
228 | |
---|
229 | $jobs[$jobid][$toganame] = $togavalue; |
---|
230 | } |
---|
231 | } |
---|
232 | |
---|
233 | if( isset( $jobs[$jobid][domain] ) and isset( $jobs[$jobid][nodes] ) ) { |
---|
234 | |
---|
235 | $nr_nodes = count( $jobs[$jobid][nodes] ); |
---|
236 | |
---|
237 | foreach( $jobs[$jobid][nodes] as $node ) { |
---|
238 | |
---|
239 | $host = $node.'.'.$jobs[$jobid][domain]; |
---|
240 | |
---|
241 | if( !isset( $this->nodes[$host] ) ) |
---|
242 | $my_node = new NodeImage( $host ); |
---|
243 | else |
---|
244 | $my_node = $this->nodes[$host]; |
---|
245 | |
---|
246 | if( !$my_node->hasJob( $jobid ) ) |
---|
247 | |
---|
248 | if( isset( $jobs[$jobid][ppn] ) ) |
---|
249 | $my_node->addJob( $jobid, ((int) $jobs[$jobid][ppn]) ); |
---|
250 | else |
---|
251 | $my_node->addJob( $jobid, 1 ); |
---|
252 | |
---|
253 | $this->nodes[$host] = $my_node; |
---|
254 | } |
---|
255 | } |
---|
256 | } |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
260 | function stopElement( $parser, $name ) { |
---|
261 | } |
---|
262 | |
---|
263 | function printInfo() { |
---|
264 | |
---|
265 | $jobs = &$this->jobs; |
---|
266 | |
---|
267 | printf( "---jobs---\n" ); |
---|
268 | |
---|
269 | foreach( $jobs as $jobid => $job ) { |
---|
270 | |
---|
271 | printf( "job %s\n", $jobid ); |
---|
272 | |
---|
273 | if( isset( $job[nodes] ) ) { |
---|
274 | |
---|
275 | foreach( $job[nodes] as $node ) { |
---|
276 | |
---|
277 | $mynode = $this->nodes[$node]; |
---|
278 | $hostname = $mynode->getHostname(); |
---|
279 | $location = $mynode->getLocation(); |
---|
280 | |
---|
281 | printf( "\t- node %s\tlocation %s\n", $hostname, $location ); |
---|
282 | //$this->nodes[$hostname]->setLocation( "hier draait job ".$jobid ); |
---|
283 | } |
---|
284 | } |
---|
285 | } |
---|
286 | |
---|
287 | printf( "---nodes---\n" ); |
---|
288 | |
---|
289 | $nodes = &$this->nodes; |
---|
290 | |
---|
291 | foreach( $nodes as $node ) { |
---|
292 | |
---|
293 | $hostname = $node->getHostname(); |
---|
294 | $location = $node->getLocation(); |
---|
295 | $jobs = implode( ' ', $node->getJobs() ); |
---|
296 | printf( "* node %s\tlocation %s\tjobs %s\n", $hostname, $location, $jobs ); |
---|
297 | } |
---|
298 | } |
---|
299 | |
---|
300 | function getNodes() { |
---|
301 | return $this->nodes; |
---|
302 | } |
---|
303 | |
---|
304 | function getJobs() { |
---|
305 | return $this->jobs; |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | class NodeImage { |
---|
310 | |
---|
311 | var $image, $x, $y, $hostname, $jobs, $tasks; |
---|
312 | |
---|
313 | function NodeImage( $hostname ) { |
---|
314 | |
---|
315 | $this->jobs = array(); |
---|
316 | //$this->image = $image; |
---|
317 | //$this->x = $x; |
---|
318 | //$this->y = $y; |
---|
319 | $this->tasks = 0; |
---|
320 | $this->hostname = $hostname; |
---|
321 | $this->cpus = $this->determineCpus(); |
---|
322 | } |
---|
323 | |
---|
324 | function addJob( $jobid, $cpus ) { |
---|
325 | $jobs = &$this->jobs; |
---|
326 | |
---|
327 | $jobs[] = $jobid; |
---|
328 | $this->jobs = $jobs; |
---|
329 | |
---|
330 | $this->addTask( $cpus ); |
---|
331 | } |
---|
332 | |
---|
333 | function hasJob( $jobid ) { |
---|
334 | |
---|
335 | $jobfound = 0; |
---|
336 | |
---|
337 | if( count( $this->jobs ) > 0 ) |
---|
338 | foreach( $this->jobs as $job ) |
---|
339 | |
---|
340 | if( $job == $jobid ) |
---|
341 | $jobfound = 1; |
---|
342 | |
---|
343 | return $jobfound; |
---|
344 | } |
---|
345 | |
---|
346 | function addTask( $cpus ) { |
---|
347 | |
---|
348 | $this->tasks = $this->tasks + $cpus; |
---|
349 | } |
---|
350 | |
---|
351 | function setImage( $image ) { |
---|
352 | |
---|
353 | $this->image = $image; |
---|
354 | } |
---|
355 | |
---|
356 | function setCoords( $x, $y ) { |
---|
357 | |
---|
358 | $this->x = $x; |
---|
359 | $this->y = $y; |
---|
360 | } |
---|
361 | |
---|
362 | function colorHex( $color ) { |
---|
363 | |
---|
364 | $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) ); |
---|
365 | |
---|
366 | return $my_color; |
---|
367 | } |
---|
368 | |
---|
369 | function setLoad( $load ) { |
---|
370 | $this->load = $load; |
---|
371 | } |
---|
372 | |
---|
373 | function setHostname( $hostname ) { |
---|
374 | $this->hostname = $hostname; |
---|
375 | } |
---|
376 | |
---|
377 | function draw() { |
---|
378 | |
---|
379 | global $SMALL_CLUSTERIMAGE_NODEWIDTH, $JOB_NODE_MARKING_ALLCPUS, $JOB_NODE_MARKING_SINGLECPU; |
---|
380 | |
---|
381 | $this->load = $this->determineLoad(); |
---|
382 | |
---|
383 | if( !isset( $this->image ) or !isset( $this->x ) or !isset( $this->y ) ) { |
---|
384 | printf( "aborting\n" ); |
---|
385 | printf( "x %d y %d load %f\n", $this->x, $this->y, $load ); |
---|
386 | return; |
---|
387 | } |
---|
388 | |
---|
389 | $black_color = imageColorAllocate( $this->image, 0, 0, 0 ); |
---|
390 | |
---|
391 | // Convert Ganglias Hexadecimal load color to a Decimal one |
---|
392 | // |
---|
393 | $load = $this->determineLoad(); |
---|
394 | $my_loadcolor = $this->colorHex( load_color($load) ); |
---|
395 | |
---|
396 | $size = $SMALL_CLUSTERIMAGE_NODEWIDTH; |
---|
397 | |
---|
398 | imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+($size), $this->y+($size), $black_color ); |
---|
399 | imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $my_loadcolor ); |
---|
400 | |
---|
401 | $nr_jobs = count( $this->jobs ); |
---|
402 | |
---|
403 | $node_mark = null; |
---|
404 | |
---|
405 | if( count( $this->jobs ) > 0 ) |
---|
406 | |
---|
407 | if( $this->tasks < $this->cpus ) |
---|
408 | $node_mark = $JOB_NODE_MARKING_SINGLECPU; |
---|
409 | |
---|
410 | else if( $this->tasks == $this->cpus ) |
---|
411 | $node_mark = $JOB_NODE_MARKING_ALLCPUS; |
---|
412 | |
---|
413 | if( $node_mark ) |
---|
414 | imageString( $this->image, 1, $this->x+(($size/2)-2), $this->y+(($size/2)-3), $node_mark, $black_color ); |
---|
415 | } |
---|
416 | |
---|
417 | function determineCpus() { |
---|
418 | |
---|
419 | global $metrics; |
---|
420 | |
---|
421 | $cpus = $metrics[$this->hostname][cpu_num][VAL]; |
---|
422 | if (!$cpus) $cpus=1; |
---|
423 | |
---|
424 | return $cpus; |
---|
425 | } |
---|
426 | |
---|
427 | function determineLoad() { |
---|
428 | |
---|
429 | global $metrics; |
---|
430 | |
---|
431 | $load_one = $metrics[$this->hostname][load_one][VAL]; |
---|
432 | $load = ((float) $load_one)/$this->cpus; |
---|
433 | |
---|
434 | return $load; |
---|
435 | } |
---|
436 | } |
---|
437 | |
---|
438 | class ClusterImage { |
---|
439 | |
---|
440 | var $dataget, $image, $clustername; |
---|
441 | |
---|
442 | function ClusterImage( $clustername ) { |
---|
443 | $this->dataget = new DataGatherer(); |
---|
444 | $this->clustername = $clustername; |
---|
445 | } |
---|
446 | |
---|
447 | function draw() { |
---|
448 | |
---|
449 | global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH; |
---|
450 | |
---|
451 | $mydatag = $this->dataget; |
---|
452 | $mydatag->parseXML(); |
---|
453 | |
---|
454 | //$max_width = 250; |
---|
455 | //$node_width = 11; |
---|
456 | |
---|
457 | $max_width = $SMALL_CLUSTERIMAGE_MAXWIDTH; |
---|
458 | $node_width = $SMALL_CLUSTERIMAGE_NODEWIDTH; |
---|
459 | |
---|
460 | //printf( "cmaxw %s nmaxw %s", $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH ); |
---|
461 | |
---|
462 | $nodes = $mydatag->getNodes(); |
---|
463 | $nodes_hosts = array_keys( $nodes ); |
---|
464 | |
---|
465 | $nodes_nr = count( $nodes ); |
---|
466 | |
---|
467 | $nodes_size = $nodes_nr*$node_width; |
---|
468 | $node_rows = 0; |
---|
469 | |
---|
470 | if( $nodes_size > $max_width ) { |
---|
471 | $nodes_per_row = ( (int) ($max_width/$node_width) ); |
---|
472 | } else { |
---|
473 | $nodes_per_row = $nodes_size; |
---|
474 | $node_rows = 1; |
---|
475 | } |
---|
476 | |
---|
477 | if( $nodes_per_row < $nodes_nr ) { |
---|
478 | $node_rows = ( (int) ($nodes_nr/$nodes_per_row) ); |
---|
479 | $node_rest = fmod( $nodes_nr, $nodes_per_row ); |
---|
480 | //printf( "nodesnr %d noderest %f\n", $nodes_nr, $node_rest ); |
---|
481 | if( $node_rest > 0 ) { |
---|
482 | $node_rows++; |
---|
483 | //printf( "noderows %d\n", $node_rows ); |
---|
484 | } |
---|
485 | } |
---|
486 | |
---|
487 | //printf( "imagecreate: %dx%d", ($nodes_per_row*$node_width), ($node_rows*$node_width) ); |
---|
488 | $image = imageCreateTrueColor( ($nodes_per_row*$node_width)+1, ($node_rows*$node_width)+1 ); |
---|
489 | $colorwhite = imageColorAllocate( $image, 255, 255, 255 ); |
---|
490 | imageFill( $image, 0, 0, $colorwhite ); |
---|
491 | |
---|
492 | for( $n = 0; $n < $node_rows; $n++ ) { |
---|
493 | |
---|
494 | for( $m = 0; $m < $nodes_per_row; $m++ ) { |
---|
495 | |
---|
496 | $x = ($m * $node_width); |
---|
497 | $y = ($n * $node_width); |
---|
498 | |
---|
499 | $cur_node = ($n * $nodes_per_row) + ($m); |
---|
500 | $host = $nodes_hosts[$cur_node]; |
---|
501 | |
---|
502 | if( isset( $nodes[$host] ) ) { |
---|
503 | |
---|
504 | $nodes[$host]->setCoords( $x, $y ); |
---|
505 | $nodes[$host]->setImage( $image ); |
---|
506 | $nodes[$host]->draw(); |
---|
507 | } |
---|
508 | } |
---|
509 | } |
---|
510 | |
---|
511 | header( 'Content-type: image/png' ); |
---|
512 | imagePNG( $image ); |
---|
513 | imageDestroy( $image ); |
---|
514 | } |
---|
515 | } |
---|
516 | |
---|
517 | //$my_data = new DataGatherer(); |
---|
518 | //$my_data->parseXML(); |
---|
519 | //$my_data->printInfo(); |
---|
520 | |
---|
521 | //$ic = new ClusterImage( "LISA Cluster" ); |
---|
522 | //$ic->draw(); |
---|
523 | ?> |
---|