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

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

job_monarch/libtoga.php:

  • parse CLUSTER_CONFS for cluster specific archive database settings
  • select descending while searching
  • add a PHP4 version of strpos

job_monarch/conf.php:

  • show nodes column by default
  • Property svn:keywords set to Id
File size: 45.9 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 454 2008-02-05 15:04:07Z 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
[106]566        function getNodes() {
567                $handler = $this->xmlhandler;
568                return $handler->getNodes();
569        }
570
[303]571        function getNode( $node ) {
572                $handler = $this->xmlhandler;
573                return $handler->getNode( $node );
574        }
575
[124]576        function getCpus() {
577                $handler = $this->xmlhandler;
578                return $handler->getCpus();
579        }
580
[106]581        function getJobs() {
582                $handler = $this->xmlhandler;
583                return $handler->getJobs();
584        }
585
[303]586        function getJob( $job ) {
587                $handler = $this->xmlhandler;
588                return $handler->getJob( $job );
589        }
590
[114]591        function getHeartbeat() {
592                $handler = $this->xmlhandler;
593                return $handler->getHeartbeat();
594        }
[298]595
596        function isJobmonRunning() {
597                $handler = $this->xmlhandler;
598                return $handler->isJobmonRunning();
599        }
[103]600}
601
602class TorqueXMLHandler {
603
[163]604        var $clusters, $heartbeat, $nodes, $jobs, $clustername, $proc_cluster;
[104]605
[163]606        function TorqueXMLHandler( $clustername ) {
[105]607                $jobs = array();
[104]608                $clusters = array();
[163]609                $this->nodes = array();
[104]610                $heartbeat = array();
[163]611                $this->clustername = $clustername;
612                //printf(" cluster set to %s \n", $this->clustername );
[103]613        }
614
[124]615        function getCpus() {
616
617                $cpus = 0;
618
[301]619                if( isset( $this->jobs ) && count( $this->jobs ) > 0 ) {
[124]620
[301]621                        foreach( $this->jobs as $jobid=>$jobattrs ) {
[124]622
[301]623                                $nodes = count( $jobattrs[nodes] );
624                                $ppn = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
625                                $mycpus = $nodes * $ppn;
626
627                                $cpus = $cpus + $mycpus;
628                        }
[124]629                }
630        }
631
[298]632        function isJobmonRunning() {
633
634                if (isset( $this->heartbeat['time'] ))
635                        return 1;
636                else
637                        return 0;
638        }
639
[103]640        function startElement( $parser, $name, $attrs ) {
641
[262]642                $jobs = $this->jobs;
643                $nodes = $this->nodes;
[105]644
[103]645                if ( $attrs[TN] ) {
646
647                        // Ignore dead metrics. Detect and mask failures.
648                        if ( $attrs[TN] > $attrs[TMAX] * 4 )
649                                return;
650                }
651
652                $jobid = null;
653
[163]654                //printf( '%s=%s', $attrs[NAME], $attrs[VAL] );
[103]655
[163]656                //printf( "clustername = %s proc_cluster = %s\n", $this->clustername, $this->proc_cluster );
657
[104]658                if( $name == 'CLUSTER' ) {
[103]659
[163]660                        $this->proc_cluster = $attrs[NAME];
661                        //printf( "Found cluster %s\n", $attrs[NAME] );
662                        //print_r( $attrs );
[103]663
[163]664                        //if( !isset( $clusters[$clustername] ) )
665                        //      $clusters[$clustername] = array();
[103]666
[163]667                } else if( $name == 'HOST' and $this->proc_cluster == $this->clustername) {
[105]668
669                        $hostname = $attrs[NAME];
670                        $location = $attrs[LOCATION];
[163]671                        //printf( "Found node %s\n", $hostname );
[105]672
[200]673                        if( !isset( $nodes[$hostname] ) )
[326]674                                $nodes[$hostname] = new NodeImage( $this->proc_cluster, $hostname );
[105]675
[219]676                } else if( $name == 'METRIC' and strstr( $attrs[NAME], 'MONARCH' ) and $this->proc_cluster == $this->clustername ) {
[103]677
[219]678                        if( strstr( $attrs[NAME], 'MONARCH-HEARTBEAT' ) ) {
[103]679
[114]680                                $this->heartbeat['time'] = $attrs[VAL];
[106]681                                //printf( "heartbeat %s\n", $heartbeat['time'] );
[104]682
[219]683                        } else if( strstr( $attrs[NAME], 'MONARCH-JOB' ) ) {
[104]684
[253]685                                sscanf( $attrs[NAME], 'MONARCH-JOB-%d-%d', $jobid, $monincr );
[104]686
[106]687                                //printf( "jobid %s\n", $jobid );
[104]688
689                                if( !isset( $jobs[$jobid] ) )
690                                        $jobs[$jobid] = array();
691
692                                $fields = explode( ' ', $attrs[VAL] );
693
694                                foreach( $fields as $f ) {
695                                        $togavalues = explode( '=', $f );
696
697                                        $toganame = $togavalues[0];
698                                        $togavalue = $togavalues[1];
699
[106]700                                        //printf( "\t%s\t= %s\n", $toganame, $togavalue );
[104]701
[103]702                                        if( $toganame == 'nodes' ) {
703
[135]704                                                if( $jobs[$jobid][status] == 'R' ) {
705                                               
706                                                        if( !isset( $jobs[$jobid][$toganame] ) )
707                                                                $jobs[$jobid][$toganame] = array();
[104]708
[135]709                                                        $mynodes = explode( ';', $togavalue );
[103]710
[300]711                                                        //print_r($mynodes);
712
[262]713                                                        foreach( $mynodes as $node ) {
[103]714
[262]715                                                                if( !in_array( $node, $jobs[$jobid][$toganame] ) ) {
[253]716                                                                        $jobs[$jobid][$toganame][] = $node;
[262]717                                                                }
718                                                        }
[135]719
720                                                } else if( $jobs[$jobid][status] == 'Q' ) {
721
722                                                        $jobs[$jobid][$toganame] = $togavalue;
723                                                }
724                                               
[104]725                                        } else {
[103]726
[105]727                                                $jobs[$jobid][$toganame] = $togavalue;
[104]728                                        }
[103]729                                }
[111]730
731                                if( isset( $jobs[$jobid][domain] ) and isset( $jobs[$jobid][nodes] ) ) {
[112]732                       
733                                        $nr_nodes = count( $jobs[$jobid][nodes] );
[300]734               
735                                        if( $jobs[$jobid][status] == 'R' ) {
[111]736
[300]737                                                foreach( $jobs[$jobid][nodes] as $node ) {
[200]738
[300]739                                                        $domain = $jobs[$jobid][domain];
740                                                        $domain_len = 0 - strlen( $domain );
[200]741
[300]742                                                        if( substr( $node, $domain_len ) != $domain ) {
743                                                                $host = $node. '.'.$domain;
744                                                        } else {
745                                                                $host = $node;
746                                                        }
747
748                                                        //$host = $node.'.'.$jobs[$jobid][domain];
[111]749                               
[300]750                                                        if( !isset( $nodes[$host] ) )
[326]751                                                                $my_node = new NodeImage( $this->proc_cluster, $host );
[300]752                                                        else
753                                                                $my_node = $nodes[$host];
[111]754
[300]755                                                        if( !$my_node->hasJob( $jobid ) )
[111]756
[300]757                                                                if( isset( $jobs[$jobid][ppn] ) )
758                                                                        $my_node->addJob( $jobid, ((int) $jobs[$jobid][ppn]) );
759                                                                else
760                                                                        $my_node->addJob( $jobid, 1 );
[111]761
[300]762                                                        $nodes[$host] = $my_node;
763                                                }
[111]764                                        }
765                                }
[103]766                        }
767                }
[114]768                $this->jobs = $jobs;
[163]769                //print_r( $nodes );
770                $this->nodes = $nodes;
[200]771                //print_r( $this->nodes );
[103]772        }
773
774        function stopElement( $parser, $name ) {
775        }
[105]776
777        function printInfo() {
778
779                $jobs = &$this->jobs;
780
781                printf( "---jobs---\n" );
782
783                foreach( $jobs as $jobid => $job ) {
784
785                        printf( "job %s\n", $jobid );
786
787                        if( isset( $job[nodes] ) ) {
788
789                                foreach( $job[nodes] as $node ) {
790
791                                        $mynode = $this->nodes[$node];
792                                        $hostname = $mynode->getHostname();
793                                        $location = $mynode->getLocation();
794
795                                        printf( "\t- node %s\tlocation %s\n", $hostname, $location );
796                                        //$this->nodes[$hostname]->setLocation( "hier draait job ".$jobid );
797                                }
798                        }
799                }
800
801                printf( "---nodes---\n" );
802
803                $nodes = &$this->nodes;
804
805                foreach( $nodes as $node ) {
806
807                        $hostname = $node->getHostname();
808                        $location = $node->getLocation();
809                        $jobs = implode( ' ', $node->getJobs() );
810                        printf( "* node %s\tlocation %s\tjobs %s\n", $hostname, $location, $jobs );
811                }
812        }
[106]813
814        function getNodes() {
[163]815                //print_r( $this->nodes );
[106]816                return $this->nodes;
817        }
818
[303]819        function getNode( $node ) {
820
821                $nodes = &$this->nodes;
822                if( isset( $nodes[$node] ) )
823                        return $nodes[$node];
824                else
825                        return NULL;
826        }
827
[106]828        function getJobs() {
829                return $this->jobs;
830        }
[114]831
[303]832        function getJob( $job ) {
833
834                $jobs = &$this->jobs;
835                if( isset( $jobs[$job] ) )
836                        return $jobs[$job];
837                else
838                        return NULL;
839        }
840
[114]841        function getHeartbeat() {
842                return $this->heartbeat['time'];
843        }
[103]844}
845
[107]846class NodeImage {
[102]847
[122]848        var $image, $x, $y, $hostname, $jobs, $tasks, $showinfo;
[102]849
[326]850        function NodeImage( $cluster, $hostname ) {
[102]851
[305]852                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
853
[111]854                $this->jobs = array();
855                //$this->image = $image;
856                //$this->x = $x;
857                //$this->y = $y;
858                $this->tasks = 0;
[110]859                $this->hostname = $hostname;
[111]860                $this->cpus = $this->determineCpus();
[326]861                $this->clustername = $cluster;
[122]862                $this->showinfo = 1;
[305]863                $this->size = $SMALL_CLUSTERIMAGE_NODEWIDTH;
[102]864        }
865
[111]866        function addJob( $jobid, $cpus ) {
867                $jobs = &$this->jobs;
868
869                $jobs[] = $jobid;
870                $this->jobs = $jobs;
871
872                $this->addTask( $cpus );
873        }
874
875        function hasJob( $jobid ) {
876
877                $jobfound = 0;
878
879                if( count( $this->jobs ) > 0 )
880                        foreach( $this->jobs as $job )
881
882                                if( $job == $jobid )
883                                        $jobfound = 1;
884
885                return $jobfound;
886        }
887
888        function addTask( $cpus ) {
889
890                $this->tasks = $this->tasks + $cpus;
891        }
892
893        function setImage( $image ) {
894
895                $this->image = $image;
896        }
897
[106]898        function setCoords( $x, $y ) {
[107]899
[106]900                $this->x = $x;
901                $this->y = $y;
902        }
903
[326]904        function getImagemapArea() {
905
906                $area_topleft           = $this->x . "," . $this->y;
907                $area_bottomright       = ($this->x + $this->size) . "," . ($this->y + $this->size);
908                $area_coords            = $area_topleft . "," . $area_bottomright;
909
910                $area_href              = "./?c=" . $this->clustername . "&h=" . $this->hostname;
911                $area_tooltip           = $this->hostname . ": " . implode( " ", $this->jobs );
912
913                $tag_href               = "HREF=\"" . $area_href . "\"";
914                $tag_coords             = "COORDS=\"" . $area_coords . "\"";
915                $tag_tooltip1           = "ALT=\"" . $area_tooltip . "\"";
916                $tag_tooltip2           = "TITLE=\"" . $area_tooltip . "\"";
917
918                return ("<AREA SHAPE=\"RECT\" " . $tag_coords . " " . $tag_href . " " . $tag_tooltip1 . " " . $tag_tooltip2 . ">");
919        }
920
[102]921        function colorHex( $color ) {
922       
[109]923                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) );
[102]924
925                return $my_color;
926        }
927
[106]928        function setLoad( $load ) {
929                $this->load = $load;
930        }
931
[108]932        function setHostname( $hostname ) {
933                $this->hostname = $hostname;
934        }
935
[122]936        function getHostname() {
937                return $this->hostname;
938        }
939
[114]940        function getJobs() {
941                return $this->jobs;
942        }
943
[122]944        function setShowinfo( $showinfo ) {
945                $this->showinfo = $showinfo;
946        }
947
[305]948        function drawSmall() {
[106]949
[305]950                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
951
952                $this->size     = $SMALL_CLUSTERIMAGE_NODEWIDTH;
953
954                $this->draw();
[122]955        }
[110]956
[122]957        function drawBig() {
[111]958
[305]959                global $BIG_CLUSTERIMAGE_NODEWIDTH;
960
961                $this->size     = $BIG_CLUSTERIMAGE_NODEWIDTH;
962
963                $this->draw();
[122]964        }
[106]965
[305]966        function draw() {
[106]967
[122]968                global $JOB_NODE_MARKING;
[102]969
[122]970                $black_color = imageColorAllocate( $this->image, 0, 0, 0 );
[305]971                $size = $this->size;
[107]972
[110]973                imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+($size), $this->y+($size), $black_color );
974
[122]975                if( $this->showinfo) {
976               
977                        $this->load = $this->determineLoad();
[111]978
[122]979                        if( !isset( $this->image ) or !isset( $this->x ) or !isset( $this->y ) ) {
980                                printf( "aborting\n" );
981                                printf( "x %d y %d load %f\n", $this->x, $this->y, $load );
982                                return;
983                        }
[111]984
985
[122]986                        // Convert Ganglias Hexadecimal load color to a Decimal one
987                        //
988                        $load = $this->determineLoad(); 
989                        $usecolor = $this->colorHex( load_color($load) );
990                        imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor );
991                        if( count( $this->jobs ) > 0 )
[280]992                                imageString( $this->image, 1, $this->x+(($size/2)-1), $this->y+(($size/2)-4), $JOB_NODE_MARKING, $black_color );
[111]993
[122]994                } else {
[111]995
[122]996                        // White
997                        $usecolor = imageColorAllocate( $this->image, 255, 255, 255 );
998                        imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor );
999                }
1000
1001
[102]1002        }
1003
[111]1004        function determineCpus() {
[102]1005
[108]1006                global $metrics;
1007
1008                $cpus = $metrics[$this->hostname][cpu_num][VAL];
[107]1009                if (!$cpus) $cpus=1;
[111]1010
1011                return $cpus;
1012        }
1013
1014        function determineLoad() {
1015
1016                global $metrics;
1017
[108]1018                $load_one = $metrics[$this->hostname][load_one][VAL];
[111]1019                $load = ((float) $load_one)/$this->cpus;
[102]1020
[111]1021                return $load;
[106]1022        }
1023}
1024
[107]1025class ClusterImage {
[106]1026
[111]1027        var $dataget, $image, $clustername;
[124]1028        var $filtername, $filters;
[106]1029
[326]1030        //function ClusterImage( $clustername ) {
1031        function ClusterImage( $data, $clustername ) {
[114]1032
[326]1033                //$this->dataget                = $dataget;
[305]1034                $this->dataget          = new DataGatherer( $clustername );
[326]1035                $this->data             = $data;
[305]1036                $this->clustername      = $clustername;
1037                $this->filters          = array();
1038                $this->size             = 's';
[326]1039                $this->width            = 0;
1040                $this->height           = 0;
1041                $this->output           = 1;
[106]1042        }
1043
[326]1044        function getWidth() {
1045                return $this->width;
1046        }
1047        function getHeight() {
1048                return $this->height;
1049        }
1050
[305]1051        function setSmall() {
1052                $this->size     = 's';
1053        }
1054
1055        function setBig() {
1056                $this->size     = 'b';
1057        }
1058
[326]1059        function setNoimage() {
1060                $this->output   = 0;
1061        }
1062
[305]1063        function isSmall() {
1064                return ($this->size == 's');
1065        }
1066
1067        function isBig() {
1068                return ($this->size == 'b');
1069        }
1070
[122]1071        function setFilter( $filtername, $filtervalue ) {
1072
[124]1073                $this->filters[$filtername] = $filtervalue;
[122]1074        }
1075
1076        function filterNodes( $jobs, $nodes ) {
1077
1078                $filtered_nodes = array();
1079
1080                foreach( $nodes as $node ) {
1081
1082                        $hostname = $node->getHostname();
1083
[124]1084                        $addhost = 1;
[122]1085
[124]1086                        if( count( $this->filters ) > 0 ) {
1087
1088                                $mynjobs = $node->getJobs();
1089
1090                                if( count( $mynjobs ) > 0 ) {
1091
1092                                        foreach( $mynjobs as $myjob ) {
1093
1094                                                foreach( $this->filters as $filtername => $filtervalue ) {
1095
1096                                                        if( $filtername!=null && $filtername!='' ) {
1097
1098                                                                if( $filtername == 'jobid' && !$node->hasJob( $filtervalue) ) {
1099                                                                        $addhost = 0;
1100                                                                } else if( $filtername != 'jobid' ) {
1101                                                                        if( $jobs[$myjob][$filtername] != $filtervalue ) {
1102                                                                                $addhost = 0;
1103                                                                        }
1104                                                                }
1105                                                        }
1106                                                }
1107                                        }
1108                                } else
1109                                        $addhost = 0;
1110                        }
1111
1112                        if( $addhost )
[122]1113                                $filtered_nodes[] = $hostname;
1114                }
1115
1116                return $filtered_nodes;
1117        }
1118
[106]1119        function draw() {
[110]1120
1121                global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH;
[305]1122                global $BIG_CLUSTERIMAGE_MAXWIDTH, $BIG_CLUSTERIMAGE_NODEWIDTH;
[339]1123                global $CLUSTER_CONFS, $confcluster;
[329]1124
[339]1125                global $SORTBY_HOSTNAME, $SORT_ORDER, $skan_str;
[329]1126                global $x_first, $y_first;
[331]1127
[337]1128                foreach( $CLUSTER_CONFS as $confcluster => $conffile )
1129                {
[339]1130                        if( strtolower( trim($this->clustername) ) == strtolower(trim($confcluster)) )
[337]1131                        {
1132                                include_once $conffile;
1133                        }
1134                }
1135
[339]1136                //global $SORTBY_HOSTNAME, $SORT_ORDER;
1137                //global $SORT_XLABEL, $SORT_YLABEL;
[110]1138       
[339]1139                //printf( "SORTBY_HOSTNAME %s SORT_YLABEL %s\n", $SORTBY_HOSTNAME, $SORT_YLABEL );
1140
[107]1141                $mydatag = $this->dataget;
[326]1142                $mydatag->parseXML( $this->data );
[106]1143
[305]1144                if( $this->isSmall() ) {
1145                        $max_width = $SMALL_CLUSTERIMAGE_MAXWIDTH;
1146                        $node_width = $SMALL_CLUSTERIMAGE_NODEWIDTH;
1147                } else if( $this->isBig() ) {
1148                        $max_width = $BIG_CLUSTERIMAGE_MAXWIDTH;
1149                        $node_width = $BIG_CLUSTERIMAGE_NODEWIDTH;
1150                }
[106]1151
1152                $nodes = $mydatag->getNodes();
[111]1153                $nodes_hosts = array_keys( $nodes );
[106]1154
1155                $nodes_nr = count( $nodes );
1156
1157                $nodes_size = $nodes_nr*$node_width;
1158                $node_rows = 0;
1159
1160                if( $nodes_size > $max_width ) {
[107]1161                        $nodes_per_row = ( (int) ($max_width/$node_width) );
[106]1162                } else {
1163                        $nodes_per_row = $nodes_size;
1164                        $node_rows = 1;
1165                }
1166
1167                if( $nodes_per_row < $nodes_nr ) {
[107]1168                        $node_rows = ( (int) ($nodes_nr/$nodes_per_row) );
1169                        $node_rest = fmod( $nodes_nr, $nodes_per_row );
1170                        //printf( "nodesnr %d noderest %f\n", $nodes_nr, $node_rest );
1171                        if( $node_rest > 0 ) {
[106]1172                                $node_rows++;
[107]1173                                //printf( "noderows %d\n", $node_rows );
[106]1174                        }
1175                }
1176
[306]1177                $y_offset       = 0;
1178                $font           = 2;
[329]1179                $fontwidth      = ImageFontWidth( $font );
[306]1180                $fontheight     = ImageFontHeight( $font );
1181                $fontspaceing   = 2;
1182                $y_offset       = $fontheight + (2 * $fontspaceing);
1183
[326]1184                $this->width    = $max_width;
1185                $this->height   = ($y_offset + (($node_rows*$node_width)+1) );
1186
[329]1187                //$image = imageCreateTrueColor( $max_width, ($y_offset + (($node_rows*$node_width)+1) ) );
1188                //$colorwhite = imageColorAllocate( $image, 255, 255, 255 );
1189                //imageFill( $image, 0, 0, $colorwhite );
[106]1190
[329]1191                //if( $this->isSmall() ) {
[306]1192
[329]1193                //      $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
[306]1194
[329]1195                //      imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1196                //}
[306]1197
[122]1198                $jobs = $mydatag->getJobs();
1199                //printf("filtername = %s\n", $filtername );
1200                $filtered_nodes = $this->filterNodes( $jobs, $nodes );
1201
1202                //print_r($filtered_nodes);
1203
[329]1204                if( $SORTBY_HOSTNAME != "" )
1205                {
1206
1207                        $sorted         = array();
1208
1209                        $x_first        = 0;
1210                        $y_first        = 0;
1211
[339]1212                        $skan_str       = $SORTBY_HOSTNAME;
[329]1213
[339]1214                        global $x_present, $y_present;
1215                        $x_present      = false;
1216                        $y_present      = false;
1217
1218                        if(stripos( $SORTBY_HOSTNAME, "{x}" ) != false )
[329]1219                        {
[339]1220                                $x_present      = true;
1221                        }
1222                        if(stripos( $SORTBY_HOSTNAME, "{y}" ) != false )
1223                        {
1224                                $y_present      = true;
1225                        }
1226
1227                        if(( strpos( $SORTBY_HOSTNAME, "{x}" ) < strpos( $SORTBY_HOSTNAME, "{y}" ) ) && ( $x_present && $y_present ))
1228                        {
[106]1229                       
[329]1230                                $x_first        = 1;
1231                        }
[339]1232                        else if(( strpos( $SORTBY_HOSTNAME, "{x}" ) > strpos( $SORTBY_HOSTNAME, "{y}" ) ) && ( $x_present && $y_present ))
[329]1233                        {
1234                                $y_first        = 1;
1235               
1236                        }
[339]1237                        else if( $x_present )
1238                        {
1239                                $x_first        = 1;
1240                        }
1241                        else if( $y_present )
1242                        {
1243                                $y_first        = 1;
1244                        }
[329]1245
[339]1246                        if(( $x_first ) && ( $x_present && $y_present ) )
1247                        {
1248                                $skan_str       = str_replace( "{x}", "%d", $skan_str );
1249                                $skan_str       = str_replace( "{y}", "%d", $skan_str );
1250                                //printf("ppoep = %s\n", $skan_str);
1251                        } 
1252                        else if( $x_present)
1253                        {
1254                                $skan_str       = str_replace( "{x}", "%d", $skan_str );
1255                        }
1256                        else if( $y_present)
1257                        {
1258                                $skan_str       = str_replace( "{y}", "%d", $skan_str );
1259                        }
[329]1260
1261                        $x_min          = null;
1262                        $x_max          = null;
1263                        $y_min          = null;
1264                        $y_max          = null;
1265
1266                        foreach( $nodes as $hostname => $node )
1267                        {
[404]1268                                $x      = null;
1269                                $y      = null;
[339]1270
1271                                if( $x_present && $y_present )
[329]1272                                {
[339]1273                                        //$n    = sscanf( $hostname, $skan_str, $i, $j );
1274                                        if( $x_first )
1275                                        {
1276                                                $n = sscanf( $hostname, $skan_str, $x, $y );
1277                                        }
1278                                        else if( $y_first )
1279                                        {
1280                                                $n = sscanf( $hostname, $skan_str, $y, $x );
1281                                        }
1282                                        // Remove nodes that don't match
1283                                        //
1284                                        if( $n < 2 )
1285                                        {
[404]1286                                                //printf( "removing node %s - x present & y present + <2 x,y matches\n", $hostname );
[339]1287                                                unset( $nodes[$hostname] );
1288                                        }
[329]1289                                }
[339]1290                                else if( $x_present && !$y_present )
[329]1291                                {
[339]1292                                        $n = sscanf( $hostname, $skan_str, $x );
1293                                        // Remove nodes that don't match
1294                                        //
1295                                        if( $n < 1 )
1296                                        {
[404]1297                                                //printf( "removing node %s - x present & !y present + <1 x match\n", $hostname );
[339]1298                                                unset( $nodes[$hostname] );
1299                                        }
1300                                        $y      = 1;
[329]1301                                }
[339]1302                                else if( $y_present && !$x_present )
1303                                {
1304                                        $n = sscanf( $hostname, $skan_str, $y );
1305                                        // Remove nodes that don't match
1306                                        //
1307                                        if( $n < 1 )
1308                                        {
[404]1309                                                //printf( "removing node %s - y present & !x present + <1 y match\n", $hostname );
[339]1310                                                unset( $nodes[$hostname] );
1311                                        }
1312                                        $x      = 1;
1313                                }
1314                                //printf( "xfirst %s yfirst %s\n", $x_first, $y_first );
[329]1315
1316                                //printf( "n %s\n", $n );
1317
1318
[404]1319                                //printf( "node %s x_min %s x %s\n", $hostname, $x_min, $x );
1320
1321                                if( !$x_min && $x != null )
[329]1322                                {
1323                                        $x_min  = $x;
1324                                }
[404]1325                                else if( $x < $x_min && $x != null )
[329]1326                                {
1327                                        $x_min  = $x;
1328                                }
[404]1329                                if( !$x_max && $x != null )
[329]1330                                {
1331                                        $x_max  = $x;
1332                                }
[404]1333                                else if( $x > $x_max && $x != null )
[329]1334                                {
1335                                        $x_max  = $x;
1336                                }
[404]1337                                if( !$y_min && $y != null )
[329]1338                                {
1339                                        $y_min  = $y;
1340                                }
[404]1341                                else if( $y < $y_min && $y != null )
[329]1342                                {
1343                                        $y_min  = $y;
1344                                }
[404]1345                                if( !$y_max && $y != null )
[329]1346                                {
1347                                        $y_max  = $y;
1348                                }
[404]1349                                else if( $y > $y_max && $y != null )
[329]1350                                {
1351                                        $y_max  = $y;
1352                                }
1353                        }
1354
1355                        //printf( "ss %s\n", $skan_str);
1356                        $sorted_nodes   = usort( $nodes, "cmp" );
1357
[339]1358                        //print_r( $nodes );
1359
[329]1360                        $cur_node       = 0;
1361
[331]1362                        $x_offset       = 0;
1363                        $y_offset       = 0;
1364                        $font           = 2;
1365                        $fontwidth      = ImageFontWidth( $font );
1366                        $fontheight     = ImageFontHeight( $font );
1367                        $fontspaceing   = 2;
1368
[333]1369                        if( $this->isSmall() ) 
1370                        {
1371                                $y_offset       = $y_offset + (2 * $fontspaceing) + $fontheight;
1372                        }
1373
[331]1374                        if( $this->isBig() ) 
1375                        {
1376
1377                                $y_offset       = ($fontheight * (1 + strlen( $x_max) ) ) + ((2 + strlen( $x_max)) * $fontspaceing);
1378                                $x_offset       = ($fontwidth * (1 + strlen( $y_max) ) ) + ((2 + strlen( $y_max)) * $fontspaceing);
1379
1380                        }
1381                        //$x_offset     = ($fontwidth * 3) + (5 * $fontspaceing);
1382
[329]1383                        //printf( "xmin %s xmax %s\n", $x_min, $x_max );
1384                        //printf( "ymin %s ymax %s\n", $y_min, $y_max );
1385
1386                        // werkt
1387                        //print_r( $nodes );
1388
[330]1389                        $image_width    = $x_offset + ($node_width * ($x_max-$x_min+2));
[428]1390
1391                        if( $this->isSmall() ) 
[340]1392                        {
[428]1393                                $image_width    = $max_width;
1394                        } else if( $this->isBig() ) 
[340]1395                        {
[428]1396                                $image_width    = ($image_width < $max_width) ? $image_width : $max_width;
[340]1397                        }
[428]1398                        //else if( $this->isSmall() )
1399                        //{
1400                        //      $image_width    = $this->width;
1401                        //}
[330]1402                        $image_height   = $y_offset + ($node_width * ($y_max-$y_min+2));
1403
1404                        $this->width    = $image_width;
1405                        $this->heigth   = $image_heigth;
1406
1407                        $image          = imageCreateTrueColor( $image_width, $image_height );
[329]1408                        $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1409
1410                        imageFill( $image, 0, 0, $colorwhite );
1411
[333]1412                        if( $this->isSmall() ) {
1413
1414                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1415
1416                                imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1417                        }
1418
[339]1419                        if( $this->isBig() && ( isset( $SORT_XLABEL ) || isset( $SORT_YLABEL ) ) )
[331]1420                        {
1421                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
[329]1422
[339]1423                                if( isset( $SORT_XLABEL ) )
1424                                {
1425                                        imageString( $image, $font, $x_offset, $fontspaceing, $SORT_XLABEL, $colorblue );
1426                                }
[329]1427
[339]1428                                if( isset( $SORT_YLABEL ) )
1429                                {
1430                                        // Stupid php without imageStringDown function
1431                                        //
1432                                        imageStringDown( $image, $font, $fontspaceing, $y_offset, $SORT_YLABEL, $colorblue );
1433                                }
[331]1434                        }
[404]1435                        //print_r( $nodes );
[329]1436
1437                        for( $n = $x_min; $n <= $x_max; $n++ )
1438                        {
1439                                for( $m = $y_min; $m <= $y_max; $m++ )
1440                                {
1441                                        if( $x_min > 0 )
1442                                        {
1443                                                $x      = $x_offset + ( ($n-$x_min) * $node_width );
1444                                        }
1445                                        if( $y_min > 0 )
1446                                        {
1447                                                $y      = $y_offset + ( ($m-$y_min) * $node_width );
1448                                        }
1449
[330]1450                                        if( isset( $nodes[$cur_node] ) ) 
1451                                        {
[329]1452                                                $host   = $nodes[$cur_node]->getHostname();
1453
[339]1454                                                if( $x_present && $y_present )
[329]1455                                                {
[339]1456                                                        if( $x_first )
1457                                                        {
1458                                                                $nn = sscanf( $host, $skan_str, $rx, $ry );
1459                                                        }
1460                                                        else if( $y_first )
1461                                                        {
1462                                                                $nn = sscanf( $host, $skan_str, $ry, $rx );
1463                                                        }
1464                                                        if ( $nn < 2 )
1465                                                        {
[404]1466                                                                //printf( "skipping node %s - y present & x present + <2 x,y matchs\n", $host);
[339]1467                                                                continue;
1468                                                        }
[404]1469                                                        if( intval( $rx ) > $n )
[339]1470                                                        {
1471                                                                $m      = $y_max + 1;
[404]1472                                                                //printf( "skipping node %s - y present & x present + rx %s > n %s\n", $rx, $n);
[339]1473                                                                continue;
1474                                                        }
[404]1475                                                        if( intval( $ry ) > $m )
1476                                                        {
1477                                                                //printf( "skipping node %s - y present & x present + ry %s > m %s\n", $ry, $m);
1478                                                                continue;
1479                                                        }
[329]1480                                                }
[339]1481                                                else if( $x_present )
[329]1482                                                {
[339]1483                                                        $nn = sscanf( $host, $skan_str, $rx );
[329]1484                                                }
[339]1485                                                else if( $y_present )
[329]1486                                                {
[339]1487                                                        $nn = sscanf( $host, $skan_str, $ry );
[329]1488                                                }
1489
1490                                                if( !in_array( $host, $filtered_nodes ) )
[404]1491                                                {
[405]1492                                                        //printf( "setting node %s showinfo to 0 - not found in filtered_nodes", $host);
[329]1493                                                        $nodes[$cur_node]->setShowinfo( 0 );
[404]1494                                                }
[329]1495
1496                                                $nodes[$cur_node]->setCoords( $x, $y );
1497                                                $nodes[$cur_node]->setImage( $image );
1498
1499                                                //print_r( $nodes[$cur_node] );
1500
1501                                                if( $this->isSmall() )
1502                                                        $nodes[$cur_node]->drawSmall();
1503                                                else if( $this->isBig() )
1504                                                        $nodes[$cur_node]->drawBig();
[404]1505
1506                                                $cur_node++;
[329]1507                                        }
[333]1508                                        if( $this->isBig() ) 
[332]1509                                        {
[339]1510                                                if(( $n == $x_min ) && ( isset($SORT_YLABEL) ) )
[333]1511                                                {
1512                                                        $mfontspacing   = 1;
1513                                                        $ylabel_x       = $x - ( $fontwidth * strlen( $y_max ) ) - $mfontspacing;
1514                                                        $ylabel_y       = $y;
[332]1515
[333]1516                                                        imageString( $image, $font, $ylabel_x, $ylabel_y, strval( $m ), $colorblue );
1517                                                }
[339]1518                                                if(( $m == $y_min ) && ( isset($SORT_XLABEL) ) )
[333]1519                                                {
1520                                                        $mfontspacing   = 2;
1521                                                        $xlabel_y       = $y - ( $fontheight * strlen( $x_max ) );
1522                                                        $xlabel_x       = $x + $mfontspacing; 
[332]1523
[333]1524                                                        imageStringDown( $image, $font, $xlabel_x, $xlabel_y, strval( $n ), $colorblue );
1525                                                }
[332]1526                                        }
1527
[329]1528                                }
1529                        }
1530
1531                }
1532                else
1533                {
[428]1534                        if( $this->isSmall() ) {
1535                                $image          = imageCreateTrueColor( $max_width, ($y_offset + (($node_rows*$node_width)+1) ) );
1536                        } else if( $this->isBig() ) {
1537                                $image_width    = ($node_width * $nodes_nr) + 2;
1538                                $image_width    = ($image_width < $max_width) ? $image_width : $max_width;
1539                                $image          = imageCreateTrueColor( $image_width, ($y_offset + (($node_rows*$node_width)+1) ) );
1540                        }
[329]1541                        $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1542
1543                        imageFill( $image, 0, 0, $colorwhite );
1544
1545                        if( $this->isSmall() ) {
1546
1547                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1548
1549                                imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1550                        }
1551
1552                        for( $n = 0; $n < $node_rows; $n++ ) {
[107]1553                       
[329]1554                                for( $m = 0; $m < $nodes_per_row; $m++ ) {
1555                       
1556                                        $x = ($m * $node_width);
1557                                        $y = $y_offset + ($n * $node_width);
[107]1558
[329]1559                                        $cur_node = ($n * $nodes_per_row) + ($m);
1560                                        $host = $nodes_hosts[$cur_node];
[107]1561
[329]1562                                        if( isset( $nodes[$host] ) ) {
[111]1563
[329]1564                                                $nodes[$host]->setCoords( $x, $y );
1565                                                $nodes[$host]->setImage( $image );
[122]1566
[329]1567                                                if( !in_array( $host, $filtered_nodes ) )
[404]1568                                                {
[329]1569                                                        $nodes[$host]->setShowinfo( 0 );
[404]1570                                                }
[122]1571
[329]1572                                                if( $this->isSmall() )
1573                                                        $nodes[$host]->drawSmall();
1574                                                else if( $this->isBig() )
1575                                                        $nodes[$host]->drawBig();
1576                                        }
[111]1577                                }
[106]1578                        }
1579                }
[326]1580       
1581                $this->nodes    = &$nodes;
1582
1583                if ($this->output) {
1584                        header( 'Content-type: image/png' );
1585                        imagePNG( $image );
1586                        imageDestroy( $image );
1587                }
[102]1588        }
[326]1589
1590        function getImagemapArea() {
1591
1592                $clusterimage_map       = "";
1593
1594                foreach( $this->nodes as $hostname => $node ) {
1595
1596                        $node_map               = $node->getImagemapArea();
1597                        $clusterimage_map       .= $node_map;
1598                }
1599
1600                return $clusterimage_map;
1601        }
[102]1602}
1603
[298]1604class EmptyImage {
1605
1606        function draw() {
1607                $image          = imageCreateTrueColor( 1, 1 );
1608                $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1609                imageFill( $image, 0, 0, $colorwhite );                         
1610
1611                header( 'Content-type: image/png' );
1612                imagePNG( $image );
1613                imageDestroy( $image );
1614        }
1615}
1616
[303]1617class HostImage {
[106]1618
[303]1619        var $data_gather, $cluster, $host, $node, $image;
1620        var $headerstrlen;
1621
1622        function HostImage( $data_gather, $cluster, $host ) {
1623
1624                $this->data_gather      = $data_gather;
1625                $this->cluster          = $cluster;
1626                $this->host             = $host;
1627                $this->y_offset         = 0;
1628                $this->font             = 2;
1629                $this->fontspaceing     = 2;
1630                $this->headerstrlen     = array();
1631
1632                $this->fontheight       = ImageFontHeight( $this->font );
1633                $this->fontwidth        = ImageFontWidth( $this->font );
1634
1635                $dg                     = &$this->data_gather;
1636                $this->node             = &$dg->getNode( $this->host );
1637                $n                      = &$this->node;
1638                $this->njobs            = $n->getJobs();
1639        }
1640
1641        function drawJobs() {
1642
1643                $dg                     = &$this->data_gather;
1644                $colorblack             = imageColorAllocate( $this->image, 0, 0, 0 );
1645
1646                for( $n = 0; $n < count( $this->njobs ); $n++ ) {
1647
1648                        $jobid                  = $this->njobs[$n];
1649                        $jobinfo                = $dg->getJob( $jobid );
1650
1651                        $xoffset                = 5;
1652                        imageString( $this->image, $this->font, $xoffset, $this->y_offset, strval( $jobid ), $colorblack );
1653
1654                        foreach( $this->headerstrlen as $headername => $headerlen ) {
1655
1656                                if( $headername == 'nodes' ) {
1657                                        $attrval        = strval( count( $jobinfo[nodes] ) );
1658                                } else if( $headername == 'cpus' ) {
1659
1660                                        if( !isset( $jobinfo[ppn] ) )
1661                                                $jobinfo[ppn] = 1;
1662
1663                                        $attrval        = strval( count( $jobinfo[nodes] ) * intval( $jobinfo[ppn] ) );
1664
1665                                } else if( $headername == 'runningtime' ) {
1666                                        $attrval        = makeTime( intval( $jobinfo[reported] ) - intval( $jobinfo[start_timestamp] ) );
1667                                } else {
1668                                        $attrval        = strval( $jobinfo[$headername] );
1669                                }
1670
1671                                imageString( $this->image, $this->font, $xoffset, $this->y_offset, $attrval, $colorblack );
1672               
1673                                $xoffset        = $xoffset + ($this->fontwidth * ( $headerlen + 1 ) );
1674
1675                        }
1676                       
1677                        $this->newLineOffset();
1678                }
1679        }
1680
1681        function drawHeader() {
1682
1683                $dg                     = &$this->data_gather;
1684
1685                for( $n = 0; $n < count( $this->njobs ); $n++ ) {
1686
1687                        $jobid                  = $this->njobs[$n];
1688                        $jobinfo                = $dg->getJob( $jobid );
1689
1690                        if( !isset( $this->headerstrlen[id] ) )
1691                                $this->headerstrlen[id] = strlen( strval( $jobid ) );
1692                        else
1693                                if( strlen( strval( $jobid ) ) > $this->headerstrlen[id] )
1694                                        $this->headerstrlen[id] = strlen( strval( $jobid ) );
1695
1696                        if( !isset( $this->headerstrlen[owner] ) )
1697                                $this->headerstrlen[owner]      = strlen( strval( $jobinfo[owner] ) );
1698                        else
1699                                if( strlen( strval( $jobinfo[owner] ) ) > $this->headerstrlen[owner] )
1700                                        $this->headerstrlen[owner]      = strlen( strval( $jobinfo[owner] ) );
1701
1702                        if( !isset( $this->headerstrlen[queue] ) )
1703                                $this->headerstrlen[queue]      = strlen( strval( $jobinfo[queue] ) );
1704                        else
1705                                if( strlen( strval( $jobinfo[queue] ) ) > $this->headerstrlen[queue] )
1706                                        $this->headerstrlen[queue]      = strlen( strval( $jobinfo[queue] ) );
1707
1708                        if( !isset( $jobinfo[ppn] ) )
1709                                $jobinfo[ppn] = 1;
1710
1711                        $cpus                   = count( $jobinfo[nodes] ) * intval( $jobinfo[ppn] );
1712
1713                        if( !isset( $this->headerstrlen[cpus] ) )
1714                                $this->headerstrlen[cpus]       = strlen( strval( $cpus ) );
1715                        else
1716                                if( strlen( strval( $cpus ) ) > $this->headerstrlen[cpus] )
1717                                        $this->headerstrlen[cpus]       = strlen( strval( $cpus ) );
1718
1719                        $nodes                  = count( $jobinfo[nodes] );
1720
1721                        if( !isset( $this->headerstrlen[nodes] ) )
1722                                $this->headerstrlen[nodes]      = strlen( strval( $nodes ) );
1723                        else
1724                                if( strlen( strval( $nodes) ) > $this->headerstrlen[nodes] )
1725                                        $this->headerstrlen[nodes]      = strlen( strval( $nodes ) );
1726
1727                        $runningtime            = makeTime( intval( $jobinfo[reported] ) - intval( $jobinfo[start_timestamp] ) );
1728
1729                        if( !isset( $this->headerstrlen[runningtime] ) )
1730                                $this->headerstrlen[runningtime]        = strlen( strval( $runningtime) );
1731                        else
1732                                if( strlen( strval( $runningtime) ) > $this->headerstrlen[runningtime] )
1733                                        $this->headerstrlen[runningtime]        = strlen( strval( $runningtime) );
1734
1735                        if( !isset( $this->headerstrlen[name] ) )
1736                                $this->headerstrlen[name]       = strlen( strval( $jobinfo[name] ) );
1737                        else
1738                                if( strlen( strval( $jobinfo[name] ) ) > $this->headerstrlen[name] )
1739                                        $this->headerstrlen[name]       = strlen( strval( $jobinfo[name] ) );
1740
1741                }
1742
1743                $xoffset        = 5;
1744
[350]1745                foreach( $this->headerstrlen as $headername => $headerlen ) {
[303]1746
1747                        $colorgreen     = imageColorAllocate( $this->image, 0, 200, 0 );
1748
1749                        if( $headerlen < strlen( $headername ) )
[350]1750                                $this->headerstrlen[$headername]        = strlen( $headername );
[303]1751
[426]1752                        imageString( $this->image, $this->font, $xoffset, $this->y_offset, ucfirst( $headername ), $colorgreen );
[303]1753
[426]1754                        $xoffset        = $xoffset + ($this->fontwidth * ( $this->headerstrlen[$headername] + 1 ) );
1755
[303]1756                }
1757                $this->newLineOffset();
1758        }
1759
1760        function newLineOffset() {
1761
1762                $this->y_offset         = $this->y_offset + $this->fontheight + $this->fontspaceing;
1763        }
1764
1765        function draw() {
1766
1767                $xlen           = 450;
1768                $ylen           = ( count( $this->njobs ) * ( $this->fontheight + $this->fontspaceing ) ) + (3 * $this->fontheight);
1769
1770                $this->image    = imageCreateTrueColor( $xlen, $ylen );
1771                $colorwhite     = imageColorAllocate( $this->image, 255, 255, 255 );
1772                imageFill( $this->image, 0, 0, $colorwhite );                         
1773
1774                $colorblue      = imageColorAllocate( $this->image, 0, 0, 255 );
1775
1776                imageString( $this->image, $this->font, 1, $this->y_offset, "Monarch Joblist - host: ".$this->host, $colorblue );
1777                $this->newLineOffset();
1778
1779                $this->drawHeader();
1780                $this->drawJobs();
1781
1782                header( 'Content-type: image/png' );
1783                imagePNG( $this->image );
1784                imageDestroy( $this->image );
1785        }
1786}
1787
[331]1788function imageStringDown( &$image, $font, $x, $y, &$s, &$col )
1789{
1790        $fw     = imagefontwidth( $font);
1791        $fh     = imagefontheight( $font);
1792       
1793        $fontspacing = 0;
1794
1795        $fx     = $x;
1796        $fy     = $y;
1797
1798        for( $n=0; $n<strlen( $s ); $n++ )
1799        {
1800                $myc    = $s{$n};
1801
1802                imagestring( $image, $font, $fx, $fy, $myc, $col );
1803
1804                $fy     += ($fontspacing + $fh );
1805        }
1806}
1807
[329]1808function array_rem( $val, &$arr )
1809{
1810        // Delete val from arr
1811        //
1812        $i      = array_search( $val, $arr );
1813
1814        if( $i == false ) return false;
1815
1816        $arr    = array_merge( array_slice( $arr, 0, $i ), array_slice( $arr, $i+1, count( $arr ) ) );
1817
1818        return true;
1819}
1820
1821function cmp( $a, $b ) 
1822{
1823        global $SORT_ORDER;
1824        global $skan_str;
1825        global $x_first, $y_first;
[339]1826        global $x_present, $y_present;
[329]1827
[339]1828        //printf("ppoep = %s\n", $skan_str);
[329]1829        $a_node         = $a;
1830        $b_node         = $b;
1831        $a              = $a_node->getHostname();
1832        $b              = $b_node->getHostname();
1833
1834        if( $a == $b ) return 0;
1835
[339]1836        $a_x            = 0;
1837        $b_x            = 0;
1838        $a_y            = 0;
1839        $b_y            = 0;
1840
1841        if( $x_present && $y_present )
[329]1842        {
[339]1843                if( $x_first )
1844                {
1845                        $n = sscanf( $a, $skan_str, $a_x, $a_y );
1846                        $n = sscanf( $b, $skan_str, $b_x, $b_y );
1847                }
1848                else if( $y_first )
1849                {
1850                        $n = sscanf( $a, $skan_str, $a_y, $a_x );
1851                        $n = sscanf( $b, $skan_str, $b_y, $b_x );
1852                }
1853        } 
1854        else if( $x_present && !$y_present )
1855        {
1856                $n = sscanf( $a, $skan_str, $a_x );
1857                $n = sscanf( $b, $skan_str, $b_x );
[329]1858        }
[339]1859        else if( $y_present && !$x_present )
[329]1860        {
[339]1861                $n = sscanf( $a, $skan_str, $a_y );
1862                $n = sscanf( $b, $skan_str, $b_y );
[329]1863        }
1864
1865        if ( $SORT_ORDER=="desc" )
1866        {
1867
[339]1868                if( $x_present && $y_present )
[329]1869                {
[339]1870                        // 1  = a < b
1871                        // -1 = a > b
1872                        //
1873                        if ($a_x == $b_x)
[329]1874                        {
[339]1875                                if ($a_y < $b_y)
1876                                {
1877                                        return 1;
1878                                }
1879                                else if ($a_y > $b_y)
1880                                {
1881                                        return -1;
1882                                }
1883                        }
1884                        else if ($a_x < $b_x)
1885                        {
[329]1886                                return 1;
1887                        }
[339]1888                        else if ($a_x > $b_x)
[329]1889                        {
1890                                return -1;
1891                        }
[339]1892                } 
1893                else if( $x_present && !$y_present )
[329]1894                {
[339]1895                        if ($a_x < $b_x)
1896                        {
1897                                return 1;
1898                        }
1899                        else if ($a_x > $b_x)
1900                        {
1901                                return -1;
1902                        }
[329]1903                }
[339]1904                else if( $y_present && !$x_present )
[329]1905                {
[339]1906                        if ($a_y < $b_y)
1907                        {
1908                                return 1;
1909                        }
1910                        else if ($a_y > $b_y)
1911                        {
1912                                return -1;
1913                        }
[329]1914                }
1915        }
1916        else if ( $SORT_ORDER == "asc" )
1917        {
1918
[339]1919                if( $x_present && $y_present )
[329]1920                {
[339]1921                        // 1  = a > b
1922                        // -1 = a < b
1923                        //
1924                        if ($a_x == $b_x)
[329]1925                        {
[339]1926                                if ($a_y > $b_y)
1927                                {
1928                                        return 1;
1929                                }
1930                                else if ($a_y < $b_y)
1931                                {
1932                                        return -1;
1933                                }
1934                        }
1935                        else if ($a_x > $b_x)
1936                        {
[329]1937                                return 1;
1938                        }
[339]1939                        else if ($a_x < $b_x)
[329]1940                        {
1941                                return -1;
1942                        }
1943                }
[339]1944                else if( $x_present && !$y_present )
[329]1945                {
[339]1946                        if ($a_x > $b_x)
1947                        {
1948                                return 1;
1949                        }
1950                        else if ($a_x < $b_x)
1951                        {
1952                                return -1;
1953                        }
[329]1954                }
[339]1955                else if( $y_present && !$x_present )
[329]1956                {
[339]1957                        if ($a_y > $b_y)
1958                        {
1959                                return 1;
1960                        }
1961                        else if ($a_y < $b_y)
1962                        {
1963                                return -1;
1964                        }
[329]1965                }
1966        }
1967}
[303]1968function makeTime( $time ) {
1969
1970        $days = intval( $time / 86400 );
1971        $time = ($days>0) ? $time % ($days * 86400) : $time;
1972
1973        //printf( "time = %s, days = %s\n", $time, $days );
1974
1975        $date_str = '';
1976        $day_str = '';
1977
1978        if( $days > 0 ) {
1979                if( $days > 1 )
1980                        $day_str .= $days . ' days';
1981                else
1982                        $day_str .= $days . ' day';
1983        }
1984
1985        $hours = intval( $time / 3600 );
1986        $time = $hours ? $time % ($hours * 3600) : $time;
1987
1988        //printf( "time = %s, days = %s, hours = %s\n", $time, $days, $hours );
1989        if( $hours > 0 ) {
1990                $date_str .= $hours . ':';
1991                $date_unit = 'hours'; 
1992        }
1993
1994        $minutes = intval( $time / 60 );
1995        $seconds = $minutes ? $time % ($minutes * 60) : $time;
1996
1997        if( $minutes > 0 ) {
1998
1999                if( $minutes >= 10 )
2000                        $date_str .= $minutes . ':';
2001                else
2002                        $date_str .= '0' . $minutes . ':';
2003
2004                $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
2005        } else {
2006                if($hours > 0 ) {
2007                        $date_str .= '00:';
2008                        $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
2009                }
2010        }
2011
2012
2013        $date_unit = (!isset($date_unit)) ? 'seconds' : $date_unit;
2014
2015        if( $seconds > 0 ) {
2016
2017                if( $seconds >= 10 )
2018                        $date_str .= $seconds . ' ' . $date_unit;
2019                else
2020                        $date_str .= '0' . $seconds . ' ' . $date_unit;
2021
2022        } else if ( $hours > 0 or $minutes > 0 )
2023
2024                $date_str .= '00 ' . $date_unit;
2025
2026        if( $days > 0) {
2027
2028                if( $hours > 0 or $minutes > 0 or $seconds > 0 )
2029                        $date_str = $day_str . ' - ' . $date_str;
2030                else
2031                        $date_str = $day_str;
2032        }
2033
2034        return $date_str;
2035}
[102]2036?>
Note: See TracBrowser for help on using the repository browser.