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

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

toga/libtoga.php:

toga/index.php:

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