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

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

web/addons/job_monarch/conf.php:

  • renamed TARCHD to JOB_ARCHIVE
  • added options: JOB_ARCHIVE_DIR, RRDTOOL

web/addons/job_monarch/overview.php:

  • renamed TARCHD to JOB_ARCHIVE

web/addons/job_monarch/index.php:

  • renamed TARCHD to JOB_ARCHIVE

web/addons/job_monarch/libtoga.php:

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