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

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

job_monarch/libtoga.php:

  • fixed small cluster image to properly align and display text

job_monarch/conf.php:

  • changed maxwidth for small cluster image

job_monarch/version.php:

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