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
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 454 2008-02-05 15:04:07Z 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                //printf(" cluster set to %s \n", $this->clustername );
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                        $location = $attrs[LOCATION];
671                        //printf( "Found node %s\n", $hostname );
672
673                        if( !isset( $nodes[$hostname] ) )
674                                $nodes[$hostname] = new NodeImage( $this->proc_cluster, $hostname );
675
676                } else if( $name == 'METRIC' and strstr( $attrs[NAME], 'MONARCH' ) and $this->proc_cluster == $this->clustername ) {
677
678                        if( strstr( $attrs[NAME], 'MONARCH-HEARTBEAT' ) ) {
679
680                                $this->heartbeat['time'] = $attrs[VAL];
681                                //printf( "heartbeat %s\n", $heartbeat['time'] );
682
683                        } else if( strstr( $attrs[NAME], 'MONARCH-JOB' ) ) {
684
685                                sscanf( $attrs[NAME], 'MONARCH-JOB-%d-%d', $jobid, $monincr );
686
687                                //printf( "jobid %s\n", $jobid );
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                                        //printf( "\t%s\t= %s\n", $toganame, $togavalue );
701
702                                        if( $toganame == 'nodes' ) {
703
704                                                if( $jobs[$jobid][status] == 'R' ) {
705                                               
706                                                        if( !isset( $jobs[$jobid][$toganame] ) )
707                                                                $jobs[$jobid][$toganame] = array();
708
709                                                        $mynodes = explode( ';', $togavalue );
710
711                                                        //print_r($mynodes);
712
713                                                        foreach( $mynodes as $node ) {
714
715                                                                if( !in_array( $node, $jobs[$jobid][$toganame] ) ) {
716                                                                        $jobs[$jobid][$toganame][] = $node;
717                                                                }
718                                                        }
719
720                                                } else if( $jobs[$jobid][status] == 'Q' ) {
721
722                                                        $jobs[$jobid][$toganame] = $togavalue;
723                                                }
724                                               
725                                        } else {
726
727                                                $jobs[$jobid][$toganame] = $togavalue;
728                                        }
729                                }
730
731                                if( isset( $jobs[$jobid][domain] ) and isset( $jobs[$jobid][nodes] ) ) {
732                       
733                                        $nr_nodes = count( $jobs[$jobid][nodes] );
734               
735                                        if( $jobs[$jobid][status] == 'R' ) {
736
737                                                foreach( $jobs[$jobid][nodes] as $node ) {
738
739                                                        $domain = $jobs[$jobid][domain];
740                                                        $domain_len = 0 - strlen( $domain );
741
742                                                        if( substr( $node, $domain_len ) != $domain ) {
743                                                                $host = $node. '.'.$domain;
744                                                        } else {
745                                                                $host = $node;
746                                                        }
747
748                                                        //$host = $node.'.'.$jobs[$jobid][domain];
749                               
750                                                        if( !isset( $nodes[$host] ) )
751                                                                $my_node = new NodeImage( $this->proc_cluster, $host );
752                                                        else
753                                                                $my_node = $nodes[$host];
754
755                                                        if( !$my_node->hasJob( $jobid ) )
756
757                                                                if( isset( $jobs[$jobid][ppn] ) )
758                                                                        $my_node->addJob( $jobid, ((int) $jobs[$jobid][ppn]) );
759                                                                else
760                                                                        $my_node->addJob( $jobid, 1 );
761
762                                                        $nodes[$host] = $my_node;
763                                                }
764                                        }
765                                }
766                        }
767                }
768                $this->jobs = $jobs;
769                //print_r( $nodes );
770                $this->nodes = $nodes;
771                //print_r( $this->nodes );
772        }
773
774        function stopElement( $parser, $name ) {
775        }
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        }
813
814        function getNodes() {
815                //print_r( $this->nodes );
816                return $this->nodes;
817        }
818
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
828        function getJobs() {
829                return $this->jobs;
830        }
831
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
841        function getHeartbeat() {
842                return $this->heartbeat['time'];
843        }
844}
845
846class NodeImage {
847
848        var $image, $x, $y, $hostname, $jobs, $tasks, $showinfo;
849
850        function NodeImage( $cluster, $hostname ) {
851
852                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
853
854                $this->jobs = array();
855                //$this->image = $image;
856                //$this->x = $x;
857                //$this->y = $y;
858                $this->tasks = 0;
859                $this->hostname = $hostname;
860                $this->cpus = $this->determineCpus();
861                $this->clustername = $cluster;
862                $this->showinfo = 1;
863                $this->size = $SMALL_CLUSTERIMAGE_NODEWIDTH;
864        }
865
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
898        function setCoords( $x, $y ) {
899
900                $this->x = $x;
901                $this->y = $y;
902        }
903
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
921        function colorHex( $color ) {
922       
923                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) );
924
925                return $my_color;
926        }
927
928        function setLoad( $load ) {
929                $this->load = $load;
930        }
931
932        function setHostname( $hostname ) {
933                $this->hostname = $hostname;
934        }
935
936        function getHostname() {
937                return $this->hostname;
938        }
939
940        function getJobs() {
941                return $this->jobs;
942        }
943
944        function setShowinfo( $showinfo ) {
945                $this->showinfo = $showinfo;
946        }
947
948        function drawSmall() {
949
950                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
951
952                $this->size     = $SMALL_CLUSTERIMAGE_NODEWIDTH;
953
954                $this->draw();
955        }
956
957        function drawBig() {
958
959                global $BIG_CLUSTERIMAGE_NODEWIDTH;
960
961                $this->size     = $BIG_CLUSTERIMAGE_NODEWIDTH;
962
963                $this->draw();
964        }
965
966        function draw() {
967
968                global $JOB_NODE_MARKING;
969
970                $black_color = imageColorAllocate( $this->image, 0, 0, 0 );
971                $size = $this->size;
972
973                imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+($size), $this->y+($size), $black_color );
974
975                if( $this->showinfo) {
976               
977                        $this->load = $this->determineLoad();
978
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                        }
984
985
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 )
992                                imageString( $this->image, 1, $this->x+(($size/2)-1), $this->y+(($size/2)-4), $JOB_NODE_MARKING, $black_color );
993
994                } else {
995
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
1002        }
1003
1004        function determineCpus() {
1005
1006                global $metrics;
1007
1008                $cpus = $metrics[$this->hostname][cpu_num][VAL];
1009                if (!$cpus) $cpus=1;
1010
1011                return $cpus;
1012        }
1013
1014        function determineLoad() {
1015
1016                global $metrics;
1017
1018                $load_one = $metrics[$this->hostname][load_one][VAL];
1019                $load = ((float) $load_one)/$this->cpus;
1020
1021                return $load;
1022        }
1023}
1024
1025class ClusterImage {
1026
1027        var $dataget, $image, $clustername;
1028        var $filtername, $filters;
1029
1030        //function ClusterImage( $clustername ) {
1031        function ClusterImage( $data, $clustername ) {
1032
1033                //$this->dataget                = $dataget;
1034                $this->dataget          = new DataGatherer( $clustername );
1035                $this->data             = $data;
1036                $this->clustername      = $clustername;
1037                $this->filters          = array();
1038                $this->size             = 's';
1039                $this->width            = 0;
1040                $this->height           = 0;
1041                $this->output           = 1;
1042        }
1043
1044        function getWidth() {
1045                return $this->width;
1046        }
1047        function getHeight() {
1048                return $this->height;
1049        }
1050
1051        function setSmall() {
1052                $this->size     = 's';
1053        }
1054
1055        function setBig() {
1056                $this->size     = 'b';
1057        }
1058
1059        function setNoimage() {
1060                $this->output   = 0;
1061        }
1062
1063        function isSmall() {
1064                return ($this->size == 's');
1065        }
1066
1067        function isBig() {
1068                return ($this->size == 'b');
1069        }
1070
1071        function setFilter( $filtername, $filtervalue ) {
1072
1073                $this->filters[$filtername] = $filtervalue;
1074        }
1075
1076        function filterNodes( $jobs, $nodes ) {
1077
1078                $filtered_nodes = array();
1079
1080                foreach( $nodes as $node ) {
1081
1082                        $hostname = $node->getHostname();
1083
1084                        $addhost = 1;
1085
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 )
1113                                $filtered_nodes[] = $hostname;
1114                }
1115
1116                return $filtered_nodes;
1117        }
1118
1119        function draw() {
1120
1121                global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH;
1122                global $BIG_CLUSTERIMAGE_MAXWIDTH, $BIG_CLUSTERIMAGE_NODEWIDTH;
1123                global $CLUSTER_CONFS, $confcluster;
1124
1125                global $SORTBY_HOSTNAME, $SORT_ORDER, $skan_str;
1126                global $x_first, $y_first;
1127
1128                foreach( $CLUSTER_CONFS as $confcluster => $conffile )
1129                {
1130                        if( strtolower( trim($this->clustername) ) == strtolower(trim($confcluster)) )
1131                        {
1132                                include_once $conffile;
1133                        }
1134                }
1135
1136                //global $SORTBY_HOSTNAME, $SORT_ORDER;
1137                //global $SORT_XLABEL, $SORT_YLABEL;
1138       
1139                //printf( "SORTBY_HOSTNAME %s SORT_YLABEL %s\n", $SORTBY_HOSTNAME, $SORT_YLABEL );
1140
1141                $mydatag = $this->dataget;
1142                $mydatag->parseXML( $this->data );
1143
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                }
1151
1152                $nodes = $mydatag->getNodes();
1153                $nodes_hosts = array_keys( $nodes );
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 ) {
1161                        $nodes_per_row = ( (int) ($max_width/$node_width) );
1162                } else {
1163                        $nodes_per_row = $nodes_size;
1164                        $node_rows = 1;
1165                }
1166
1167                if( $nodes_per_row < $nodes_nr ) {
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 ) {
1172                                $node_rows++;
1173                                //printf( "noderows %d\n", $node_rows );
1174                        }
1175                }
1176
1177                $y_offset       = 0;
1178                $font           = 2;
1179                $fontwidth      = ImageFontWidth( $font );
1180                $fontheight     = ImageFontHeight( $font );
1181                $fontspaceing   = 2;
1182                $y_offset       = $fontheight + (2 * $fontspaceing);
1183
1184                $this->width    = $max_width;
1185                $this->height   = ($y_offset + (($node_rows*$node_width)+1) );
1186
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 );
1190
1191                //if( $this->isSmall() ) {
1192
1193                //      $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1194
1195                //      imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1196                //}
1197
1198                $jobs = $mydatag->getJobs();
1199                //printf("filtername = %s\n", $filtername );
1200                $filtered_nodes = $this->filterNodes( $jobs, $nodes );
1201
1202                //print_r($filtered_nodes);
1203
1204                if( $SORTBY_HOSTNAME != "" )
1205                {
1206
1207                        $sorted         = array();
1208
1209                        $x_first        = 0;
1210                        $y_first        = 0;
1211
1212                        $skan_str       = $SORTBY_HOSTNAME;
1213
1214                        global $x_present, $y_present;
1215                        $x_present      = false;
1216                        $y_present      = false;
1217
1218                        if(stripos( $SORTBY_HOSTNAME, "{x}" ) != false )
1219                        {
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                        {
1229                       
1230                                $x_first        = 1;
1231                        }
1232                        else if(( strpos( $SORTBY_HOSTNAME, "{x}" ) > strpos( $SORTBY_HOSTNAME, "{y}" ) ) && ( $x_present && $y_present ))
1233                        {
1234                                $y_first        = 1;
1235               
1236                        }
1237                        else if( $x_present )
1238                        {
1239                                $x_first        = 1;
1240                        }
1241                        else if( $y_present )
1242                        {
1243                                $y_first        = 1;
1244                        }
1245
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                        }
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                        {
1268                                $x      = null;
1269                                $y      = null;
1270
1271                                if( $x_present && $y_present )
1272                                {
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                                        {
1286                                                //printf( "removing node %s - x present & y present + <2 x,y matches\n", $hostname );
1287                                                unset( $nodes[$hostname] );
1288                                        }
1289                                }
1290                                else if( $x_present && !$y_present )
1291                                {
1292                                        $n = sscanf( $hostname, $skan_str, $x );
1293                                        // Remove nodes that don't match
1294                                        //
1295                                        if( $n < 1 )
1296                                        {
1297                                                //printf( "removing node %s - x present & !y present + <1 x match\n", $hostname );
1298                                                unset( $nodes[$hostname] );
1299                                        }
1300                                        $y      = 1;
1301                                }
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                                        {
1309                                                //printf( "removing node %s - y present & !x present + <1 y match\n", $hostname );
1310                                                unset( $nodes[$hostname] );
1311                                        }
1312                                        $x      = 1;
1313                                }
1314                                //printf( "xfirst %s yfirst %s\n", $x_first, $y_first );
1315
1316                                //printf( "n %s\n", $n );
1317
1318
1319                                //printf( "node %s x_min %s x %s\n", $hostname, $x_min, $x );
1320
1321                                if( !$x_min && $x != null )
1322                                {
1323                                        $x_min  = $x;
1324                                }
1325                                else if( $x < $x_min && $x != null )
1326                                {
1327                                        $x_min  = $x;
1328                                }
1329                                if( !$x_max && $x != null )
1330                                {
1331                                        $x_max  = $x;
1332                                }
1333                                else if( $x > $x_max && $x != null )
1334                                {
1335                                        $x_max  = $x;
1336                                }
1337                                if( !$y_min && $y != null )
1338                                {
1339                                        $y_min  = $y;
1340                                }
1341                                else if( $y < $y_min && $y != null )
1342                                {
1343                                        $y_min  = $y;
1344                                }
1345                                if( !$y_max && $y != null )
1346                                {
1347                                        $y_max  = $y;
1348                                }
1349                                else if( $y > $y_max && $y != null )
1350                                {
1351                                        $y_max  = $y;
1352                                }
1353                        }
1354
1355                        //printf( "ss %s\n", $skan_str);
1356                        $sorted_nodes   = usort( $nodes, "cmp" );
1357
1358                        //print_r( $nodes );
1359
1360                        $cur_node       = 0;
1361
1362                        $x_offset       = 0;
1363                        $y_offset       = 0;
1364                        $font           = 2;
1365                        $fontwidth      = ImageFontWidth( $font );
1366                        $fontheight     = ImageFontHeight( $font );
1367                        $fontspaceing   = 2;
1368
1369                        if( $this->isSmall() ) 
1370                        {
1371                                $y_offset       = $y_offset + (2 * $fontspaceing) + $fontheight;
1372                        }
1373
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
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
1389                        $image_width    = $x_offset + ($node_width * ($x_max-$x_min+2));
1390
1391                        if( $this->isSmall() ) 
1392                        {
1393                                $image_width    = $max_width;
1394                        } else if( $this->isBig() ) 
1395                        {
1396                                $image_width    = ($image_width < $max_width) ? $image_width : $max_width;
1397                        }
1398                        //else if( $this->isSmall() )
1399                        //{
1400                        //      $image_width    = $this->width;
1401                        //}
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 );
1408                        $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1409
1410                        imageFill( $image, 0, 0, $colorwhite );
1411
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
1419                        if( $this->isBig() && ( isset( $SORT_XLABEL ) || isset( $SORT_YLABEL ) ) )
1420                        {
1421                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1422
1423                                if( isset( $SORT_XLABEL ) )
1424                                {
1425                                        imageString( $image, $font, $x_offset, $fontspaceing, $SORT_XLABEL, $colorblue );
1426                                }
1427
1428                                if( isset( $SORT_YLABEL ) )
1429                                {
1430                                        // Stupid php without imageStringDown function
1431                                        //
1432                                        imageStringDown( $image, $font, $fontspaceing, $y_offset, $SORT_YLABEL, $colorblue );
1433                                }
1434                        }
1435                        //print_r( $nodes );
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
1450                                        if( isset( $nodes[$cur_node] ) ) 
1451                                        {
1452                                                $host   = $nodes[$cur_node]->getHostname();
1453
1454                                                if( $x_present && $y_present )
1455                                                {
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                                                        {
1466                                                                //printf( "skipping node %s - y present & x present + <2 x,y matchs\n", $host);
1467                                                                continue;
1468                                                        }
1469                                                        if( intval( $rx ) > $n )
1470                                                        {
1471                                                                $m      = $y_max + 1;
1472                                                                //printf( "skipping node %s - y present & x present + rx %s > n %s\n", $rx, $n);
1473                                                                continue;
1474                                                        }
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                                                        }
1480                                                }
1481                                                else if( $x_present )
1482                                                {
1483                                                        $nn = sscanf( $host, $skan_str, $rx );
1484                                                }
1485                                                else if( $y_present )
1486                                                {
1487                                                        $nn = sscanf( $host, $skan_str, $ry );
1488                                                }
1489
1490                                                if( !in_array( $host, $filtered_nodes ) )
1491                                                {
1492                                                        //printf( "setting node %s showinfo to 0 - not found in filtered_nodes", $host);
1493                                                        $nodes[$cur_node]->setShowinfo( 0 );
1494                                                }
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();
1505
1506                                                $cur_node++;
1507                                        }
1508                                        if( $this->isBig() ) 
1509                                        {
1510                                                if(( $n == $x_min ) && ( isset($SORT_YLABEL) ) )
1511                                                {
1512                                                        $mfontspacing   = 1;
1513                                                        $ylabel_x       = $x - ( $fontwidth * strlen( $y_max ) ) - $mfontspacing;
1514                                                        $ylabel_y       = $y;
1515
1516                                                        imageString( $image, $font, $ylabel_x, $ylabel_y, strval( $m ), $colorblue );
1517                                                }
1518                                                if(( $m == $y_min ) && ( isset($SORT_XLABEL) ) )
1519                                                {
1520                                                        $mfontspacing   = 2;
1521                                                        $xlabel_y       = $y - ( $fontheight * strlen( $x_max ) );
1522                                                        $xlabel_x       = $x + $mfontspacing; 
1523
1524                                                        imageStringDown( $image, $font, $xlabel_x, $xlabel_y, strval( $n ), $colorblue );
1525                                                }
1526                                        }
1527
1528                                }
1529                        }
1530
1531                }
1532                else
1533                {
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                        }
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++ ) {
1553                       
1554                                for( $m = 0; $m < $nodes_per_row; $m++ ) {
1555                       
1556                                        $x = ($m * $node_width);
1557                                        $y = $y_offset + ($n * $node_width);
1558
1559                                        $cur_node = ($n * $nodes_per_row) + ($m);
1560                                        $host = $nodes_hosts[$cur_node];
1561
1562                                        if( isset( $nodes[$host] ) ) {
1563
1564                                                $nodes[$host]->setCoords( $x, $y );
1565                                                $nodes[$host]->setImage( $image );
1566
1567                                                if( !in_array( $host, $filtered_nodes ) )
1568                                                {
1569                                                        $nodes[$host]->setShowinfo( 0 );
1570                                                }
1571
1572                                                if( $this->isSmall() )
1573                                                        $nodes[$host]->drawSmall();
1574                                                else if( $this->isBig() )
1575                                                        $nodes[$host]->drawBig();
1576                                        }
1577                                }
1578                        }
1579                }
1580       
1581                $this->nodes    = &$nodes;
1582
1583                if ($this->output) {
1584                        header( 'Content-type: image/png' );
1585                        imagePNG( $image );
1586                        imageDestroy( $image );
1587                }
1588        }
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        }
1602}
1603
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
1617class HostImage {
1618
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
1745                foreach( $this->headerstrlen as $headername => $headerlen ) {
1746
1747                        $colorgreen     = imageColorAllocate( $this->image, 0, 200, 0 );
1748
1749                        if( $headerlen < strlen( $headername ) )
1750                                $this->headerstrlen[$headername]        = strlen( $headername );
1751
1752                        imageString( $this->image, $this->font, $xoffset, $this->y_offset, ucfirst( $headername ), $colorgreen );
1753
1754                        $xoffset        = $xoffset + ($this->fontwidth * ( $this->headerstrlen[$headername] + 1 ) );
1755
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
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
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;
1826        global $x_present, $y_present;
1827
1828        //printf("ppoep = %s\n", $skan_str);
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
1836        $a_x            = 0;
1837        $b_x            = 0;
1838        $a_y            = 0;
1839        $b_y            = 0;
1840
1841        if( $x_present && $y_present )
1842        {
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 );
1858        }
1859        else if( $y_present && !$x_present )
1860        {
1861                $n = sscanf( $a, $skan_str, $a_y );
1862                $n = sscanf( $b, $skan_str, $b_y );
1863        }
1864
1865        if ( $SORT_ORDER=="desc" )
1866        {
1867
1868                if( $x_present && $y_present )
1869                {
1870                        // 1  = a < b
1871                        // -1 = a > b
1872                        //
1873                        if ($a_x == $b_x)
1874                        {
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                        {
1886                                return 1;
1887                        }
1888                        else if ($a_x > $b_x)
1889                        {
1890                                return -1;
1891                        }
1892                } 
1893                else if( $x_present && !$y_present )
1894                {
1895                        if ($a_x < $b_x)
1896                        {
1897                                return 1;
1898                        }
1899                        else if ($a_x > $b_x)
1900                        {
1901                                return -1;
1902                        }
1903                }
1904                else if( $y_present && !$x_present )
1905                {
1906                        if ($a_y < $b_y)
1907                        {
1908                                return 1;
1909                        }
1910                        else if ($a_y > $b_y)
1911                        {
1912                                return -1;
1913                        }
1914                }
1915        }
1916        else if ( $SORT_ORDER == "asc" )
1917        {
1918
1919                if( $x_present && $y_present )
1920                {
1921                        // 1  = a > b
1922                        // -1 = a < b
1923                        //
1924                        if ($a_x == $b_x)
1925                        {
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                        {
1937                                return 1;
1938                        }
1939                        else if ($a_x < $b_x)
1940                        {
1941                                return -1;
1942                        }
1943                }
1944                else if( $x_present && !$y_present )
1945                {
1946                        if ($a_x > $b_x)
1947                        {
1948                                return 1;
1949                        }
1950                        else if ($a_x < $b_x)
1951                        {
1952                                return -1;
1953                        }
1954                }
1955                else if( $y_present && !$x_present )
1956                {
1957                        if ($a_y > $b_y)
1958                        {
1959                                return 1;
1960                        }
1961                        else if ($a_y < $b_y)
1962                        {
1963                                return -1;
1964                        }
1965                }
1966        }
1967}
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}
2036?>
Note: See TracBrowser for help on using the repository browser.