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

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

web/addons/job_monarch/conf.php:

  • added SEARCH_RESULT_LIMIT option to prevent HUGE result lists

web/addons/job_monarch/libtoga.php:

  • added a resultcount
  • changed sql query to count first and limit the results

web/addons/job_monarch/search.php:

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