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

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

toga/toga-functions.php:

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