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

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

toga/libtoga.php:

  • Code cleanup
  • Http vars fix

toga/image.php:

  • Http vars fix
File size: 11.0 KB
RevLine 
[102]1<?php
[103]2class HTTPVariables {
3
4        var $clustername, $metricname;
[112]5        var $restvars, $httpvars;
[103]6
[112]7        function HTTPVariables( $vars ) {
[103]8
[110]9                $this->restvars = array();
10
[112]11                $this->clustername = $vars["c"] ? $vars["c"] : null;
12                $this->metricname = $vars["m"] ? $vars["m"] : null;
[103]13
[112]14                foreach( $vars as $httpvar => $httpval ) {
[110]15                       
16                        if( $httpval ) {
17                                $this->restvars[$httpvar] = $httpval;
18                        }
19                }
[103]20        }
21
22        function getClusterName() {
23                return $this->clustername;
24        }
25
26        function getMetricName() {
27                return $this->metricname;
28        }
[110]29
30        function getHttpVar( $var ) {
31                if( isset( $this->restvars[$var] ) )
[112]32                        return $this->restvars[$var];
[110]33                else
34                        return null;
35        }
[103]36}
37
[112]38// Toga's conf
39//
40include_once "./conf.php";
41
42global $GANGLIA_PATH, $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH, $DATA_SOURCE;
43
44include_once "$GANGLIA_PATH/conf.php";
45include_once "$GANGLIA_PATH/functions.php";
46include_once "$GANGLIA_PATH/ganglia.php";
47
48global $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//
54global $context, $clustername;
55$clustername = $httpvars->getClusterName();
56$context = 'cluster';
57
58include_once "$GANGLIA_PATH/get_ganglia.php";
59
60// Ganglia's array of host metrics
61//
62global $metrics;
63
64// If php is compiled without globals
65//
66if ( !empty( $_GET ) ) {
67        extract( $_GET );
68}
69
[103]70class 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
[106]87                if( !$fp ) {
[103]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
103class DataGatherer {
104
[105]105        var $xmlhandler, $data, $httpvars;
[103]106
107        function DataGatherer() {
108
[110]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
[103]117                $this->parser = xml_parser_create();
[112]118                $this->httpvars = $httpvars;
[103]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
[112]128                if ( !xml_parse( &$this->parser, $this->data ) )
[103]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
[105]132        function printInfo() {
133                $handler = $this->xmlhandler;
134                $handler->printInfo();
135        }
136
[106]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
[103]147}
148
149class TorqueXMLHandler {
150
[105]151        var $clusters, $heartbeat, $nodes, $jobs;
[104]152
[103]153        function TorqueXMLHandler() {
[105]154                $jobs = array();
[104]155                $clusters = array();
[105]156                $nodes = array();
[104]157                $heartbeat = array();
[103]158        }
159
160        function startElement( $parser, $name, $attrs ) {
161
[105]162                $jobs = &$this->jobs;
[111]163                $nodes = &$this->nodes;
[105]164
[103]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
[104]174                // printf( '%s=%s', $attrs[NAME], $attrs[VAL] );
[103]175
[104]176                if( $name == 'CLUSTER' ) {
[103]177
[104]178                        $clustername = $attrs[VAL];
[103]179
[104]180                        if( !isset( $clusters[$clustername] ) )
181                                $clusters[$clustername] = array();
[103]182
[105]183                } else if( $name == 'HOST' ) {
184
185                        $hostname = $attrs[NAME];
186                        $location = $attrs[LOCATION];
187
[111]188                        if( !isset( $this->nodes[$hostname] ) )
189                                $this->nodes[$hostname] = new NodeImage( $hostname );
[105]190
[104]191                } else if( $name == 'METRIC' and strstr( $attrs[NAME], 'TOGA' ) ) {
[103]192
[104]193                        if( strstr( $attrs[NAME], 'TOGA-HEARTBEAT' ) ) {
[103]194
[104]195                                $heartbeat['time'] = $attrs[VAL];
[106]196                                //printf( "heartbeat %s\n", $heartbeat['time'] );
[104]197
198                        } else if( strstr( $attrs[NAME], 'TOGA-JOB' ) ) {
199
200                                sscanf( $attrs[NAME], 'TOGA-JOB-%d', $jobid );
201
[106]202                                //printf( "jobid %s\n", $jobid );
[104]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
[106]215                                        //printf( "\t%s\t= %s\n", $toganame, $togavalue );
[104]216
[103]217                                        if( $toganame == 'nodes' ) {
218
[106]219                                                if( !isset( $jobs[$jobid][$toganame] ) )
[105]220                                                        $jobs[$jobid][$toganame] = array();
[104]221
[111]222                                                $mynodes = explode( ';', $togavalue );
[103]223
[112]224                                                foreach( $mynodes as $node )
[103]225
[111]226                                                        $jobs[$jobid][$toganame][] = $node;
[104]227                                        } else {
[103]228
[105]229                                                $jobs[$jobid][$toganame] = $togavalue;
[104]230                                        }
[103]231                                }
[111]232
233                                if( isset( $jobs[$jobid][domain] ) and isset( $jobs[$jobid][nodes] ) ) {
[112]234                       
235                                        $nr_nodes = count( $jobs[$jobid][nodes] );
236                       
[111]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                                }
[103]256                        }
257                }
258        }
259
260        function stopElement( $parser, $name ) {
261        }
[105]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        }
[106]299
300        function getNodes() {
301                return $this->nodes;
302        }
303
304        function getJobs() {
305                return $this->jobs;
306        }
[103]307}
308
[107]309class NodeImage {
[102]310
[111]311        var $image, $x, $y, $hostname, $jobs, $tasks;
[102]312
[111]313        function NodeImage( $hostname ) {
[102]314
[111]315                $this->jobs = array();
316                //$this->image = $image;
317                //$this->x = $x;
318                //$this->y = $y;
319                $this->tasks = 0;
[110]320                $this->hostname = $hostname;
[111]321                $this->cpus = $this->determineCpus();
[102]322        }
323
[111]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
[106]356        function setCoords( $x, $y ) {
[107]357
[106]358                $this->x = $x;
359                $this->y = $y;
360        }
361
[102]362        function colorHex( $color ) {
363       
[109]364                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) );
[102]365
366                return $my_color;
367        }
368
[106]369        function setLoad( $load ) {
370                $this->load = $load;
371        }
372
[108]373        function setHostname( $hostname ) {
374                $this->hostname = $hostname;
375        }
376
[111]377        function draw() {
[106]378
[111]379                global $SMALL_CLUSTERIMAGE_NODEWIDTH, $JOB_NODE_MARKING_ALLCPUS, $JOB_NODE_MARKING_SINGLECPU;
[110]380
[111]381                $this->load = $this->determineLoad();
382
[112]383                if( !isset( $this->image ) or !isset( $this->x ) or !isset( $this->y ) ) {
[107]384                        printf( "aborting\n" );
385                        printf( "x %d y %d load %f\n", $this->x, $this->y, $load );
[106]386                        return;
387                }
388
[111]389                $black_color = imageColorAllocate( $this->image, 0, 0, 0 );
[106]390
[103]391                // Convert Ganglias Hexadecimal load color to a Decimal one
[111]392                //
393                $load = $this->determineLoad(); 
[102]394                $my_loadcolor = $this->colorHex( load_color($load) );
395
[110]396                $size = $SMALL_CLUSTERIMAGE_NODEWIDTH;
[107]397
[110]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
[111]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 );
[102]415        }
416
[111]417        function determineCpus() {
[102]418
[108]419                global $metrics;
420
421                $cpus = $metrics[$this->hostname][cpu_num][VAL];
[107]422                if (!$cpus) $cpus=1;
[111]423
424                return $cpus;
425        }
426
427        function determineLoad() {
428
429                global $metrics;
430
[108]431                $load_one = $metrics[$this->hostname][load_one][VAL];
[111]432                $load = ((float) $load_one)/$this->cpus;
[102]433
[111]434                return $load;
[106]435        }
436}
437
[107]438class ClusterImage {
[106]439
[111]440        var $dataget, $image, $clustername;
[106]441
[111]442        function ClusterImage( $clustername ) {
[106]443                $this->dataget = new DataGatherer();
[111]444                $this->clustername = $clustername;
[106]445        }
446
447        function draw() {
[110]448
449                global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH;
450       
[107]451                $mydatag = $this->dataget;
[106]452                $mydatag->parseXML();
453
[110]454                //$max_width = 250;
455                //$node_width = 11;
[106]456
[110]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
[106]462                $nodes = $mydatag->getNodes();
[111]463                $nodes_hosts = array_keys( $nodes );
[106]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 ) {
[107]471                        $nodes_per_row = ( (int) ($max_width/$node_width) );
[106]472                } else {
473                        $nodes_per_row = $nodes_size;
474                        $node_rows = 1;
475                }
476
477                if( $nodes_per_row < $nodes_nr ) {
[107]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 ) {
[106]482                                $node_rows++;
[107]483                                //printf( "noderows %d\n", $node_rows );
[106]484                        }
485                }
486
[107]487                //printf( "imagecreate: %dx%d", ($nodes_per_row*$node_width), ($node_rows*$node_width) );
[109]488                $image = imageCreateTrueColor( ($nodes_per_row*$node_width)+1, ($node_rows*$node_width)+1 );
[107]489                $colorwhite = imageColorAllocate( $image, 255, 255, 255 );
490                imageFill( $image, 0, 0, $colorwhite );
[106]491
492                for( $n = 0; $n < $node_rows; $n++ ) {
493                       
494                        for( $m = 0; $m < $nodes_per_row; $m++ ) {
[107]495                       
496                                $x = ($m * $node_width);
497                                $y = ($n * $node_width);
498
[109]499                                $cur_node = ($n * $nodes_per_row) + ($m);
[111]500                                $host = $nodes_hosts[$cur_node];
[107]501
[111]502                                if( isset( $nodes[$host] ) ) {
503
504                                        $nodes[$host]->setCoords( $x, $y );
505                                        $nodes[$host]->setImage( $image );
506                                        $nodes[$host]->draw();
507                                }
[106]508                        }
509                }
510               
[103]511                header( 'Content-type: image/png' );
[106]512                imagePNG( $image );
513                imageDestroy( $image );
[102]514        }
515}
516
[106]517//$my_data = new DataGatherer();
518//$my_data->parseXML();
519//$my_data->printInfo();
520
[112]521//$ic = new ClusterImage( "LISA Cluster" );
522//$ic->draw();
[102]523?>
Note: See TracBrowser for help on using the repository browser.