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

Last change on this file since 492 was 492, checked in by bastiaans, 16 years ago

libtoga.php:

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