source: trunk/web/addons/job_monarch/libtoga.php @ 208

Last change on this file since 208 was 207, checked in by bastiaans, 18 years ago

job_monarch/conf.php:

  • added option JOB_ARCHIVE_DBASE

job_monarch/libtoga.php:

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