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

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

TODO,AUTHORS:

  • First files for future open source release

ganglia_example.xml:

  • No longer needed

web/addons/toga/version.php:

  • Added a version.php for joblist

web/addons/toga/libtoga.php:

  • Included version.php

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

  • Removed bogus/non-sence version stuff

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

  • Minor cosmetic layout fix

web/addons/toga/index.php:

  • Changed versioning variables

web/addons/toga/overview.php:

  • Fixed hostname/domain determination. Only add domain if not allready there.
File size: 20.9 KB
Line 
1<?php
2// If php is compiled without globals
3//
4//if ( !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";
54include_once "./version.php";
55
56global $GANGLIA_PATH;
57
58$my_dir = getcwd();
59
60// Load Ganglia's PHP
61chdir( $GANGLIA_PATH );
62
63include_once "./conf.php";
64include_once "./functions.php";
65include_once "./ganglia.php";
66include_once "./get_context.php";
67unset( $start );
68$context = 'cluster';
69include_once "./get_ganglia.php";
70
71// Back to our PHP
72chdir( $my_dir );
73
74global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH, $DATA_SOURCE, $HTTP_GET_VARS, $_GET;
75$httpvars = new HTTPVariables( $HTTP_GET_VARS, $_GET );
76
77// Set cluster context so that Ganglia will
78// provide us with the correct metrics array
79//
80global $context, $clustername, $reports;
81
82//$clustername = $httpvars->getClusterName();
83
84global $default_metric;
85
86// Ganglia's array of host metrics
87//
88global $metrics, $hosts_up;
89
90class TarchDbase {
91
92        var $ip, $dbase, $conn;
93
94        function TarchDbase( $ip = null, $dbase = 'toga' ) {
95                $this->ip = $ip;
96                $this->dbase = $dbase;
97                $this->conn = null;
98        }
99
100        function connect() {
101
102                if( $this->ip == null and $this->dbase == 'toga' )
103                        $this->conn = pg_connect( "dbname=".$this->dbase );
104                else
105                        $this->conn = pg_connect( "host=".$this->ip." dbase=".$this->dbase );
106        }
107
108        function searchDbase( $id = null, $queue = null, $user = null, $name = null, $start_from_time = null, $start_to_time = null, $end_from_time = null, $end_to_time = null ) {
109
110                if( $id ) 
111                        $query = "SELECT job_id FROM jobs WHERE job_id = '$id' AND job_status = 'F'";
112                else {
113                        $query_args = array();
114                       
115                        if( $queue )
116                                $query_args[] = "job_queue ='$queue'";
117                        if( $user )
118                                $query_args[] = "job_owner ='$user'";
119                        if( $name )
120                                $query_args[] = "job_name = '$name'";
121                        if( $start_from_time )
122                                $query_args[] = "job_start_timestamp >= $start_from_time";
123                        if( $start_to_time )
124                                $query_args[] = "job_start_timestamp <= $start_to_time";
125                        if( $end_from_time )
126                                $query_args[] = "job_stop_timestamp >= $end_from_time";
127                        if( $end_to_time )
128                                $query_args[] = "job_stop_timestamp <= $end_to_time";
129
130                        $query = "SELECT job_id FROM jobs WHERE job_status = 'F' AND ";
131                        $extra_query_args = '';
132
133                        foreach( $query_args as $myquery ) {
134
135                                if( $extra_query_args == '' )
136                                        $extra_query_args = $myquery;
137                                else
138                                        $extra_query_args .= " AND ".$myquery;
139                        }
140                        $query .= $extra_query_args;
141                }
142
143                $ids = $this->queryDbase( $query );
144
145                $ret = array();
146
147                foreach( $ids as $crow)
148                        $ret[] = $crow[job_id];
149
150                return $ret;
151        }
152
153        function getNodesForJob( $jobid ) {
154
155                $result = $this->queryDbase( "SELECT node_id FROM job_nodes WHERE job_id = '$jobid'" );
156
157                $nodes = array();
158
159                foreach( $result as $result_row ) 
160
161                        $nodes[] = $this->getNodeArray( $result_row[node_id] );
162
163                return $nodes;
164        }
165
166        function getJobsForNode( $nodeid ) {
167
168                $result = $this->queryDbase( "SELECT job_id FROM job_nodes WHERE node_id = '$nodeid'" );
169
170                $jobs = array();
171
172                foreach( $result as $result_row )
173
174                        $jobs[] = $this->getJobArray( $result_row[job_id] );
175
176                return $jobs;
177        }
178
179        function getJobArray( $id ) {
180                $result = $this->queryDbase( "SELECT * FROM jobs WHERE job_id = '$id'" );
181
182                return ( $this->makeArray( $result[0] ) );
183        }
184
185        function getNodeArray( $id ) {
186
187                $result = $this->queryDbase( "SELECT * FROM nodes WHERE node_id = '$id'" );
188
189                return ( $this->makeArray( $result[0] ) );
190        }
191
192        function makeArray( $result_row ) {
193
194                $myar = array();
195
196                foreach( $result_row as $mykey => $myval ) {
197
198                        $map_key = explode( '_', $mykey );
199
200                        $rmap_key = array_reverse( $map_key );
201                        array_pop( $rmap_key );
202                        $map_key = array_reverse( $rmap_key );
203                       
204                        $newkey = implode( '_', $map_key );
205                       
206                        $myar[$newkey] = $result_row[$mykey];
207                }
208
209                return $myar;
210        }
211
212        function queryDbase( $query ) {
213
214                $result_rows = array();
215       
216                if( !$this->conn )
217                        $this->connect();
218
219                //printf( "query = [%s]\n", $query );
220                $result = pg_query( $this->conn, $query );
221
222                while ($row = pg_fetch_assoc($result))
223                        $result_rows[] = $row;
224
225                return $result_rows;
226        }
227}
228
229class TarchRrdGraph {
230        var $rrdbin, $rrdvalues, $clustername, $hostname, $tempdir, $tarchdir, $metrics;
231
232        function TarchRrdGraph( $clustername, $hostname, $rrdbin = '/usr/bin/rrdtool', $tarchdir = '/data/toga/rrds' ) {
233       
234                $this->rrdbin = $rrdbin;
235                $this->rrdvalues = array();
236                $this->tarchdir = $tarchdir;
237                $this->clustername = $clustername;
238                $this->hostname = $hostname;
239        }
240
241        function doCmd( $command ) {
242
243                printf( "command = %s\n", $command );
244                $pipe = popen( $command . ' 2>&1', 'r' );
245
246                if (!$pipe) {
247                        print "pipe failed.";
248                        return "";
249                }
250
251                $output = '';
252                while(!feof($pipe))
253                        $output .= fread($pipe, 1024);
254
255                pclose($pipe);
256
257                $output = explode( "\n", $output );
258                //print_r( $output );
259                return $output;
260        }
261
262        function dirList( $dir ) {
263
264                $dirlist = array();
265
266                if ($handle = opendir( $dir )) {
267                        while (false !== ($file = readdir($handle))) {
268                                if ($file != "." && $file != "..") {
269                                        $dirlist[] = $file;
270                                }
271                        }
272                        closedir($handle);
273                }
274
275                return $dirlist;
276        }
277
278        function getTimePeriods( $start, $end ) {
279
280                //printf("start = %s end = %s\n", $start, $end );
281                $times = array();
282                $dirlist = $this->dirList( $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname );
283
284                //print_r( $dirlist );
285
286                $first = 0;
287                $last = 9999999999999;
288
289                foreach( $dirlist as $dir ) {
290
291                        if( $dir > $first and $dir <= $start )
292                                $first = $dir;
293                        if( $dir < $last and $dir >= $end )
294                                $last = $dir;
295                }
296
297                //printf( "first = %s last = %s\n", $first, $last );
298
299                foreach( $dirlist as $dir ) {
300
301                        //printf( "dir %s ", $dir );
302
303                        if( $dir >= $first and $dir <= $last and !array_key_exists( $dir, $times ) ) {
304                       
305                                $times[] = $dir;
306                                //printf("newtime %s ", $dir );
307
308                        }
309                }
310
311                //print_r( $times );
312
313                sort( $times );
314
315                //print_r( $times );
316
317                return $times;
318        }
319
320        function getRrdDirs( $start, $stop ) {
321
322                //printf( "tarchdir = %s\n", $this->tarchdir );
323                $timess = $this->getTimePeriods( $start, $stop );
324                //print_r( $timess );
325
326                $rrd_files = array();
327
328                foreach( $timess as $time ) {
329
330                        $rrd_files[] = $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname. '/'.$time;
331                }
332
333                return $rrd_files;
334        }
335
336        function getRrdFiles( $metric, $start, $stop ) {
337
338                $times = $this->getTimePeriods( $start, $stop );
339
340                $rrd_files = array();
341
342                foreach( $times as $time ) {
343
344                        $rrd_files[] = $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname . '/' .$time. '/' . $metric. '.rrd';
345                }
346
347                return $rrd_files;
348        }
349
350        function graph( $descr ) {
351//      monitor2:/data/toga/rrds/LISA Cluster/gb-r15n11.irc.sara.nl# rrdtool graph /var/www/ganglia/test1.png --start 1118683231 --end 1118750431 --width 300 --height 400 DEF:'1'='./1118647515/load_one.rrd':'sum':AVERAGE DEF:'2'='./1118690723/load_one.rrd':'sum':AVERAGE DEF:'3'='./1118733925/load_one.rrd':'sum':AVERAGE AREA:1#555555:"load_one" AREA:2#555555 AREA:3#555555
352//      380x461
353//      monitor2:/data/toga/rrds/LISA Cluster/gb-r15n11.irc.sara.nl#
354                //$command = $this->rrdbin . " graph - --start $start --end $end ".
355                        "--width $width --height $height $upper_limit $lower_limit ".
356                        "--title '$title' $vertical_label $extras $background ". $series;
357
358                //$graph = $this->doCmd( $command );
359
360                //return $graph;
361                return 0;
362        }
363}
364
365class DataSource {
366
367        var $data, $ip, $port;
368
369        function DataSource( $ip = '127.0.0.1', $port = 8649 ) {
370                $this->ip = $ip;
371                $this->port = $port;
372        }
373
374        function getData() {
375
376                $errstr;
377                $errno = 0;
378                $timeout = 3;
379
380                $fp = fsockopen( $this->ip, $this->port, &$errno, &$errstr, $timeout );
381
382                if( !$fp ) {
383                        echo 'Unable to connect to '.$this->ip.':'.$this->port; // printf( 'Unable to connect to [%s:%.0f]', $this->ip, $this->port );
384                        return;
385                }
386
387                stream_set_timeout( $fp, 30 );
388
389                while ( !feof( $fp ) ) {
390                       
391                        $data .= fread( $fp, 16384 );
392                }
393
394                fclose( $fp );
395
396                return $data;
397        }
398}
399
400class DataGatherer {
401
402        var $xmlhandler, $data, $httpvars;
403
404        function DataGatherer( $cluster ) {
405
406                global $DATA_SOURCE;
407       
408                //printf("dg cluster = %s\n", $cluster );
409                $ds_fields = explode( ':', $DATA_SOURCE );
410                $ds_ip = $ds_fields[0];
411                $ds_port = $ds_fields[1];
412
413                $this->source = new DataSource( $ds_ip, $ds_port );
414
415                $this->parser = xml_parser_create();
416                $this->httpvars = $httpvars;
417                $this->xmlhandler = new TorqueXMLHandler( $cluster );
418                xml_set_element_handler( $this->parser, array( &$this->xmlhandler, 'startElement' ), array( &$this->xmlhandler, 'stopElement' ) );
419        }
420
421        function parseXML() {
422
423                $src = &$this->source;
424                $this->data = $src->getData();
425
426                if ( !xml_parse( &$this->parser, $this->data ) )
427                        $error = sprintf( 'XML error: %s at %d', xml_error_string( xml_get_error_code( &$this->parser ) ), xml_get_current_line_number( &$this->parser ) );
428        }
429
430        function printInfo() {
431                $handler = $this->xmlhandler;
432                $handler->printInfo();
433        }
434
435        function getNodes() {
436                $handler = $this->xmlhandler;
437                return $handler->getNodes();
438        }
439
440        function getCpus() {
441                $handler = $this->xmlhandler;
442                return $handler->getCpus();
443        }
444
445        function getJobs() {
446                $handler = $this->xmlhandler;
447                return $handler->getJobs();
448        }
449
450        function getHeartbeat() {
451                $handler = $this->xmlhandler;
452                return $handler->getHeartbeat();
453        }
454}
455
456class TorqueXMLHandler {
457
458        var $clusters, $heartbeat, $nodes, $jobs, $clustername, $proc_cluster;
459
460        function TorqueXMLHandler( $clustername ) {
461                $jobs = array();
462                $clusters = array();
463                $this->nodes = array();
464                $heartbeat = array();
465                $this->clustername = $clustername;
466                //printf(" cluster set to %s \n", $this->clustername );
467        }
468
469        function getCpus() {
470
471                $cpus = 0;
472
473                foreach( $this->jobs as $jobid=>$jobattrs ) {
474
475                        $nodes = count( $jobattrs[nodes] );
476                        $ppn = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
477                        $mycpus = $nodes * $ppn;
478
479                        $cpus = $cpus + $mycpus;
480                }
481        }
482
483        function startElement( $parser, $name, $attrs ) {
484
485                $jobs = &$this->jobs;
486                $nodes = &$this->nodes;
487
488                if ( $attrs[TN] ) {
489
490                        // Ignore dead metrics. Detect and mask failures.
491                        if ( $attrs[TN] > $attrs[TMAX] * 4 )
492                                return;
493                }
494
495                $jobid = null;
496
497                //printf( '%s=%s', $attrs[NAME], $attrs[VAL] );
498
499                //printf( "clustername = %s proc_cluster = %s\n", $this->clustername, $this->proc_cluster );
500
501                if( $name == 'CLUSTER' ) {
502
503                        $this->proc_cluster = $attrs[NAME];
504                        //printf( "Found cluster %s\n", $attrs[NAME] );
505                        //print_r( $attrs );
506
507                        //if( !isset( $clusters[$clustername] ) )
508                        //      $clusters[$clustername] = array();
509
510                } else if( $name == 'HOST' and $this->proc_cluster == $this->clustername) {
511
512                        $hostname = $attrs[NAME];
513                        $location = $attrs[LOCATION];
514                        //printf( "Found node %s\n", $hostname );
515
516                        if( !isset( $this->nodes[$hostname] ) )
517                                $nodes[$hostname] = new NodeImage( $hostname );
518
519                } else if( $name == 'METRIC' and strstr( $attrs[NAME], 'TOGA' ) and $this->proc_cluster == $this->clustername ) {
520
521                        if( strstr( $attrs[NAME], 'TOGA-HEARTBEAT' ) ) {
522
523                                $this->heartbeat['time'] = $attrs[VAL];
524                                //printf( "heartbeat %s\n", $heartbeat['time'] );
525
526                        } else if( strstr( $attrs[NAME], 'TOGA-JOB' ) ) {
527
528                                sscanf( $attrs[NAME], 'TOGA-JOB-%d', $jobid );
529
530                                //printf( "jobid %s\n", $jobid );
531
532                                if( !isset( $jobs[$jobid] ) )
533                                        $jobs[$jobid] = array();
534
535                                $fields = explode( ' ', $attrs[VAL] );
536
537                                foreach( $fields as $f ) {
538                                        $togavalues = explode( '=', $f );
539
540                                        $toganame = $togavalues[0];
541                                        $togavalue = $togavalues[1];
542
543                                        //printf( "\t%s\t= %s\n", $toganame, $togavalue );
544
545                                        if( $toganame == 'nodes' ) {
546
547                                                if( $jobs[$jobid][status] == 'R' ) {
548                                               
549                                                        if( !isset( $jobs[$jobid][$toganame] ) )
550                                                                $jobs[$jobid][$toganame] = array();
551
552                                                        $mynodes = explode( ';', $togavalue );
553
554                                                        foreach( $mynodes as $node )
555
556                                                                $jobs[$jobid][$toganame][] = $node;
557
558                                                } else if( $jobs[$jobid][status] == 'Q' ) {
559
560                                                        $jobs[$jobid][$toganame] = $togavalue;
561                                                }
562                                               
563                                        } else {
564
565                                                $jobs[$jobid][$toganame] = $togavalue;
566                                        }
567                                }
568
569                                if( isset( $jobs[$jobid][domain] ) and isset( $jobs[$jobid][nodes] ) ) {
570                       
571                                        $nr_nodes = count( $jobs[$jobid][nodes] );
572                       
573                                        foreach( $jobs[$jobid][nodes] as $node ) {
574
575                                                $host = $node.'.'.$jobs[$jobid][domain];
576                               
577                                                if( !isset( $this->nodes[$host] ) )
578                                                        $my_node = new NodeImage( $host );
579                                                else
580                                                        $my_node = $this->nodes[$host];
581
582                                                if( !$my_node->hasJob( $jobid ) )
583
584                                                        if( isset( $jobs[$jobid][ppn] ) )
585                                                                $my_node->addJob( $jobid, ((int) $jobs[$jobid][ppn]) );
586                                                        else
587                                                                $my_node->addJob( $jobid, 1 );
588
589                                                $nodes[$host] = $my_node;
590                                        }
591                                }
592                        }
593                }
594                $this->jobs = $jobs;
595                //print_r( $nodes );
596                $this->nodes = $nodes;
597        }
598
599        function stopElement( $parser, $name ) {
600        }
601
602        function printInfo() {
603
604                $jobs = &$this->jobs;
605
606                printf( "---jobs---\n" );
607
608                foreach( $jobs as $jobid => $job ) {
609
610                        printf( "job %s\n", $jobid );
611
612                        if( isset( $job[nodes] ) ) {
613
614                                foreach( $job[nodes] as $node ) {
615
616                                        $mynode = $this->nodes[$node];
617                                        $hostname = $mynode->getHostname();
618                                        $location = $mynode->getLocation();
619
620                                        printf( "\t- node %s\tlocation %s\n", $hostname, $location );
621                                        //$this->nodes[$hostname]->setLocation( "hier draait job ".$jobid );
622                                }
623                        }
624                }
625
626                printf( "---nodes---\n" );
627
628                $nodes = &$this->nodes;
629
630                foreach( $nodes as $node ) {
631
632                        $hostname = $node->getHostname();
633                        $location = $node->getLocation();
634                        $jobs = implode( ' ', $node->getJobs() );
635                        printf( "* node %s\tlocation %s\tjobs %s\n", $hostname, $location, $jobs );
636                }
637        }
638
639        function getNodes() {
640                //print_r( $this->nodes );
641                return $this->nodes;
642        }
643
644        function getJobs() {
645                return $this->jobs;
646        }
647
648        function getHeartbeat() {
649                return $this->heartbeat['time'];
650        }
651}
652
653class NodeImage {
654
655        var $image, $x, $y, $hostname, $jobs, $tasks, $showinfo;
656
657        function NodeImage( $hostname ) {
658
659                $this->jobs = array();
660                //$this->image = $image;
661                //$this->x = $x;
662                //$this->y = $y;
663                $this->tasks = 0;
664                $this->hostname = $hostname;
665                $this->cpus = $this->determineCpus();
666                $this->showinfo = 1;
667        }
668
669        function addJob( $jobid, $cpus ) {
670                $jobs = &$this->jobs;
671
672                $jobs[] = $jobid;
673                $this->jobs = $jobs;
674
675                $this->addTask( $cpus );
676        }
677
678        function hasJob( $jobid ) {
679
680                $jobfound = 0;
681
682                if( count( $this->jobs ) > 0 )
683                        foreach( $this->jobs as $job )
684
685                                if( $job == $jobid )
686                                        $jobfound = 1;
687
688                return $jobfound;
689        }
690
691        function addTask( $cpus ) {
692
693                $this->tasks = $this->tasks + $cpus;
694        }
695
696        function setImage( $image ) {
697
698                $this->image = $image;
699        }
700
701        function setCoords( $x, $y ) {
702
703                $this->x = $x;
704                $this->y = $y;
705        }
706
707        function colorHex( $color ) {
708       
709                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) );
710
711                return $my_color;
712        }
713
714        function setLoad( $load ) {
715                $this->load = $load;
716        }
717
718        function setHostname( $hostname ) {
719                $this->hostname = $hostname;
720        }
721
722        function getHostname() {
723                return $this->hostname;
724        }
725
726        function getJobs() {
727                return $this->jobs;
728        }
729
730        function setShowinfo( $showinfo ) {
731                $this->showinfo = $showinfo;
732        }
733
734        function draw() {
735
736                $this->drawSmall();
737        }
738
739        function drawBig() {
740
741        }
742
743        function drawSmall() {
744
745                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
746                global $JOB_NODE_MARKING;
747
748                $black_color = imageColorAllocate( $this->image, 0, 0, 0 );
749                $size = $SMALL_CLUSTERIMAGE_NODEWIDTH;
750
751                imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+($size), $this->y+($size), $black_color );
752
753                if( $this->showinfo) {
754               
755                        $this->load = $this->determineLoad();
756
757                        if( !isset( $this->image ) or !isset( $this->x ) or !isset( $this->y ) ) {
758                                printf( "aborting\n" );
759                                printf( "x %d y %d load %f\n", $this->x, $this->y, $load );
760                                return;
761                        }
762
763
764                        // Convert Ganglias Hexadecimal load color to a Decimal one
765                        //
766                        $load = $this->determineLoad(); 
767                        $usecolor = $this->colorHex( load_color($load) );
768                        imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor );
769                        if( count( $this->jobs ) > 0 )
770                                imageString( $this->image, 1, $this->x+(($size/2)-2), $this->y+(($size/2)-3), $JOB_NODE_MARKING, $black_color );
771
772                } else {
773
774                        // White
775                        $usecolor = imageColorAllocate( $this->image, 255, 255, 255 );
776                        imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor );
777                }
778
779
780        }
781
782        function determineCpus() {
783
784                global $metrics;
785
786                $cpus = $metrics[$this->hostname][cpu_num][VAL];
787                if (!$cpus) $cpus=1;
788
789                return $cpus;
790        }
791
792        function determineLoad() {
793
794                global $metrics;
795
796                $load_one = $metrics[$this->hostname][load_one][VAL];
797                $load = ((float) $load_one)/$this->cpus;
798
799                return $load;
800        }
801}
802
803class ClusterImage {
804
805        var $dataget, $image, $clustername;
806        var $filtername, $filters;
807
808        function ClusterImage( $clustername ) {
809
810                //printf( "image cluster = %s\n", $clustername );
811                $this->dataget = new DataGatherer( $clustername );
812                $this->clustername = $clustername;
813                $this->filters = array();
814        }
815
816        function setFilter( $filtername, $filtervalue ) {
817
818                //printf("filter %s = %s\n", $filtername, $filtervalue );
819                //printf( "filter set to %s = %s\n", $filtername, $filtervalue );
820                $this->filters[$filtername] = $filtervalue;
821                //print_r($this->filters);
822        }
823
824        function filterNodes( $jobs, $nodes ) {
825
826                $filtered_nodes = array();
827
828                foreach( $nodes as $node ) {
829
830                        $hostname = $node->getHostname();
831
832                        $addhost = 1;
833
834                        if( count( $this->filters ) > 0 ) {
835
836                                $mynjobs = $node->getJobs();
837
838                                if( count( $mynjobs ) > 0 ) {
839
840                                        foreach( $mynjobs as $myjob ) {
841
842                                                foreach( $this->filters as $filtername => $filtervalue ) {
843
844                                                        //printf("filter bla %s = %s\n", $filtername,$filtervalue );
845
846                                                        if( $filtername!=null && $filtername!='' ) {
847
848                                                                if( $filtername == 'jobid' && !$node->hasJob( $filtervalue) ) {
849                                                                        $addhost = 0;
850                                                                        //printf("host %s has no job %s\n", $hostname, $filtervalue);
851                                                                } else if( $filtername != 'jobid' ) {
852                                                                        //printf("myjob is %s\n", $myjob );
853                                                                        if( $jobs[$myjob][$filtername] != $filtervalue ) {
854                                                                                //printf("host %s has no job with %s=%s\n", $hostname, $filtername, $filtervalue);
855                                                                                $addhost = 0;
856                                                                        }
857                                                                }
858                                                        }
859                                                }
860                                        }
861                                } else
862                                        $addhost = 0;
863                        }
864
865                        if( $addhost )
866                                $filtered_nodes[] = $hostname;
867                }
868
869                return $filtered_nodes;
870        }
871
872        function draw() {
873
874                //printf("stopt met uitvoer");
875                //return;
876
877                global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH;
878       
879                $mydatag = $this->dataget;
880                $mydatag->parseXML();
881
882                //$max_width = 250;
883                //$node_width = 11;
884
885                $max_width = $SMALL_CLUSTERIMAGE_MAXWIDTH;
886                $node_width = $SMALL_CLUSTERIMAGE_NODEWIDTH;
887
888                //printf( "cmaxw %s nmaxw %s", $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH );
889
890                $nodes = $mydatag->getNodes();
891                $nodes_hosts = array_keys( $nodes );
892
893                $nodes_nr = count( $nodes );
894
895                $nodes_size = $nodes_nr*$node_width;
896                $node_rows = 0;
897
898                if( $nodes_size > $max_width ) {
899                        $nodes_per_row = ( (int) ($max_width/$node_width) );
900                } else {
901                        $nodes_per_row = $nodes_size;
902                        $node_rows = 1;
903                }
904
905                if( $nodes_per_row < $nodes_nr ) {
906                        $node_rows = ( (int) ($nodes_nr/$nodes_per_row) );
907                        $node_rest = fmod( $nodes_nr, $nodes_per_row );
908                        //printf( "nodesnr %d noderest %f\n", $nodes_nr, $node_rest );
909                        if( $node_rest > 0 ) {
910                                $node_rows++;
911                                //printf( "noderows %d\n", $node_rows );
912                        }
913                }
914
915                //printf( "imagecreate: %dx%d", ($nodes_per_row*$node_width), ($node_rows*$node_width) );
916                //$image = imageCreateTrueColor( ($nodes_per_row*$node_width)+1, ($node_rows*$node_width)+1 );
917                $image = imageCreateTrueColor( $max_width, ($node_rows*$node_width)+1 );
918                $colorwhite = imageColorAllocate( $image, 255, 255, 255 );
919                imageFill( $image, 0, 0, $colorwhite );
920
921                $jobs = $mydatag->getJobs();
922                //printf("filtername = %s\n", $filtername );
923                $filtered_nodes = $this->filterNodes( $jobs, $nodes );
924
925                //print_r($filtered_nodes);
926
927                for( $n = 0; $n < $node_rows; $n++ ) {
928                       
929                        for( $m = 0; $m < $nodes_per_row; $m++ ) {
930                       
931                                $x = ($m * $node_width);
932                                $y = ($n * $node_width);
933
934                                $cur_node = ($n * $nodes_per_row) + ($m);
935                                $host = $nodes_hosts[$cur_node];
936
937
938                                if( isset( $nodes[$host] ) ) {
939
940                                        $nodes[$host]->setCoords( $x, $y );
941                                        $nodes[$host]->setImage( $image );
942
943                                        if( !in_array( $host, $filtered_nodes ) )
944                                                $nodes[$host]->setShowinfo( 0 );
945
946                                        $nodes[$host]->draw();
947                                }
948                        }
949                }
950               
951                header( 'Content-type: image/png' );
952                imagePNG( $image );
953                imageDestroy( $image );
954        }
955}
956
957//$my_data = new DataGatherer();
958//$my_data->parseXML();
959//$my_data->printInfo();
960
961//$ic = new ClusterImage( "LISA Cluster" );
962//$ic->draw();
963?>
Note: See TracBrowser for help on using the repository browser.