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

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

libtoga.php:

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