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

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

web/addons/toga/templates/header.tpl:

  • Changed form action to /

web/addons/toga/templates/overview.tpl:

  • Added showhosts HTML

web/addons/toga/index.php:

  • Header will now show metricmenu
  • Form changed to 1 global form

web/addons/toga/overview.php:

  • Setup for different pie graphs for filters
  • Added showhosts code for a job

web/addons/toga/libtoga.php:

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