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

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

web/addons/toga/libtoga.php:

  • Added timeout for socket reading
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";
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                stream_set_timeout( $fp, 30 );
387
388                while ( !feof( $fp ) ) {
389                       
390                        $data .= fread( $fp, 16384 );
391                }
392
393                fclose( $fp );
394
395                return $data;
396        }
397}
398
399class DataGatherer {
400
401        var $xmlhandler, $data, $httpvars;
402
403        function DataGatherer( $cluster ) {
404
405                global $DATA_SOURCE;
406       
407                //printf("dg cluster = %s\n", $cluster );
408                $ds_fields = explode( ':', $DATA_SOURCE );
409                $ds_ip = $ds_fields[0];
410                $ds_port = $ds_fields[1];
411
412                $this->source = new DataSource( $ds_ip, $ds_port );
413
414                $this->parser = xml_parser_create();
415                $this->httpvars = $httpvars;
416                $this->xmlhandler = new TorqueXMLHandler( $cluster );
417                xml_set_element_handler( $this->parser, array( &$this->xmlhandler, 'startElement' ), array( &$this->xmlhandler, 'stopElement' ) );
418        }
419
420        function parseXML() {
421
422                $src = &$this->source;
423                $this->data = $src->getData();
424
425                if ( !xml_parse( &$this->parser, $this->data ) )
426                        $error = sprintf( 'XML error: %s at %d', xml_error_string( xml_get_error_code( &$this->parser ) ), xml_get_current_line_number( &$this->parser ) );
427        }
428
429        function printInfo() {
430                $handler = $this->xmlhandler;
431                $handler->printInfo();
432        }
433
434        function getNodes() {
435                $handler = $this->xmlhandler;
436                return $handler->getNodes();
437        }
438
439        function getCpus() {
440                $handler = $this->xmlhandler;
441                return $handler->getCpus();
442        }
443
444        function getJobs() {
445                $handler = $this->xmlhandler;
446                return $handler->getJobs();
447        }
448
449        function getHeartbeat() {
450                $handler = $this->xmlhandler;
451                return $handler->getHeartbeat();
452        }
453}
454
455class TorqueXMLHandler {
456
457        var $clusters, $heartbeat, $nodes, $jobs, $clustername, $proc_cluster;
458
459        function TorqueXMLHandler( $clustername ) {
460                $jobs = array();
461                $clusters = array();
462                $this->nodes = array();
463                $heartbeat = array();
464                $this->clustername = $clustername;
465                //printf(" cluster set to %s \n", $this->clustername );
466        }
467
468        function getCpus() {
469
470                $cpus = 0;
471
472                foreach( $this->jobs as $jobid=>$jobattrs ) {
473
474                        $nodes = count( $jobattrs[nodes] );
475                        $ppn = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
476                        $mycpus = $nodes * $ppn;
477
478                        $cpus = $cpus + $mycpus;
479                }
480        }
481
482        function startElement( $parser, $name, $attrs ) {
483
484                $jobs = &$this->jobs;
485                $nodes = &$this->nodes;
486
487                if ( $attrs[TN] ) {
488
489                        // Ignore dead metrics. Detect and mask failures.
490                        if ( $attrs[TN] > $attrs[TMAX] * 4 )
491                                return;
492                }
493
494                $jobid = null;
495
496                //printf( '%s=%s', $attrs[NAME], $attrs[VAL] );
497
498                //printf( "clustername = %s proc_cluster = %s\n", $this->clustername, $this->proc_cluster );
499
500                if( $name == 'CLUSTER' ) {
501
502                        $this->proc_cluster = $attrs[NAME];
503                        //printf( "Found cluster %s\n", $attrs[NAME] );
504                        //print_r( $attrs );
505
506                        //if( !isset( $clusters[$clustername] ) )
507                        //      $clusters[$clustername] = array();
508
509                } else if( $name == 'HOST' and $this->proc_cluster == $this->clustername) {
510
511                        $hostname = $attrs[NAME];
512                        $location = $attrs[LOCATION];
513                        //printf( "Found node %s\n", $hostname );
514
515                        if( !isset( $this->nodes[$hostname] ) )
516                                $nodes[$hostname] = new NodeImage( $hostname );
517
518                } else if( $name == 'METRIC' and strstr( $attrs[NAME], 'TOGA' ) and $this->proc_cluster == $this->clustername ) {
519
520                        if( strstr( $attrs[NAME], 'TOGA-HEARTBEAT' ) ) {
521
522                                $this->heartbeat['time'] = $attrs[VAL];
523                                //printf( "heartbeat %s\n", $heartbeat['time'] );
524
525                        } else if( strstr( $attrs[NAME], 'TOGA-JOB' ) ) {
526
527                                sscanf( $attrs[NAME], 'TOGA-JOB-%d', $jobid );
528
529                                //printf( "jobid %s\n", $jobid );
530
531                                if( !isset( $jobs[$jobid] ) )
532                                        $jobs[$jobid] = array();
533
534                                $fields = explode( ' ', $attrs[VAL] );
535
536                                foreach( $fields as $f ) {
537                                        $togavalues = explode( '=', $f );
538
539                                        $toganame = $togavalues[0];
540                                        $togavalue = $togavalues[1];
541
542                                        //printf( "\t%s\t= %s\n", $toganame, $togavalue );
543
544                                        if( $toganame == 'nodes' ) {
545
546                                                if( $jobs[$jobid][status] == 'R' ) {
547                                               
548                                                        if( !isset( $jobs[$jobid][$toganame] ) )
549                                                                $jobs[$jobid][$toganame] = array();
550
551                                                        $mynodes = explode( ';', $togavalue );
552
553                                                        foreach( $mynodes as $node )
554
555                                                                $jobs[$jobid][$toganame][] = $node;
556
557                                                } else if( $jobs[$jobid][status] == 'Q' ) {
558
559                                                        $jobs[$jobid][$toganame] = $togavalue;
560                                                }
561                                               
562                                        } else {
563
564                                                $jobs[$jobid][$toganame] = $togavalue;
565                                        }
566                                }
567
568                                if( isset( $jobs[$jobid][domain] ) and isset( $jobs[$jobid][nodes] ) ) {
569                       
570                                        $nr_nodes = count( $jobs[$jobid][nodes] );
571                       
572                                        foreach( $jobs[$jobid][nodes] as $node ) {
573
574                                                $host = $node.'.'.$jobs[$jobid][domain];
575                               
576                                                if( !isset( $this->nodes[$host] ) )
577                                                        $my_node = new NodeImage( $host );
578                                                else
579                                                        $my_node = $this->nodes[$host];
580
581                                                if( !$my_node->hasJob( $jobid ) )
582
583                                                        if( isset( $jobs[$jobid][ppn] ) )
584                                                                $my_node->addJob( $jobid, ((int) $jobs[$jobid][ppn]) );
585                                                        else
586                                                                $my_node->addJob( $jobid, 1 );
587
588                                                $nodes[$host] = $my_node;
589                                        }
590                                }
591                        }
592                }
593                $this->jobs = $jobs;
594                //print_r( $nodes );
595                $this->nodes = $nodes;
596        }
597
598        function stopElement( $parser, $name ) {
599        }
600
601        function printInfo() {
602
603                $jobs = &$this->jobs;
604
605                printf( "---jobs---\n" );
606
607                foreach( $jobs as $jobid => $job ) {
608
609                        printf( "job %s\n", $jobid );
610
611                        if( isset( $job[nodes] ) ) {
612
613                                foreach( $job[nodes] as $node ) {
614
615                                        $mynode = $this->nodes[$node];
616                                        $hostname = $mynode->getHostname();
617                                        $location = $mynode->getLocation();
618
619                                        printf( "\t- node %s\tlocation %s\n", $hostname, $location );
620                                        //$this->nodes[$hostname]->setLocation( "hier draait job ".$jobid );
621                                }
622                        }
623                }
624
625                printf( "---nodes---\n" );
626
627                $nodes = &$this->nodes;
628
629                foreach( $nodes as $node ) {
630
631                        $hostname = $node->getHostname();
632                        $location = $node->getLocation();
633                        $jobs = implode( ' ', $node->getJobs() );
634                        printf( "* node %s\tlocation %s\tjobs %s\n", $hostname, $location, $jobs );
635                }
636        }
637
638        function getNodes() {
639                //print_r( $this->nodes );
640                return $this->nodes;
641        }
642
643        function getJobs() {
644                return $this->jobs;
645        }
646
647        function getHeartbeat() {
648                return $this->heartbeat['time'];
649        }
650}
651
652class NodeImage {
653
654        var $image, $x, $y, $hostname, $jobs, $tasks, $showinfo;
655
656        function NodeImage( $hostname ) {
657
658                $this->jobs = array();
659                //$this->image = $image;
660                //$this->x = $x;
661                //$this->y = $y;
662                $this->tasks = 0;
663                $this->hostname = $hostname;
664                $this->cpus = $this->determineCpus();
665                $this->showinfo = 1;
666        }
667
668        function addJob( $jobid, $cpus ) {
669                $jobs = &$this->jobs;
670
671                $jobs[] = $jobid;
672                $this->jobs = $jobs;
673
674                $this->addTask( $cpus );
675        }
676
677        function hasJob( $jobid ) {
678
679                $jobfound = 0;
680
681                if( count( $this->jobs ) > 0 )
682                        foreach( $this->jobs as $job )
683
684                                if( $job == $jobid )
685                                        $jobfound = 1;
686
687                return $jobfound;
688        }
689
690        function addTask( $cpus ) {
691
692                $this->tasks = $this->tasks + $cpus;
693        }
694
695        function setImage( $image ) {
696
697                $this->image = $image;
698        }
699
700        function setCoords( $x, $y ) {
701
702                $this->x = $x;
703                $this->y = $y;
704        }
705
706        function colorHex( $color ) {
707       
708                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) );
709
710                return $my_color;
711        }
712
713        function setLoad( $load ) {
714                $this->load = $load;
715        }
716
717        function setHostname( $hostname ) {
718                $this->hostname = $hostname;
719        }
720
721        function getHostname() {
722                return $this->hostname;
723        }
724
725        function getJobs() {
726                return $this->jobs;
727        }
728
729        function setShowinfo( $showinfo ) {
730                $this->showinfo = $showinfo;
731        }
732
733        function draw() {
734
735                $this->drawSmall();
736        }
737
738        function drawBig() {
739
740        }
741
742        function drawSmall() {
743
744                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
745                global $JOB_NODE_MARKING;
746
747                $black_color = imageColorAllocate( $this->image, 0, 0, 0 );
748                $size = $SMALL_CLUSTERIMAGE_NODEWIDTH;
749
750                imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+($size), $this->y+($size), $black_color );
751
752                if( $this->showinfo) {
753               
754                        $this->load = $this->determineLoad();
755
756                        if( !isset( $this->image ) or !isset( $this->x ) or !isset( $this->y ) ) {
757                                printf( "aborting\n" );
758                                printf( "x %d y %d load %f\n", $this->x, $this->y, $load );
759                                return;
760                        }
761
762
763                        // Convert Ganglias Hexadecimal load color to a Decimal one
764                        //
765                        $load = $this->determineLoad(); 
766                        $usecolor = $this->colorHex( load_color($load) );
767                        imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor );
768                        if( count( $this->jobs ) > 0 )
769                                imageString( $this->image, 1, $this->x+(($size/2)-2), $this->y+(($size/2)-3), $JOB_NODE_MARKING, $black_color );
770
771                } else {
772
773                        // White
774                        $usecolor = imageColorAllocate( $this->image, 255, 255, 255 );
775                        imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor );
776                }
777
778
779        }
780
781        function determineCpus() {
782
783                global $metrics;
784
785                $cpus = $metrics[$this->hostname][cpu_num][VAL];
786                if (!$cpus) $cpus=1;
787
788                return $cpus;
789        }
790
791        function determineLoad() {
792
793                global $metrics;
794
795                $load_one = $metrics[$this->hostname][load_one][VAL];
796                $load = ((float) $load_one)/$this->cpus;
797
798                return $load;
799        }
800}
801
802class ClusterImage {
803
804        var $dataget, $image, $clustername;
805        var $filtername, $filters;
806
807        function ClusterImage( $clustername ) {
808
809                //printf( "image cluster = %s\n", $clustername );
810                $this->dataget = new DataGatherer( $clustername );
811                $this->clustername = $clustername;
812                $this->filters = array();
813        }
814
815        function setFilter( $filtername, $filtervalue ) {
816
817                //printf("filter %s = %s\n", $filtername, $filtervalue );
818                //printf( "filter set to %s = %s\n", $filtername, $filtervalue );
819                $this->filters[$filtername] = $filtervalue;
820                //print_r($this->filters);
821        }
822
823        function filterNodes( $jobs, $nodes ) {
824
825                $filtered_nodes = array();
826
827                foreach( $nodes as $node ) {
828
829                        $hostname = $node->getHostname();
830
831                        $addhost = 1;
832
833                        if( count( $this->filters ) > 0 ) {
834
835                                $mynjobs = $node->getJobs();
836
837                                if( count( $mynjobs ) > 0 ) {
838
839                                        foreach( $mynjobs as $myjob ) {
840
841                                                foreach( $this->filters as $filtername => $filtervalue ) {
842
843                                                        //printf("filter bla %s = %s\n", $filtername,$filtervalue );
844
845                                                        if( $filtername!=null && $filtername!='' ) {
846
847                                                                if( $filtername == 'jobid' && !$node->hasJob( $filtervalue) ) {
848                                                                        $addhost = 0;
849                                                                        //printf("host %s has no job %s\n", $hostname, $filtervalue);
850                                                                } else if( $filtername != 'jobid' ) {
851                                                                        //printf("myjob is %s\n", $myjob );
852                                                                        if( $jobs[$myjob][$filtername] != $filtervalue ) {
853                                                                                //printf("host %s has no job with %s=%s\n", $hostname, $filtername, $filtervalue);
854                                                                                $addhost = 0;
855                                                                        }
856                                                                }
857                                                        }
858                                                }
859                                        }
860                                } else
861                                        $addhost = 0;
862                        }
863
864                        if( $addhost )
865                                $filtered_nodes[] = $hostname;
866                }
867
868                return $filtered_nodes;
869        }
870
871        function draw() {
872
873                //printf("stopt met uitvoer");
874                //return;
875
876                global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH;
877       
878                $mydatag = $this->dataget;
879                $mydatag->parseXML();
880
881                //$max_width = 250;
882                //$node_width = 11;
883
884                $max_width = $SMALL_CLUSTERIMAGE_MAXWIDTH;
885                $node_width = $SMALL_CLUSTERIMAGE_NODEWIDTH;
886
887                //printf( "cmaxw %s nmaxw %s", $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH );
888
889                $nodes = $mydatag->getNodes();
890                $nodes_hosts = array_keys( $nodes );
891
892                $nodes_nr = count( $nodes );
893
894                $nodes_size = $nodes_nr*$node_width;
895                $node_rows = 0;
896
897                if( $nodes_size > $max_width ) {
898                        $nodes_per_row = ( (int) ($max_width/$node_width) );
899                } else {
900                        $nodes_per_row = $nodes_size;
901                        $node_rows = 1;
902                }
903
904                if( $nodes_per_row < $nodes_nr ) {
905                        $node_rows = ( (int) ($nodes_nr/$nodes_per_row) );
906                        $node_rest = fmod( $nodes_nr, $nodes_per_row );
907                        //printf( "nodesnr %d noderest %f\n", $nodes_nr, $node_rest );
908                        if( $node_rest > 0 ) {
909                                $node_rows++;
910                                //printf( "noderows %d\n", $node_rows );
911                        }
912                }
913
914                //printf( "imagecreate: %dx%d", ($nodes_per_row*$node_width), ($node_rows*$node_width) );
915                //$image = imageCreateTrueColor( ($nodes_per_row*$node_width)+1, ($node_rows*$node_width)+1 );
916                $image = imageCreateTrueColor( $max_width, ($node_rows*$node_width)+1 );
917                $colorwhite = imageColorAllocate( $image, 255, 255, 255 );
918                imageFill( $image, 0, 0, $colorwhite );
919
920                $jobs = $mydatag->getJobs();
921                //printf("filtername = %s\n", $filtername );
922                $filtered_nodes = $this->filterNodes( $jobs, $nodes );
923
924                //print_r($filtered_nodes);
925
926                for( $n = 0; $n < $node_rows; $n++ ) {
927                       
928                        for( $m = 0; $m < $nodes_per_row; $m++ ) {
929                       
930                                $x = ($m * $node_width);
931                                $y = ($n * $node_width);
932
933                                $cur_node = ($n * $nodes_per_row) + ($m);
934                                $host = $nodes_hosts[$cur_node];
935
936
937                                if( isset( $nodes[$host] ) ) {
938
939                                        $nodes[$host]->setCoords( $x, $y );
940                                        $nodes[$host]->setImage( $image );
941
942                                        if( !in_array( $host, $filtered_nodes ) )
943                                                $nodes[$host]->setShowinfo( 0 );
944
945                                        $nodes[$host]->draw();
946                                }
947                        }
948                }
949               
950                header( 'Content-type: image/png' );
951                imagePNG( $image );
952                imageDestroy( $image );
953        }
954}
955
956//$my_data = new DataGatherer();
957//$my_data->parseXML();
958//$my_data->printInfo();
959
960//$ic = new ClusterImage( "LISA Cluster" );
961//$ic->draw();
962?>
Note: See TracBrowser for help on using the repository browser.