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

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

Renamed: web/addons/toga -> web/addons/job_monarch

web/templates/job_monarch/cluster_extra.tpl:

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