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

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

web/templates/toga/cluster_extra.tpl:

  • Renamed link to joblist

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

  • Inclusion to archivelink template

web/addons/toga/index.php:

  • Misc cleanup
  • Setup skeleton for search

web/addons/toga/overview.php:

  • Fixed showhosts bugs

web/addons/toga/conf.php:

  • Cleanup
  • Added TARCHD option for if a archive is present

web/addons/toga/libtoga.php:

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