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

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

web/addons/job_monarch/conf.php:

  • added CLUSTER_CONFS example for cluster specific config's

web/addons/job_monarch/libtoga.php:

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