Changeset 106 for trunk/web/addons/toga


Ignore:
Timestamp:
05/17/05 17:01:38 (19 years ago)
Author:
bastiaans
Message:

toga/toga-functions.php:


  • Weird - In browser it says:

"Image can not be shown, because it contains errors"

  • Will investigate more
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/web/addons/toga/toga-functions.php

    r105 r106  
    1 <PRE>
    21<?php
    32$GANGLIA_PATH = "/var/www/ganglia";
     
    4847                $fp = fsockopen( $this->ip, $this->port, &$errno, &$errstr, $timeout );
    4948
    50                 if( ! $fp ) {
     49                if( !$fp ) {
    5150                        echo 'Unable to connect to '.$this->ip.':'.$this->port; // printf( 'Unable to connect to [%s:%.0f]', $this->ip, $this->port );
    5251                        return;
     
    9392        }
    9493
     94        function getNodes() {
     95                $handler = $this->xmlhandler;
     96                return $handler->getNodes();
     97        }
     98
     99        function getJobs() {
     100                $handler = $this->xmlhandler;
     101                return $handler->getJobs();
     102        }
     103
    95104}
    96105
     
    106115        }
    107116
    108         function gotNode( $hostname, $jobid = null, $location = 'unspecified' ) {
     117        function gotNode( $hostname, $location = 'unspecified', $jobid = null ) {
    109118
    110119                $nodes = &$this->nodes;
     
    115124                }
    116125
    117                 if( $location != 'unspecified' ) {
     126                if( $location ) {
    118127
    119128                        $nodes[$hostname]->setLocation( $location );
    120                 //} else {
    121 
    122                         // Return pointer to this node if location was not set
    123                         // Which means that this is for a jobinfo
    124                         //$mynode = &$nodes[$hostname];
    125                         //return $mynode;
    126129                }
    127130
     
    158161                        $location = $attrs[LOCATION];
    159162
    160                         $this->gotNode( $hostname, null,$location );
     163                        $this->gotNode( $hostname, $location, null );
    161164
    162165                } else if( $name == 'METRIC' and strstr( $attrs[NAME], 'TOGA' ) ) {
     
    165168
    166169                                $heartbeat['time'] = $attrs[VAL];
    167                                 printf( "heartbeat %s\n", $heartbeat['time'] );
     170                                //printf( "heartbeat %s\n", $heartbeat['time'] );
    168171
    169172                        } else if( strstr( $attrs[NAME], 'TOGA-JOB' ) ) {
     
    171174                                sscanf( $attrs[NAME], 'TOGA-JOB-%d', $jobid );
    172175
    173                                 printf( "jobid %s\n", $jobid );
     176                                //printf( "jobid %s\n", $jobid );
    174177
    175178                                if( !isset( $jobs[$jobid] ) )
     
    184187                                        $togavalue = $togavalues[1];
    185188
    186                                         printf( "\t%s\t= %s\n", $toganame, $togavalue );
     189                                        //printf( "\t%s\t= %s\n", $toganame, $togavalue );
    187190
    188191                                        if( $toganame == 'nodes' ) {
    189192
    190                                                 if( !isset( $jobs[$toganame] ) )
     193                                                if( !isset( $jobs[$jobid][$toganame] ) )
    191194                                                        $jobs[$jobid][$toganame] = array();
    192195
     
    194197
    195198                                                foreach( $nodes as $node ) {
    196 
    197                                                         // printf( "node %s\n", $node );
    198199
    199200                                                        $hostname = $node.'.'.$jobs[$jobid][domain];
    200201                                                        $jobs[$jobid][$toganame][] = $hostname;
    201                                                         $this->gotNode( $hostname, $jobid );
    202 
    203                                                         //$jobs[$jobid][$toganame][$hostname] = $this->gotNode( $hostname );
    204                                                         // $jobs[$toganame][$node] = new Node( $node );
     202                                                        $this->gotNode( $hostname, null, $jobid );
    205203                                                }
    206204
     
    208206
    209207                                                $jobs[$jobid][$toganame] = $togavalue;
    210 
    211208                                        }
    212209                                }
     
    254251                }
    255252        }
     253
     254        function getNodes() {
     255                return $this->nodes;
     256        }
     257
     258        function getJobs() {
     259                return $this->jobs;
     260        }
    256261}
    257262
     
    259264
    260265        var $img, $hostname, $location, $jobs;
     266        var $x, $y;
    261267
    262268        function Node( $hostname ) {
     
    300306                return $this->jobs;
    301307        }
     308
     309        function setCoords( $x, $y ) {
     310                $myimg = $this->img;
     311                $myimg->setCoords( $x, $y );
     312        }
     313
     314        function setImage( $image ) {
     315                $myimg = $this->img;
     316                $myimg->setImage( &$image );
     317        }
     318
     319        function draw() {
     320                $myimg = $this->img;
     321
     322                $cpus = $metrics[$this->hostname]["cpu_num"][VAL];
     323                if (!$cpus) $cpus=1;
     324                $load_one = $metrics[$this->hostname]["load_one"][VAL];
     325                $load = ((float) $load_one)/$cpus;
     326
     327                $myimg->setLoad( $load );
     328                $myimg->draw();
     329        }
    302330}
    303331
    304332class NodeImg {
    305333
    306         var $image;
     334        var $image, $x, $y;
    307335
    308336        function NodeImg( $image = null ) {
     
    321349                $black_color = imageColorAllocate( $this->image, 0, 0, 0 );
    322350                imageRectangle( $this->image, 0, 0, $imageWidth-1, $imageHeight-1, $black_color );
     351                $this->x = null;
     352                $this->y = null;
     353        }
     354
     355        function setCoords( $x, $y ) {
     356                $this->x = $x;
     357                $this->y = $y;
    323358        }
    324359
     
    330365        }
    331366
    332         function drawNode( $x, $y, &$queuecolor, $load, &$jobcolor ) {
     367        function setImage( $image ) {
     368                $this->image = $image;
     369        }
     370
     371        function setLoad( $load ) {
     372                $this->load = $load;
     373        }
     374
     375        function drawNode( &$queuecolor, $load, &$jobcolor ) {
     376
     377                if( !$this->x or !$this->y or !$this->load ) {
     378                        return;
     379                }
     380
    333381
    334382                // Convert Ganglias Hexadecimal load color to a Decimal one
    335383                $my_loadcolor = $this->colorHex( load_color($load) );
    336384
    337                 imageFilledRectangle( $this->image, $x, $y, $x+12, $y+12, $queuecolor );
    338                 imageFilledRectangle( $this->image, $x+2, $y+2, $x+10, $y+10, $my_loadcolor );
    339                 //imageFilledEllipse( $this->image, ($x+9)/2, ($y+9)/2, 6, 6, $jobcolor );
    340         }
    341 
    342         function drawImage() {
     385                imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+12, $this->y+12, $queuecolor );
     386                imageFilledRectangle( $this->image, $this->x+2, $this->y+2, $this->x+10, $this->y+10, $my_loadcolor );
     387                // Een job markering?
     388                //imageFilledEllipse( $this->image, ($this->x+9)/2, ($this->y+9)/2, 6, 6, $jobcolor );
     389        }
     390
     391        function draw() {
    343392
    344393                $queue_color = imageColorAllocate( $this->image, 0, 102, 304 );
    345394                $job_color = imageColorAllocate( $this->image, 204, 204, 0 );
    346395
    347                 $this->drawNode( 1, 1, $queue_color, 0.1, $job_color );
     396                $this->drawNode( $queue_color, 0.1, $job_color );
     397        }
     398}
     399
     400class ImageCreator {
     401
     402        var $dataget, $image;
     403
     404        function ImageCreator() {
     405                $this->dataget = new DataGatherer();
     406        }
     407
     408        function draw() {
     409                $mydatag = &$this->dataget;
     410                $mydatag->parseXML();
     411
     412                $max_width = 150;
     413                $node_width = 12;
     414
     415                $nodes = $mydatag->getNodes();
     416
     417                $nodes_nr = count( $nodes );
     418                $node_keys = array_keys( $nodes );
     419
     420                $nodes_size = $nodes_nr*$node_width;
     421                $node_rows = 0;
     422
     423                if( $nodes_size > $max_width ) {
     424                        $nodes_per_row = (int) $max_width/$node_width;
     425                } else {
     426                        $nodes_per_row = $nodes_size;
     427                        $node_rows = 1;
     428                }
     429
     430                if( $nodes_per_row < $nodes_nr ) {
     431                        $node_rows = (int) $nodes_nr/$nodes_per_row;
     432                        if( ((int) fmod( $node_nr, ($nodes_nr*$nodes_per_row) )) > 0 ) {
     433                                $node_rows++;
     434                        }
     435                }
     436
     437                $image = imageCreate( ($nodes_per_row*$node_width), ($node_rows*$node_width) );
     438
     439                $cur_node = 0;
     440
     441                for( $n = 0; $n < $node_rows; $n++ ) {
     442                       
     443                        for( $m = 0; $m < $nodes_per_row; $m++ ) {
     444                               
     445                                $x = ($m*$node_width);
     446                                $y = ($m*$node_width)+($n*$node_width);
     447                                $host = $node_keys[$cur_node];
     448                                //printf( "cur_node %d, host %s\n", $cur_node, $host );
     449                                if( isset( $nodes[$host] ) and ($cur_node < $nodes_nr) ) {
     450                                        $nodes[$host]->setCoords( $x, $y );
     451                                        $nodes[$host]->setImage( &$image );
     452                                        $nodes[$host]->draw();
     453                                        $cur_node++;
     454                                }
     455                        }
     456                }
     457               
    348458                header( 'Content-type: image/png' );
    349                 imagePNG( $this->image );
    350                 imageDestroy( $this->image );
     459                imagePNG( $image );
     460                imageDestroy( $image );
    351461        }
    352462}
     
    355465//$my_node->drawImage();
    356466
    357 $my_data = new DataGatherer();
    358 $my_data->parseXML();
    359 $my_data->printInfo();
    360 $my_data->printInfo();
     467//$my_data = new DataGatherer();
     468//$my_data->parseXML();
     469//$my_data->printInfo();
     470
     471$ic = new ImageCreator();
     472$ic->draw();
    361473?>
    362 </PRE>
Note: See TracChangeset for help on using the changeset viewer.