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

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

web/addons/job_monarch/libtoga.php:

  • fixed per-cluster settings
  • changed node sorting algorythm to handle when only one of x or y is set
  • Property svn:keywords set to Id
File size: 43.0 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 339 2007-04-23 22:41:54Z 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      = 0;
1213                                $y      = 0;
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                                                unset( $nodes[$hostname] );
1231                                        }
1232                                }
1233                                else if( $x_present && !$y_present )
1234                                {
1235                                        $n = sscanf( $hostname, $skan_str, $x );
1236                                        // Remove nodes that don't match
1237                                        //
1238                                        if( $n < 1 )
1239                                        {
1240                                                unset( $nodes[$hostname] );
1241                                        }
1242                                        $y      = 1;
1243                                }
1244                                else if( $y_present && !$x_present )
1245                                {
1246                                        $n = sscanf( $hostname, $skan_str, $y );
1247                                        // Remove nodes that don't match
1248                                        //
1249                                        if( $n < 1 )
1250                                        {
1251                                                unset( $nodes[$hostname] );
1252                                        }
1253                                        $x      = 1;
1254                                }
1255                                //printf( "xfirst %s yfirst %s\n", $x_first, $y_first );
1256
1257                                //printf( "n %s\n", $n );
1258
1259
1260                                if( !$x_min )
1261                                {
1262                                        $x_min  = $x;
1263                                }
1264                                else if( $x < $x_min )
1265                                {
1266                                        $x_min  = $x;
1267                                }
1268                                if( !$x_max )
1269                                {
1270                                        $x_max  = $x;
1271                                }
1272                                else if( $x > $x_max )
1273                                {
1274                                        $x_max  = $x;
1275                                }
1276                                if( !$y_min )
1277                                {
1278                                        $y_min  = $y;
1279                                }
1280                                else if( $y < $y_min )
1281                                {
1282                                        $y_min  = $y;
1283                                }
1284                                if( !$y_max )
1285                                {
1286                                        $y_max  = $y;
1287                                }
1288                                else if( $y > $y_max )
1289                                {
1290                                        $y_max  = $y;
1291                                }
1292                        }
1293
1294                        //printf( "ss %s\n", $skan_str);
1295                        $sorted_nodes   = usort( $nodes, "cmp" );
1296
1297                        //print_r( $nodes );
1298
1299                        $cur_node       = 0;
1300
1301                        $x_offset       = 0;
1302                        $y_offset       = 0;
1303                        $font           = 2;
1304                        $fontwidth      = ImageFontWidth( $font );
1305                        $fontheight     = ImageFontHeight( $font );
1306                        $fontspaceing   = 2;
1307
1308                        if( $this->isSmall() ) 
1309                        {
1310                                $y_offset       = $y_offset + (2 * $fontspaceing) + $fontheight;
1311                        }
1312
1313                        if( $this->isBig() ) 
1314                        {
1315
1316                                $y_offset       = ($fontheight * (1 + strlen( $x_max) ) ) + ((2 + strlen( $x_max)) * $fontspaceing);
1317                                $x_offset       = ($fontwidth * (1 + strlen( $y_max) ) ) + ((2 + strlen( $y_max)) * $fontspaceing);
1318
1319                        }
1320                        //$x_offset     = ($fontwidth * 3) + (5 * $fontspaceing);
1321
1322                        //printf( "xmin %s xmax %s\n", $x_min, $x_max );
1323                        //printf( "ymin %s ymax %s\n", $y_min, $y_max );
1324
1325                        // werkt
1326                        //print_r( $nodes );
1327
1328                        $image_width    = $x_offset + ($node_width * ($x_max-$x_min+2));
1329                        $image_width    = ($image_width < $this->width) ? $image_width : $this->width;
1330                        $image_height   = $y_offset + ($node_width * ($y_max-$y_min+2));
1331
1332                        $this->width    = $image_width;
1333                        $this->heigth   = $image_heigth;
1334
1335                        $image          = imageCreateTrueColor( $image_width, $image_height );
1336                        $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1337
1338                        imageFill( $image, 0, 0, $colorwhite );
1339
1340                        if( $this->isSmall() ) {
1341
1342                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1343
1344                                imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1345                        }
1346
1347                        if( $this->isBig() && ( isset( $SORT_XLABEL ) || isset( $SORT_YLABEL ) ) )
1348                        {
1349                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1350
1351                                if( isset( $SORT_XLABEL ) )
1352                                {
1353                                        imageString( $image, $font, $x_offset, $fontspaceing, $SORT_XLABEL, $colorblue );
1354                                }
1355
1356                                if( isset( $SORT_YLABEL ) )
1357                                {
1358                                        // Stupid php without imageStringDown function
1359                                        //
1360                                        imageStringDown( $image, $font, $fontspaceing, $y_offset, $SORT_YLABEL, $colorblue );
1361                                }
1362                        }
1363
1364                        for( $n = $x_min; $n <= $x_max; $n++ )
1365                        {
1366                                for( $m = $y_min; $m <= $y_max; $m++ )
1367                                {
1368                                        if( $x_min > 0 )
1369                                        {
1370                                                $x      = $x_offset + ( ($n-$x_min) * $node_width );
1371                                        }
1372                                        if( $y_min > 0 )
1373                                        {
1374                                                $y      = $y_offset + ( ($m-$y_min) * $node_width );
1375                                        }
1376
1377                                        if( isset( $nodes[$cur_node] ) ) 
1378                                        {
1379                                                $host   = $nodes[$cur_node]->getHostname();
1380
1381                                                if( $x_present && $y_present )
1382                                                {
1383                                                        if( $x_first )
1384                                                        {
1385                                                                $nn = sscanf( $host, $skan_str, $rx, $ry );
1386                                                        }
1387                                                        else if( $y_first )
1388                                                        {
1389                                                                $nn = sscanf( $host, $skan_str, $ry, $rx );
1390                                                        }
1391                                                        if ( $nn < 2 )
1392                                                        {
1393                                                                continue;
1394                                                        }
1395                                                        if( ( $rx ) > $n )
1396                                                        {
1397                                                                $m      = $y_max + 1;
1398                                                                continue;
1399                                                        }
1400                                                }
1401                                                else if( $x_present )
1402                                                {
1403                                                        $nn = sscanf( $host, $skan_str, $rx );
1404                                                }
1405                                                else if( $y_present )
1406                                                {
1407                                                        $nn = sscanf( $host, $skan_str, $ry );
1408                                                }
1409
1410                                                if( !in_array( $host, $filtered_nodes ) )
1411                                                        $nodes[$cur_node]->setShowinfo( 0 );
1412
1413                                                $nodes[$cur_node]->setCoords( $x, $y );
1414                                                $nodes[$cur_node]->setImage( $image );
1415
1416                                                //print_r( $nodes[$cur_node] );
1417
1418                                                if( $this->isSmall() )
1419                                                        $nodes[$cur_node]->drawSmall();
1420                                                else if( $this->isBig() )
1421                                                        $nodes[$cur_node]->drawBig();
1422                                        }
1423                                        if( $this->isBig() ) 
1424                                        {
1425                                                if(( $n == $x_min ) && ( isset($SORT_YLABEL) ) )
1426                                                {
1427                                                        $mfontspacing   = 1;
1428                                                        $ylabel_x       = $x - ( $fontwidth * strlen( $y_max ) ) - $mfontspacing;
1429                                                        $ylabel_y       = $y;
1430
1431                                                        imageString( $image, $font, $ylabel_x, $ylabel_y, strval( $m ), $colorblue );
1432                                                }
1433                                                if(( $m == $y_min ) && ( isset($SORT_XLABEL) ) )
1434                                                {
1435                                                        $mfontspacing   = 2;
1436                                                        $xlabel_y       = $y - ( $fontheight * strlen( $x_max ) );
1437                                                        $xlabel_x       = $x + $mfontspacing; 
1438
1439                                                        imageStringDown( $image, $font, $xlabel_x, $xlabel_y, strval( $n ), $colorblue );
1440                                                }
1441                                        }
1442
1443                                        $cur_node++;
1444                                }
1445                        }
1446
1447                }
1448                else
1449                {
1450                        $image          = imageCreateTrueColor( $max_width, ($y_offset + (($node_rows*$node_width)+1) ) );
1451                        $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1452
1453                        imageFill( $image, 0, 0, $colorwhite );
1454
1455                        if( $this->isSmall() ) {
1456
1457                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1458
1459                                imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1460                        }
1461
1462                        for( $n = 0; $n < $node_rows; $n++ ) {
1463                       
1464                                for( $m = 0; $m < $nodes_per_row; $m++ ) {
1465                       
1466                                        $x = ($m * $node_width);
1467                                        $y = $y_offset + ($n * $node_width);
1468
1469                                        $cur_node = ($n * $nodes_per_row) + ($m);
1470                                        $host = $nodes_hosts[$cur_node];
1471
1472                                        if( isset( $nodes[$host] ) ) {
1473
1474                                                $nodes[$host]->setCoords( $x, $y );
1475                                                $nodes[$host]->setImage( $image );
1476
1477                                                if( !in_array( $host, $filtered_nodes ) )
1478                                                        $nodes[$host]->setShowinfo( 0 );
1479
1480                                                if( $this->isSmall() )
1481                                                        $nodes[$host]->drawSmall();
1482                                                else if( $this->isBig() )
1483                                                        $nodes[$host]->drawBig();
1484                                        }
1485                                }
1486                        }
1487                }
1488       
1489                $this->nodes    = &$nodes;
1490
1491                if ($this->output) {
1492                        header( 'Content-type: image/png' );
1493                        imagePNG( $image );
1494                        imageDestroy( $image );
1495                }
1496        }
1497
1498        function getImagemapArea() {
1499
1500                $clusterimage_map       = "";
1501
1502                foreach( $this->nodes as $hostname => $node ) {
1503
1504                        $node_map               = $node->getImagemapArea();
1505                        $clusterimage_map       .= $node_map;
1506                }
1507
1508                return $clusterimage_map;
1509        }
1510}
1511
1512class EmptyImage {
1513
1514        function draw() {
1515                $image          = imageCreateTrueColor( 1, 1 );
1516                $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1517                imageFill( $image, 0, 0, $colorwhite );                         
1518
1519                header( 'Content-type: image/png' );
1520                imagePNG( $image );
1521                imageDestroy( $image );
1522        }
1523}
1524
1525class HostImage {
1526
1527        var $data_gather, $cluster, $host, $node, $image;
1528        var $headerstrlen;
1529
1530        function HostImage( $data_gather, $cluster, $host ) {
1531
1532                $this->data_gather      = $data_gather;
1533                $this->cluster          = $cluster;
1534                $this->host             = $host;
1535                $this->y_offset         = 0;
1536                $this->font             = 2;
1537                $this->fontspaceing     = 2;
1538                $this->headerstrlen     = array();
1539
1540                $this->fontheight       = ImageFontHeight( $this->font );
1541                $this->fontwidth        = ImageFontWidth( $this->font );
1542
1543                $dg                     = &$this->data_gather;
1544                $this->node             = &$dg->getNode( $this->host );
1545                $n                      = &$this->node;
1546                $this->njobs            = $n->getJobs();
1547        }
1548
1549        function drawJobs() {
1550
1551                $dg                     = &$this->data_gather;
1552                $colorblack             = imageColorAllocate( $this->image, 0, 0, 0 );
1553
1554                for( $n = 0; $n < count( $this->njobs ); $n++ ) {
1555
1556                        $jobid                  = $this->njobs[$n];
1557                        $jobinfo                = $dg->getJob( $jobid );
1558
1559                        $xoffset                = 5;
1560                        imageString( $this->image, $this->font, $xoffset, $this->y_offset, strval( $jobid ), $colorblack );
1561
1562                        foreach( $this->headerstrlen as $headername => $headerlen ) {
1563
1564                                if( $headername == 'nodes' ) {
1565                                        $attrval        = strval( count( $jobinfo[nodes] ) );
1566                                } else if( $headername == 'cpus' ) {
1567
1568                                        if( !isset( $jobinfo[ppn] ) )
1569                                                $jobinfo[ppn] = 1;
1570
1571                                        $attrval        = strval( count( $jobinfo[nodes] ) * intval( $jobinfo[ppn] ) );
1572
1573                                } else if( $headername == 'runningtime' ) {
1574                                        $attrval        = makeTime( intval( $jobinfo[reported] ) - intval( $jobinfo[start_timestamp] ) );
1575                                } else {
1576                                        $attrval        = strval( $jobinfo[$headername] );
1577                                }
1578
1579                                imageString( $this->image, $this->font, $xoffset, $this->y_offset, $attrval, $colorblack );
1580               
1581                                $xoffset        = $xoffset + ($this->fontwidth * ( $headerlen + 1 ) );
1582
1583                        }
1584                       
1585                        $this->newLineOffset();
1586                }
1587        }
1588
1589        function drawHeader() {
1590
1591                $dg                     = &$this->data_gather;
1592
1593                for( $n = 0; $n < count( $this->njobs ); $n++ ) {
1594
1595                        $jobid                  = $this->njobs[$n];
1596                        $jobinfo                = $dg->getJob( $jobid );
1597
1598                        if( !isset( $this->headerstrlen[id] ) )
1599                                $this->headerstrlen[id] = strlen( strval( $jobid ) );
1600                        else
1601                                if( strlen( strval( $jobid ) ) > $this->headerstrlen[id] )
1602                                        $this->headerstrlen[id] = strlen( strval( $jobid ) );
1603
1604                        if( !isset( $this->headerstrlen[owner] ) )
1605                                $this->headerstrlen[owner]      = strlen( strval( $jobinfo[owner] ) );
1606                        else
1607                                if( strlen( strval( $jobinfo[owner] ) ) > $this->headerstrlen[owner] )
1608                                        $this->headerstrlen[owner]      = strlen( strval( $jobinfo[owner] ) );
1609
1610                        if( !isset( $this->headerstrlen[queue] ) )
1611                                $this->headerstrlen[queue]      = strlen( strval( $jobinfo[queue] ) );
1612                        else
1613                                if( strlen( strval( $jobinfo[queue] ) ) > $this->headerstrlen[queue] )
1614                                        $this->headerstrlen[queue]      = strlen( strval( $jobinfo[queue] ) );
1615
1616                        if( !isset( $jobinfo[ppn] ) )
1617                                $jobinfo[ppn] = 1;
1618
1619                        $cpus                   = count( $jobinfo[nodes] ) * intval( $jobinfo[ppn] );
1620
1621                        if( !isset( $this->headerstrlen[cpus] ) )
1622                                $this->headerstrlen[cpus]       = strlen( strval( $cpus ) );
1623                        else
1624                                if( strlen( strval( $cpus ) ) > $this->headerstrlen[cpus] )
1625                                        $this->headerstrlen[cpus]       = strlen( strval( $cpus ) );
1626
1627                        $nodes                  = count( $jobinfo[nodes] );
1628
1629                        if( !isset( $this->headerstrlen[nodes] ) )
1630                                $this->headerstrlen[nodes]      = strlen( strval( $nodes ) );
1631                        else
1632                                if( strlen( strval( $nodes) ) > $this->headerstrlen[nodes] )
1633                                        $this->headerstrlen[nodes]      = strlen( strval( $nodes ) );
1634
1635                        $runningtime            = makeTime( intval( $jobinfo[reported] ) - intval( $jobinfo[start_timestamp] ) );
1636
1637                        if( !isset( $this->headerstrlen[runningtime] ) )
1638                                $this->headerstrlen[runningtime]        = strlen( strval( $runningtime) );
1639                        else
1640                                if( strlen( strval( $runningtime) ) > $this->headerstrlen[runningtime] )
1641                                        $this->headerstrlen[runningtime]        = strlen( strval( $runningtime) );
1642
1643                        if( !isset( $this->headerstrlen[name] ) )
1644                                $this->headerstrlen[name]       = strlen( strval( $jobinfo[name] ) );
1645                        else
1646                                if( strlen( strval( $jobinfo[name] ) ) > $this->headerstrlen[name] )
1647                                        $this->headerstrlen[name]       = strlen( strval( $jobinfo[name] ) );
1648
1649                }
1650
1651                $xoffset        = 5;
1652
1653                foreach( $this->headerstrlen as $headername => &$headerlen ) {
1654
1655                        $colorgreen     = imageColorAllocate( $this->image, 0, 200, 0 );
1656
1657                        imageString( $this->image, $this->font, $xoffset, $this->y_offset, ucfirst( $headername ), $colorgreen );
1658               
1659                        if( $headerlen < strlen( $headername ) )
1660                                $headerlen      = strlen( $headername );
1661
1662                        $xoffset        = $xoffset + ($this->fontwidth * ( $headerlen + 1 ) );
1663
1664                }
1665                $this->newLineOffset();
1666        }
1667
1668        function newLineOffset() {
1669
1670                $this->y_offset         = $this->y_offset + $this->fontheight + $this->fontspaceing;
1671        }
1672
1673        function draw() {
1674
1675                $xlen           = 450;
1676                $ylen           = ( count( $this->njobs ) * ( $this->fontheight + $this->fontspaceing ) ) + (3 * $this->fontheight);
1677
1678                $this->image    = imageCreateTrueColor( $xlen, $ylen );
1679                $colorwhite     = imageColorAllocate( $this->image, 255, 255, 255 );
1680                imageFill( $this->image, 0, 0, $colorwhite );                         
1681
1682                $colorblue      = imageColorAllocate( $this->image, 0, 0, 255 );
1683
1684                imageString( $this->image, $this->font, 1, $this->y_offset, "Monarch Joblist - host: ".$this->host, $colorblue );
1685                $this->newLineOffset();
1686
1687                $this->drawHeader();
1688                $this->drawJobs();
1689
1690                header( 'Content-type: image/png' );
1691                imagePNG( $this->image );
1692                imageDestroy( $this->image );
1693        }
1694}
1695
1696function imageStringDown( &$image, $font, $x, $y, &$s, &$col )
1697{
1698        $fw     = imagefontwidth( $font);
1699        $fh     = imagefontheight( $font);
1700       
1701        $fontspacing = 0;
1702
1703        $fx     = $x;
1704        $fy     = $y;
1705
1706        for( $n=0; $n<strlen( $s ); $n++ )
1707        {
1708                $myc    = $s{$n};
1709
1710                imagestring( $image, $font, $fx, $fy, $myc, $col );
1711
1712                $fy     += ($fontspacing + $fh );
1713        }
1714}
1715
1716function array_rem( $val, &$arr )
1717{
1718        // Delete val from arr
1719        //
1720        $i      = array_search( $val, $arr );
1721
1722        if( $i == false ) return false;
1723
1724        $arr    = array_merge( array_slice( $arr, 0, $i ), array_slice( $arr, $i+1, count( $arr ) ) );
1725
1726        return true;
1727}
1728
1729function cmp( $a, $b ) 
1730{
1731        global $SORT_ORDER;
1732        global $skan_str;
1733        global $x_first, $y_first;
1734        global $x_present, $y_present;
1735
1736        //printf("ppoep = %s\n", $skan_str);
1737        $a_node         = $a;
1738        $b_node         = $b;
1739        $a              = $a_node->getHostname();
1740        $b              = $b_node->getHostname();
1741
1742        if( $a == $b ) return 0;
1743
1744        $a_x            = 0;
1745        $b_x            = 0;
1746        $a_y            = 0;
1747        $b_y            = 0;
1748
1749        if( $x_present && $y_present )
1750        {
1751                if( $x_first )
1752                {
1753                        $n = sscanf( $a, $skan_str, $a_x, $a_y );
1754                        $n = sscanf( $b, $skan_str, $b_x, $b_y );
1755                }
1756                else if( $y_first )
1757                {
1758                        $n = sscanf( $a, $skan_str, $a_y, $a_x );
1759                        $n = sscanf( $b, $skan_str, $b_y, $b_x );
1760                }
1761        } 
1762        else if( $x_present && !$y_present )
1763        {
1764                $n = sscanf( $a, $skan_str, $a_x );
1765                $n = sscanf( $b, $skan_str, $b_x );
1766        }
1767        else if( $y_present && !$x_present )
1768        {
1769                $n = sscanf( $a, $skan_str, $a_y );
1770                $n = sscanf( $b, $skan_str, $b_y );
1771        }
1772
1773        if ( $SORT_ORDER=="desc" )
1774        {
1775
1776                if( $x_present && $y_present )
1777                {
1778                        // 1  = a < b
1779                        // -1 = a > b
1780                        //
1781                        if ($a_x == $b_x)
1782                        {
1783                                if ($a_y < $b_y)
1784                                {
1785                                        return 1;
1786                                }
1787                                else if ($a_y > $b_y)
1788                                {
1789                                        return -1;
1790                                }
1791                        }
1792                        else if ($a_x < $b_x)
1793                        {
1794                                return 1;
1795                        }
1796                        else if ($a_x > $b_x)
1797                        {
1798                                return -1;
1799                        }
1800                } 
1801                else if( $x_present && !$y_present )
1802                {
1803                        if ($a_x < $b_x)
1804                        {
1805                                return 1;
1806                        }
1807                        else if ($a_x > $b_x)
1808                        {
1809                                return -1;
1810                        }
1811                }
1812                else if( $y_present && !$x_present )
1813                {
1814                        if ($a_y < $b_y)
1815                        {
1816                                return 1;
1817                        }
1818                        else if ($a_y > $b_y)
1819                        {
1820                                return -1;
1821                        }
1822                }
1823        }
1824        else if ( $SORT_ORDER == "asc" )
1825        {
1826
1827                if( $x_present && $y_present )
1828                {
1829                        // 1  = a > b
1830                        // -1 = a < b
1831                        //
1832                        if ($a_x == $b_x)
1833                        {
1834                                if ($a_y > $b_y)
1835                                {
1836                                        return 1;
1837                                }
1838                                else if ($a_y < $b_y)
1839                                {
1840                                        return -1;
1841                                }
1842                        }
1843                        else if ($a_x > $b_x)
1844                        {
1845                                return 1;
1846                        }
1847                        else if ($a_x < $b_x)
1848                        {
1849                                return -1;
1850                        }
1851                }
1852                else if( $x_present && !$y_present )
1853                {
1854                        if ($a_x > $b_x)
1855                        {
1856                                return 1;
1857                        }
1858                        else if ($a_x < $b_x)
1859                        {
1860                                return -1;
1861                        }
1862                }
1863                else if( $y_present && !$x_present )
1864                {
1865                        if ($a_y > $b_y)
1866                        {
1867                                return 1;
1868                        }
1869                        else if ($a_y < $b_y)
1870                        {
1871                                return -1;
1872                        }
1873                }
1874        }
1875}
1876function makeTime( $time ) {
1877
1878        $days = intval( $time / 86400 );
1879        $time = ($days>0) ? $time % ($days * 86400) : $time;
1880
1881        //printf( "time = %s, days = %s\n", $time, $days );
1882
1883        $date_str = '';
1884        $day_str = '';
1885
1886        if( $days > 0 ) {
1887                if( $days > 1 )
1888                        $day_str .= $days . ' days';
1889                else
1890                        $day_str .= $days . ' day';
1891        }
1892
1893        $hours = intval( $time / 3600 );
1894        $time = $hours ? $time % ($hours * 3600) : $time;
1895
1896        //printf( "time = %s, days = %s, hours = %s\n", $time, $days, $hours );
1897        if( $hours > 0 ) {
1898                $date_str .= $hours . ':';
1899                $date_unit = 'hours'; 
1900        }
1901
1902        $minutes = intval( $time / 60 );
1903        $seconds = $minutes ? $time % ($minutes * 60) : $time;
1904
1905        if( $minutes > 0 ) {
1906
1907                if( $minutes >= 10 )
1908                        $date_str .= $minutes . ':';
1909                else
1910                        $date_str .= '0' . $minutes . ':';
1911
1912                $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
1913        } else {
1914                if($hours > 0 ) {
1915                        $date_str .= '00:';
1916                        $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
1917                }
1918        }
1919
1920
1921        $date_unit = (!isset($date_unit)) ? 'seconds' : $date_unit;
1922
1923        if( $seconds > 0 ) {
1924
1925                if( $seconds >= 10 )
1926                        $date_str .= $seconds . ' ' . $date_unit;
1927                else
1928                        $date_str .= '0' . $seconds . ' ' . $date_unit;
1929
1930        } else if ( $hours > 0 or $minutes > 0 )
1931
1932                $date_str .= '00 ' . $date_unit;
1933
1934        if( $days > 0) {
1935
1936                if( $hours > 0 or $minutes > 0 or $seconds > 0 )
1937                        $date_str = $day_str . ' - ' . $date_str;
1938                else
1939                        $date_str = $day_str;
1940        }
1941
1942        return $date_str;
1943}
1944?>
Note: See TracBrowser for help on using the repository browser.