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

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

web/addons/job_monarch/libtoga.php:

  • removed debug statement
  • Property svn:keywords set to Id
File size: 44.1 KB
Line 
1<?php
2/*
3 *
4 * This file is part of Jobmonarch
5 *
6 * Copyright (C) 2006  Ramon Bastiaans
7 *
8 * Jobmonarch is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * Jobmonarch is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 *
22 * SVN $Id: libtoga.php 405 2007-07-04 20:43:40Z 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, $confcluster;
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                        //printf( "cf %s cc %s\n", $this->clustername, $confcluster);
1072                        //printf( "cf %s cc %s\n", strtolower( trim($this->clustername)), trim($confcluster) );
1073                        if( strtolower( trim($this->clustername) ) == strtolower(trim($confcluster)) )
1074                        {
1075                                //printf( "cf %s cc %s\n", $conffile, $confcluster);
1076                                include_once $conffile;
1077                        }
1078                }
1079
1080                //global $SORTBY_HOSTNAME, $SORT_ORDER;
1081                //global $SORT_XLABEL, $SORT_YLABEL;
1082       
1083                //printf( "SORTBY_HOSTNAME %s SORT_YLABEL %s\n", $SORTBY_HOSTNAME, $SORT_YLABEL );
1084
1085                $mydatag = $this->dataget;
1086                $mydatag->parseXML( $this->data );
1087
1088                if( $this->isSmall() ) {
1089                        $max_width = $SMALL_CLUSTERIMAGE_MAXWIDTH;
1090                        $node_width = $SMALL_CLUSTERIMAGE_NODEWIDTH;
1091                } else if( $this->isBig() ) {
1092                        $max_width = $BIG_CLUSTERIMAGE_MAXWIDTH;
1093                        $node_width = $BIG_CLUSTERIMAGE_NODEWIDTH;
1094                }
1095
1096                $nodes = $mydatag->getNodes();
1097                $nodes_hosts = array_keys( $nodes );
1098
1099                $nodes_nr = count( $nodes );
1100
1101                $nodes_size = $nodes_nr*$node_width;
1102                $node_rows = 0;
1103
1104                if( $nodes_size > $max_width ) {
1105                        $nodes_per_row = ( (int) ($max_width/$node_width) );
1106                } else {
1107                        $nodes_per_row = $nodes_size;
1108                        $node_rows = 1;
1109                }
1110
1111                if( $nodes_per_row < $nodes_nr ) {
1112                        $node_rows = ( (int) ($nodes_nr/$nodes_per_row) );
1113                        $node_rest = fmod( $nodes_nr, $nodes_per_row );
1114                        //printf( "nodesnr %d noderest %f\n", $nodes_nr, $node_rest );
1115                        if( $node_rest > 0 ) {
1116                                $node_rows++;
1117                                //printf( "noderows %d\n", $node_rows );
1118                        }
1119                }
1120
1121                $y_offset       = 0;
1122                $font           = 2;
1123                $fontwidth      = ImageFontWidth( $font );
1124                $fontheight     = ImageFontHeight( $font );
1125                $fontspaceing   = 2;
1126                $y_offset       = $fontheight + (2 * $fontspaceing);
1127
1128                $this->width    = $max_width;
1129                $this->height   = ($y_offset + (($node_rows*$node_width)+1) );
1130
1131                //$image = imageCreateTrueColor( $max_width, ($y_offset + (($node_rows*$node_width)+1) ) );
1132                //$colorwhite = imageColorAllocate( $image, 255, 255, 255 );
1133                //imageFill( $image, 0, 0, $colorwhite );
1134
1135                //if( $this->isSmall() ) {
1136
1137                //      $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1138
1139                //      imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1140                //}
1141
1142                $jobs = $mydatag->getJobs();
1143                //printf("filtername = %s\n", $filtername );
1144                $filtered_nodes = $this->filterNodes( $jobs, $nodes );
1145
1146                //print_r($filtered_nodes);
1147
1148                if( $SORTBY_HOSTNAME != "" )
1149                {
1150
1151                        $sorted         = array();
1152
1153                        $x_first        = 0;
1154                        $y_first        = 0;
1155
1156                        $skan_str       = $SORTBY_HOSTNAME;
1157
1158                        global $x_present, $y_present;
1159                        $x_present      = false;
1160                        $y_present      = false;
1161
1162                        if(stripos( $SORTBY_HOSTNAME, "{x}" ) != false )
1163                        {
1164                                $x_present      = true;
1165                        }
1166                        if(stripos( $SORTBY_HOSTNAME, "{y}" ) != false )
1167                        {
1168                                $y_present      = true;
1169                        }
1170
1171                        if(( strpos( $SORTBY_HOSTNAME, "{x}" ) < strpos( $SORTBY_HOSTNAME, "{y}" ) ) && ( $x_present && $y_present ))
1172                        {
1173                       
1174                                $x_first        = 1;
1175                        }
1176                        else if(( strpos( $SORTBY_HOSTNAME, "{x}" ) > strpos( $SORTBY_HOSTNAME, "{y}" ) ) && ( $x_present && $y_present ))
1177                        {
1178                                $y_first        = 1;
1179               
1180                        }
1181                        else if( $x_present )
1182                        {
1183                                $x_first        = 1;
1184                        }
1185                        else if( $y_present )
1186                        {
1187                                $y_first        = 1;
1188                        }
1189
1190                        if(( $x_first ) && ( $x_present && $y_present ) )
1191                        {
1192                                $skan_str       = str_replace( "{x}", "%d", $skan_str );
1193                                $skan_str       = str_replace( "{y}", "%d", $skan_str );
1194                                //printf("ppoep = %s\n", $skan_str);
1195                        } 
1196                        else if( $x_present)
1197                        {
1198                                $skan_str       = str_replace( "{x}", "%d", $skan_str );
1199                        }
1200                        else if( $y_present)
1201                        {
1202                                $skan_str       = str_replace( "{y}", "%d", $skan_str );
1203                        }
1204
1205                        $x_min          = null;
1206                        $x_max          = null;
1207                        $y_min          = null;
1208                        $y_max          = null;
1209
1210                        foreach( $nodes as $hostname => $node )
1211                        {
1212                                $x      = null;
1213                                $y      = null;
1214
1215                                if( $x_present && $y_present )
1216                                {
1217                                        //$n    = sscanf( $hostname, $skan_str, $i, $j );
1218                                        if( $x_first )
1219                                        {
1220                                                $n = sscanf( $hostname, $skan_str, $x, $y );
1221                                        }
1222                                        else if( $y_first )
1223                                        {
1224                                                $n = sscanf( $hostname, $skan_str, $y, $x );
1225                                        }
1226                                        // Remove nodes that don't match
1227                                        //
1228                                        if( $n < 2 )
1229                                        {
1230                                                //printf( "removing node %s - x present & y present + <2 x,y matches\n", $hostname );
1231                                                unset( $nodes[$hostname] );
1232                                        }
1233                                }
1234                                else if( $x_present && !$y_present )
1235                                {
1236                                        $n = sscanf( $hostname, $skan_str, $x );
1237                                        // Remove nodes that don't match
1238                                        //
1239                                        if( $n < 1 )
1240                                        {
1241                                                //printf( "removing node %s - x present & !y present + <1 x match\n", $hostname );
1242                                                unset( $nodes[$hostname] );
1243                                        }
1244                                        $y      = 1;
1245                                }
1246                                else if( $y_present && !$x_present )
1247                                {
1248                                        $n = sscanf( $hostname, $skan_str, $y );
1249                                        // Remove nodes that don't match
1250                                        //
1251                                        if( $n < 1 )
1252                                        {
1253                                                //printf( "removing node %s - y present & !x present + <1 y match\n", $hostname );
1254                                                unset( $nodes[$hostname] );
1255                                        }
1256                                        $x      = 1;
1257                                }
1258                                //printf( "xfirst %s yfirst %s\n", $x_first, $y_first );
1259
1260                                //printf( "n %s\n", $n );
1261
1262
1263                                //printf( "node %s x_min %s x %s\n", $hostname, $x_min, $x );
1264
1265                                if( !$x_min && $x != null )
1266                                {
1267                                        $x_min  = $x;
1268                                }
1269                                else if( $x < $x_min && $x != null )
1270                                {
1271                                        $x_min  = $x;
1272                                }
1273                                if( !$x_max && $x != null )
1274                                {
1275                                        $x_max  = $x;
1276                                }
1277                                else if( $x > $x_max && $x != null )
1278                                {
1279                                        $x_max  = $x;
1280                                }
1281                                if( !$y_min && $y != null )
1282                                {
1283                                        $y_min  = $y;
1284                                }
1285                                else if( $y < $y_min && $y != null )
1286                                {
1287                                        $y_min  = $y;
1288                                }
1289                                if( !$y_max && $y != null )
1290                                {
1291                                        $y_max  = $y;
1292                                }
1293                                else if( $y > $y_max && $y != null )
1294                                {
1295                                        $y_max  = $y;
1296                                }
1297                        }
1298
1299                        //printf( "ss %s\n", $skan_str);
1300                        $sorted_nodes   = usort( $nodes, "cmp" );
1301
1302                        //print_r( $nodes );
1303
1304                        $cur_node       = 0;
1305
1306                        $x_offset       = 0;
1307                        $y_offset       = 0;
1308                        $font           = 2;
1309                        $fontwidth      = ImageFontWidth( $font );
1310                        $fontheight     = ImageFontHeight( $font );
1311                        $fontspaceing   = 2;
1312
1313                        if( $this->isSmall() ) 
1314                        {
1315                                $y_offset       = $y_offset + (2 * $fontspaceing) + $fontheight;
1316                        }
1317
1318                        if( $this->isBig() ) 
1319                        {
1320
1321                                $y_offset       = ($fontheight * (1 + strlen( $x_max) ) ) + ((2 + strlen( $x_max)) * $fontspaceing);
1322                                $x_offset       = ($fontwidth * (1 + strlen( $y_max) ) ) + ((2 + strlen( $y_max)) * $fontspaceing);
1323
1324                        }
1325                        //$x_offset     = ($fontwidth * 3) + (5 * $fontspaceing);
1326
1327                        //printf( "xmin %s xmax %s\n", $x_min, $x_max );
1328                        //printf( "ymin %s ymax %s\n", $y_min, $y_max );
1329
1330                        // werkt
1331                        //print_r( $nodes );
1332
1333                        $image_width    = $x_offset + ($node_width * ($x_max-$x_min+2));
1334                        if( $this->isBig() ) 
1335                        {
1336                                $image_width    = ($image_width < $this->width) ? $image_width : $this->width;
1337                        }
1338                        else if( $this->isSmall() )
1339                        {
1340                                $image_width    = $this->width;
1341                        }
1342                        $image_height   = $y_offset + ($node_width * ($y_max-$y_min+2));
1343
1344                        $this->width    = $image_width;
1345                        $this->heigth   = $image_heigth;
1346
1347                        $image          = imageCreateTrueColor( $image_width, $image_height );
1348                        $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1349
1350                        imageFill( $image, 0, 0, $colorwhite );
1351
1352                        if( $this->isSmall() ) {
1353
1354                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1355
1356                                imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1357                        }
1358
1359                        if( $this->isBig() && ( isset( $SORT_XLABEL ) || isset( $SORT_YLABEL ) ) )
1360                        {
1361                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1362
1363                                if( isset( $SORT_XLABEL ) )
1364                                {
1365                                        imageString( $image, $font, $x_offset, $fontspaceing, $SORT_XLABEL, $colorblue );
1366                                }
1367
1368                                if( isset( $SORT_YLABEL ) )
1369                                {
1370                                        // Stupid php without imageStringDown function
1371                                        //
1372                                        imageStringDown( $image, $font, $fontspaceing, $y_offset, $SORT_YLABEL, $colorblue );
1373                                }
1374                        }
1375                        //print_r( $nodes );
1376
1377                        for( $n = $x_min; $n <= $x_max; $n++ )
1378                        {
1379                                for( $m = $y_min; $m <= $y_max; $m++ )
1380                                {
1381                                        if( $x_min > 0 )
1382                                        {
1383                                                $x      = $x_offset + ( ($n-$x_min) * $node_width );
1384                                        }
1385                                        if( $y_min > 0 )
1386                                        {
1387                                                $y      = $y_offset + ( ($m-$y_min) * $node_width );
1388                                        }
1389
1390                                        if( isset( $nodes[$cur_node] ) ) 
1391                                        {
1392                                                $host   = $nodes[$cur_node]->getHostname();
1393
1394                                                if( $x_present && $y_present )
1395                                                {
1396                                                        if( $x_first )
1397                                                        {
1398                                                                $nn = sscanf( $host, $skan_str, $rx, $ry );
1399                                                        }
1400                                                        else if( $y_first )
1401                                                        {
1402                                                                $nn = sscanf( $host, $skan_str, $ry, $rx );
1403                                                        }
1404                                                        if ( $nn < 2 )
1405                                                        {
1406                                                                //printf( "skipping node %s - y present & x present + <2 x,y matchs\n", $host);
1407                                                                continue;
1408                                                        }
1409                                                        if( intval( $rx ) > $n )
1410                                                        {
1411                                                                $m      = $y_max + 1;
1412                                                                //printf( "skipping node %s - y present & x present + rx %s > n %s\n", $rx, $n);
1413                                                                continue;
1414                                                        }
1415                                                        if( intval( $ry ) > $m )
1416                                                        {
1417                                                                //printf( "skipping node %s - y present & x present + ry %s > m %s\n", $ry, $m);
1418                                                                continue;
1419                                                        }
1420                                                }
1421                                                else if( $x_present )
1422                                                {
1423                                                        $nn = sscanf( $host, $skan_str, $rx );
1424                                                }
1425                                                else if( $y_present )
1426                                                {
1427                                                        $nn = sscanf( $host, $skan_str, $ry );
1428                                                }
1429
1430                                                if( !in_array( $host, $filtered_nodes ) )
1431                                                {
1432                                                        //printf( "setting node %s showinfo to 0 - not found in filtered_nodes", $host);
1433                                                        $nodes[$cur_node]->setShowinfo( 0 );
1434                                                }
1435
1436                                                $nodes[$cur_node]->setCoords( $x, $y );
1437                                                $nodes[$cur_node]->setImage( $image );
1438
1439                                                //print_r( $nodes[$cur_node] );
1440
1441                                                if( $this->isSmall() )
1442                                                        $nodes[$cur_node]->drawSmall();
1443                                                else if( $this->isBig() )
1444                                                        $nodes[$cur_node]->drawBig();
1445
1446                                                $cur_node++;
1447                                        }
1448                                        if( $this->isBig() ) 
1449                                        {
1450                                                if(( $n == $x_min ) && ( isset($SORT_YLABEL) ) )
1451                                                {
1452                                                        $mfontspacing   = 1;
1453                                                        $ylabel_x       = $x - ( $fontwidth * strlen( $y_max ) ) - $mfontspacing;
1454                                                        $ylabel_y       = $y;
1455
1456                                                        imageString( $image, $font, $ylabel_x, $ylabel_y, strval( $m ), $colorblue );
1457                                                }
1458                                                if(( $m == $y_min ) && ( isset($SORT_XLABEL) ) )
1459                                                {
1460                                                        $mfontspacing   = 2;
1461                                                        $xlabel_y       = $y - ( $fontheight * strlen( $x_max ) );
1462                                                        $xlabel_x       = $x + $mfontspacing; 
1463
1464                                                        imageStringDown( $image, $font, $xlabel_x, $xlabel_y, strval( $n ), $colorblue );
1465                                                }
1466                                        }
1467
1468                                }
1469                        }
1470
1471                }
1472                else
1473                {
1474                        $image          = imageCreateTrueColor( $max_width, ($y_offset + (($node_rows*$node_width)+1) ) );
1475                        $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1476
1477                        imageFill( $image, 0, 0, $colorwhite );
1478
1479                        if( $this->isSmall() ) {
1480
1481                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1482
1483                                imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1484                        }
1485
1486                        for( $n = 0; $n < $node_rows; $n++ ) {
1487                       
1488                                for( $m = 0; $m < $nodes_per_row; $m++ ) {
1489                       
1490                                        $x = ($m * $node_width);
1491                                        $y = $y_offset + ($n * $node_width);
1492
1493                                        $cur_node = ($n * $nodes_per_row) + ($m);
1494                                        $host = $nodes_hosts[$cur_node];
1495
1496                                        if( isset( $nodes[$host] ) ) {
1497
1498                                                $nodes[$host]->setCoords( $x, $y );
1499                                                $nodes[$host]->setImage( $image );
1500
1501                                                if( !in_array( $host, $filtered_nodes ) )
1502                                                {
1503                                                        $nodes[$host]->setShowinfo( 0 );
1504                                                }
1505
1506                                                if( $this->isSmall() )
1507                                                        $nodes[$host]->drawSmall();
1508                                                else if( $this->isBig() )
1509                                                        $nodes[$host]->drawBig();
1510                                        }
1511                                }
1512                        }
1513                }
1514       
1515                $this->nodes    = &$nodes;
1516
1517                if ($this->output) {
1518                        header( 'Content-type: image/png' );
1519                        imagePNG( $image );
1520                        imageDestroy( $image );
1521                }
1522        }
1523
1524        function getImagemapArea() {
1525
1526                $clusterimage_map       = "";
1527
1528                foreach( $this->nodes as $hostname => $node ) {
1529
1530                        $node_map               = $node->getImagemapArea();
1531                        $clusterimage_map       .= $node_map;
1532                }
1533
1534                return $clusterimage_map;
1535        }
1536}
1537
1538class EmptyImage {
1539
1540        function draw() {
1541                $image          = imageCreateTrueColor( 1, 1 );
1542                $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1543                imageFill( $image, 0, 0, $colorwhite );                         
1544
1545                header( 'Content-type: image/png' );
1546                imagePNG( $image );
1547                imageDestroy( $image );
1548        }
1549}
1550
1551class HostImage {
1552
1553        var $data_gather, $cluster, $host, $node, $image;
1554        var $headerstrlen;
1555
1556        function HostImage( $data_gather, $cluster, $host ) {
1557
1558                $this->data_gather      = $data_gather;
1559                $this->cluster          = $cluster;
1560                $this->host             = $host;
1561                $this->y_offset         = 0;
1562                $this->font             = 2;
1563                $this->fontspaceing     = 2;
1564                $this->headerstrlen     = array();
1565
1566                $this->fontheight       = ImageFontHeight( $this->font );
1567                $this->fontwidth        = ImageFontWidth( $this->font );
1568
1569                $dg                     = &$this->data_gather;
1570                $this->node             = &$dg->getNode( $this->host );
1571                $n                      = &$this->node;
1572                $this->njobs            = $n->getJobs();
1573        }
1574
1575        function drawJobs() {
1576
1577                $dg                     = &$this->data_gather;
1578                $colorblack             = imageColorAllocate( $this->image, 0, 0, 0 );
1579
1580                for( $n = 0; $n < count( $this->njobs ); $n++ ) {
1581
1582                        $jobid                  = $this->njobs[$n];
1583                        $jobinfo                = $dg->getJob( $jobid );
1584
1585                        $xoffset                = 5;
1586                        imageString( $this->image, $this->font, $xoffset, $this->y_offset, strval( $jobid ), $colorblack );
1587
1588                        foreach( $this->headerstrlen as $headername => $headerlen ) {
1589
1590                                if( $headername == 'nodes' ) {
1591                                        $attrval        = strval( count( $jobinfo[nodes] ) );
1592                                } else if( $headername == 'cpus' ) {
1593
1594                                        if( !isset( $jobinfo[ppn] ) )
1595                                                $jobinfo[ppn] = 1;
1596
1597                                        $attrval        = strval( count( $jobinfo[nodes] ) * intval( $jobinfo[ppn] ) );
1598
1599                                } else if( $headername == 'runningtime' ) {
1600                                        $attrval        = makeTime( intval( $jobinfo[reported] ) - intval( $jobinfo[start_timestamp] ) );
1601                                } else {
1602                                        $attrval        = strval( $jobinfo[$headername] );
1603                                }
1604
1605                                imageString( $this->image, $this->font, $xoffset, $this->y_offset, $attrval, $colorblack );
1606               
1607                                $xoffset        = $xoffset + ($this->fontwidth * ( $headerlen + 1 ) );
1608
1609                        }
1610                       
1611                        $this->newLineOffset();
1612                }
1613        }
1614
1615        function drawHeader() {
1616
1617                $dg                     = &$this->data_gather;
1618
1619                for( $n = 0; $n < count( $this->njobs ); $n++ ) {
1620
1621                        $jobid                  = $this->njobs[$n];
1622                        $jobinfo                = $dg->getJob( $jobid );
1623
1624                        if( !isset( $this->headerstrlen[id] ) )
1625                                $this->headerstrlen[id] = strlen( strval( $jobid ) );
1626                        else
1627                                if( strlen( strval( $jobid ) ) > $this->headerstrlen[id] )
1628                                        $this->headerstrlen[id] = strlen( strval( $jobid ) );
1629
1630                        if( !isset( $this->headerstrlen[owner] ) )
1631                                $this->headerstrlen[owner]      = strlen( strval( $jobinfo[owner] ) );
1632                        else
1633                                if( strlen( strval( $jobinfo[owner] ) ) > $this->headerstrlen[owner] )
1634                                        $this->headerstrlen[owner]      = strlen( strval( $jobinfo[owner] ) );
1635
1636                        if( !isset( $this->headerstrlen[queue] ) )
1637                                $this->headerstrlen[queue]      = strlen( strval( $jobinfo[queue] ) );
1638                        else
1639                                if( strlen( strval( $jobinfo[queue] ) ) > $this->headerstrlen[queue] )
1640                                        $this->headerstrlen[queue]      = strlen( strval( $jobinfo[queue] ) );
1641
1642                        if( !isset( $jobinfo[ppn] ) )
1643                                $jobinfo[ppn] = 1;
1644
1645                        $cpus                   = count( $jobinfo[nodes] ) * intval( $jobinfo[ppn] );
1646
1647                        if( !isset( $this->headerstrlen[cpus] ) )
1648                                $this->headerstrlen[cpus]       = strlen( strval( $cpus ) );
1649                        else
1650                                if( strlen( strval( $cpus ) ) > $this->headerstrlen[cpus] )
1651                                        $this->headerstrlen[cpus]       = strlen( strval( $cpus ) );
1652
1653                        $nodes                  = count( $jobinfo[nodes] );
1654
1655                        if( !isset( $this->headerstrlen[nodes] ) )
1656                                $this->headerstrlen[nodes]      = strlen( strval( $nodes ) );
1657                        else
1658                                if( strlen( strval( $nodes) ) > $this->headerstrlen[nodes] )
1659                                        $this->headerstrlen[nodes]      = strlen( strval( $nodes ) );
1660
1661                        $runningtime            = makeTime( intval( $jobinfo[reported] ) - intval( $jobinfo[start_timestamp] ) );
1662
1663                        if( !isset( $this->headerstrlen[runningtime] ) )
1664                                $this->headerstrlen[runningtime]        = strlen( strval( $runningtime) );
1665                        else
1666                                if( strlen( strval( $runningtime) ) > $this->headerstrlen[runningtime] )
1667                                        $this->headerstrlen[runningtime]        = strlen( strval( $runningtime) );
1668
1669                        if( !isset( $this->headerstrlen[name] ) )
1670                                $this->headerstrlen[name]       = strlen( strval( $jobinfo[name] ) );
1671                        else
1672                                if( strlen( strval( $jobinfo[name] ) ) > $this->headerstrlen[name] )
1673                                        $this->headerstrlen[name]       = strlen( strval( $jobinfo[name] ) );
1674
1675                }
1676
1677                $xoffset        = 5;
1678
1679                foreach( $this->headerstrlen as $headername => $headerlen ) {
1680
1681                        $colorgreen     = imageColorAllocate( $this->image, 0, 200, 0 );
1682
1683                        imageString( $this->image, $this->font, $xoffset, $this->y_offset, ucfirst( $headername ), $colorgreen );
1684               
1685                        if( $headerlen < strlen( $headername ) )
1686                                $this->headerstrlen[$headername]        = strlen( $headername );
1687
1688                        $xoffset        = $xoffset + ($this->fontwidth * ( $headerlen + 1 ) );
1689
1690                }
1691                $this->newLineOffset();
1692        }
1693
1694        function newLineOffset() {
1695
1696                $this->y_offset         = $this->y_offset + $this->fontheight + $this->fontspaceing;
1697        }
1698
1699        function draw() {
1700
1701                $xlen           = 450;
1702                $ylen           = ( count( $this->njobs ) * ( $this->fontheight + $this->fontspaceing ) ) + (3 * $this->fontheight);
1703
1704                $this->image    = imageCreateTrueColor( $xlen, $ylen );
1705                $colorwhite     = imageColorAllocate( $this->image, 255, 255, 255 );
1706                imageFill( $this->image, 0, 0, $colorwhite );                         
1707
1708                $colorblue      = imageColorAllocate( $this->image, 0, 0, 255 );
1709
1710                imageString( $this->image, $this->font, 1, $this->y_offset, "Monarch Joblist - host: ".$this->host, $colorblue );
1711                $this->newLineOffset();
1712
1713                $this->drawHeader();
1714                $this->drawJobs();
1715
1716                header( 'Content-type: image/png' );
1717                imagePNG( $this->image );
1718                imageDestroy( $this->image );
1719        }
1720}
1721
1722function imageStringDown( &$image, $font, $x, $y, &$s, &$col )
1723{
1724        $fw     = imagefontwidth( $font);
1725        $fh     = imagefontheight( $font);
1726       
1727        $fontspacing = 0;
1728
1729        $fx     = $x;
1730        $fy     = $y;
1731
1732        for( $n=0; $n<strlen( $s ); $n++ )
1733        {
1734                $myc    = $s{$n};
1735
1736                imagestring( $image, $font, $fx, $fy, $myc, $col );
1737
1738                $fy     += ($fontspacing + $fh );
1739        }
1740}
1741
1742function array_rem( $val, &$arr )
1743{
1744        // Delete val from arr
1745        //
1746        $i      = array_search( $val, $arr );
1747
1748        if( $i == false ) return false;
1749
1750        $arr    = array_merge( array_slice( $arr, 0, $i ), array_slice( $arr, $i+1, count( $arr ) ) );
1751
1752        return true;
1753}
1754
1755function cmp( $a, $b ) 
1756{
1757        global $SORT_ORDER;
1758        global $skan_str;
1759        global $x_first, $y_first;
1760        global $x_present, $y_present;
1761
1762        //printf("ppoep = %s\n", $skan_str);
1763        $a_node         = $a;
1764        $b_node         = $b;
1765        $a              = $a_node->getHostname();
1766        $b              = $b_node->getHostname();
1767
1768        if( $a == $b ) return 0;
1769
1770        $a_x            = 0;
1771        $b_x            = 0;
1772        $a_y            = 0;
1773        $b_y            = 0;
1774
1775        if( $x_present && $y_present )
1776        {
1777                if( $x_first )
1778                {
1779                        $n = sscanf( $a, $skan_str, $a_x, $a_y );
1780                        $n = sscanf( $b, $skan_str, $b_x, $b_y );
1781                }
1782                else if( $y_first )
1783                {
1784                        $n = sscanf( $a, $skan_str, $a_y, $a_x );
1785                        $n = sscanf( $b, $skan_str, $b_y, $b_x );
1786                }
1787        } 
1788        else if( $x_present && !$y_present )
1789        {
1790                $n = sscanf( $a, $skan_str, $a_x );
1791                $n = sscanf( $b, $skan_str, $b_x );
1792        }
1793        else if( $y_present && !$x_present )
1794        {
1795                $n = sscanf( $a, $skan_str, $a_y );
1796                $n = sscanf( $b, $skan_str, $b_y );
1797        }
1798
1799        if ( $SORT_ORDER=="desc" )
1800        {
1801
1802                if( $x_present && $y_present )
1803                {
1804                        // 1  = a < b
1805                        // -1 = a > b
1806                        //
1807                        if ($a_x == $b_x)
1808                        {
1809                                if ($a_y < $b_y)
1810                                {
1811                                        return 1;
1812                                }
1813                                else if ($a_y > $b_y)
1814                                {
1815                                        return -1;
1816                                }
1817                        }
1818                        else if ($a_x < $b_x)
1819                        {
1820                                return 1;
1821                        }
1822                        else if ($a_x > $b_x)
1823                        {
1824                                return -1;
1825                        }
1826                } 
1827                else if( $x_present && !$y_present )
1828                {
1829                        if ($a_x < $b_x)
1830                        {
1831                                return 1;
1832                        }
1833                        else if ($a_x > $b_x)
1834                        {
1835                                return -1;
1836                        }
1837                }
1838                else if( $y_present && !$x_present )
1839                {
1840                        if ($a_y < $b_y)
1841                        {
1842                                return 1;
1843                        }
1844                        else if ($a_y > $b_y)
1845                        {
1846                                return -1;
1847                        }
1848                }
1849        }
1850        else if ( $SORT_ORDER == "asc" )
1851        {
1852
1853                if( $x_present && $y_present )
1854                {
1855                        // 1  = a > b
1856                        // -1 = a < b
1857                        //
1858                        if ($a_x == $b_x)
1859                        {
1860                                if ($a_y > $b_y)
1861                                {
1862                                        return 1;
1863                                }
1864                                else if ($a_y < $b_y)
1865                                {
1866                                        return -1;
1867                                }
1868                        }
1869                        else if ($a_x > $b_x)
1870                        {
1871                                return 1;
1872                        }
1873                        else if ($a_x < $b_x)
1874                        {
1875                                return -1;
1876                        }
1877                }
1878                else if( $x_present && !$y_present )
1879                {
1880                        if ($a_x > $b_x)
1881                        {
1882                                return 1;
1883                        }
1884                        else if ($a_x < $b_x)
1885                        {
1886                                return -1;
1887                        }
1888                }
1889                else if( $y_present && !$x_present )
1890                {
1891                        if ($a_y > $b_y)
1892                        {
1893                                return 1;
1894                        }
1895                        else if ($a_y < $b_y)
1896                        {
1897                                return -1;
1898                        }
1899                }
1900        }
1901}
1902function makeTime( $time ) {
1903
1904        $days = intval( $time / 86400 );
1905        $time = ($days>0) ? $time % ($days * 86400) : $time;
1906
1907        //printf( "time = %s, days = %s\n", $time, $days );
1908
1909        $date_str = '';
1910        $day_str = '';
1911
1912        if( $days > 0 ) {
1913                if( $days > 1 )
1914                        $day_str .= $days . ' days';
1915                else
1916                        $day_str .= $days . ' day';
1917        }
1918
1919        $hours = intval( $time / 3600 );
1920        $time = $hours ? $time % ($hours * 3600) : $time;
1921
1922        //printf( "time = %s, days = %s, hours = %s\n", $time, $days, $hours );
1923        if( $hours > 0 ) {
1924                $date_str .= $hours . ':';
1925                $date_unit = 'hours'; 
1926        }
1927
1928        $minutes = intval( $time / 60 );
1929        $seconds = $minutes ? $time % ($minutes * 60) : $time;
1930
1931        if( $minutes > 0 ) {
1932
1933                if( $minutes >= 10 )
1934                        $date_str .= $minutes . ':';
1935                else
1936                        $date_str .= '0' . $minutes . ':';
1937
1938                $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
1939        } else {
1940                if($hours > 0 ) {
1941                        $date_str .= '00:';
1942                        $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
1943                }
1944        }
1945
1946
1947        $date_unit = (!isset($date_unit)) ? 'seconds' : $date_unit;
1948
1949        if( $seconds > 0 ) {
1950
1951                if( $seconds >= 10 )
1952                        $date_str .= $seconds . ' ' . $date_unit;
1953                else
1954                        $date_str .= '0' . $seconds . ' ' . $date_unit;
1955
1956        } else if ( $hours > 0 or $minutes > 0 )
1957
1958                $date_str .= '00 ' . $date_unit;
1959
1960        if( $days > 0) {
1961
1962                if( $hours > 0 or $minutes > 0 or $seconds > 0 )
1963                        $date_str = $day_str . ' - ' . $date_str;
1964                else
1965                        $date_str = $day_str;
1966        }
1967
1968        return $date_str;
1969}
1970?>
Note: See TracBrowser for help on using the repository browser.