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

Last change on this file since 463 was 463, checked in by bastiaans, 16 years ago

libtoga.php:

  • added getUsingFQDN() functions to determine FQDN usage from overview etc
  • fixed: moved column and row number header printing so that it will always be printed, even if there are nodes missing
  • code cleanup
  • added more comments

overview.php:

  • check getUsingFQDN() whether or not to use a domain name for node hostnames
  • added more comments

version.php:

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