source: trunk/web/addons/toga/libtoga.php @ 110

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

web/addons/toga/toga-functions.php:

Renamed -> libtoga.php

web/addons/toga/libtoga.php:

new toga-functions.php:

  • Now nodes are correctly marked in clusterimage with a 'j' for single processor jobs and a 'J' for multi processor jobs

web/addons/toga/image.php:

  • Script for generating images

web/addons/toga/conf.php:

  • Configuration for web frontend

web/templates/toga/cluster_extra.tpl:

  • Plugin html template for ganglia

client:

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