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

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

toga/conf.php, toga/libtoga.php:

  • Will now use a 'J' marking for all nodes with a running job Not depending on number of cpus in use anymore

toga/overview.php:

  • Fixed makeTime()
File size: 11.4 KB
Line 
1<?php
2class HTTPVariables {
3
4        var $clustername, $metricname;
5        var $restvars, $httpvars;
6
7        function HTTPVariables( $vars ) {
8
9                $this->restvars = array();
10
11                $this->clustername = $vars["c"] ? $vars["c"] : null;
12                $this->metricname = $vars["m"] ? $vars["m"] : null;
13
14                foreach( $vars as $httpvar => $httpval ) {
15                       
16                        if( $httpval ) {
17                                $this->restvars[$httpvar] = $httpval;
18                        }
19                }
20        }
21
22        function getClusterName() {
23                return $this->clustername;
24        }
25
26        function getMetricName() {
27                return $this->metricname;
28        }
29
30        function getHttpVar( $var ) {
31                if( isset( $this->restvars[$var] ) )
32                        return $this->restvars[$var];
33                else
34                        return null;
35        }
36}
37
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
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
87                if( !$fp ) {
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        var $xmlhandler, $data, $httpvars;
106
107        function DataGatherer() {
108
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
117                $this->parser = xml_parser_create();
118                $this->httpvars = $httpvars;
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
128                if ( !xml_parse( &$this->parser, $this->data ) )
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
132        function printInfo() {
133                $handler = $this->xmlhandler;
134                $handler->printInfo();
135        }
136
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
147        function getHeartbeat() {
148                $handler = $this->xmlhandler;
149                return $handler->getHeartbeat();
150        }
151}
152
153class TorqueXMLHandler {
154
155        var $clusters, $heartbeat, $nodes, $jobs;
156
157        function TorqueXMLHandler() {
158                $jobs = array();
159                $clusters = array();
160                $nodes = array();
161                $heartbeat = array();
162        }
163
164        function startElement( $parser, $name, $attrs ) {
165
166                $jobs = &$this->jobs;
167                $nodes = &$this->nodes;
168
169                if ( $attrs[TN] ) {
170
171                        // Ignore dead metrics. Detect and mask failures.
172                        if ( $attrs[TN] > $attrs[TMAX] * 4 )
173                                return;
174                }
175
176                $jobid = null;
177
178                // printf( '%s=%s', $attrs[NAME], $attrs[VAL] );
179
180                if( $name == 'CLUSTER' ) {
181
182                        $clustername = $attrs[VAL];
183
184                        if( !isset( $clusters[$clustername] ) )
185                                $clusters[$clustername] = array();
186
187                } else if( $name == 'HOST' ) {
188
189                        $hostname = $attrs[NAME];
190                        $location = $attrs[LOCATION];
191
192                        if( !isset( $this->nodes[$hostname] ) )
193                                $this->nodes[$hostname] = new NodeImage( $hostname );
194
195                } else if( $name == 'METRIC' and strstr( $attrs[NAME], 'TOGA' ) ) {
196
197                        if( strstr( $attrs[NAME], 'TOGA-HEARTBEAT' ) ) {
198
199                                $this->heartbeat['time'] = $attrs[VAL];
200                                //printf( "heartbeat %s\n", $heartbeat['time'] );
201
202                        } else if( strstr( $attrs[NAME], 'TOGA-JOB' ) ) {
203
204                                sscanf( $attrs[NAME], 'TOGA-JOB-%d', $jobid );
205
206                                //printf( "jobid %s\n", $jobid );
207
208                                if( !isset( $jobs[$jobid] ) )
209                                        $jobs[$jobid] = array();
210
211                                $fields = explode( ' ', $attrs[VAL] );
212
213                                foreach( $fields as $f ) {
214                                        $togavalues = explode( '=', $f );
215
216                                        $toganame = $togavalues[0];
217                                        $togavalue = $togavalues[1];
218
219                                        //printf( "\t%s\t= %s\n", $toganame, $togavalue );
220
221                                        if( $toganame == 'nodes' ) {
222
223                                                if( !isset( $jobs[$jobid][$toganame] ) )
224                                                        $jobs[$jobid][$toganame] = array();
225
226                                                $mynodes = explode( ';', $togavalue );
227
228                                                foreach( $mynodes as $node )
229
230                                                        $jobs[$jobid][$toganame][] = $node;
231                                        } else {
232
233                                                $jobs[$jobid][$toganame] = $togavalue;
234                                        }
235                                }
236
237                                if( isset( $jobs[$jobid][domain] ) and isset( $jobs[$jobid][nodes] ) ) {
238                       
239                                        $nr_nodes = count( $jobs[$jobid][nodes] );
240                       
241                                        foreach( $jobs[$jobid][nodes] as $node ) {
242
243                                                $host = $node.'.'.$jobs[$jobid][domain];
244                               
245                                                if( !isset( $this->nodes[$host] ) )
246                                                        $my_node = new NodeImage( $host );
247                                                else
248                                                        $my_node = $this->nodes[$host];
249
250                                                if( !$my_node->hasJob( $jobid ) )
251
252                                                        if( isset( $jobs[$jobid][ppn] ) )
253                                                                $my_node->addJob( $jobid, ((int) $jobs[$jobid][ppn]) );
254                                                        else
255                                                                $my_node->addJob( $jobid, 1 );
256
257                                                $this->nodes[$host] = $my_node;
258                                        }
259                                }
260                        }
261                }
262                $this->jobs = $jobs;
263        }
264
265        function stopElement( $parser, $name ) {
266        }
267
268        function printInfo() {
269
270                $jobs = &$this->jobs;
271
272                printf( "---jobs---\n" );
273
274                foreach( $jobs as $jobid => $job ) {
275
276                        printf( "job %s\n", $jobid );
277
278                        if( isset( $job[nodes] ) ) {
279
280                                foreach( $job[nodes] as $node ) {
281
282                                        $mynode = $this->nodes[$node];
283                                        $hostname = $mynode->getHostname();
284                                        $location = $mynode->getLocation();
285
286                                        printf( "\t- node %s\tlocation %s\n", $hostname, $location );
287                                        //$this->nodes[$hostname]->setLocation( "hier draait job ".$jobid );
288                                }
289                        }
290                }
291
292                printf( "---nodes---\n" );
293
294                $nodes = &$this->nodes;
295
296                foreach( $nodes as $node ) {
297
298                        $hostname = $node->getHostname();
299                        $location = $node->getLocation();
300                        $jobs = implode( ' ', $node->getJobs() );
301                        printf( "* node %s\tlocation %s\tjobs %s\n", $hostname, $location, $jobs );
302                }
303        }
304
305        function getNodes() {
306                return $this->nodes;
307        }
308
309        function getJobs() {
310                return $this->jobs;
311        }
312
313        function getHeartbeat() {
314                return $this->heartbeat['time'];
315        }
316}
317
318class NodeImage {
319
320        var $image, $x, $y, $hostname, $jobs, $tasks;
321
322        function NodeImage( $hostname ) {
323
324                $this->jobs = array();
325                //$this->image = $image;
326                //$this->x = $x;
327                //$this->y = $y;
328                $this->tasks = 0;
329                $this->hostname = $hostname;
330                $this->cpus = $this->determineCpus();
331        }
332
333        function addJob( $jobid, $cpus ) {
334                $jobs = &$this->jobs;
335
336                $jobs[] = $jobid;
337                $this->jobs = $jobs;
338
339                $this->addTask( $cpus );
340        }
341
342        function hasJob( $jobid ) {
343
344                $jobfound = 0;
345
346                if( count( $this->jobs ) > 0 )
347                        foreach( $this->jobs as $job )
348
349                                if( $job == $jobid )
350                                        $jobfound = 1;
351
352                return $jobfound;
353        }
354
355        function addTask( $cpus ) {
356
357                $this->tasks = $this->tasks + $cpus;
358        }
359
360        function setImage( $image ) {
361
362                $this->image = $image;
363        }
364
365        function setCoords( $x, $y ) {
366
367                $this->x = $x;
368                $this->y = $y;
369        }
370
371        function colorHex( $color ) {
372       
373                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) );
374
375                return $my_color;
376        }
377
378        function setLoad( $load ) {
379                $this->load = $load;
380        }
381
382        function setHostname( $hostname ) {
383                $this->hostname = $hostname;
384        }
385
386        function getJobs() {
387                return $this->jobs;
388        }
389
390        function draw() {
391
392                global $SMALL_CLUSTERIMAGE_NODEWIDTH, $JOB_NODE_MARKING_ALLCPUS, $JOB_NODE_MARKING_SINGLECPU;
393                global $JOB_NODE_MARKING;
394
395                $this->load = $this->determineLoad();
396
397                if( !isset( $this->image ) or !isset( $this->x ) or !isset( $this->y ) ) {
398                        printf( "aborting\n" );
399                        printf( "x %d y %d load %f\n", $this->x, $this->y, $load );
400                        return;
401                }
402
403                $black_color = imageColorAllocate( $this->image, 0, 0, 0 );
404
405                // Convert Ganglias Hexadecimal load color to a Decimal one
406                //
407                $load = $this->determineLoad(); 
408                $my_loadcolor = $this->colorHex( load_color($load) );
409
410                $size = $SMALL_CLUSTERIMAGE_NODEWIDTH;
411
412                imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+($size), $this->y+($size), $black_color );
413                imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $my_loadcolor );
414
415                $nr_jobs = count( $this->jobs );
416
417                $node_mark = null;
418
419                if( count( $this->jobs ) > 0 )
420                        $node_mark = $JOB_NODE_MARKING;
421
422                //      if( $this->tasks < $this->cpus )
423                //              $node_mark = $JOB_NODE_MARKING_SINGLECPU;
424
425                //      else if( $this->tasks == $this->cpus )
426                //              $node_mark = $JOB_NODE_MARKING_ALLCPUS;
427
428                if( $node_mark )
429                        imageString( $this->image, 1, $this->x+(($size/2)-2), $this->y+(($size/2)-3), $node_mark, $black_color );
430        }
431
432        function determineCpus() {
433
434                global $metrics;
435
436                $cpus = $metrics[$this->hostname][cpu_num][VAL];
437                if (!$cpus) $cpus=1;
438
439                return $cpus;
440        }
441
442        function determineLoad() {
443
444                global $metrics;
445
446                $load_one = $metrics[$this->hostname][load_one][VAL];
447                $load = ((float) $load_one)/$this->cpus;
448
449                return $load;
450        }
451}
452
453class ClusterImage {
454
455        var $dataget, $image, $clustername;
456
457        function ClusterImage( $clustername, $data_gather ) {
458
459                if( !isset( $data_gather ) )
460                        $this->dataget = new DataGatherer();
461                else
462                        $this->dataget = $data_gather;
463
464                $this->clustername = $clustername;
465        }
466
467        function draw() {
468
469                global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH;
470       
471                $mydatag = $this->dataget;
472                $mydatag->parseXML();
473
474                //$max_width = 250;
475                //$node_width = 11;
476
477                $max_width = $SMALL_CLUSTERIMAGE_MAXWIDTH;
478                $node_width = $SMALL_CLUSTERIMAGE_NODEWIDTH;
479
480                //printf( "cmaxw %s nmaxw %s", $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH );
481
482                $nodes = $mydatag->getNodes();
483                $nodes_hosts = array_keys( $nodes );
484
485                $nodes_nr = count( $nodes );
486
487                $nodes_size = $nodes_nr*$node_width;
488                $node_rows = 0;
489
490                if( $nodes_size > $max_width ) {
491                        $nodes_per_row = ( (int) ($max_width/$node_width) );
492                } else {
493                        $nodes_per_row = $nodes_size;
494                        $node_rows = 1;
495                }
496
497                if( $nodes_per_row < $nodes_nr ) {
498                        $node_rows = ( (int) ($nodes_nr/$nodes_per_row) );
499                        $node_rest = fmod( $nodes_nr, $nodes_per_row );
500                        //printf( "nodesnr %d noderest %f\n", $nodes_nr, $node_rest );
501                        if( $node_rest > 0 ) {
502                                $node_rows++;
503                                //printf( "noderows %d\n", $node_rows );
504                        }
505                }
506
507                //printf( "imagecreate: %dx%d", ($nodes_per_row*$node_width), ($node_rows*$node_width) );
508                $image = imageCreateTrueColor( ($nodes_per_row*$node_width)+1, ($node_rows*$node_width)+1 );
509                $colorwhite = imageColorAllocate( $image, 255, 255, 255 );
510                imageFill( $image, 0, 0, $colorwhite );
511
512                for( $n = 0; $n < $node_rows; $n++ ) {
513                       
514                        for( $m = 0; $m < $nodes_per_row; $m++ ) {
515                       
516                                $x = ($m * $node_width);
517                                $y = ($n * $node_width);
518
519                                $cur_node = ($n * $nodes_per_row) + ($m);
520                                $host = $nodes_hosts[$cur_node];
521
522                                if( isset( $nodes[$host] ) ) {
523
524                                        $nodes[$host]->setCoords( $x, $y );
525                                        $nodes[$host]->setImage( $image );
526                                        $nodes[$host]->draw();
527                                }
528                        }
529                }
530               
531                header( 'Content-type: image/png' );
532                imagePNG( $image );
533                imageDestroy( $image );
534        }
535}
536
537//$my_data = new DataGatherer();
538//$my_data->parseXML();
539//$my_data->printInfo();
540
541//$ic = new ClusterImage( "LISA Cluster" );
542//$ic->draw();
543?>
Note: See TracBrowser for help on using the repository browser.