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
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";
[195]54include_once "./version.php";
[112]55
[117]56global $GANGLIA_PATH;
[206]57global $RRDTOOL;
58global $JOB_ARCHIVE_DIR;
[207]59global $JOB_ARCHIVE_DBASE;
[112]60
[117]61$my_dir = getcwd();
[112]62
[117]63// Load Ganglia's PHP
64chdir( $GANGLIA_PATH );
[112]65
[117]66include_once "./conf.php";
67include_once "./functions.php";
68include_once "./ganglia.php";
[145]69include_once "./get_context.php";
70unset( $start );
71$context = 'cluster';
[117]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
[112]80// Set cluster context so that Ganglia will
81// provide us with the correct metrics array
82//
[126]83global $context, $clustername, $reports;
[129]84
[117]85//$clustername = $httpvars->getClusterName();
[112]86
[126]87global $default_metric;
88
[112]89// Ganglia's array of host metrics
90//
[126]91global $metrics, $hosts_up;
[112]92
[130]93class TarchDbase {
94
[138]95        var $ip, $dbase, $conn;
[130]96
[204]97        function TarchDbase( $ip = null, $dbase = 'jobarch' ) {
[207]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];
[138]105                $this->conn = null;
[130]106        }
107
108        function connect() {
109
110                if( $this->ip == null and $this->dbase == 'toga' )
[138]111                        $this->conn = pg_connect( "dbname=".$this->dbase );
[130]112                else
[204]113                        $this->conn = pg_connect( "host=".$this->ip." dbname=".$this->dbase );
[130]114        }
[138]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 ) 
[152]119                        $query = "SELECT job_id FROM jobs WHERE job_id = '$id' AND job_status = 'F'";
[138]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";
[142]131                        if( $start_to_time )
[138]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
[152]138                        $query = "SELECT job_id FROM jobs WHERE job_status = 'F' AND ";
[138]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 );
[140]152
153                $ret = array();
154
155                foreach( $ids as $crow)
156                        $ret[] = $crow[job_id];
157
158                return $ret;
[138]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
[141]169                        $nodes[] = $this->getNodeArray( $result_row[node_id] );
[138]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
[141]182                        $jobs[] = $this->getJobArray( $result_row[job_id] );
[138]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
[141]208                        $rmap_key = array_reverse( $map_key );
209                        array_pop( $rmap_key );
210                        $map_key = array_reverse( $rmap_key );
[138]211                       
[141]212                        $newkey = implode( '_', $map_key );
213                       
[138]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
[143]227                //printf( "query = [%s]\n", $query );
[138]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        }
[130]235}
236
[138]237class TarchRrdGraph {
[130]238        var $rrdbin, $rrdvalues, $clustername, $hostname, $tempdir, $tarchdir, $metrics;
239
[206]240        function TarchRrdGraph( $clustername, $hostname ) {
241
242                global $RRDTOOL;
243                global $JOB_ARCHIVE_DIR;
244
245                $this->rrdbin = $RRDTOOL;
[130]246                $this->rrdvalues = array();
[206]247                $this->tarchdir = $JOB_ARCHIVE_DIR;
[145]248                $this->clustername = $clustername;
249                $this->hostname = $hostname;
[130]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 );
[135]269                //print_r( $output );
[130]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
[145]291                //printf("start = %s end = %s\n", $start, $end );
[130]292                $times = array();
293                $dirlist = $this->dirList( $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname );
[145]294
295                //print_r( $dirlist );
296
[130]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
[145]308                //printf( "first = %s last = %s\n", $first, $last );
309
[130]310                foreach( $dirlist as $dir ) {
311
[145]312                        //printf( "dir %s ", $dir );
313
314                        if( $dir >= $first and $dir <= $last and !array_key_exists( $dir, $times ) ) {
315                       
[130]316                                $times[] = $dir;
[145]317                                //printf("newtime %s ", $dir );
318
319                        }
[130]320                }
321
[145]322                //print_r( $times );
323
[130]324                sort( $times );
325
[145]326                //print_r( $times );
327
[130]328                return $times;
329        }
330
[145]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
[130]361        function graph( $descr ) {
[141]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#
[130]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;
[145]372                return 0;
[130]373        }
374}
375
[103]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
[106]393                if( !$fp ) {
[103]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
[177]398                stream_set_timeout( $fp, 30 );
399
[103]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
[105]413        var $xmlhandler, $data, $httpvars;
[103]414
[163]415        function DataGatherer( $cluster ) {
[103]416
[110]417                global $DATA_SOURCE;
[163]418       
419                //printf("dg cluster = %s\n", $cluster );
[110]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
[103]426                $this->parser = xml_parser_create();
[112]427                $this->httpvars = $httpvars;
[163]428                $this->xmlhandler = new TorqueXMLHandler( $cluster );
[103]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
[112]437                if ( !xml_parse( &$this->parser, $this->data ) )
[103]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
[105]441        function printInfo() {
442                $handler = $this->xmlhandler;
443                $handler->printInfo();
444        }
445
[106]446        function getNodes() {
447                $handler = $this->xmlhandler;
448                return $handler->getNodes();
449        }
450
[124]451        function getCpus() {
452                $handler = $this->xmlhandler;
453                return $handler->getCpus();
454        }
455
[106]456        function getJobs() {
457                $handler = $this->xmlhandler;
458                return $handler->getJobs();
459        }
460
[114]461        function getHeartbeat() {
462                $handler = $this->xmlhandler;
463                return $handler->getHeartbeat();
464        }
[103]465}
466
467class TorqueXMLHandler {
468
[163]469        var $clusters, $heartbeat, $nodes, $jobs, $clustername, $proc_cluster;
[104]470
[163]471        function TorqueXMLHandler( $clustername ) {
[105]472                $jobs = array();
[104]473                $clusters = array();
[163]474                $this->nodes = array();
[104]475                $heartbeat = array();
[163]476                $this->clustername = $clustername;
477                //printf(" cluster set to %s \n", $this->clustername );
[103]478        }
479
[124]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
[103]494        function startElement( $parser, $name, $attrs ) {
495
[105]496                $jobs = &$this->jobs;
[111]497                $nodes = &$this->nodes;
[105]498
[103]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
[163]508                //printf( '%s=%s', $attrs[NAME], $attrs[VAL] );
[103]509
[163]510                //printf( "clustername = %s proc_cluster = %s\n", $this->clustername, $this->proc_cluster );
511
[104]512                if( $name == 'CLUSTER' ) {
[103]513
[163]514                        $this->proc_cluster = $attrs[NAME];
515                        //printf( "Found cluster %s\n", $attrs[NAME] );
516                        //print_r( $attrs );
[103]517
[163]518                        //if( !isset( $clusters[$clustername] ) )
519                        //      $clusters[$clustername] = array();
[103]520
[163]521                } else if( $name == 'HOST' and $this->proc_cluster == $this->clustername) {
[105]522
523                        $hostname = $attrs[NAME];
524                        $location = $attrs[LOCATION];
[163]525                        //printf( "Found node %s\n", $hostname );
[105]526
[200]527                        if( !isset( $nodes[$hostname] ) )
[163]528                                $nodes[$hostname] = new NodeImage( $hostname );
[105]529
[163]530                } else if( $name == 'METRIC' and strstr( $attrs[NAME], 'TOGA' ) and $this->proc_cluster == $this->clustername ) {
[103]531
[104]532                        if( strstr( $attrs[NAME], 'TOGA-HEARTBEAT' ) ) {
[103]533
[114]534                                $this->heartbeat['time'] = $attrs[VAL];
[106]535                                //printf( "heartbeat %s\n", $heartbeat['time'] );
[104]536
537                        } else if( strstr( $attrs[NAME], 'TOGA-JOB' ) ) {
538
539                                sscanf( $attrs[NAME], 'TOGA-JOB-%d', $jobid );
540
[106]541                                //printf( "jobid %s\n", $jobid );
[104]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
[106]554                                        //printf( "\t%s\t= %s\n", $toganame, $togavalue );
[104]555
[103]556                                        if( $toganame == 'nodes' ) {
557
[135]558                                                if( $jobs[$jobid][status] == 'R' ) {
559                                               
560                                                        if( !isset( $jobs[$jobid][$toganame] ) )
561                                                                $jobs[$jobid][$toganame] = array();
[104]562
[135]563                                                        $mynodes = explode( ';', $togavalue );
[103]564
[135]565                                                        foreach( $mynodes as $node )
[103]566
[135]567                                                                $jobs[$jobid][$toganame][] = $node;
568
569                                                } else if( $jobs[$jobid][status] == 'Q' ) {
570
571                                                        $jobs[$jobid][$toganame] = $togavalue;
572                                                }
573                                               
[104]574                                        } else {
[103]575
[105]576                                                $jobs[$jobid][$toganame] = $togavalue;
[104]577                                        }
[103]578                                }
[111]579
580                                if( isset( $jobs[$jobid][domain] ) and isset( $jobs[$jobid][nodes] ) ) {
[112]581                       
582                                        $nr_nodes = count( $jobs[$jobid][nodes] );
583                       
[111]584                                        foreach( $jobs[$jobid][nodes] as $node ) {
585
[200]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];
[111]596                               
[200]597                                                if( !isset( $nodes[$host] ) )
[111]598                                                        $my_node = new NodeImage( $host );
599                                                else
[200]600                                                        $my_node = $nodes[$host];
[111]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
[163]609                                                $nodes[$host] = $my_node;
[111]610                                        }
611                                }
[103]612                        }
613                }
[114]614                $this->jobs = $jobs;
[163]615                //print_r( $nodes );
616                $this->nodes = $nodes;
[200]617                //print_r( $this->nodes );
[103]618        }
619
620        function stopElement( $parser, $name ) {
621        }
[105]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        }
[106]659
660        function getNodes() {
[163]661                //print_r( $this->nodes );
[106]662                return $this->nodes;
663        }
664
665        function getJobs() {
666                return $this->jobs;
667        }
[114]668
669        function getHeartbeat() {
670                return $this->heartbeat['time'];
671        }
[103]672}
673
[107]674class NodeImage {
[102]675
[122]676        var $image, $x, $y, $hostname, $jobs, $tasks, $showinfo;
[102]677
[111]678        function NodeImage( $hostname ) {
[102]679
[111]680                $this->jobs = array();
681                //$this->image = $image;
682                //$this->x = $x;
683                //$this->y = $y;
684                $this->tasks = 0;
[110]685                $this->hostname = $hostname;
[111]686                $this->cpus = $this->determineCpus();
[122]687                $this->showinfo = 1;
[102]688        }
689
[111]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
[106]722        function setCoords( $x, $y ) {
[107]723
[106]724                $this->x = $x;
725                $this->y = $y;
726        }
727
[102]728        function colorHex( $color ) {
729       
[109]730                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) );
[102]731
732                return $my_color;
733        }
734
[106]735        function setLoad( $load ) {
736                $this->load = $load;
737        }
738
[108]739        function setHostname( $hostname ) {
740                $this->hostname = $hostname;
741        }
742
[122]743        function getHostname() {
744                return $this->hostname;
745        }
746
[114]747        function getJobs() {
748                return $this->jobs;
749        }
750
[122]751        function setShowinfo( $showinfo ) {
752                $this->showinfo = $showinfo;
753        }
754
[111]755        function draw() {
[106]756
[122]757                $this->drawSmall();
758        }
[110]759
[122]760        function drawBig() {
[111]761
[122]762        }
[106]763
[122]764        function drawSmall() {
[106]765
[122]766                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
767                global $JOB_NODE_MARKING;
[102]768
[122]769                $black_color = imageColorAllocate( $this->image, 0, 0, 0 );
[110]770                $size = $SMALL_CLUSTERIMAGE_NODEWIDTH;
[107]771
[110]772                imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+($size), $this->y+($size), $black_color );
773
[122]774                if( $this->showinfo) {
775               
776                        $this->load = $this->determineLoad();
[111]777
[122]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                        }
[111]783
784
[122]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 );
[111]792
[122]793                } else {
[111]794
[122]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
[102]801        }
802
[111]803        function determineCpus() {
[102]804
[108]805                global $metrics;
806
807                $cpus = $metrics[$this->hostname][cpu_num][VAL];
[107]808                if (!$cpus) $cpus=1;
[111]809
810                return $cpus;
811        }
812
813        function determineLoad() {
814
815                global $metrics;
816
[108]817                $load_one = $metrics[$this->hostname][load_one][VAL];
[111]818                $load = ((float) $load_one)/$this->cpus;
[102]819
[111]820                return $load;
[106]821        }
822}
823
[107]824class ClusterImage {
[106]825
[111]826        var $dataget, $image, $clustername;
[124]827        var $filtername, $filters;
[106]828
[122]829        function ClusterImage( $clustername ) {
[114]830
[163]831                //printf( "image cluster = %s\n", $clustername );
832                $this->dataget = new DataGatherer( $clustername );
[111]833                $this->clustername = $clustername;
[124]834                $this->filters = array();
[106]835        }
836
[122]837        function setFilter( $filtername, $filtervalue ) {
838
[124]839                //printf("filter %s = %s\n", $filtername, $filtervalue );
[122]840                //printf( "filter set to %s = %s\n", $filtername, $filtervalue );
[124]841                $this->filters[$filtername] = $filtervalue;
842                //print_r($this->filters);
[122]843        }
844
845        function filterNodes( $jobs, $nodes ) {
846
847                $filtered_nodes = array();
848
849                foreach( $nodes as $node ) {
850
851                        $hostname = $node->getHostname();
852
[124]853                        $addhost = 1;
[122]854
[124]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 )
[122]887                                $filtered_nodes[] = $hostname;
888                }
889
890                return $filtered_nodes;
891        }
892
[106]893        function draw() {
[110]894
[124]895                //printf("stopt met uitvoer");
896                //return;
897
[110]898                global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH;
899       
[107]900                $mydatag = $this->dataget;
[106]901                $mydatag->parseXML();
902
[110]903                //$max_width = 250;
904                //$node_width = 11;
[106]905
[110]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
[106]911                $nodes = $mydatag->getNodes();
[111]912                $nodes_hosts = array_keys( $nodes );
[106]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 ) {
[107]920                        $nodes_per_row = ( (int) ($max_width/$node_width) );
[106]921                } else {
922                        $nodes_per_row = $nodes_size;
923                        $node_rows = 1;
924                }
925
926                if( $nodes_per_row < $nodes_nr ) {
[107]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 ) {
[106]931                                $node_rows++;
[107]932                                //printf( "noderows %d\n", $node_rows );
[106]933                        }
934                }
935
[107]936                //printf( "imagecreate: %dx%d", ($nodes_per_row*$node_width), ($node_rows*$node_width) );
[171]937                //$image = imageCreateTrueColor( ($nodes_per_row*$node_width)+1, ($node_rows*$node_width)+1 );
938                $image = imageCreateTrueColor( $max_width, ($node_rows*$node_width)+1 );
[107]939                $colorwhite = imageColorAllocate( $image, 255, 255, 255 );
940                imageFill( $image, 0, 0, $colorwhite );
[106]941
[122]942                $jobs = $mydatag->getJobs();
943                //printf("filtername = %s\n", $filtername );
944                $filtered_nodes = $this->filterNodes( $jobs, $nodes );
945
946                //print_r($filtered_nodes);
947
[106]948                for( $n = 0; $n < $node_rows; $n++ ) {
949                       
950                        for( $m = 0; $m < $nodes_per_row; $m++ ) {
[107]951                       
952                                $x = ($m * $node_width);
953                                $y = ($n * $node_width);
954
[109]955                                $cur_node = ($n * $nodes_per_row) + ($m);
[111]956                                $host = $nodes_hosts[$cur_node];
[107]957
[122]958
[111]959                                if( isset( $nodes[$host] ) ) {
960
961                                        $nodes[$host]->setCoords( $x, $y );
962                                        $nodes[$host]->setImage( $image );
[122]963
964                                        if( !in_array( $host, $filtered_nodes ) )
965                                                $nodes[$host]->setShowinfo( 0 );
966
[111]967                                        $nodes[$host]->draw();
968                                }
[106]969                        }
970                }
971               
[103]972                header( 'Content-type: image/png' );
[106]973                imagePNG( $image );
974                imageDestroy( $image );
[102]975        }
976}
977
[106]978//$my_data = new DataGatherer();
979//$my_data->parseXML();
980//$my_data->printInfo();
981
[112]982//$ic = new ClusterImage( "LISA Cluster" );
983//$ic->draw();
[102]984?>
Note: See TracBrowser for help on using the repository browser.