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

Last change on this file since 332 was 332, checked in by bastiaans, 17 years ago

web/addons/job_monarch/conf.php:

  • incread BIG_CLUSTERIMAGE_MAXWIDTH so that all LISA's 39 racks will fit :P

web/addons/job_monarch/libtoga.php:

  • draw x/y labels not until node draw loop complete
  • fixed valid x row determination
  • Property svn:keywords set to Id
File size: 39.2 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 332 2007-04-22 20:32:52Z 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
[280]36                $this->clustername = $httpvars["c"] ? $httpvars["c"] : $getvars["c"];
37                $this->metricname = $httpvars["m"] ? $httpvars["m"] : $getvars["m"];
[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;
[329]79global $SORTBY_HOSTNAME;
80global $SORT_ORDER;
81global $skan_str;
82global $x_first, $y_first;
[331]83global $SORT_XLABEL, $SORT_YLABEL;
[112]84
[117]85$my_dir = getcwd();
[112]86
[117]87// Load Ganglia's PHP
88chdir( $GANGLIA_PATH );
[112]89
[117]90include_once "./conf.php";
91include_once "./functions.php";
92include_once "./ganglia.php";
[145]93include_once "./get_context.php";
94unset( $start );
95$context = 'cluster';
[117]96include_once "./get_ganglia.php";
97
98// Back to our PHP
99chdir( $my_dir );
100
101global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH, $DATA_SOURCE, $HTTP_GET_VARS, $_GET;
102$httpvars = new HTTPVariables( $HTTP_GET_VARS, $_GET );
103
[112]104// Set cluster context so that Ganglia will
105// provide us with the correct metrics array
106//
[126]107global $context, $clustername, $reports;
[129]108
[117]109//$clustername = $httpvars->getClusterName();
[112]110
[126]111global $default_metric;
112
[112]113// Ganglia's array of host metrics
114//
[126]115global $metrics, $hosts_up;
[112]116
[246]117global $DATETIME_FORMAT;
118
119function makeDate( $time ) {
120        global $DATETIME_FORMAT;
121        return strftime( $DATETIME_FORMAT, $time );
122}
123
[130]124class TarchDbase {
125
[138]126        var $ip, $dbase, $conn;
[130]127
[294]128        function TarchDbase( $ip = null, $dbase = null ) {
[207]129
130                global $JOB_ARCHIVE_DBASE;
131
132                $db_fields = explode( '/', $JOB_ARCHIVE_DBASE );
133
134                $this->ip = $db_fields[0];
135                $this->dbase = $db_fields[1];
[138]136                $this->conn = null;
[130]137        }
138
139        function connect() {
140
[294]141                if( $this->ip == null )
[138]142                        $this->conn = pg_connect( "dbname=".$this->dbase );
[130]143                else
[204]144                        $this->conn = pg_connect( "host=".$this->ip." dbname=".$this->dbase );
[130]145        }
[138]146
147        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 ) {
148
[248]149                global $SEARCH_RESULT_LIMIT;
150
[251]151                if( $id ) {
152                        $select_query = "SELECT job_id FROM jobs WHERE job_id = '$id' AND job_status = 'F'";
153                        $this->resultcount = 1;
154                } else {
[138]155                        $query_args = array();
156                       
157                        if( $queue )
158                                $query_args[] = "job_queue ='$queue'";
159                        if( $user )
160                                $query_args[] = "job_owner ='$user'";
161                        if( $name )
162                                $query_args[] = "job_name = '$name'";
163                        if( $start_from_time )
164                                $query_args[] = "job_start_timestamp >= $start_from_time";
[142]165                        if( $start_to_time )
[138]166                                $query_args[] = "job_start_timestamp <= $start_to_time";
167                        if( $end_from_time )
168                                $query_args[] = "job_stop_timestamp >= $end_from_time";
169                        if( $end_to_time )
170                                $query_args[] = "job_stop_timestamp <= $end_to_time";
171
[248]172                        $query = "FROM jobs WHERE job_status = 'F' AND ";
[138]173                        $extra_query_args = '';
174
175                        foreach( $query_args as $myquery ) {
176
177                                if( $extra_query_args == '' )
178                                        $extra_query_args = $myquery;
179                                else
180                                        $extra_query_args .= " AND ".$myquery;
181                        }
182                        $query .= $extra_query_args;
183
[251]184                        $count_result_idname = "COUNT(job_id)";
185                        $select_result_idname = "job_id";
[140]186
[251]187                        $count_query = "SELECT " . $count_result_idname . " " . $query;
[248]188
[251]189                        $count_result = $this->queryDbase( $count_query );
190                        $this->resultcount = (int) $count_result[0][count];
[248]191
[251]192                        $select_query = "SELECT " . $select_result_idname . " " . $query . " ORDER BY job_id LIMIT " . $SEARCH_RESULT_LIMIT;
193                }
194
[248]195                $ids = $this->queryDbase( $select_query );
196
[140]197                $ret = array();
198
199                foreach( $ids as $crow)
200                        $ret[] = $crow[job_id];
201
202                return $ret;
[138]203        }
204
205        function getNodesForJob( $jobid ) {
206
207                $result = $this->queryDbase( "SELECT node_id FROM job_nodes WHERE job_id = '$jobid'" );
208
209                $nodes = array();
210
211                foreach( $result as $result_row ) 
212
[141]213                        $nodes[] = $this->getNodeArray( $result_row[node_id] );
[138]214
215                return $nodes;
216        }
217
218        function getJobsForNode( $nodeid ) {
219
220                $result = $this->queryDbase( "SELECT job_id FROM job_nodes WHERE node_id = '$nodeid'" );
221
222                $jobs = array();
223
224                foreach( $result as $result_row )
225
[141]226                        $jobs[] = $this->getJobArray( $result_row[job_id] );
[138]227
228                return $jobs;
229        }
230
231        function getJobArray( $id ) {
232                $result = $this->queryDbase( "SELECT * FROM jobs WHERE job_id = '$id'" );
233
234                return ( $this->makeArray( $result[0] ) );
235        }
236
237        function getNodeArray( $id ) {
238
239                $result = $this->queryDbase( "SELECT * FROM nodes WHERE node_id = '$id'" );
240
241                return ( $this->makeArray( $result[0] ) );
242        }
243
244        function makeArray( $result_row ) {
245
246                $myar = array();
247
248                foreach( $result_row as $mykey => $myval ) {
249
250                        $map_key = explode( '_', $mykey );
251
[141]252                        $rmap_key = array_reverse( $map_key );
253                        array_pop( $rmap_key );
254                        $map_key = array_reverse( $rmap_key );
[138]255                       
[141]256                        $newkey = implode( '_', $map_key );
257                       
[138]258                        $myar[$newkey] = $result_row[$mykey];
259                }
260
261                return $myar;
262        }
263
264        function queryDbase( $query ) {
265
266                $result_rows = array();
267       
268                if( !$this->conn )
269                        $this->connect();
270
[143]271                //printf( "query = [%s]\n", $query );
[138]272                $result = pg_query( $this->conn, $query );
273
274                while ($row = pg_fetch_assoc($result))
275                        $result_rows[] = $row;
276
277                return $result_rows;
278        }
[130]279}
280
[138]281class TarchRrdGraph {
[130]282        var $rrdbin, $rrdvalues, $clustername, $hostname, $tempdir, $tarchdir, $metrics;
283
[206]284        function TarchRrdGraph( $clustername, $hostname ) {
285
286                global $RRDTOOL;
287                global $JOB_ARCHIVE_DIR;
288
289                $this->rrdbin = $RRDTOOL;
[130]290                $this->rrdvalues = array();
[206]291                $this->tarchdir = $JOB_ARCHIVE_DIR;
[145]292                $this->clustername = $clustername;
293                $this->hostname = $hostname;
[130]294        }
295
296        function doCmd( $command ) {
297
298                printf( "command = %s\n", $command );
299                $pipe = popen( $command . ' 2>&1', 'r' );
300
301                if (!$pipe) {
302                        print "pipe failed.";
303                        return "";
304                }
305
306                $output = '';
307                while(!feof($pipe))
308                        $output .= fread($pipe, 1024);
309
310                pclose($pipe);
311
312                $output = explode( "\n", $output );
[135]313                //print_r( $output );
[130]314                return $output;
315        }
316
317        function dirList( $dir ) {
318
319                $dirlist = array();
320
321                if ($handle = opendir( $dir )) {
322                        while (false !== ($file = readdir($handle))) {
323                                if ($file != "." && $file != "..") {
324                                        $dirlist[] = $file;
325                                }
326                        }
327                        closedir($handle);
328                }
329
330                return $dirlist;
331        }
332
333        function getTimePeriods( $start, $end ) {
334
[145]335                //printf("start = %s end = %s\n", $start, $end );
[130]336                $times = array();
337                $dirlist = $this->dirList( $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname );
[145]338
339                //print_r( $dirlist );
340
[130]341                $first = 0;
342                $last = 9999999999999;
343
344                foreach( $dirlist as $dir ) {
345
346                        if( $dir > $first and $dir <= $start )
347                                $first = $dir;
348                        if( $dir < $last and $dir >= $end )
349                                $last = $dir;
350                }
351
[145]352                //printf( "first = %s last = %s\n", $first, $last );
353
[130]354                foreach( $dirlist as $dir ) {
355
[145]356                        //printf( "dir %s ", $dir );
357
358                        if( $dir >= $first and $dir <= $last and !array_key_exists( $dir, $times ) ) {
359                       
[130]360                                $times[] = $dir;
[145]361                                //printf("newtime %s ", $dir );
362
363                        }
[130]364                }
365
[145]366                //print_r( $times );
367
[130]368                sort( $times );
369
[145]370                //print_r( $times );
371
[130]372                return $times;
373        }
374
[145]375        function getRrdDirs( $start, $stop ) {
376
377                //printf( "tarchdir = %s\n", $this->tarchdir );
378                $timess = $this->getTimePeriods( $start, $stop );
379                //print_r( $timess );
380
381                $rrd_files = array();
382
383                foreach( $timess as $time ) {
384
385                        $rrd_files[] = $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname. '/'.$time;
386                }
387
388                return $rrd_files;
389        }
390
391        function getRrdFiles( $metric, $start, $stop ) {
392
393                $times = $this->getTimePeriods( $start, $stop );
394
395                $rrd_files = array();
396
397                foreach( $times as $time ) {
398
399                        $rrd_files[] = $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname . '/' .$time. '/' . $metric. '.rrd';
400                }
401
402                return $rrd_files;
403        }
404
[130]405        function graph( $descr ) {
[141]406//      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
407//      380x461
408//      monitor2:/data/toga/rrds/LISA Cluster/gb-r15n11.irc.sara.nl#
[130]409                //$command = $this->rrdbin . " graph - --start $start --end $end ".
410                        "--width $width --height $height $upper_limit $lower_limit ".
411                        "--title '$title' $vertical_label $extras $background ". $series;
412
413                //$graph = $this->doCmd( $command );
414
415                //return $graph;
[145]416                return 0;
[130]417        }
418}
419
[103]420class DataSource {
421
422        var $data, $ip, $port;
423
[326]424        //function DataSource( $ip = '127.0.0.1', $port = 8649 ) {
425        function DataSource() {
426
427                global $DATA_SOURCE;
428
429                $ds_fields      = explode( ':', $DATA_SOURCE );
430
431                $ds_ip          = $ds_fields[0];
432                $ds_port        = $ds_fields[1];
433
434                $this->ip       = $ds_ip;
435                $this->port     = $ds_port;
436
[103]437        }
438
439        function getData() {
440
441                $errstr;
442                $errno = 0;
443                $timeout = 3;
444
[313]445                $fp = fsockopen( $this->ip, $this->port, $errno, $errstr, $timeout );
[103]446
[106]447                if( !$fp ) {
[103]448                        echo 'Unable to connect to '.$this->ip.':'.$this->port; // printf( 'Unable to connect to [%s:%.0f]', $this->ip, $this->port );
449                        return;
450                }
451
[177]452                stream_set_timeout( $fp, 30 );
453
[103]454                while ( !feof( $fp ) ) {
455                       
456                        $data .= fread( $fp, 16384 );
457                }
458
459                fclose( $fp );
460
461                return $data;
462        }
463}
464
465class DataGatherer {
466
[105]467        var $xmlhandler, $data, $httpvars;
[103]468
[163]469        function DataGatherer( $cluster ) {
[103]470
[326]471                //global $DATA_SOURCE;
[163]472       
473                //printf("dg cluster = %s\n", $cluster );
[326]474                //$ds_fields = explode( ':', $DATA_SOURCE );
475                //$ds_ip = $ds_fields[0];
476                //$ds_port = $ds_fields[1];
[110]477
[326]478                //$this->source = new DataSource( $ds_ip, $ds_port );
[110]479
[326]480                $this->cluster  = $cluster;
[112]481                $this->httpvars = $httpvars;
[103]482        }
483
[326]484        function parseXML( $data ) {
[103]485
[326]486                //$src = &$this->source;
487                //$this->data = $src->getData();
[103]488
[326]489                $this->parser           = xml_parser_create();
490                $this->xmlhandler       = new TorqueXMLHandler( $this->cluster );
491
492                xml_set_element_handler( $this->parser, array( &$this->xmlhandler, 'startElement' ), array( &$this->xmlhandler, 'stopElement' ) );
493                //if ( !xml_parse( $this->parser, $this->data ) )
494                if ( !xml_parse( $this->parser, $data ) )
[313]495                        $error = sprintf( 'XML error: %s at %d', xml_error_string( xml_get_error_code( $this->parser ) ), xml_get_current_line_number( $this->parser ) );
[103]496        }
497
[105]498        function printInfo() {
499                $handler = $this->xmlhandler;
500                $handler->printInfo();
501        }
502
[106]503        function getNodes() {
504                $handler = $this->xmlhandler;
505                return $handler->getNodes();
506        }
507
[303]508        function getNode( $node ) {
509                $handler = $this->xmlhandler;
510                return $handler->getNode( $node );
511        }
512
[124]513        function getCpus() {
514                $handler = $this->xmlhandler;
515                return $handler->getCpus();
516        }
517
[106]518        function getJobs() {
519                $handler = $this->xmlhandler;
520                return $handler->getJobs();
521        }
522
[303]523        function getJob( $job ) {
524                $handler = $this->xmlhandler;
525                return $handler->getJob( $job );
526        }
527
[114]528        function getHeartbeat() {
529                $handler = $this->xmlhandler;
530                return $handler->getHeartbeat();
531        }
[298]532
533        function isJobmonRunning() {
534                $handler = $this->xmlhandler;
535                return $handler->isJobmonRunning();
536        }
[103]537}
538
539class TorqueXMLHandler {
540
[163]541        var $clusters, $heartbeat, $nodes, $jobs, $clustername, $proc_cluster;
[104]542
[163]543        function TorqueXMLHandler( $clustername ) {
[105]544                $jobs = array();
[104]545                $clusters = array();
[163]546                $this->nodes = array();
[104]547                $heartbeat = array();
[163]548                $this->clustername = $clustername;
549                //printf(" cluster set to %s \n", $this->clustername );
[103]550        }
551
[124]552        function getCpus() {
553
554                $cpus = 0;
555
[301]556                if( isset( $this->jobs ) && count( $this->jobs ) > 0 ) {
[124]557
[301]558                        foreach( $this->jobs as $jobid=>$jobattrs ) {
[124]559
[301]560                                $nodes = count( $jobattrs[nodes] );
561                                $ppn = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
562                                $mycpus = $nodes * $ppn;
563
564                                $cpus = $cpus + $mycpus;
565                        }
[124]566                }
567        }
568
[298]569        function isJobmonRunning() {
570
571                if (isset( $this->heartbeat['time'] ))
572                        return 1;
573                else
574                        return 0;
575        }
576
[103]577        function startElement( $parser, $name, $attrs ) {
578
[262]579                $jobs = $this->jobs;
580                $nodes = $this->nodes;
[105]581
[103]582                if ( $attrs[TN] ) {
583
584                        // Ignore dead metrics. Detect and mask failures.
585                        if ( $attrs[TN] > $attrs[TMAX] * 4 )
586                                return;
587                }
588
589                $jobid = null;
590
[163]591                //printf( '%s=%s', $attrs[NAME], $attrs[VAL] );
[103]592
[163]593                //printf( "clustername = %s proc_cluster = %s\n", $this->clustername, $this->proc_cluster );
594
[104]595                if( $name == 'CLUSTER' ) {
[103]596
[163]597                        $this->proc_cluster = $attrs[NAME];
598                        //printf( "Found cluster %s\n", $attrs[NAME] );
599                        //print_r( $attrs );
[103]600
[163]601                        //if( !isset( $clusters[$clustername] ) )
602                        //      $clusters[$clustername] = array();
[103]603
[163]604                } else if( $name == 'HOST' and $this->proc_cluster == $this->clustername) {
[105]605
606                        $hostname = $attrs[NAME];
607                        $location = $attrs[LOCATION];
[163]608                        //printf( "Found node %s\n", $hostname );
[105]609
[200]610                        if( !isset( $nodes[$hostname] ) )
[326]611                                $nodes[$hostname] = new NodeImage( $this->proc_cluster, $hostname );
[105]612
[219]613                } else if( $name == 'METRIC' and strstr( $attrs[NAME], 'MONARCH' ) and $this->proc_cluster == $this->clustername ) {
[103]614
[219]615                        if( strstr( $attrs[NAME], 'MONARCH-HEARTBEAT' ) ) {
[103]616
[114]617                                $this->heartbeat['time'] = $attrs[VAL];
[106]618                                //printf( "heartbeat %s\n", $heartbeat['time'] );
[104]619
[219]620                        } else if( strstr( $attrs[NAME], 'MONARCH-JOB' ) ) {
[104]621
[253]622                                sscanf( $attrs[NAME], 'MONARCH-JOB-%d-%d', $jobid, $monincr );
[104]623
[106]624                                //printf( "jobid %s\n", $jobid );
[104]625
626                                if( !isset( $jobs[$jobid] ) )
627                                        $jobs[$jobid] = array();
628
629                                $fields = explode( ' ', $attrs[VAL] );
630
631                                foreach( $fields as $f ) {
632                                        $togavalues = explode( '=', $f );
633
634                                        $toganame = $togavalues[0];
635                                        $togavalue = $togavalues[1];
636
[106]637                                        //printf( "\t%s\t= %s\n", $toganame, $togavalue );
[104]638
[103]639                                        if( $toganame == 'nodes' ) {
640
[135]641                                                if( $jobs[$jobid][status] == 'R' ) {
642                                               
643                                                        if( !isset( $jobs[$jobid][$toganame] ) )
644                                                                $jobs[$jobid][$toganame] = array();
[104]645
[135]646                                                        $mynodes = explode( ';', $togavalue );
[103]647
[300]648                                                        //print_r($mynodes);
649
[262]650                                                        foreach( $mynodes as $node ) {
[103]651
[262]652                                                                if( !in_array( $node, $jobs[$jobid][$toganame] ) ) {
[253]653                                                                        $jobs[$jobid][$toganame][] = $node;
[262]654                                                                }
655                                                        }
[135]656
657                                                } else if( $jobs[$jobid][status] == 'Q' ) {
658
659                                                        $jobs[$jobid][$toganame] = $togavalue;
660                                                }
661                                               
[104]662                                        } else {
[103]663
[105]664                                                $jobs[$jobid][$toganame] = $togavalue;
[104]665                                        }
[103]666                                }
[111]667
668                                if( isset( $jobs[$jobid][domain] ) and isset( $jobs[$jobid][nodes] ) ) {
[112]669                       
670                                        $nr_nodes = count( $jobs[$jobid][nodes] );
[300]671               
672                                        if( $jobs[$jobid][status] == 'R' ) {
[111]673
[300]674                                                foreach( $jobs[$jobid][nodes] as $node ) {
[200]675
[300]676                                                        $domain = $jobs[$jobid][domain];
677                                                        $domain_len = 0 - strlen( $domain );
[200]678
[300]679                                                        if( substr( $node, $domain_len ) != $domain ) {
680                                                                $host = $node. '.'.$domain;
681                                                        } else {
682                                                                $host = $node;
683                                                        }
684
685                                                        //$host = $node.'.'.$jobs[$jobid][domain];
[111]686                               
[300]687                                                        if( !isset( $nodes[$host] ) )
[326]688                                                                $my_node = new NodeImage( $this->proc_cluster, $host );
[300]689                                                        else
690                                                                $my_node = $nodes[$host];
[111]691
[300]692                                                        if( !$my_node->hasJob( $jobid ) )
[111]693
[300]694                                                                if( isset( $jobs[$jobid][ppn] ) )
695                                                                        $my_node->addJob( $jobid, ((int) $jobs[$jobid][ppn]) );
696                                                                else
697                                                                        $my_node->addJob( $jobid, 1 );
[111]698
[300]699                                                        $nodes[$host] = $my_node;
700                                                }
[111]701                                        }
702                                }
[103]703                        }
704                }
[114]705                $this->jobs = $jobs;
[163]706                //print_r( $nodes );
707                $this->nodes = $nodes;
[200]708                //print_r( $this->nodes );
[103]709        }
710
711        function stopElement( $parser, $name ) {
712        }
[105]713
714        function printInfo() {
715
716                $jobs = &$this->jobs;
717
718                printf( "---jobs---\n" );
719
720                foreach( $jobs as $jobid => $job ) {
721
722                        printf( "job %s\n", $jobid );
723
724                        if( isset( $job[nodes] ) ) {
725
726                                foreach( $job[nodes] as $node ) {
727
728                                        $mynode = $this->nodes[$node];
729                                        $hostname = $mynode->getHostname();
730                                        $location = $mynode->getLocation();
731
732                                        printf( "\t- node %s\tlocation %s\n", $hostname, $location );
733                                        //$this->nodes[$hostname]->setLocation( "hier draait job ".$jobid );
734                                }
735                        }
736                }
737
738                printf( "---nodes---\n" );
739
740                $nodes = &$this->nodes;
741
742                foreach( $nodes as $node ) {
743
744                        $hostname = $node->getHostname();
745                        $location = $node->getLocation();
746                        $jobs = implode( ' ', $node->getJobs() );
747                        printf( "* node %s\tlocation %s\tjobs %s\n", $hostname, $location, $jobs );
748                }
749        }
[106]750
751        function getNodes() {
[163]752                //print_r( $this->nodes );
[106]753                return $this->nodes;
754        }
755
[303]756        function getNode( $node ) {
757
758                $nodes = &$this->nodes;
759                if( isset( $nodes[$node] ) )
760                        return $nodes[$node];
761                else
762                        return NULL;
763        }
764
[106]765        function getJobs() {
766                return $this->jobs;
767        }
[114]768
[303]769        function getJob( $job ) {
770
771                $jobs = &$this->jobs;
772                if( isset( $jobs[$job] ) )
773                        return $jobs[$job];
774                else
775                        return NULL;
776        }
777
[114]778        function getHeartbeat() {
779                return $this->heartbeat['time'];
780        }
[103]781}
782
[107]783class NodeImage {
[102]784
[122]785        var $image, $x, $y, $hostname, $jobs, $tasks, $showinfo;
[102]786
[326]787        function NodeImage( $cluster, $hostname ) {
[102]788
[305]789                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
790
[111]791                $this->jobs = array();
792                //$this->image = $image;
793                //$this->x = $x;
794                //$this->y = $y;
795                $this->tasks = 0;
[110]796                $this->hostname = $hostname;
[111]797                $this->cpus = $this->determineCpus();
[326]798                $this->clustername = $cluster;
[122]799                $this->showinfo = 1;
[305]800                $this->size = $SMALL_CLUSTERIMAGE_NODEWIDTH;
[102]801        }
802
[111]803        function addJob( $jobid, $cpus ) {
804                $jobs = &$this->jobs;
805
806                $jobs[] = $jobid;
807                $this->jobs = $jobs;
808
809                $this->addTask( $cpus );
810        }
811
812        function hasJob( $jobid ) {
813
814                $jobfound = 0;
815
816                if( count( $this->jobs ) > 0 )
817                        foreach( $this->jobs as $job )
818
819                                if( $job == $jobid )
820                                        $jobfound = 1;
821
822                return $jobfound;
823        }
824
825        function addTask( $cpus ) {
826
827                $this->tasks = $this->tasks + $cpus;
828        }
829
830        function setImage( $image ) {
831
832                $this->image = $image;
833        }
834
[106]835        function setCoords( $x, $y ) {
[107]836
[106]837                $this->x = $x;
838                $this->y = $y;
839        }
840
[326]841        function getImagemapArea() {
842
843                $area_topleft           = $this->x . "," . $this->y;
844                $area_bottomright       = ($this->x + $this->size) . "," . ($this->y + $this->size);
845                $area_coords            = $area_topleft . "," . $area_bottomright;
846
847                $area_href              = "./?c=" . $this->clustername . "&h=" . $this->hostname;
848                $area_tooltip           = $this->hostname . ": " . implode( " ", $this->jobs );
849
850                $tag_href               = "HREF=\"" . $area_href . "\"";
851                $tag_coords             = "COORDS=\"" . $area_coords . "\"";
852                $tag_tooltip1           = "ALT=\"" . $area_tooltip . "\"";
853                $tag_tooltip2           = "TITLE=\"" . $area_tooltip . "\"";
854
855                return ("<AREA SHAPE=\"RECT\" " . $tag_coords . " " . $tag_href . " " . $tag_tooltip1 . " " . $tag_tooltip2 . ">");
856        }
857
[102]858        function colorHex( $color ) {
859       
[109]860                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) );
[102]861
862                return $my_color;
863        }
864
[106]865        function setLoad( $load ) {
866                $this->load = $load;
867        }
868
[108]869        function setHostname( $hostname ) {
870                $this->hostname = $hostname;
871        }
872
[122]873        function getHostname() {
874                return $this->hostname;
875        }
876
[114]877        function getJobs() {
878                return $this->jobs;
879        }
880
[122]881        function setShowinfo( $showinfo ) {
882                $this->showinfo = $showinfo;
883        }
884
[305]885        function drawSmall() {
[106]886
[305]887                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
888
889                $this->size     = $SMALL_CLUSTERIMAGE_NODEWIDTH;
890
891                $this->draw();
[122]892        }
[110]893
[122]894        function drawBig() {
[111]895
[305]896                global $BIG_CLUSTERIMAGE_NODEWIDTH;
897
898                $this->size     = $BIG_CLUSTERIMAGE_NODEWIDTH;
899
900                $this->draw();
[122]901        }
[106]902
[305]903        function draw() {
[106]904
[122]905                global $JOB_NODE_MARKING;
[102]906
[122]907                $black_color = imageColorAllocate( $this->image, 0, 0, 0 );
[305]908                $size = $this->size;
[107]909
[110]910                imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+($size), $this->y+($size), $black_color );
911
[122]912                if( $this->showinfo) {
913               
914                        $this->load = $this->determineLoad();
[111]915
[122]916                        if( !isset( $this->image ) or !isset( $this->x ) or !isset( $this->y ) ) {
917                                printf( "aborting\n" );
918                                printf( "x %d y %d load %f\n", $this->x, $this->y, $load );
919                                return;
920                        }
[111]921
922
[122]923                        // Convert Ganglias Hexadecimal load color to a Decimal one
924                        //
925                        $load = $this->determineLoad(); 
926                        $usecolor = $this->colorHex( load_color($load) );
927                        imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor );
928                        if( count( $this->jobs ) > 0 )
[280]929                                imageString( $this->image, 1, $this->x+(($size/2)-1), $this->y+(($size/2)-4), $JOB_NODE_MARKING, $black_color );
[111]930
[122]931                } else {
[111]932
[122]933                        // White
934                        $usecolor = imageColorAllocate( $this->image, 255, 255, 255 );
935                        imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor );
936                }
937
938
[102]939        }
940
[111]941        function determineCpus() {
[102]942
[108]943                global $metrics;
944
945                $cpus = $metrics[$this->hostname][cpu_num][VAL];
[107]946                if (!$cpus) $cpus=1;
[111]947
948                return $cpus;
949        }
950
951        function determineLoad() {
952
953                global $metrics;
954
[108]955                $load_one = $metrics[$this->hostname][load_one][VAL];
[111]956                $load = ((float) $load_one)/$this->cpus;
[102]957
[111]958                return $load;
[106]959        }
960}
961
[107]962class ClusterImage {
[106]963
[111]964        var $dataget, $image, $clustername;
[124]965        var $filtername, $filters;
[106]966
[326]967        //function ClusterImage( $clustername ) {
968        function ClusterImage( $data, $clustername ) {
[114]969
[326]970                //$this->dataget                = $dataget;
[305]971                $this->dataget          = new DataGatherer( $clustername );
[326]972                $this->data             = $data;
[305]973                $this->clustername      = $clustername;
974                $this->filters          = array();
975                $this->size             = 's';
[326]976                $this->width            = 0;
977                $this->height           = 0;
978                $this->output           = 1;
[106]979        }
980
[326]981        function getWidth() {
982                return $this->width;
983        }
984        function getHeight() {
985                return $this->height;
986        }
987
[305]988        function setSmall() {
989                $this->size     = 's';
990        }
991
992        function setBig() {
993                $this->size     = 'b';
994        }
995
[326]996        function setNoimage() {
997                $this->output   = 0;
998        }
999
[305]1000        function isSmall() {
1001                return ($this->size == 's');
1002        }
1003
1004        function isBig() {
1005                return ($this->size == 'b');
1006        }
1007
[122]1008        function setFilter( $filtername, $filtervalue ) {
1009
[124]1010                $this->filters[$filtername] = $filtervalue;
[122]1011        }
1012
1013        function filterNodes( $jobs, $nodes ) {
1014
1015                $filtered_nodes = array();
1016
1017                foreach( $nodes as $node ) {
1018
1019                        $hostname = $node->getHostname();
1020
[124]1021                        $addhost = 1;
[122]1022
[124]1023                        if( count( $this->filters ) > 0 ) {
1024
1025                                $mynjobs = $node->getJobs();
1026
1027                                if( count( $mynjobs ) > 0 ) {
1028
1029                                        foreach( $mynjobs as $myjob ) {
1030
1031                                                foreach( $this->filters as $filtername => $filtervalue ) {
1032
1033                                                        if( $filtername!=null && $filtername!='' ) {
1034
1035                                                                if( $filtername == 'jobid' && !$node->hasJob( $filtervalue) ) {
1036                                                                        $addhost = 0;
1037                                                                } else if( $filtername != 'jobid' ) {
1038                                                                        if( $jobs[$myjob][$filtername] != $filtervalue ) {
1039                                                                                $addhost = 0;
1040                                                                        }
1041                                                                }
1042                                                        }
1043                                                }
1044                                        }
1045                                } else
1046                                        $addhost = 0;
1047                        }
1048
1049                        if( $addhost )
[122]1050                                $filtered_nodes[] = $hostname;
1051                }
1052
1053                return $filtered_nodes;
1054        }
1055
[106]1056        function draw() {
[110]1057
1058                global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH;
[305]1059                global $BIG_CLUSTERIMAGE_MAXWIDTH, $BIG_CLUSTERIMAGE_NODEWIDTH;
[329]1060
1061                global $SORTBY_HOSTNAME, $SORT_ORDER, $skan_str;
1062                global $x_first, $y_first;
[331]1063
1064                global $SORT_XLABEL, $SORT_YLABEL;
[110]1065       
[107]1066                $mydatag = $this->dataget;
[326]1067                $mydatag->parseXML( $this->data );
[106]1068
[305]1069                if( $this->isSmall() ) {
1070                        $max_width = $SMALL_CLUSTERIMAGE_MAXWIDTH;
1071                        $node_width = $SMALL_CLUSTERIMAGE_NODEWIDTH;
1072                } else if( $this->isBig() ) {
1073                        $max_width = $BIG_CLUSTERIMAGE_MAXWIDTH;
1074                        $node_width = $BIG_CLUSTERIMAGE_NODEWIDTH;
1075                }
[106]1076
1077                $nodes = $mydatag->getNodes();
[111]1078                $nodes_hosts = array_keys( $nodes );
[106]1079
1080                $nodes_nr = count( $nodes );
1081
1082                $nodes_size = $nodes_nr*$node_width;
1083                $node_rows = 0;
1084
1085                if( $nodes_size > $max_width ) {
[107]1086                        $nodes_per_row = ( (int) ($max_width/$node_width) );
[106]1087                } else {
1088                        $nodes_per_row = $nodes_size;
1089                        $node_rows = 1;
1090                }
1091
1092                if( $nodes_per_row < $nodes_nr ) {
[107]1093                        $node_rows = ( (int) ($nodes_nr/$nodes_per_row) );
1094                        $node_rest = fmod( $nodes_nr, $nodes_per_row );
1095                        //printf( "nodesnr %d noderest %f\n", $nodes_nr, $node_rest );
1096                        if( $node_rest > 0 ) {
[106]1097                                $node_rows++;
[107]1098                                //printf( "noderows %d\n", $node_rows );
[106]1099                        }
1100                }
1101
[306]1102                $y_offset       = 0;
1103                $font           = 2;
[329]1104                $fontwidth      = ImageFontWidth( $font );
[306]1105                $fontheight     = ImageFontHeight( $font );
1106                $fontspaceing   = 2;
1107                $y_offset       = $fontheight + (2 * $fontspaceing);
1108
[326]1109                $this->width    = $max_width;
1110                $this->height   = ($y_offset + (($node_rows*$node_width)+1) );
1111
[329]1112                //$image = imageCreateTrueColor( $max_width, ($y_offset + (($node_rows*$node_width)+1) ) );
1113                //$colorwhite = imageColorAllocate( $image, 255, 255, 255 );
1114                //imageFill( $image, 0, 0, $colorwhite );
[106]1115
[329]1116                //if( $this->isSmall() ) {
[306]1117
[329]1118                //      $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
[306]1119
[329]1120                //      imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1121                //}
[306]1122
[122]1123                $jobs = $mydatag->getJobs();
1124                //printf("filtername = %s\n", $filtername );
1125                $filtered_nodes = $this->filterNodes( $jobs, $nodes );
1126
1127                //print_r($filtered_nodes);
1128
[329]1129                if( $SORTBY_HOSTNAME != "" )
1130                {
1131
1132                        $sorted         = array();
1133
1134                        $x_first        = 0;
1135                        $y_first        = 0;
1136
1137
1138                        if( strpos( $SORTBY_HOSTNAME, "{x}" ) < strpos( $SORTBY_HOSTNAME, "{y}" ) )
1139                        {
[106]1140                       
[329]1141                                $x_first        = 1;
1142                        }
1143                        else
1144                        {
1145                                $y_first        = 1;
1146               
1147                        }
1148
1149                        $skan_str       = str_replace( "{x}", "%d", $SORTBY_HOSTNAME );
1150                        $skan_str       = str_replace( "{y}", "%d", $skan_str );
1151
1152                        $x_min          = null;
1153                        $x_max          = null;
1154                        $y_min          = null;
1155                        $y_max          = null;
1156
1157                        foreach( $nodes as $hostname => $node )
1158                        {
1159                                //$n    = sscanf( $hostname, $skan_str, $i, $j );
1160                                if( $x_first )
1161                                {
1162                                        $n = sscanf( $hostname, $skan_str, $x, $y );
1163                                }
1164                                else if( $y_first )
1165                                {
1166                                        $n = sscanf( $hostname, $skan_str, $y, $x );
1167                                }
1168
1169                                //printf( "n %s\n", $n );
1170
1171                                // Remove nodes that don't match
1172                                //
1173                                if( $n < 2 )
1174                                {
1175                                        unset( $nodes[$hostname] );
1176                                }
1177
1178                                if( !$x_min )
1179                                {
1180                                        $x_min  = $x;
1181                                }
1182                                else if( $x < $x_min )
1183                                {
1184                                        $x_min  = $x;
1185                                }
1186                                if( !$x_max )
1187                                {
1188                                        $x_max  = $x;
1189                                }
1190                                else if( $x > $x_max )
1191                                {
1192                                        $x_max  = $x;
1193                                }
1194                                if( !$y_min )
1195                                {
1196                                        $y_min  = $y;
1197                                }
1198                                else if( $y < $y_min )
1199                                {
1200                                        $y_min  = $y;
1201                                }
1202                                if( !$y_max )
1203                                {
1204                                        $y_max  = $y;
1205                                }
1206                                else if( $y > $y_max )
1207                                {
1208                                        $y_max  = $y;
1209                                }
1210                        }
1211
1212                        //printf( "ss %s\n", $skan_str);
1213                        $sorted_nodes   = usort( $nodes, "cmp" );
1214
1215                        $cur_node       = 0;
1216
[331]1217                        $x_offset       = 0;
1218                        $y_offset       = 0;
1219                        $font           = 2;
1220                        $fontwidth      = ImageFontWidth( $font );
1221                        $fontheight     = ImageFontHeight( $font );
1222                        $fontspaceing   = 2;
1223
1224                        if( $this->isBig() ) 
1225                        {
1226
1227                                $y_offset       = ($fontheight * (1 + strlen( $x_max) ) ) + ((2 + strlen( $x_max)) * $fontspaceing);
1228                                $x_offset       = ($fontwidth * (1 + strlen( $y_max) ) ) + ((2 + strlen( $y_max)) * $fontspaceing);
1229
1230                        }
1231                        //$x_offset     = ($fontwidth * 3) + (5 * $fontspaceing);
1232
[329]1233                        //printf( "xmin %s xmax %s\n", $x_min, $x_max );
1234                        //printf( "ymin %s ymax %s\n", $y_min, $y_max );
1235
1236                        // werkt
1237                        //print_r( $nodes );
1238
[330]1239                        $image_width    = $x_offset + ($node_width * ($x_max-$x_min+2));
1240                        $image_width    = ($image_width < $this->width) ? $image_width : $this->width;
1241                        $image_height   = $y_offset + ($node_width * ($y_max-$y_min+2));
1242
1243                        $this->width    = $image_width;
1244                        $this->heigth   = $image_heigth;
1245
1246                        $image          = imageCreateTrueColor( $image_width, $image_height );
[329]1247                        $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1248
1249                        imageFill( $image, 0, 0, $colorwhite );
1250
[331]1251                        if( $this->isBig() ) 
1252                        {
1253                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
[329]1254
[331]1255                                imageString( $image, $font, $x_offset, $fontspaceing, $SORT_XLABEL, $colorblue );
[329]1256
[331]1257                                // Stupid php without imageStringDown function
1258                                //
1259                                imageStringDown( $image, $font, $fontspaceing, $y_offset, $SORT_YLABEL, $colorblue );
1260                        }
[329]1261
1262                        for( $n = $x_min; $n <= $x_max; $n++ )
1263                        {
1264                                for( $m = $y_min; $m <= $y_max; $m++ )
1265                                {
1266                                        if( $x_min > 0 )
1267                                        {
1268                                                $x      = $x_offset + ( ($n-$x_min) * $node_width );
1269                                        }
1270                                        if( $y_min > 0 )
1271                                        {
1272                                                $y      = $y_offset + ( ($m-$y_min) * $node_width );
1273                                        }
1274
[330]1275                                        if( isset( $nodes[$cur_node] ) ) 
1276                                        {
[329]1277                                                $host   = $nodes[$cur_node]->getHostname();
1278
1279                                                if( $x_first )
1280                                                {
1281                                                        $nn = sscanf( $host, $skan_str, $rx, $ry );
1282                                                }
1283                                                else if( $y_first )
1284                                                {
1285                                                        $nn = sscanf( $host, $skan_str, $ry, $rx );
1286                                                }
1287                                                if ( $nn < 2 )
1288                                                {
1289                                                        continue;
1290                                                }
[332]1291                                                if( ( $rx ) > $n )
[329]1292                                                {
[332]1293                                                        $m      = $y_max + 1;
[329]1294                                                        continue;
1295                                                }
1296
1297                                                if( !in_array( $host, $filtered_nodes ) )
1298                                                        $nodes[$cur_node]->setShowinfo( 0 );
1299
1300                                                $nodes[$cur_node]->setCoords( $x, $y );
1301                                                $nodes[$cur_node]->setImage( $image );
1302
1303                                                //print_r( $nodes[$cur_node] );
1304
1305                                                if( $this->isSmall() )
1306                                                        $nodes[$cur_node]->drawSmall();
1307                                                else if( $this->isBig() )
1308                                                        $nodes[$cur_node]->drawBig();
1309                                        }
[332]1310                                        if( $n == $x_min )
1311                                        {
1312                                                $mfontspacing   = 1;
1313                                                $ylabel_x       = $x - ( $fontwidth * strlen( $y_max ) ) - $mfontspacing;
1314                                                $ylabel_y       = $y;
1315
1316                                                imageString( $image, $font, $ylabel_x, $ylabel_y, strval( $m ), $colorblue );
1317                                        }
1318                                        if( $m == $y_min )
1319                                        {
1320                                                $mfontspacing   = 2;
1321                                                $xlabel_y       = $y - ( $fontheight * strlen( $x_max ) );
1322                                                $xlabel_x       = $x + $mfontspacing; 
1323
1324                                                imageStringDown( $image, $font, $xlabel_x, $xlabel_y, strval( $n ), $colorblue );
1325                                        }
1326
[329]1327                                        $cur_node++;
1328                                }
1329                        }
1330
1331                }
1332                else
1333                {
1334                        $image          = imageCreateTrueColor( $max_width, ($y_offset + (($node_rows*$node_width)+1) ) );
1335                        $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1336
1337                        imageFill( $image, 0, 0, $colorwhite );
1338
1339                        if( $this->isSmall() ) {
1340
1341                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1342
1343                                imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1344                        }
1345
1346                        for( $n = 0; $n < $node_rows; $n++ ) {
[107]1347                       
[329]1348                                for( $m = 0; $m < $nodes_per_row; $m++ ) {
1349                       
1350                                        $x = ($m * $node_width);
1351                                        $y = $y_offset + ($n * $node_width);
[107]1352
[329]1353                                        $cur_node = ($n * $nodes_per_row) + ($m);
1354                                        $host = $nodes_hosts[$cur_node];
[107]1355
[329]1356                                        if( isset( $nodes[$host] ) ) {
[111]1357
[329]1358                                                $nodes[$host]->setCoords( $x, $y );
1359                                                $nodes[$host]->setImage( $image );
[122]1360
[329]1361                                                if( !in_array( $host, $filtered_nodes ) )
1362                                                        $nodes[$host]->setShowinfo( 0 );
[122]1363
[329]1364                                                if( $this->isSmall() )
1365                                                        $nodes[$host]->drawSmall();
1366                                                else if( $this->isBig() )
1367                                                        $nodes[$host]->drawBig();
1368                                        }
[111]1369                                }
[106]1370                        }
1371                }
[326]1372       
1373                $this->nodes    = &$nodes;
1374
1375                if ($this->output) {
1376                        header( 'Content-type: image/png' );
1377                        imagePNG( $image );
1378                        imageDestroy( $image );
1379                }
[102]1380        }
[326]1381
1382        function getImagemapArea() {
1383
1384                $clusterimage_map       = "";
1385
1386                foreach( $this->nodes as $hostname => $node ) {
1387
1388                        $node_map               = $node->getImagemapArea();
1389                        $clusterimage_map       .= $node_map;
1390                }
1391
1392                return $clusterimage_map;
1393        }
[102]1394}
1395
[298]1396class EmptyImage {
1397
1398        function draw() {
1399                $image          = imageCreateTrueColor( 1, 1 );
1400                $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1401                imageFill( $image, 0, 0, $colorwhite );                         
1402
1403                header( 'Content-type: image/png' );
1404                imagePNG( $image );
1405                imageDestroy( $image );
1406        }
1407}
1408
[303]1409class HostImage {
[106]1410
[303]1411        var $data_gather, $cluster, $host, $node, $image;
1412        var $headerstrlen;
1413
1414        function HostImage( $data_gather, $cluster, $host ) {
1415
1416                $this->data_gather      = $data_gather;
1417                $this->cluster          = $cluster;
1418                $this->host             = $host;
1419                $this->y_offset         = 0;
1420                $this->font             = 2;
1421                $this->fontspaceing     = 2;
1422                $this->headerstrlen     = array();
1423
1424                $this->fontheight       = ImageFontHeight( $this->font );
1425                $this->fontwidth        = ImageFontWidth( $this->font );
1426
1427                $dg                     = &$this->data_gather;
1428                $this->node             = &$dg->getNode( $this->host );
1429                $n                      = &$this->node;
1430                $this->njobs            = $n->getJobs();
1431        }
1432
1433        function drawJobs() {
1434
1435                $dg                     = &$this->data_gather;
1436                $colorblack             = imageColorAllocate( $this->image, 0, 0, 0 );
1437
1438                for( $n = 0; $n < count( $this->njobs ); $n++ ) {
1439
1440                        $jobid                  = $this->njobs[$n];
1441                        $jobinfo                = $dg->getJob( $jobid );
1442
1443                        $xoffset                = 5;
1444                        imageString( $this->image, $this->font, $xoffset, $this->y_offset, strval( $jobid ), $colorblack );
1445
1446                        foreach( $this->headerstrlen as $headername => $headerlen ) {
1447
1448                                if( $headername == 'nodes' ) {
1449                                        $attrval        = strval( count( $jobinfo[nodes] ) );
1450                                } else if( $headername == 'cpus' ) {
1451
1452                                        if( !isset( $jobinfo[ppn] ) )
1453                                                $jobinfo[ppn] = 1;
1454
1455                                        $attrval        = strval( count( $jobinfo[nodes] ) * intval( $jobinfo[ppn] ) );
1456
1457                                } else if( $headername == 'runningtime' ) {
1458                                        $attrval        = makeTime( intval( $jobinfo[reported] ) - intval( $jobinfo[start_timestamp] ) );
1459                                } else {
1460                                        $attrval        = strval( $jobinfo[$headername] );
1461                                }
1462
1463                                imageString( $this->image, $this->font, $xoffset, $this->y_offset, $attrval, $colorblack );
1464               
1465                                $xoffset        = $xoffset + ($this->fontwidth * ( $headerlen + 1 ) );
1466
1467                        }
1468                       
1469                        $this->newLineOffset();
1470                }
1471        }
1472
1473        function drawHeader() {
1474
1475                $dg                     = &$this->data_gather;
1476
1477                for( $n = 0; $n < count( $this->njobs ); $n++ ) {
1478
1479                        $jobid                  = $this->njobs[$n];
1480                        $jobinfo                = $dg->getJob( $jobid );
1481
1482                        if( !isset( $this->headerstrlen[id] ) )
1483                                $this->headerstrlen[id] = strlen( strval( $jobid ) );
1484                        else
1485                                if( strlen( strval( $jobid ) ) > $this->headerstrlen[id] )
1486                                        $this->headerstrlen[id] = strlen( strval( $jobid ) );
1487
1488                        if( !isset( $this->headerstrlen[owner] ) )
1489                                $this->headerstrlen[owner]      = strlen( strval( $jobinfo[owner] ) );
1490                        else
1491                                if( strlen( strval( $jobinfo[owner] ) ) > $this->headerstrlen[owner] )
1492                                        $this->headerstrlen[owner]      = strlen( strval( $jobinfo[owner] ) );
1493
1494                        if( !isset( $this->headerstrlen[queue] ) )
1495                                $this->headerstrlen[queue]      = strlen( strval( $jobinfo[queue] ) );
1496                        else
1497                                if( strlen( strval( $jobinfo[queue] ) ) > $this->headerstrlen[queue] )
1498                                        $this->headerstrlen[queue]      = strlen( strval( $jobinfo[queue] ) );
1499
1500                        if( !isset( $jobinfo[ppn] ) )
1501                                $jobinfo[ppn] = 1;
1502
1503                        $cpus                   = count( $jobinfo[nodes] ) * intval( $jobinfo[ppn] );
1504
1505                        if( !isset( $this->headerstrlen[cpus] ) )
1506                                $this->headerstrlen[cpus]       = strlen( strval( $cpus ) );
1507                        else
1508                                if( strlen( strval( $cpus ) ) > $this->headerstrlen[cpus] )
1509                                        $this->headerstrlen[cpus]       = strlen( strval( $cpus ) );
1510
1511                        $nodes                  = count( $jobinfo[nodes] );
1512
1513                        if( !isset( $this->headerstrlen[nodes] ) )
1514                                $this->headerstrlen[nodes]      = strlen( strval( $nodes ) );
1515                        else
1516                                if( strlen( strval( $nodes) ) > $this->headerstrlen[nodes] )
1517                                        $this->headerstrlen[nodes]      = strlen( strval( $nodes ) );
1518
1519                        $runningtime            = makeTime( intval( $jobinfo[reported] ) - intval( $jobinfo[start_timestamp] ) );
1520
1521                        if( !isset( $this->headerstrlen[runningtime] ) )
1522                                $this->headerstrlen[runningtime]        = strlen( strval( $runningtime) );
1523                        else
1524                                if( strlen( strval( $runningtime) ) > $this->headerstrlen[runningtime] )
1525                                        $this->headerstrlen[runningtime]        = strlen( strval( $runningtime) );
1526
1527                        if( !isset( $this->headerstrlen[name] ) )
1528                                $this->headerstrlen[name]       = strlen( strval( $jobinfo[name] ) );
1529                        else
1530                                if( strlen( strval( $jobinfo[name] ) ) > $this->headerstrlen[name] )
1531                                        $this->headerstrlen[name]       = strlen( strval( $jobinfo[name] ) );
1532
1533                }
1534
1535                $xoffset        = 5;
1536
1537                foreach( $this->headerstrlen as $headername => &$headerlen ) {
1538
1539                        $colorgreen     = imageColorAllocate( $this->image, 0, 200, 0 );
1540
1541                        imageString( $this->image, $this->font, $xoffset, $this->y_offset, ucfirst( $headername ), $colorgreen );
1542               
1543                        if( $headerlen < strlen( $headername ) )
1544                                $headerlen      = strlen( $headername );
1545
1546                        $xoffset        = $xoffset + ($this->fontwidth * ( $headerlen + 1 ) );
1547
1548                }
1549                $this->newLineOffset();
1550        }
1551
1552        function newLineOffset() {
1553
1554                $this->y_offset         = $this->y_offset + $this->fontheight + $this->fontspaceing;
1555        }
1556
1557        function draw() {
1558
1559                $xlen           = 450;
1560                $ylen           = ( count( $this->njobs ) * ( $this->fontheight + $this->fontspaceing ) ) + (3 * $this->fontheight);
1561
1562                $this->image    = imageCreateTrueColor( $xlen, $ylen );
1563                $colorwhite     = imageColorAllocate( $this->image, 255, 255, 255 );
1564                imageFill( $this->image, 0, 0, $colorwhite );                         
1565
1566                $colorblue      = imageColorAllocate( $this->image, 0, 0, 255 );
1567
1568                imageString( $this->image, $this->font, 1, $this->y_offset, "Monarch Joblist - host: ".$this->host, $colorblue );
1569                $this->newLineOffset();
1570
1571                $this->drawHeader();
1572                $this->drawJobs();
1573
1574                header( 'Content-type: image/png' );
1575                imagePNG( $this->image );
1576                imageDestroy( $this->image );
1577        }
1578}
1579
[331]1580function imageStringDown( &$image, $font, $x, $y, &$s, &$col )
1581{
1582        $fw     = imagefontwidth( $font);
1583        $fh     = imagefontheight( $font);
1584       
1585        $fontspacing = 0;
1586
1587        $fx     = $x;
1588        $fy     = $y;
1589
1590        for( $n=0; $n<strlen( $s ); $n++ )
1591        {
1592                $myc    = $s{$n};
1593
1594                imagestring( $image, $font, $fx, $fy, $myc, $col );
1595
1596                $fy     += ($fontspacing + $fh );
1597        }
1598}
1599
[329]1600function array_rem( $val, &$arr )
1601{
1602        // Delete val from arr
1603        //
1604        $i      = array_search( $val, $arr );
1605
1606        if( $i == false ) return false;
1607
1608        $arr    = array_merge( array_slice( $arr, 0, $i ), array_slice( $arr, $i+1, count( $arr ) ) );
1609
1610        return true;
1611}
1612
1613function cmp( $a, $b ) 
1614{
1615        global $SORT_ORDER;
1616        global $skan_str;
1617        global $x_first, $y_first;
1618
1619        $a_node         = $a;
1620        $b_node         = $b;
1621        $a              = $a_node->getHostname();
1622        $b              = $b_node->getHostname();
1623
1624        if( $a == $b ) return 0;
1625
1626        if( $x_first )
1627        {
1628                $n = sscanf( $a, $skan_str, $a_x, $a_y );
1629                $n = sscanf( $b, $skan_str, $b_x, $b_y );
1630        }
1631        else if( $y_first )
1632        {
1633                $n = sscanf( $a, $skan_str, $a_y, $a_x );
1634                $n = sscanf( $b, $skan_str, $b_y, $b_x );
1635        }
1636
1637        if ( $SORT_ORDER=="desc" )
1638        {
1639
1640                // 1  = a < b
1641                // -1 = a > b
1642                //
1643                if ($a_x == $b_x)
1644                {
1645                        if ($a_y < $b_y)
1646                        {
1647                                return 1;
1648                        }
1649                        else if ($a_y > $b_y)
1650                        {
1651                                return -1;
1652                        }
1653                }
1654                else if ($a_x < $b_x)
1655                {
1656                        return 1;
1657                }
1658                else if ($a_x > $b_x)
1659                {
1660                        return -1;
1661                }
1662        }
1663        else if ( $SORT_ORDER == "asc" )
1664        {
1665
1666                // 1  = a > b
1667                // -1 = a < b
1668                //
1669                if ($a_x == $b_x)
1670                {
1671                        if ($a_y > $b_y)
1672                        {
1673                                return 1;
1674                        }
1675                        else if ($a_y < $b_y)
1676                        {
1677                                return -1;
1678                        }
1679                }
1680                else if ($a_x > $b_x)
1681                {
1682                        return 1;
1683                }
1684                else if ($a_x < $b_x)
1685                {
1686                        return -1;
1687                }
1688        }
1689}
[303]1690function makeTime( $time ) {
1691
1692        $days = intval( $time / 86400 );
1693        $time = ($days>0) ? $time % ($days * 86400) : $time;
1694
1695        //printf( "time = %s, days = %s\n", $time, $days );
1696
1697        $date_str = '';
1698        $day_str = '';
1699
1700        if( $days > 0 ) {
1701                if( $days > 1 )
1702                        $day_str .= $days . ' days';
1703                else
1704                        $day_str .= $days . ' day';
1705        }
1706
1707        $hours = intval( $time / 3600 );
1708        $time = $hours ? $time % ($hours * 3600) : $time;
1709
1710        //printf( "time = %s, days = %s, hours = %s\n", $time, $days, $hours );
1711        if( $hours > 0 ) {
1712                $date_str .= $hours . ':';
1713                $date_unit = 'hours'; 
1714        }
1715
1716        $minutes = intval( $time / 60 );
1717        $seconds = $minutes ? $time % ($minutes * 60) : $time;
1718
1719        if( $minutes > 0 ) {
1720
1721                if( $minutes >= 10 )
1722                        $date_str .= $minutes . ':';
1723                else
1724                        $date_str .= '0' . $minutes . ':';
1725
1726                $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
1727        } else {
1728                if($hours > 0 ) {
1729                        $date_str .= '00:';
1730                        $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
1731                }
1732        }
1733
1734
1735        $date_unit = (!isset($date_unit)) ? 'seconds' : $date_unit;
1736
1737        if( $seconds > 0 ) {
1738
1739                if( $seconds >= 10 )
1740                        $date_str .= $seconds . ' ' . $date_unit;
1741                else
1742                        $date_str .= '0' . $seconds . ' ' . $date_unit;
1743
1744        } else if ( $hours > 0 or $minutes > 0 )
1745
1746                $date_str .= '00 ' . $date_unit;
1747
1748        if( $days > 0) {
1749
1750                if( $hours > 0 or $minutes > 0 or $seconds > 0 )
1751                        $date_str = $day_str . ' - ' . $date_str;
1752                else
1753                        $date_str = $day_str;
1754        }
1755
1756        return $date_str;
1757}
[102]1758?>
Note: See TracBrowser for help on using the repository browser.