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
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
62$context = 'cluster';
63
64include_once "./conf.php";
65include_once "./functions.php";
66include_once "./ganglia.php";
67//include_once "./get_context.php";
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
76// Set cluster context so that Ganglia will
77// provide us with the correct metrics array
78//
79global $context, $clustername, $reports;
80
81//$clustername = $httpvars->getClusterName();
82
83global $default_metric;
84
85// Ganglia's array of host metrics
86//
87global $metrics, $hosts_up;
88
89
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
107                if( !$fp ) {
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
125        var $xmlhandler, $data, $httpvars;
126
127        function DataGatherer() {
128
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
137                $this->parser = xml_parser_create();
138                $this->httpvars = $httpvars;
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
148                if ( !xml_parse( &$this->parser, $this->data ) )
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
152        function printInfo() {
153                $handler = $this->xmlhandler;
154                $handler->printInfo();
155        }
156
157        function getNodes() {
158                $handler = $this->xmlhandler;
159                return $handler->getNodes();
160        }
161
162        function getCpus() {
163                $handler = $this->xmlhandler;
164                return $handler->getCpus();
165        }
166
167        function getJobs() {
168                $handler = $this->xmlhandler;
169                return $handler->getJobs();
170        }
171
172        function getHeartbeat() {
173                $handler = $this->xmlhandler;
174                return $handler->getHeartbeat();
175        }
176}
177
178class TorqueXMLHandler {
179
180        var $clusters, $heartbeat, $nodes, $jobs;
181
182        function TorqueXMLHandler() {
183                $jobs = array();
184                $clusters = array();
185                $nodes = array();
186                $heartbeat = array();
187        }
188
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
203        function startElement( $parser, $name, $attrs ) {
204
205                $jobs = &$this->jobs;
206                $nodes = &$this->nodes;
207
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
217                // printf( '%s=%s', $attrs[NAME], $attrs[VAL] );
218
219                if( $name == 'CLUSTER' ) {
220
221                        $clustername = $attrs[VAL];
222
223                        if( !isset( $clusters[$clustername] ) )
224                                $clusters[$clustername] = array();
225
226                } else if( $name == 'HOST' ) {
227
228                        $hostname = $attrs[NAME];
229                        $location = $attrs[LOCATION];
230
231                        if( !isset( $this->nodes[$hostname] ) )
232                                $this->nodes[$hostname] = new NodeImage( $hostname );
233
234                } else if( $name == 'METRIC' and strstr( $attrs[NAME], 'TOGA' ) ) {
235
236                        if( strstr( $attrs[NAME], 'TOGA-HEARTBEAT' ) ) {
237
238                                $this->heartbeat['time'] = $attrs[VAL];
239                                //printf( "heartbeat %s\n", $heartbeat['time'] );
240
241                        } else if( strstr( $attrs[NAME], 'TOGA-JOB' ) ) {
242
243                                sscanf( $attrs[NAME], 'TOGA-JOB-%d', $jobid );
244
245                                //printf( "jobid %s\n", $jobid );
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
258                                        //printf( "\t%s\t= %s\n", $toganame, $togavalue );
259
260                                        if( $toganame == 'nodes' ) {
261
262                                                if( !isset( $jobs[$jobid][$toganame] ) )
263                                                        $jobs[$jobid][$toganame] = array();
264
265                                                $mynodes = explode( ';', $togavalue );
266
267                                                foreach( $mynodes as $node )
268
269                                                        $jobs[$jobid][$toganame][] = $node;
270                                        } else {
271
272                                                $jobs[$jobid][$toganame] = $togavalue;
273                                        }
274                                }
275
276                                if( isset( $jobs[$jobid][domain] ) and isset( $jobs[$jobid][nodes] ) ) {
277                       
278                                        $nr_nodes = count( $jobs[$jobid][nodes] );
279                       
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                                }
299                        }
300                }
301                $this->jobs = $jobs;
302        }
303
304        function stopElement( $parser, $name ) {
305        }
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        }
343
344        function getNodes() {
345                return $this->nodes;
346        }
347
348        function getJobs() {
349                return $this->jobs;
350        }
351
352        function getHeartbeat() {
353                return $this->heartbeat['time'];
354        }
355}
356
357class NodeImage {
358
359        var $image, $x, $y, $hostname, $jobs, $tasks, $showinfo;
360
361        function NodeImage( $hostname ) {
362
363                $this->jobs = array();
364                //$this->image = $image;
365                //$this->x = $x;
366                //$this->y = $y;
367                $this->tasks = 0;
368                $this->hostname = $hostname;
369                $this->cpus = $this->determineCpus();
370                $this->showinfo = 1;
371        }
372
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
405        function setCoords( $x, $y ) {
406
407                $this->x = $x;
408                $this->y = $y;
409        }
410
411        function colorHex( $color ) {
412       
413                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) );
414
415                return $my_color;
416        }
417
418        function setLoad( $load ) {
419                $this->load = $load;
420        }
421
422        function setHostname( $hostname ) {
423                $this->hostname = $hostname;
424        }
425
426        function getHostname() {
427                return $this->hostname;
428        }
429
430        function getJobs() {
431                return $this->jobs;
432        }
433
434        function setShowinfo( $showinfo ) {
435                $this->showinfo = $showinfo;
436        }
437
438        function draw() {
439
440                $this->drawSmall();
441        }
442
443        function drawBig() {
444
445        }
446
447        function drawSmall() {
448
449                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
450                global $JOB_NODE_MARKING;
451
452                $black_color = imageColorAllocate( $this->image, 0, 0, 0 );
453                $size = $SMALL_CLUSTERIMAGE_NODEWIDTH;
454
455                imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+($size), $this->y+($size), $black_color );
456
457                if( $this->showinfo) {
458               
459                        $this->load = $this->determineLoad();
460
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                        }
466
467
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 );
475
476                } else {
477
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
484        }
485
486        function determineCpus() {
487
488                global $metrics;
489
490                $cpus = $metrics[$this->hostname][cpu_num][VAL];
491                if (!$cpus) $cpus=1;
492
493                return $cpus;
494        }
495
496        function determineLoad() {
497
498                global $metrics;
499
500                $load_one = $metrics[$this->hostname][load_one][VAL];
501                $load = ((float) $load_one)/$this->cpus;
502
503                return $load;
504        }
505}
506
507class ClusterImage {
508
509        var $dataget, $image, $clustername;
510        var $filtername, $filters;
511
512        function ClusterImage( $clustername ) {
513
514                $this->dataget = new DataGatherer();
515                $this->clustername = $clustername;
516                $this->filters = array();
517        }
518
519        function setFilter( $filtername, $filtervalue ) {
520
521                //printf("filter %s = %s\n", $filtername, $filtervalue );
522                //printf( "filter set to %s = %s\n", $filtername, $filtervalue );
523                $this->filters[$filtername] = $filtervalue;
524                //print_r($this->filters);
525        }
526
527        function filterNodes( $jobs, $nodes ) {
528
529                $filtered_nodes = array();
530
531                foreach( $nodes as $node ) {
532
533                        $hostname = $node->getHostname();
534
535                        $addhost = 1;
536
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 )
569                                $filtered_nodes[] = $hostname;
570                }
571
572                return $filtered_nodes;
573        }
574
575        function draw() {
576
577                //printf("stopt met uitvoer");
578                //return;
579
580                global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH;
581       
582                $mydatag = $this->dataget;
583                $mydatag->parseXML();
584
585                //$max_width = 250;
586                //$node_width = 11;
587
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
593                $nodes = $mydatag->getNodes();
594                $nodes_hosts = array_keys( $nodes );
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 ) {
602                        $nodes_per_row = ( (int) ($max_width/$node_width) );
603                } else {
604                        $nodes_per_row = $nodes_size;
605                        $node_rows = 1;
606                }
607
608                if( $nodes_per_row < $nodes_nr ) {
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 ) {
613                                $node_rows++;
614                                //printf( "noderows %d\n", $node_rows );
615                        }
616                }
617
618                //printf( "imagecreate: %dx%d", ($nodes_per_row*$node_width), ($node_rows*$node_width) );
619                $image = imageCreateTrueColor( ($nodes_per_row*$node_width)+1, ($node_rows*$node_width)+1 );
620                $colorwhite = imageColorAllocate( $image, 255, 255, 255 );
621                imageFill( $image, 0, 0, $colorwhite );
622
623                $jobs = $mydatag->getJobs();
624                //printf("filtername = %s\n", $filtername );
625                $filtered_nodes = $this->filterNodes( $jobs, $nodes );
626
627                //print_r($filtered_nodes);
628
629                for( $n = 0; $n < $node_rows; $n++ ) {
630                       
631                        for( $m = 0; $m < $nodes_per_row; $m++ ) {
632                       
633                                $x = ($m * $node_width);
634                                $y = ($n * $node_width);
635
636                                $cur_node = ($n * $nodes_per_row) + ($m);
637                                $host = $nodes_hosts[$cur_node];
638
639
640                                if( isset( $nodes[$host] ) ) {
641
642                                        $nodes[$host]->setCoords( $x, $y );
643                                        $nodes[$host]->setImage( $image );
644
645                                        if( !in_array( $host, $filtered_nodes ) )
646                                                $nodes[$host]->setShowinfo( 0 );
647
648                                        $nodes[$host]->draw();
649                                }
650                        }
651                }
652               
653                header( 'Content-type: image/png' );
654                imagePNG( $image );
655                imageDestroy( $image );
656        }
657}
658
659//$my_data = new DataGatherer();
660//$my_data->parseXML();
661//$my_data->printInfo();
662
663//$ic = new ClusterImage( "LISA Cluster" );
664//$ic->draw();
665?>
Note: See TracBrowser for help on using the repository browser.