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

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

web/addons/job_monarch/libtoga.php:

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