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

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

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

  • Added closing </FORM>

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

  • Skeleton template for base page

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

  • Added javascript and FORM to be able to sort joblist

web/addons/toga/index.php:

  • Changed to be basis for all
  • Will present a job/queue or overview based on arguments

web/addons/toga/overview.php:

  • Changed for new index skeleton
  • Will sort joblist now

web/addons/toga/libtoga.php, web/addons/toga/image.php:

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