source: trunk/web/addons/toga/toga-functions.php @ 109

Last change on this file since 109 was 109, checked in by bastiaans, 19 years ago

toga/toga-functions.php:

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