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

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

web/addons/toga/libtoga.php:

  • Fixed typo

web/addons/toga/search.php:

  • Timebased searching works now
File size: 19.0 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
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'";
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 ";
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 TarchRrd( $clustername, $rrdbin = '/usr/bin/rrdtool', $tarchdir = '/data/toga/rrds' ) {
232                $this->rrdbin = $rrdbin;
233                $this->rrdvalues = array();
234                $this->tarchdir = $tarchdir;
235        }
236
237        function doCmd( $command ) {
238
239                printf( "command = %s\n", $command );
240                $pipe = popen( $command . ' 2>&1', 'r' );
241
242                if (!$pipe) {
243                        print "pipe failed.";
244                        return "";
245                }
246
247                $output = '';
248                while(!feof($pipe))
249                        $output .= fread($pipe, 1024);
250
251                pclose($pipe);
252
253                $output = explode( "\n", $output );
254                //print_r( $output );
255                return $output;
256        }
257
258        function dirList( $dir ) {
259
260                $dirlist = array();
261
262                if ($handle = opendir( $dir )) {
263                        while (false !== ($file = readdir($handle))) {
264                                if ($file != "." && $file != "..") {
265                                        $dirlist[] = $file;
266                                }
267                        }
268                        closedir($handle);
269                }
270
271                return $dirlist;
272        }
273
274        function getTimePeriods( $start, $end ) {
275
276                $times = array();
277                $dirlist = $this->dirList( $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname );
278                $first = 0;
279                $last = 9999999999999;
280
281                foreach( $dirlist as $dir ) {
282
283                        if( $dir > $first and $dir <= $start )
284                                $first = $dir;
285                        if( $dir < $last and $dir >= $end )
286                                $last = $dir;
287                }
288
289                foreach( $dirlist as $dir ) {
290
291                        if( $dir >= $first and $dir <= $last and !array_key_exists( $dir, $times ) )
292                                $times[] = $dir;
293                }
294
295                sort( $times );
296
297                return $times;
298        }
299
300        function graph( $descr ) {
301//      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
302//      380x461
303//      monitor2:/data/toga/rrds/LISA Cluster/gb-r15n11.irc.sara.nl#
304                //$command = $this->rrdbin . " graph - --start $start --end $end ".
305                        "--width $width --height $height $upper_limit $lower_limit ".
306                        "--title '$title' $vertical_label $extras $background ". $series;
307
308                //$graph = $this->doCmd( $command );
309
310                //return $graph;
311        }
312}
313
314class DataSource {
315
316        var $data, $ip, $port;
317
318        function DataSource( $ip = '127.0.0.1', $port = 8649 ) {
319                $this->ip = $ip;
320                $this->port = $port;
321        }
322
323        function getData() {
324
325                $errstr;
326                $errno = 0;
327                $timeout = 3;
328
329                $fp = fsockopen( $this->ip, $this->port, &$errno, &$errstr, $timeout );
330
331                if( !$fp ) {
332                        echo 'Unable to connect to '.$this->ip.':'.$this->port; // printf( 'Unable to connect to [%s:%.0f]', $this->ip, $this->port );
333                        return;
334                }
335
336                while ( !feof( $fp ) ) {
337                       
338                        $data .= fread( $fp, 16384 );
339                }
340
341                fclose( $fp );
342
343                return $data;
344        }
345}
346
347class DataGatherer {
348
349        var $xmlhandler, $data, $httpvars;
350
351        function DataGatherer() {
352
353                global $DATA_SOURCE;
354               
355                $ds_fields = explode( ':', $DATA_SOURCE );
356                $ds_ip = $ds_fields[0];
357                $ds_port = $ds_fields[1];
358
359                $this->source = new DataSource( $ds_ip, $ds_port );
360
361                $this->parser = xml_parser_create();
362                $this->httpvars = $httpvars;
363                $this->xmlhandler = new TorqueXMLHandler();
364                xml_set_element_handler( $this->parser, array( &$this->xmlhandler, 'startElement' ), array( &$this->xmlhandler, 'stopElement' ) );
365        }
366
367        function parseXML() {
368
369                $src = &$this->source;
370                $this->data = $src->getData();
371
372                if ( !xml_parse( &$this->parser, $this->data ) )
373                        $error = sprintf( 'XML error: %s at %d', xml_error_string( xml_get_error_code( &$this->parser ) ), xml_get_current_line_number( &$this->parser ) );
374        }
375
376        function printInfo() {
377                $handler = $this->xmlhandler;
378                $handler->printInfo();
379        }
380
381        function getNodes() {
382                $handler = $this->xmlhandler;
383                return $handler->getNodes();
384        }
385
386        function getCpus() {
387                $handler = $this->xmlhandler;
388                return $handler->getCpus();
389        }
390
391        function getJobs() {
392                $handler = $this->xmlhandler;
393                return $handler->getJobs();
394        }
395
396        function getHeartbeat() {
397                $handler = $this->xmlhandler;
398                return $handler->getHeartbeat();
399        }
400}
401
402class TorqueXMLHandler {
403
404        var $clusters, $heartbeat, $nodes, $jobs;
405
406        function TorqueXMLHandler() {
407                $jobs = array();
408                $clusters = array();
409                $nodes = array();
410                $heartbeat = array();
411        }
412
413        function getCpus() {
414
415                $cpus = 0;
416
417                foreach( $this->jobs as $jobid=>$jobattrs ) {
418
419                        $nodes = count( $jobattrs[nodes] );
420                        $ppn = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
421                        $mycpus = $nodes * $ppn;
422
423                        $cpus = $cpus + $mycpus;
424                }
425        }
426
427        function startElement( $parser, $name, $attrs ) {
428
429                $jobs = &$this->jobs;
430                $nodes = &$this->nodes;
431
432                if ( $attrs[TN] ) {
433
434                        // Ignore dead metrics. Detect and mask failures.
435                        if ( $attrs[TN] > $attrs[TMAX] * 4 )
436                                return;
437                }
438
439                $jobid = null;
440
441                // printf( '%s=%s', $attrs[NAME], $attrs[VAL] );
442
443                if( $name == 'CLUSTER' ) {
444
445                        $clustername = $attrs[VAL];
446
447                        if( !isset( $clusters[$clustername] ) )
448                                $clusters[$clustername] = array();
449
450                } else if( $name == 'HOST' ) {
451
452                        $hostname = $attrs[NAME];
453                        $location = $attrs[LOCATION];
454
455                        if( !isset( $this->nodes[$hostname] ) )
456                                $this->nodes[$hostname] = new NodeImage( $hostname );
457
458                } else if( $name == 'METRIC' and strstr( $attrs[NAME], 'TOGA' ) ) {
459
460                        if( strstr( $attrs[NAME], 'TOGA-HEARTBEAT' ) ) {
461
462                                $this->heartbeat['time'] = $attrs[VAL];
463                                //printf( "heartbeat %s\n", $heartbeat['time'] );
464
465                        } else if( strstr( $attrs[NAME], 'TOGA-JOB' ) ) {
466
467                                sscanf( $attrs[NAME], 'TOGA-JOB-%d', $jobid );
468
469                                //printf( "jobid %s\n", $jobid );
470
471                                if( !isset( $jobs[$jobid] ) )
472                                        $jobs[$jobid] = array();
473
474                                $fields = explode( ' ', $attrs[VAL] );
475
476                                foreach( $fields as $f ) {
477                                        $togavalues = explode( '=', $f );
478
479                                        $toganame = $togavalues[0];
480                                        $togavalue = $togavalues[1];
481
482                                        //printf( "\t%s\t= %s\n", $toganame, $togavalue );
483
484                                        if( $toganame == 'nodes' ) {
485
486                                                if( $jobs[$jobid][status] == 'R' ) {
487                                               
488                                                        if( !isset( $jobs[$jobid][$toganame] ) )
489                                                                $jobs[$jobid][$toganame] = array();
490
491                                                        $mynodes = explode( ';', $togavalue );
492
493                                                        foreach( $mynodes as $node )
494
495                                                                $jobs[$jobid][$toganame][] = $node;
496
497                                                } else if( $jobs[$jobid][status] == 'Q' ) {
498
499                                                        $jobs[$jobid][$toganame] = $togavalue;
500                                                }
501                                               
502                                        } else {
503
504                                                $jobs[$jobid][$toganame] = $togavalue;
505                                        }
506                                }
507
508                                if( isset( $jobs[$jobid][domain] ) and isset( $jobs[$jobid][nodes] ) ) {
509                       
510                                        $nr_nodes = count( $jobs[$jobid][nodes] );
511                       
512                                        foreach( $jobs[$jobid][nodes] as $node ) {
513
514                                                $host = $node.'.'.$jobs[$jobid][domain];
515                               
516                                                if( !isset( $this->nodes[$host] ) )
517                                                        $my_node = new NodeImage( $host );
518                                                else
519                                                        $my_node = $this->nodes[$host];
520
521                                                if( !$my_node->hasJob( $jobid ) )
522
523                                                        if( isset( $jobs[$jobid][ppn] ) )
524                                                                $my_node->addJob( $jobid, ((int) $jobs[$jobid][ppn]) );
525                                                        else
526                                                                $my_node->addJob( $jobid, 1 );
527
528                                                $this->nodes[$host] = $my_node;
529                                        }
530                                }
531                        }
532                }
533                $this->jobs = $jobs;
534        }
535
536        function stopElement( $parser, $name ) {
537        }
538
539        function printInfo() {
540
541                $jobs = &$this->jobs;
542
543                printf( "---jobs---\n" );
544
545                foreach( $jobs as $jobid => $job ) {
546
547                        printf( "job %s\n", $jobid );
548
549                        if( isset( $job[nodes] ) ) {
550
551                                foreach( $job[nodes] as $node ) {
552
553                                        $mynode = $this->nodes[$node];
554                                        $hostname = $mynode->getHostname();
555                                        $location = $mynode->getLocation();
556
557                                        printf( "\t- node %s\tlocation %s\n", $hostname, $location );
558                                        //$this->nodes[$hostname]->setLocation( "hier draait job ".$jobid );
559                                }
560                        }
561                }
562
563                printf( "---nodes---\n" );
564
565                $nodes = &$this->nodes;
566
567                foreach( $nodes as $node ) {
568
569                        $hostname = $node->getHostname();
570                        $location = $node->getLocation();
571                        $jobs = implode( ' ', $node->getJobs() );
572                        printf( "* node %s\tlocation %s\tjobs %s\n", $hostname, $location, $jobs );
573                }
574        }
575
576        function getNodes() {
577                return $this->nodes;
578        }
579
580        function getJobs() {
581                return $this->jobs;
582        }
583
584        function getHeartbeat() {
585                return $this->heartbeat['time'];
586        }
587}
588
589class NodeImage {
590
591        var $image, $x, $y, $hostname, $jobs, $tasks, $showinfo;
592
593        function NodeImage( $hostname ) {
594
595                $this->jobs = array();
596                //$this->image = $image;
597                //$this->x = $x;
598                //$this->y = $y;
599                $this->tasks = 0;
600                $this->hostname = $hostname;
601                $this->cpus = $this->determineCpus();
602                $this->showinfo = 1;
603        }
604
605        function addJob( $jobid, $cpus ) {
606                $jobs = &$this->jobs;
607
608                $jobs[] = $jobid;
609                $this->jobs = $jobs;
610
611                $this->addTask( $cpus );
612        }
613
614        function hasJob( $jobid ) {
615
616                $jobfound = 0;
617
618                if( count( $this->jobs ) > 0 )
619                        foreach( $this->jobs as $job )
620
621                                if( $job == $jobid )
622                                        $jobfound = 1;
623
624                return $jobfound;
625        }
626
627        function addTask( $cpus ) {
628
629                $this->tasks = $this->tasks + $cpus;
630        }
631
632        function setImage( $image ) {
633
634                $this->image = $image;
635        }
636
637        function setCoords( $x, $y ) {
638
639                $this->x = $x;
640                $this->y = $y;
641        }
642
643        function colorHex( $color ) {
644       
645                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) );
646
647                return $my_color;
648        }
649
650        function setLoad( $load ) {
651                $this->load = $load;
652        }
653
654        function setHostname( $hostname ) {
655                $this->hostname = $hostname;
656        }
657
658        function getHostname() {
659                return $this->hostname;
660        }
661
662        function getJobs() {
663                return $this->jobs;
664        }
665
666        function setShowinfo( $showinfo ) {
667                $this->showinfo = $showinfo;
668        }
669
670        function draw() {
671
672                $this->drawSmall();
673        }
674
675        function drawBig() {
676
677        }
678
679        function drawSmall() {
680
681                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
682                global $JOB_NODE_MARKING;
683
684                $black_color = imageColorAllocate( $this->image, 0, 0, 0 );
685                $size = $SMALL_CLUSTERIMAGE_NODEWIDTH;
686
687                imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+($size), $this->y+($size), $black_color );
688
689                if( $this->showinfo) {
690               
691                        $this->load = $this->determineLoad();
692
693                        if( !isset( $this->image ) or !isset( $this->x ) or !isset( $this->y ) ) {
694                                printf( "aborting\n" );
695                                printf( "x %d y %d load %f\n", $this->x, $this->y, $load );
696                                return;
697                        }
698
699
700                        // Convert Ganglias Hexadecimal load color to a Decimal one
701                        //
702                        $load = $this->determineLoad(); 
703                        $usecolor = $this->colorHex( load_color($load) );
704                        imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor );
705                        if( count( $this->jobs ) > 0 )
706                                imageString( $this->image, 1, $this->x+(($size/2)-2), $this->y+(($size/2)-3), $JOB_NODE_MARKING, $black_color );
707
708                } else {
709
710                        // White
711                        $usecolor = imageColorAllocate( $this->image, 255, 255, 255 );
712                        imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor );
713                }
714
715
716        }
717
718        function determineCpus() {
719
720                global $metrics;
721
722                $cpus = $metrics[$this->hostname][cpu_num][VAL];
723                if (!$cpus) $cpus=1;
724
725                return $cpus;
726        }
727
728        function determineLoad() {
729
730                global $metrics;
731
732                $load_one = $metrics[$this->hostname][load_one][VAL];
733                $load = ((float) $load_one)/$this->cpus;
734
735                return $load;
736        }
737}
738
739class ClusterImage {
740
741        var $dataget, $image, $clustername;
742        var $filtername, $filters;
743
744        function ClusterImage( $clustername ) {
745
746                $this->dataget = new DataGatherer();
747                $this->clustername = $clustername;
748                $this->filters = array();
749        }
750
751        function setFilter( $filtername, $filtervalue ) {
752
753                //printf("filter %s = %s\n", $filtername, $filtervalue );
754                //printf( "filter set to %s = %s\n", $filtername, $filtervalue );
755                $this->filters[$filtername] = $filtervalue;
756                //print_r($this->filters);
757        }
758
759        function filterNodes( $jobs, $nodes ) {
760
761                $filtered_nodes = array();
762
763                foreach( $nodes as $node ) {
764
765                        $hostname = $node->getHostname();
766
767                        $addhost = 1;
768
769                        if( count( $this->filters ) > 0 ) {
770
771                                $mynjobs = $node->getJobs();
772
773                                if( count( $mynjobs ) > 0 ) {
774
775                                        foreach( $mynjobs as $myjob ) {
776
777                                                foreach( $this->filters as $filtername => $filtervalue ) {
778
779                                                        //printf("filter bla %s = %s\n", $filtername,$filtervalue );
780
781                                                        if( $filtername!=null && $filtername!='' ) {
782
783                                                                if( $filtername == 'jobid' && !$node->hasJob( $filtervalue) ) {
784                                                                        $addhost = 0;
785                                                                        //printf("host %s has no job %s\n", $hostname, $filtervalue);
786                                                                } else if( $filtername != 'jobid' ) {
787                                                                        //printf("myjob is %s\n", $myjob );
788                                                                        if( $jobs[$myjob][$filtername] != $filtervalue ) {
789                                                                                //printf("host %s has no job with %s=%s\n", $hostname, $filtername, $filtervalue);
790                                                                                $addhost = 0;
791                                                                        }
792                                                                }
793                                                        }
794                                                }
795                                        }
796                                } else
797                                        $addhost = 0;
798                        }
799
800                        if( $addhost )
801                                $filtered_nodes[] = $hostname;
802                }
803
804                return $filtered_nodes;
805        }
806
807        function draw() {
808
809                //printf("stopt met uitvoer");
810                //return;
811
812                global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH;
813       
814                $mydatag = $this->dataget;
815                $mydatag->parseXML();
816
817                //$max_width = 250;
818                //$node_width = 11;
819
820                $max_width = $SMALL_CLUSTERIMAGE_MAXWIDTH;
821                $node_width = $SMALL_CLUSTERIMAGE_NODEWIDTH;
822
823                //printf( "cmaxw %s nmaxw %s", $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH );
824
825                $nodes = $mydatag->getNodes();
826                $nodes_hosts = array_keys( $nodes );
827
828                $nodes_nr = count( $nodes );
829
830                $nodes_size = $nodes_nr*$node_width;
831                $node_rows = 0;
832
833                if( $nodes_size > $max_width ) {
834                        $nodes_per_row = ( (int) ($max_width/$node_width) );
835                } else {
836                        $nodes_per_row = $nodes_size;
837                        $node_rows = 1;
838                }
839
840                if( $nodes_per_row < $nodes_nr ) {
841                        $node_rows = ( (int) ($nodes_nr/$nodes_per_row) );
842                        $node_rest = fmod( $nodes_nr, $nodes_per_row );
843                        //printf( "nodesnr %d noderest %f\n", $nodes_nr, $node_rest );
844                        if( $node_rest > 0 ) {
845                                $node_rows++;
846                                //printf( "noderows %d\n", $node_rows );
847                        }
848                }
849
850                //printf( "imagecreate: %dx%d", ($nodes_per_row*$node_width), ($node_rows*$node_width) );
851                $image = imageCreateTrueColor( ($nodes_per_row*$node_width)+1, ($node_rows*$node_width)+1 );
852                $colorwhite = imageColorAllocate( $image, 255, 255, 255 );
853                imageFill( $image, 0, 0, $colorwhite );
854
855                $jobs = $mydatag->getJobs();
856                //printf("filtername = %s\n", $filtername );
857                $filtered_nodes = $this->filterNodes( $jobs, $nodes );
858
859                //print_r($filtered_nodes);
860
861                for( $n = 0; $n < $node_rows; $n++ ) {
862                       
863                        for( $m = 0; $m < $nodes_per_row; $m++ ) {
864                       
865                                $x = ($m * $node_width);
866                                $y = ($n * $node_width);
867
868                                $cur_node = ($n * $nodes_per_row) + ($m);
869                                $host = $nodes_hosts[$cur_node];
870
871
872                                if( isset( $nodes[$host] ) ) {
873
874                                        $nodes[$host]->setCoords( $x, $y );
875                                        $nodes[$host]->setImage( $image );
876
877                                        if( !in_array( $host, $filtered_nodes ) )
878                                                $nodes[$host]->setShowinfo( 0 );
879
880                                        $nodes[$host]->draw();
881                                }
882                        }
883                }
884               
885                header( 'Content-type: image/png' );
886                imagePNG( $image );
887                imageDestroy( $image );
888        }
889}
890
891//$my_data = new DataGatherer();
892//$my_data->parseXML();
893//$my_data->printInfo();
894
895//$ic = new ClusterImage( "LISA Cluster" );
896//$ic->draw();
897?>
Note: See TracBrowser for help on using the repository browser.