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