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

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

web/addons/toga/search.php:

  • Main search

web/addons/toga/libtoga.php:

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