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

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

toga/libtoga.php:

  • Only search jobs in 'F' state

toga/search.php:

  • Fixed form validation

web/addons/toga/graph.php:

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