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
RevLine 
[102]1<?php
[110]2//$GANGLIA_PATH = "/var/www/ganglia";
[102]3
[110]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
[102]12include_once "$GANGLIA_PATH/conf.php";
13include_once "$GANGLIA_PATH/functions.php";
[108]14include_once "$GANGLIA_PATH/ganglia.php";
[102]15
[109]16// Set cluster context so that Ganglia will
17// provide us with the correct metrics array
18//
[108]19global $context;
20$context = 'cluster';
21
22include_once "$GANGLIA_PATH/get_ganglia.php";
23
[109]24// If php is compiled without globals
25//
[103]26if ( !empty( $_GET ) ) {
27        extract( $_GET );
28}
29
[109]30// Ganglia's array of host metrics
31//
[108]32global $metrics;
33
34//print_r($metrics);
35
[103]36class HTTPVariables {
37
38        var $clustername, $metricname;
[110]39        var $restvars;
[103]40
41        function HTTPVariables() {
42
[110]43                $this->restvars = array();
44
[103]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;
[110]49
50                foreach( $HTTP_GET_VARS as $httpvar => $httpval ) {
51                       
52                        if( $httpval ) {
53                                $this->restvars[$httpvar] = $httpval;
54                        }
55                }
[103]56        }
57
58        function getClusterName() {
59                return $this->clustername;
60        }
61
62        function getMetricName() {
63                return $this->metricname;
64        }
[110]65
66        function getHttpVar( $var ) {
67                if( isset( $this->restvars[$var] ) )
68                        return $retval;
69                else
70                        return null;
71        }
[103]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
[106]91                if( !$fp ) {
[103]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
[105]109        var $xmlhandler, $data, $httpvars;
[103]110
111        function DataGatherer() {
112
[110]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
[103]121                $this->parser = xml_parser_create();
[105]122                $this->httpvars = new HTTPVariables();
[103]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
[105]138        function printInfo() {
139                $handler = $this->xmlhandler;
140                $handler->printInfo();
141        }
142
[106]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
[103]153}
154
155class TorqueXMLHandler {
156
[105]157        var $clusters, $heartbeat, $nodes, $jobs;
[104]158
[103]159        function TorqueXMLHandler() {
[105]160                $jobs = array();
[104]161                $clusters = array();
[105]162                $nodes = array();
[104]163                $heartbeat = array();
[103]164        }
165
[110]166        function gotNode( $hostname, $location = 'unspecified', $jobid ) {
[105]167
168                $nodes = &$this->nodes;
169
170                if( !isset( $nodes[$hostname] ) ) {
171
172                        $nodes[$hostname] = new Node( $hostname );
173                }
174
[106]175                if( $location ) {
[105]176
177                        $nodes[$hostname]->setLocation( $location );
178                }
179
180                if( $jobid ) {
181                        $nodes[$hostname]->addJob( $jobid );
[110]182                        //printf("add job %s to node %s", $jobid, $hostname );
[105]183                }
184        }
185
[103]186        function startElement( $parser, $name, $attrs ) {
187
[105]188                $jobs = &$this->jobs;
189
[103]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
[104]199                // printf( '%s=%s', $attrs[NAME], $attrs[VAL] );
[103]200
[104]201                if( $name == 'CLUSTER' ) {
[103]202
[104]203                        $clustername = $attrs[VAL];
[103]204
[104]205                        if( !isset( $clusters[$clustername] ) )
206                                $clusters[$clustername] = array();
[103]207
[105]208                } else if( $name == 'HOST' ) {
209
210                        $hostname = $attrs[NAME];
211                        $location = $attrs[LOCATION];
212
[106]213                        $this->gotNode( $hostname, $location, null );
[105]214
[104]215                } else if( $name == 'METRIC' and strstr( $attrs[NAME], 'TOGA' ) ) {
[103]216
[104]217                        if( strstr( $attrs[NAME], 'TOGA-HEARTBEAT' ) ) {
[103]218
[104]219                                $heartbeat['time'] = $attrs[VAL];
[106]220                                //printf( "heartbeat %s\n", $heartbeat['time'] );
[104]221
222                        } else if( strstr( $attrs[NAME], 'TOGA-JOB' ) ) {
223
224                                sscanf( $attrs[NAME], 'TOGA-JOB-%d', $jobid );
225
[106]226                                //printf( "jobid %s\n", $jobid );
[104]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
[106]239                                        //printf( "\t%s\t= %s\n", $toganame, $togavalue );
[104]240
[103]241                                        if( $toganame == 'nodes' ) {
242
[106]243                                                if( !isset( $jobs[$jobid][$toganame] ) )
[105]244                                                        $jobs[$jobid][$toganame] = array();
[104]245
[103]246                                                $nodes = explode( ';', $togavalue );
247
248                                                foreach( $nodes as $node ) {
249
[105]250                                                        $hostname = $node.'.'.$jobs[$jobid][domain];
251                                                        $jobs[$jobid][$toganame][] = $hostname;
[106]252                                                        $this->gotNode( $hostname, null, $jobid );
[110]253                                                        //printf( "got job %s on node %s", $jobid, $hostname );
[103]254                                                }
255
[104]256                                        } else {
[103]257
[105]258                                                $jobs[$jobid][$toganame] = $togavalue;
[104]259                                        }
[103]260                                }
261                        }
262                }
263        }
264
265        function stopElement( $parser, $name ) {
266        }
[105]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        }
[106]304
305        function getNodes() {
306                return $this->nodes;
307        }
308
309        function getJobs() {
310                return $this->jobs;
311        }
[103]312}
313
314class Node {
315
[105]316        var $img, $hostname, $location, $jobs;
[106]317        var $x, $y;
[103]318
[104]319        function Node( $hostname ) {
[103]320
[104]321                $this->hostname = $hostname;
[107]322                //$this->img = new NodeImg();
[105]323                $this->jobs = array();
[103]324        }
[104]325
[105]326        function addJob( $jobid ) {
327                $jobs = &$this->jobs;
328
329                $jobs[] = $jobid;
[110]330                //print_r( $jobs );
331                $this->jobs = $jobs;
[105]332        }
333
[104]334        function setLocation( $location ) {
335                $this->location = $location;
336        }
337
[105]338        function setHostname( $hostname ) {
339                $this->hostname = $hostname;
340        }
341
[104]342        function setCpus( $cpus ) {
343                $this->cpus = $cpus;
344        }
[105]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() {
[110]359                //print_r( $this->jobs );
[105]360                return $this->jobs;
361        }
[106]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() {
[108]374                global $metrics;
375       
[106]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        }
[103]386}
387
[107]388class NodeImage {
[102]389
[110]390        var $image, $x, $y, $hostname, $jobs;
[102]391
[110]392        function NodeImage( $image, $x, $y, $hostname, $multiproc_job ) {
[102]393
[107]394                $this->image = $image;
395                $this->x = $x;
396                $this->y = $y;
[110]397                $this->hostname = $hostname;
398                $this->multiproc_job = $multiproc_job;
[102]399        }
400
[106]401        function setCoords( $x, $y ) {
[107]402
[106]403                $this->x = $x;
404                $this->y = $y;
405        }
406
[102]407        function colorHex( $color ) {
408       
[109]409                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) );
[102]410
411                return $my_color;
412        }
413
[106]414        function setImage( $image ) {
415                $this->image = $image;
416        }
[102]417
[106]418        function setLoad( $load ) {
419                $this->load = $load;
420        }
421
[108]422        function setHostname( $hostname ) {
423                $this->hostname = $hostname;
424        }
425
[107]426        function drawNode( $load ) {
[106]427
[110]428                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
429
[107]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 );
[106]433                        return;
434                }
435
[110]436                $black_color = imageColorAllocate( &$this->image, 0, 0, 0 );
[106]437
[103]438                // Convert Ganglias Hexadecimal load color to a Decimal one
[102]439                $my_loadcolor = $this->colorHex( load_color($load) );
440
[110]441                $size = $SMALL_CLUSTERIMAGE_NODEWIDTH;
[107]442
[110]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
[106]450                // Een job markering?
451                //imageFilledEllipse( $this->image, ($this->x+9)/2, ($this->y+9)/2, 6, 6, $jobcolor );
[102]452        }
453
[106]454        function draw() {
[102]455
[108]456                global $metrics;
457
458                $cpus = $metrics[$this->hostname][cpu_num][VAL];
[107]459                if (!$cpus) $cpus=1;
[108]460                $load_one = $metrics[$this->hostname][load_one][VAL];
[107]461                $load = ((float) $load_one)/$cpus;
[109]462                //printf( "hostname %s cpus %s load_one %s load %f\n", $this->hostname, $cpus, $load_one, $load );
[102]463
[107]464                $this->drawNode( $load );
[106]465        }
466}
467
[107]468class ClusterImage {
[106]469
470        var $dataget, $image;
471
[107]472        function ClusterImage() {
[106]473                $this->dataget = new DataGatherer();
474        }
475
476        function draw() {
[110]477
478                global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH;
479       
[107]480                $mydatag = $this->dataget;
[106]481                $mydatag->parseXML();
482
[110]483                //$max_width = 250;
484                //$node_width = 11;
[106]485
[110]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
[106]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 ) {
[107]500                        $nodes_per_row = ( (int) ($max_width/$node_width) );
[106]501                } else {
502                        $nodes_per_row = $nodes_size;
503                        $node_rows = 1;
504                }
505
506                if( $nodes_per_row < $nodes_nr ) {
[107]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 ) {
[106]511                                $node_rows++;
[107]512                                //printf( "noderows %d\n", $node_rows );
[106]513                        }
514                }
515
[107]516                //printf( "imagecreate: %dx%d", ($nodes_per_row*$node_width), ($node_rows*$node_width) );
[109]517                $image = imageCreateTrueColor( ($nodes_per_row*$node_width)+1, ($node_rows*$node_width)+1 );
[107]518                $colorwhite = imageColorAllocate( $image, 255, 255, 255 );
519                imageFill( $image, 0, 0, $colorwhite );
[106]520
521                for( $n = 0; $n < $node_rows; $n++ ) {
522                       
523                        for( $m = 0; $m < $nodes_per_row; $m++ ) {
[107]524                       
525                                $x = ($m * $node_width);
526                                $y = ($n * $node_width);
527
[109]528                                $cur_node = ($n * $nodes_per_row) + ($m);
[106]529                                $host = $node_keys[$cur_node];
[107]530
[109]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 );
[110]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 );
[107]553                                        $node->draw();
554                                        //$nodes[$host]->setCoords( $x, $y );
555                                        //$nodes[$host]->setImage( &$image );
556                                        //$nodes[$host]->draw();
557                                        //$cur_node++;
[106]558                                }
559                        }
560                }
561               
[103]562                header( 'Content-type: image/png' );
[106]563                imagePNG( $image );
564                imageDestroy( $image );
[102]565        }
566}
567
[106]568//$my_data = new DataGatherer();
569//$my_data->parseXML();
570//$my_data->printInfo();
571
[107]572$ic = new ClusterImage();
[106]573$ic->draw();
[102]574?>
Note: See TracBrowser for help on using the repository browser.