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

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

web/addons/toga/conf.php:

  • Set to actual gmetad port (was gmond's)

web/addons/toga/image.php:

  • clustername parse fix

web/addons/toga/libtoga.php:

web/addons/toga/overview.php:

  • Use clustername when getting data
File size: 20.7 KB
RevLine 
[102]1<?php
[117]2// If php is compiled without globals
3//
[145]4//if ( !empty( $_GET ) ) {
5//        extract( $_GET );
6//}
[117]7
[103]8class HTTPVariables {
9
10        var $clustername, $metricname;
[112]11        var $restvars, $httpvars;
[103]12
[117]13        function HTTPVariables( $httpvars, $getvars ) {
[103]14
[110]15                $this->restvars = array();
16
[117]17                $this->clustername = $httpvars["c"] ? $httpvars["c"] : null;
18                $this->metricname = $httpvars["m"] ? $httpvars["m"] : null;
[103]19
[117]20                foreach( $httpvars as $httpvar => $httpval ) {
[110]21                       
22                        if( $httpval ) {
23                                $this->restvars[$httpvar] = $httpval;
24                        }
25                }
[117]26
27                foreach( $getvars as $getvar => $getval ) {
28
29                        if( $getval ) {
30                                $this->restvars[$getvar] = $getval;
31                        }
32                }
[103]33        }
34
35        function getClusterName() {
36                return $this->clustername;
37        }
38
39        function getMetricName() {
40                return $this->metricname;
41        }
[110]42
43        function getHttpVar( $var ) {
44                if( isset( $this->restvars[$var] ) )
[112]45                        return $this->restvars[$var];
[110]46                else
47                        return null;
48        }
[103]49}
50
[112]51// Toga's conf
52//
53include_once "./conf.php";
54
[117]55global $GANGLIA_PATH;
[112]56
[117]57$my_dir = getcwd();
[112]58
[117]59// Load Ganglia's PHP
60chdir( $GANGLIA_PATH );
[112]61
[117]62include_once "./conf.php";
63include_once "./functions.php";
64include_once "./ganglia.php";
[145]65include_once "./get_context.php";
66unset( $start );
67$context = 'cluster';
[117]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
[112]76// Set cluster context so that Ganglia will
77// provide us with the correct metrics array
78//
[126]79global $context, $clustername, $reports;
[129]80
[117]81//$clustername = $httpvars->getClusterName();
[112]82
[126]83global $default_metric;
84
[112]85// Ganglia's array of host metrics
86//
[126]87global $metrics, $hosts_up;
[112]88
[130]89class TarchDbase {
90
[138]91        var $ip, $dbase, $conn;
[130]92
93        function TarchDbase( $ip = null, $dbase = 'toga' ) {
94                $this->ip = $ip;
95                $this->dbase = $dbase;
[138]96                $this->conn = null;
[130]97        }
98
99        function connect() {
100
101                if( $this->ip == null and $this->dbase == 'toga' )
[138]102                        $this->conn = pg_connect( "dbname=".$this->dbase );
[130]103                else
104                        $this->conn = pg_connect( "host=".$this->ip." dbase=".$this->dbase );
105        }
[138]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 ) 
[152]110                        $query = "SELECT job_id FROM jobs WHERE job_id = '$id' AND job_status = 'F'";
[138]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";
[142]122                        if( $start_to_time )
[138]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
[152]129                        $query = "SELECT job_id FROM jobs WHERE job_status = 'F' AND ";
[138]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 );
[140]143
144                $ret = array();
145
146                foreach( $ids as $crow)
147                        $ret[] = $crow[job_id];
148
149                return $ret;
[138]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
[141]160                        $nodes[] = $this->getNodeArray( $result_row[node_id] );
[138]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
[141]173                        $jobs[] = $this->getJobArray( $result_row[job_id] );
[138]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
[141]199                        $rmap_key = array_reverse( $map_key );
200                        array_pop( $rmap_key );
201                        $map_key = array_reverse( $rmap_key );
[138]202                       
[141]203                        $newkey = implode( '_', $map_key );
204                       
[138]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
[143]218                //printf( "query = [%s]\n", $query );
[138]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        }
[130]226}
227
[138]228class TarchRrdGraph {
[130]229        var $rrdbin, $rrdvalues, $clustername, $hostname, $tempdir, $tarchdir, $metrics;
230
[145]231        function TarchRrdGraph( $clustername, $hostname, $rrdbin = '/usr/bin/rrdtool', $tarchdir = '/data/toga/rrds' ) {
232       
[130]233                $this->rrdbin = $rrdbin;
234                $this->rrdvalues = array();
235                $this->tarchdir = $tarchdir;
[145]236                $this->clustername = $clustername;
237                $this->hostname = $hostname;
[130]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 );
[135]257                //print_r( $output );
[130]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
[145]279                //printf("start = %s end = %s\n", $start, $end );
[130]280                $times = array();
281                $dirlist = $this->dirList( $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname );
[145]282
283                //print_r( $dirlist );
284
[130]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
[145]296                //printf( "first = %s last = %s\n", $first, $last );
297
[130]298                foreach( $dirlist as $dir ) {
299
[145]300                        //printf( "dir %s ", $dir );
301
302                        if( $dir >= $first and $dir <= $last and !array_key_exists( $dir, $times ) ) {
303                       
[130]304                                $times[] = $dir;
[145]305                                //printf("newtime %s ", $dir );
306
307                        }
[130]308                }
309
[145]310                //print_r( $times );
311
[130]312                sort( $times );
313
[145]314                //print_r( $times );
315
[130]316                return $times;
317        }
318
[145]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
[130]349        function graph( $descr ) {
[141]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#
[130]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;
[145]360                return 0;
[130]361        }
362}
363
[103]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
[106]381                if( !$fp ) {
[103]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
[105]399        var $xmlhandler, $data, $httpvars;
[103]400
[163]401        function DataGatherer( $cluster ) {
[103]402
[110]403                global $DATA_SOURCE;
[163]404       
405                //printf("dg cluster = %s\n", $cluster );
[110]406                $ds_fields = explode( ':', $DATA_SOURCE );
407                $ds_ip = $ds_fields[0];
408                $ds_port = $ds_fields[1];
409
410                $this->source = new DataSource( $ds_ip, $ds_port );
411
[103]412                $this->parser = xml_parser_create();
[112]413                $this->httpvars = $httpvars;
[163]414                $this->xmlhandler = new TorqueXMLHandler( $cluster );
[103]415                xml_set_element_handler( $this->parser, array( &$this->xmlhandler, 'startElement' ), array( &$this->xmlhandler, 'stopElement' ) );
416        }
417
418        function parseXML() {
419
420                $src = &$this->source;
421                $this->data = $src->getData();
422
[112]423                if ( !xml_parse( &$this->parser, $this->data ) )
[103]424                        $error = sprintf( 'XML error: %s at %d', xml_error_string( xml_get_error_code( &$this->parser ) ), xml_get_current_line_number( &$this->parser ) );
425        }
426
[105]427        function printInfo() {
428                $handler = $this->xmlhandler;
429                $handler->printInfo();
430        }
431
[106]432        function getNodes() {
433                $handler = $this->xmlhandler;
434                return $handler->getNodes();
435        }
436
[124]437        function getCpus() {
438                $handler = $this->xmlhandler;
439                return $handler->getCpus();
440        }
441
[106]442        function getJobs() {
443                $handler = $this->xmlhandler;
444                return $handler->getJobs();
445        }
446
[114]447        function getHeartbeat() {
448                $handler = $this->xmlhandler;
449                return $handler->getHeartbeat();
450        }
[103]451}
452
453class TorqueXMLHandler {
454
[163]455        var $clusters, $heartbeat, $nodes, $jobs, $clustername, $proc_cluster;
[104]456
[163]457        function TorqueXMLHandler( $clustername ) {
[105]458                $jobs = array();
[104]459                $clusters = array();
[163]460                $this->nodes = array();
[104]461                $heartbeat = array();
[163]462                $this->clustername = $clustername;
463                //printf(" cluster set to %s \n", $this->clustername );
[103]464        }
465
[124]466        function getCpus() {
467
468                $cpus = 0;
469
470                foreach( $this->jobs as $jobid=>$jobattrs ) {
471
472                        $nodes = count( $jobattrs[nodes] );
473                        $ppn = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
474                        $mycpus = $nodes * $ppn;
475
476                        $cpus = $cpus + $mycpus;
477                }
478        }
479
[103]480        function startElement( $parser, $name, $attrs ) {
481
[105]482                $jobs = &$this->jobs;
[111]483                $nodes = &$this->nodes;
[105]484
[103]485                if ( $attrs[TN] ) {
486
487                        // Ignore dead metrics. Detect and mask failures.
488                        if ( $attrs[TN] > $attrs[TMAX] * 4 )
489                                return;
490                }
491
492                $jobid = null;
493
[163]494                //printf( '%s=%s', $attrs[NAME], $attrs[VAL] );
[103]495
[163]496                //printf( "clustername = %s proc_cluster = %s\n", $this->clustername, $this->proc_cluster );
497
[104]498                if( $name == 'CLUSTER' ) {
[103]499
[163]500                        $this->proc_cluster = $attrs[NAME];
501                        //printf( "Found cluster %s\n", $attrs[NAME] );
502                        //print_r( $attrs );
[103]503
[163]504                        //if( !isset( $clusters[$clustername] ) )
505                        //      $clusters[$clustername] = array();
[103]506
[163]507                } else if( $name == 'HOST' and $this->proc_cluster == $this->clustername) {
[105]508
509                        $hostname = $attrs[NAME];
510                        $location = $attrs[LOCATION];
[163]511                        //printf( "Found node %s\n", $hostname );
[105]512
[111]513                        if( !isset( $this->nodes[$hostname] ) )
[163]514                                $nodes[$hostname] = new NodeImage( $hostname );
[105]515
[163]516                } else if( $name == 'METRIC' and strstr( $attrs[NAME], 'TOGA' ) and $this->proc_cluster == $this->clustername ) {
[103]517
[104]518                        if( strstr( $attrs[NAME], 'TOGA-HEARTBEAT' ) ) {
[103]519
[114]520                                $this->heartbeat['time'] = $attrs[VAL];
[106]521                                //printf( "heartbeat %s\n", $heartbeat['time'] );
[104]522
523                        } else if( strstr( $attrs[NAME], 'TOGA-JOB' ) ) {
524
525                                sscanf( $attrs[NAME], 'TOGA-JOB-%d', $jobid );
526
[106]527                                //printf( "jobid %s\n", $jobid );
[104]528
529                                if( !isset( $jobs[$jobid] ) )
530                                        $jobs[$jobid] = array();
531
532                                $fields = explode( ' ', $attrs[VAL] );
533
534                                foreach( $fields as $f ) {
535                                        $togavalues = explode( '=', $f );
536
537                                        $toganame = $togavalues[0];
538                                        $togavalue = $togavalues[1];
539
[106]540                                        //printf( "\t%s\t= %s\n", $toganame, $togavalue );
[104]541
[103]542                                        if( $toganame == 'nodes' ) {
543
[135]544                                                if( $jobs[$jobid][status] == 'R' ) {
545                                               
546                                                        if( !isset( $jobs[$jobid][$toganame] ) )
547                                                                $jobs[$jobid][$toganame] = array();
[104]548
[135]549                                                        $mynodes = explode( ';', $togavalue );
[103]550
[135]551                                                        foreach( $mynodes as $node )
[103]552
[135]553                                                                $jobs[$jobid][$toganame][] = $node;
554
555                                                } else if( $jobs[$jobid][status] == 'Q' ) {
556
557                                                        $jobs[$jobid][$toganame] = $togavalue;
558                                                }
559                                               
[104]560                                        } else {
[103]561
[105]562                                                $jobs[$jobid][$toganame] = $togavalue;
[104]563                                        }
[103]564                                }
[111]565
566                                if( isset( $jobs[$jobid][domain] ) and isset( $jobs[$jobid][nodes] ) ) {
[112]567                       
568                                        $nr_nodes = count( $jobs[$jobid][nodes] );
569                       
[111]570                                        foreach( $jobs[$jobid][nodes] as $node ) {
571
572                                                $host = $node.'.'.$jobs[$jobid][domain];
573                               
574                                                if( !isset( $this->nodes[$host] ) )
575                                                        $my_node = new NodeImage( $host );
576                                                else
577                                                        $my_node = $this->nodes[$host];
578
579                                                if( !$my_node->hasJob( $jobid ) )
580
581                                                        if( isset( $jobs[$jobid][ppn] ) )
582                                                                $my_node->addJob( $jobid, ((int) $jobs[$jobid][ppn]) );
583                                                        else
584                                                                $my_node->addJob( $jobid, 1 );
585
[163]586                                                $nodes[$host] = $my_node;
[111]587                                        }
588                                }
[103]589                        }
590                }
[114]591                $this->jobs = $jobs;
[163]592                //print_r( $nodes );
593                $this->nodes = $nodes;
[103]594        }
595
596        function stopElement( $parser, $name ) {
597        }
[105]598
599        function printInfo() {
600
601                $jobs = &$this->jobs;
602
603                printf( "---jobs---\n" );
604
605                foreach( $jobs as $jobid => $job ) {
606
607                        printf( "job %s\n", $jobid );
608
609                        if( isset( $job[nodes] ) ) {
610
611                                foreach( $job[nodes] as $node ) {
612
613                                        $mynode = $this->nodes[$node];
614                                        $hostname = $mynode->getHostname();
615                                        $location = $mynode->getLocation();
616
617                                        printf( "\t- node %s\tlocation %s\n", $hostname, $location );
618                                        //$this->nodes[$hostname]->setLocation( "hier draait job ".$jobid );
619                                }
620                        }
621                }
622
623                printf( "---nodes---\n" );
624
625                $nodes = &$this->nodes;
626
627                foreach( $nodes as $node ) {
628
629                        $hostname = $node->getHostname();
630                        $location = $node->getLocation();
631                        $jobs = implode( ' ', $node->getJobs() );
632                        printf( "* node %s\tlocation %s\tjobs %s\n", $hostname, $location, $jobs );
633                }
634        }
[106]635
636        function getNodes() {
[163]637                //print_r( $this->nodes );
[106]638                return $this->nodes;
639        }
640
641        function getJobs() {
642                return $this->jobs;
643        }
[114]644
645        function getHeartbeat() {
646                return $this->heartbeat['time'];
647        }
[103]648}
649
[107]650class NodeImage {
[102]651
[122]652        var $image, $x, $y, $hostname, $jobs, $tasks, $showinfo;
[102]653
[111]654        function NodeImage( $hostname ) {
[102]655
[111]656                $this->jobs = array();
657                //$this->image = $image;
658                //$this->x = $x;
659                //$this->y = $y;
660                $this->tasks = 0;
[110]661                $this->hostname = $hostname;
[111]662                $this->cpus = $this->determineCpus();
[122]663                $this->showinfo = 1;
[102]664        }
665
[111]666        function addJob( $jobid, $cpus ) {
667                $jobs = &$this->jobs;
668
669                $jobs[] = $jobid;
670                $this->jobs = $jobs;
671
672                $this->addTask( $cpus );
673        }
674
675        function hasJob( $jobid ) {
676
677                $jobfound = 0;
678
679                if( count( $this->jobs ) > 0 )
680                        foreach( $this->jobs as $job )
681
682                                if( $job == $jobid )
683                                        $jobfound = 1;
684
685                return $jobfound;
686        }
687
688        function addTask( $cpus ) {
689
690                $this->tasks = $this->tasks + $cpus;
691        }
692
693        function setImage( $image ) {
694
695                $this->image = $image;
696        }
697
[106]698        function setCoords( $x, $y ) {
[107]699
[106]700                $this->x = $x;
701                $this->y = $y;
702        }
703
[102]704        function colorHex( $color ) {
705       
[109]706                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) );
[102]707
708                return $my_color;
709        }
710
[106]711        function setLoad( $load ) {
712                $this->load = $load;
713        }
714
[108]715        function setHostname( $hostname ) {
716                $this->hostname = $hostname;
717        }
718
[122]719        function getHostname() {
720                return $this->hostname;
721        }
722
[114]723        function getJobs() {
724                return $this->jobs;
725        }
726
[122]727        function setShowinfo( $showinfo ) {
728                $this->showinfo = $showinfo;
729        }
730
[111]731        function draw() {
[106]732
[122]733                $this->drawSmall();
734        }
[110]735
[122]736        function drawBig() {
[111]737
[122]738        }
[106]739
[122]740        function drawSmall() {
[106]741
[122]742                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
743                global $JOB_NODE_MARKING;
[102]744
[122]745                $black_color = imageColorAllocate( $this->image, 0, 0, 0 );
[110]746                $size = $SMALL_CLUSTERIMAGE_NODEWIDTH;
[107]747
[110]748                imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+($size), $this->y+($size), $black_color );
749
[122]750                if( $this->showinfo) {
751               
752                        $this->load = $this->determineLoad();
[111]753
[122]754                        if( !isset( $this->image ) or !isset( $this->x ) or !isset( $this->y ) ) {
755                                printf( "aborting\n" );
756                                printf( "x %d y %d load %f\n", $this->x, $this->y, $load );
757                                return;
758                        }
[111]759
760
[122]761                        // Convert Ganglias Hexadecimal load color to a Decimal one
762                        //
763                        $load = $this->determineLoad(); 
764                        $usecolor = $this->colorHex( load_color($load) );
765                        imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor );
766                        if( count( $this->jobs ) > 0 )
767                                imageString( $this->image, 1, $this->x+(($size/2)-2), $this->y+(($size/2)-3), $JOB_NODE_MARKING, $black_color );
[111]768
[122]769                } else {
[111]770
[122]771                        // White
772                        $usecolor = imageColorAllocate( $this->image, 255, 255, 255 );
773                        imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor );
774                }
775
776
[102]777        }
778
[111]779        function determineCpus() {
[102]780
[108]781                global $metrics;
782
783                $cpus = $metrics[$this->hostname][cpu_num][VAL];
[107]784                if (!$cpus) $cpus=1;
[111]785
786                return $cpus;
787        }
788
789        function determineLoad() {
790
791                global $metrics;
792
[108]793                $load_one = $metrics[$this->hostname][load_one][VAL];
[111]794                $load = ((float) $load_one)/$this->cpus;
[102]795
[111]796                return $load;
[106]797        }
798}
799
[107]800class ClusterImage {
[106]801
[111]802        var $dataget, $image, $clustername;
[124]803        var $filtername, $filters;
[106]804
[122]805        function ClusterImage( $clustername ) {
[114]806
[163]807                //printf( "image cluster = %s\n", $clustername );
808                $this->dataget = new DataGatherer( $clustername );
[111]809                $this->clustername = $clustername;
[124]810                $this->filters = array();
[106]811        }
812
[122]813        function setFilter( $filtername, $filtervalue ) {
814
[124]815                //printf("filter %s = %s\n", $filtername, $filtervalue );
[122]816                //printf( "filter set to %s = %s\n", $filtername, $filtervalue );
[124]817                $this->filters[$filtername] = $filtervalue;
818                //print_r($this->filters);
[122]819        }
820
821        function filterNodes( $jobs, $nodes ) {
822
823                $filtered_nodes = array();
824
825                foreach( $nodes as $node ) {
826
827                        $hostname = $node->getHostname();
828
[124]829                        $addhost = 1;
[122]830
[124]831                        if( count( $this->filters ) > 0 ) {
832
833                                $mynjobs = $node->getJobs();
834
835                                if( count( $mynjobs ) > 0 ) {
836
837                                        foreach( $mynjobs as $myjob ) {
838
839                                                foreach( $this->filters as $filtername => $filtervalue ) {
840
841                                                        //printf("filter bla %s = %s\n", $filtername,$filtervalue );
842
843                                                        if( $filtername!=null && $filtername!='' ) {
844
845                                                                if( $filtername == 'jobid' && !$node->hasJob( $filtervalue) ) {
846                                                                        $addhost = 0;
847                                                                        //printf("host %s has no job %s\n", $hostname, $filtervalue);
848                                                                } else if( $filtername != 'jobid' ) {
849                                                                        //printf("myjob is %s\n", $myjob );
850                                                                        if( $jobs[$myjob][$filtername] != $filtervalue ) {
851                                                                                //printf("host %s has no job with %s=%s\n", $hostname, $filtername, $filtervalue);
852                                                                                $addhost = 0;
853                                                                        }
854                                                                }
855                                                        }
856                                                }
857                                        }
858                                } else
859                                        $addhost = 0;
860                        }
861
862                        if( $addhost )
[122]863                                $filtered_nodes[] = $hostname;
864                }
865
866                return $filtered_nodes;
867        }
868
[106]869        function draw() {
[110]870
[124]871                //printf("stopt met uitvoer");
872                //return;
873
[110]874                global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH;
875       
[107]876                $mydatag = $this->dataget;
[106]877                $mydatag->parseXML();
878
[110]879                //$max_width = 250;
880                //$node_width = 11;
[106]881
[110]882                $max_width = $SMALL_CLUSTERIMAGE_MAXWIDTH;
883                $node_width = $SMALL_CLUSTERIMAGE_NODEWIDTH;
884
885                //printf( "cmaxw %s nmaxw %s", $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH );
886
[106]887                $nodes = $mydatag->getNodes();
[111]888                $nodes_hosts = array_keys( $nodes );
[106]889
890                $nodes_nr = count( $nodes );
891
892                $nodes_size = $nodes_nr*$node_width;
893                $node_rows = 0;
894
895                if( $nodes_size > $max_width ) {
[107]896                        $nodes_per_row = ( (int) ($max_width/$node_width) );
[106]897                } else {
898                        $nodes_per_row = $nodes_size;
899                        $node_rows = 1;
900                }
901
902                if( $nodes_per_row < $nodes_nr ) {
[107]903                        $node_rows = ( (int) ($nodes_nr/$nodes_per_row) );
904                        $node_rest = fmod( $nodes_nr, $nodes_per_row );
905                        //printf( "nodesnr %d noderest %f\n", $nodes_nr, $node_rest );
906                        if( $node_rest > 0 ) {
[106]907                                $node_rows++;
[107]908                                //printf( "noderows %d\n", $node_rows );
[106]909                        }
910                }
911
[107]912                //printf( "imagecreate: %dx%d", ($nodes_per_row*$node_width), ($node_rows*$node_width) );
[109]913                $image = imageCreateTrueColor( ($nodes_per_row*$node_width)+1, ($node_rows*$node_width)+1 );
[107]914                $colorwhite = imageColorAllocate( $image, 255, 255, 255 );
915                imageFill( $image, 0, 0, $colorwhite );
[106]916
[122]917                $jobs = $mydatag->getJobs();
918                //printf("filtername = %s\n", $filtername );
919                $filtered_nodes = $this->filterNodes( $jobs, $nodes );
920
921                //print_r($filtered_nodes);
922
[106]923                for( $n = 0; $n < $node_rows; $n++ ) {
924                       
925                        for( $m = 0; $m < $nodes_per_row; $m++ ) {
[107]926                       
927                                $x = ($m * $node_width);
928                                $y = ($n * $node_width);
929
[109]930                                $cur_node = ($n * $nodes_per_row) + ($m);
[111]931                                $host = $nodes_hosts[$cur_node];
[107]932
[122]933
[111]934                                if( isset( $nodes[$host] ) ) {
935
936                                        $nodes[$host]->setCoords( $x, $y );
937                                        $nodes[$host]->setImage( $image );
[122]938
939                                        if( !in_array( $host, $filtered_nodes ) )
940                                                $nodes[$host]->setShowinfo( 0 );
941
[111]942                                        $nodes[$host]->draw();
943                                }
[106]944                        }
945                }
946               
[103]947                header( 'Content-type: image/png' );
[106]948                imagePNG( $image );
949                imageDestroy( $image );
[102]950        }
951}
952
[106]953//$my_data = new DataGatherer();
954//$my_data->parseXML();
955//$my_data->printInfo();
956
[112]957//$ic = new ClusterImage( "LISA Cluster" );
958//$ic->draw();
[102]959?>
Note: See TracBrowser for help on using the repository browser.