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

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

libtoga.php:

  • fix: removed php4 stripos(), use strpos() in stead
  • add: check for upper or lowercase {x} and {y}
  • Property svn:keywords set to Id
File size: 45.8 KB
Line 
1<?php
2/*
3 *
4 * This file is part of Jobmonarch
5 *
6 * Copyright (C) 2006  Ramon Bastiaans
7 *
8 * Jobmonarch is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * Jobmonarch is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 *
22 * SVN $Id: libtoga.php 464 2008-02-20 15:42:41Z 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                                                $domain         = $jobs[$jobid][domain];
698                                                $domain_len     = 0 - strlen( $domain );
699
700                                                // Let's see if Ganglia use's FQDN or short hostnames
701                                                //
702                                                foreach( $nodes as $hostname => $nimage ) {
703                                       
704                                                        if( substr( $hostname, $domain_len ) != $domain )
705                                                        {
706                                                                $this->fqdn     = 0;
707                                                        }
708                                                }
709
710                                                foreach( $jobs[$jobid][nodes] as $node ) {
711
712                                                        // Only add domain name to the hostname if Ganglia is doing that too
713                                                        //
714                                                        if( $this->fqdn )
715                                                        {
716                                                                if( substr( $node, $domain_len ) != $domain ) {
717                                                                        $host = $node. '.'.$domain;
718                                                                } else {
719                                                                        $host = $node;
720                                                                }
721                                                        }
722                                                        else
723                                                        {
724                                                                $host   = $node;
725                                                        }
726
727                                                        if( !isset( $nodes[$host] ) )
728                                                                $my_node = new NodeImage( $this->proc_cluster, $host );
729                                                        else
730                                                                $my_node = $nodes[$host];
731
732                                                        if( !$my_node->hasJob( $jobid ) )
733
734                                                                if( isset( $jobs[$jobid][ppn] ) )
735                                                                        $my_node->addJob( $jobid, ((int) $jobs[$jobid][ppn]) );
736                                                                else
737                                                                        $my_node->addJob( $jobid, 1 );
738
739                                                        $nodes[$host] = $my_node;
740                                                }
741                                        }
742                                }
743                        }
744                }
745                $this->jobs = $jobs;
746                //print_r( $nodes );
747                $this->nodes = $nodes;
748                //print_r( $this->nodes );
749        }
750
751        function stopElement( $parser, $name ) {
752        }
753
754        function printInfo() {
755
756                $jobs = &$this->jobs;
757
758                printf( "---jobs---\n" );
759
760                foreach( $jobs as $jobid => $job ) {
761
762                        printf( "job %s\n", $jobid );
763
764                        if( isset( $job[nodes] ) ) {
765
766                                foreach( $job[nodes] as $node ) {
767
768                                        $mynode = $this->nodes[$node];
769                                        $hostname = $mynode->getHostname();
770                                        $location = $mynode->getLocation();
771
772                                        printf( "\t- node %s\tlocation %s\n", $hostname, $location );
773                                        //$this->nodes[$hostname]->setLocation( "hier draait job ".$jobid );
774                                }
775                        }
776                }
777
778                printf( "---nodes---\n" );
779
780                $nodes = &$this->nodes;
781
782                foreach( $nodes as $node ) {
783
784                        $hostname = $node->getHostname();
785                        $location = $node->getLocation();
786                        $jobs = implode( ' ', $node->getJobs() );
787                        printf( "* node %s\tlocation %s\tjobs %s\n", $hostname, $location, $jobs );
788                }
789        }
790
791        function getNodes() {
792                //print_r( $this->nodes );
793                return $this->nodes;
794        }
795
796        function getNode( $node ) {
797
798                $nodes = &$this->nodes;
799                if( isset( $nodes[$node] ) )
800                        return $nodes[$node];
801                else
802                        return NULL;
803        }
804
805        function getJobs() {
806                return $this->jobs;
807        }
808
809        function getJob( $job ) {
810
811                $jobs = &$this->jobs;
812                if( isset( $jobs[$job] ) )
813                        return $jobs[$job];
814                else
815                        return NULL;
816        }
817
818        function getHeartbeat() {
819                return $this->heartbeat['time'];
820        }
821}
822
823class NodeImage {
824
825        var $image, $x, $y, $hostname, $jobs, $tasks, $showinfo;
826
827        function NodeImage( $cluster, $hostname ) {
828
829                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
830
831                $this->jobs = array();
832                //$this->image = $image;
833                //$this->x = $x;
834                //$this->y = $y;
835                $this->tasks = 0;
836                $this->hostname = $hostname;
837                $this->cpus = $this->determineCpus();
838                $this->clustername = $cluster;
839                $this->showinfo = 1;
840                $this->size = $SMALL_CLUSTERIMAGE_NODEWIDTH;
841        }
842
843        function addJob( $jobid, $cpus ) {
844                $jobs = &$this->jobs;
845
846                $jobs[] = $jobid;
847                $this->jobs = $jobs;
848
849                $this->addTask( $cpus );
850        }
851
852        function hasJob( $jobid ) {
853
854                $jobfound = 0;
855
856                if( count( $this->jobs ) > 0 )
857                        foreach( $this->jobs as $job )
858
859                                if( $job == $jobid )
860                                        $jobfound = 1;
861
862                return $jobfound;
863        }
864
865        function addTask( $cpus ) {
866
867                $this->tasks = $this->tasks + $cpus;
868        }
869
870        function setImage( $image ) {
871
872                $this->image = $image;
873        }
874
875        function setCoords( $x, $y ) {
876
877                $this->x = $x;
878                $this->y = $y;
879        }
880
881        function getImagemapArea() {
882
883                $area_topleft           = $this->x . "," . $this->y;
884                $area_bottomright       = ($this->x + $this->size) . "," . ($this->y + $this->size);
885                $area_coords            = $area_topleft . "," . $area_bottomright;
886
887                $area_href              = "./?c=" . $this->clustername . "&h=" . $this->hostname;
888                $area_tooltip           = $this->hostname . ": " . implode( " ", $this->jobs );
889
890                $tag_href               = "HREF=\"" . $area_href . "\"";
891                $tag_coords             = "COORDS=\"" . $area_coords . "\"";
892                $tag_tooltip1           = "ALT=\"" . $area_tooltip . "\"";
893                $tag_tooltip2           = "TITLE=\"" . $area_tooltip . "\"";
894
895                return ("<AREA SHAPE=\"RECT\" " . $tag_coords . " " . $tag_href . " " . $tag_tooltip1 . " " . $tag_tooltip2 . ">");
896        }
897
898        function colorHex( $color ) {
899       
900                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) );
901
902                return $my_color;
903        }
904
905        function setLoad( $load ) {
906                $this->load = $load;
907        }
908
909        function setHostname( $hostname ) {
910                $this->hostname = $hostname;
911        }
912
913        function getHostname() {
914                return $this->hostname;
915        }
916
917        function getJobs() {
918                return $this->jobs;
919        }
920
921        function setShowinfo( $showinfo ) {
922                $this->showinfo = $showinfo;
923        }
924
925        function drawSmall() {
926
927                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
928
929                $this->size     = $SMALL_CLUSTERIMAGE_NODEWIDTH;
930
931                $this->draw();
932        }
933
934        function drawBig() {
935
936                global $BIG_CLUSTERIMAGE_NODEWIDTH;
937
938                $this->size     = $BIG_CLUSTERIMAGE_NODEWIDTH;
939
940                $this->draw();
941        }
942
943        function draw() {
944
945                global $JOB_NODE_MARKING;
946
947                $black_color = imageColorAllocate( $this->image, 0, 0, 0 );
948                $size = $this->size;
949
950                imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+($size), $this->y+($size), $black_color );
951
952                if( $this->showinfo) {
953               
954                        $this->load = $this->determineLoad();
955
956                        if( !isset( $this->image ) or !isset( $this->x ) or !isset( $this->y ) ) {
957                                printf( "aborting\n" );
958                                printf( "x %d y %d load %f\n", $this->x, $this->y, $load );
959                                return;
960                        }
961
962
963                        // Convert Ganglias Hexadecimal load color to a Decimal one
964                        //
965                        $load = $this->determineLoad(); 
966                        $usecolor = $this->colorHex( load_color($load) );
967                        imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor );
968                        if( count( $this->jobs ) > 0 )
969                                imageString( $this->image, 1, $this->x+(($size/2)-1), $this->y+(($size/2)-4), $JOB_NODE_MARKING, $black_color );
970
971                } else {
972
973                        // White
974                        $usecolor = imageColorAllocate( $this->image, 255, 255, 255 );
975                        imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor );
976                }
977
978
979        }
980
981        function determineCpus() {
982
983                global $metrics;
984
985                $cpus = $metrics[$this->hostname][cpu_num][VAL];
986                if (!$cpus) $cpus=1;
987
988                return $cpus;
989        }
990
991        function determineLoad() {
992
993                global $metrics;
994
995                $load_one = $metrics[$this->hostname][load_one][VAL];
996                $load = ((float) $load_one)/$this->cpus;
997
998                return $load;
999        }
1000}
1001
1002class ClusterImage {
1003
1004        var $dataget, $image, $clustername;
1005        var $filtername, $filters;
1006
1007        //function ClusterImage( $clustername ) {
1008        function ClusterImage( $data, $clustername ) {
1009
1010                //$this->dataget                = $dataget;
1011                $this->dataget          = new DataGatherer( $clustername );
1012                $this->data             = $data;
1013                $this->clustername      = $clustername;
1014                $this->filters          = array();
1015                $this->size             = 's';
1016                $this->width            = 0;
1017                $this->height           = 0;
1018                $this->output           = 1;
1019        }
1020
1021        function getWidth() {
1022                return $this->width;
1023        }
1024        function getHeight() {
1025                return $this->height;
1026        }
1027
1028        function setSmall() {
1029                $this->size     = 's';
1030        }
1031
1032        function setBig() {
1033                $this->size     = 'b';
1034        }
1035
1036        function setNoimage() {
1037                $this->output   = 0;
1038        }
1039
1040        function isSmall() {
1041                return ($this->size == 's');
1042        }
1043
1044        function isBig() {
1045                return ($this->size == 'b');
1046        }
1047
1048        function setFilter( $filtername, $filtervalue ) {
1049
1050                $this->filters[$filtername] = $filtervalue;
1051        }
1052
1053        function filterNodes( $jobs, $nodes ) {
1054
1055                $filtered_nodes = array();
1056
1057                foreach( $nodes as $node ) {
1058
1059                        $hostname = $node->getHostname();
1060
1061                        $addhost = 1;
1062
1063                        if( count( $this->filters ) > 0 ) {
1064
1065                                $mynjobs = $node->getJobs();
1066
1067                                if( count( $mynjobs ) > 0 ) {
1068
1069                                        foreach( $mynjobs as $myjob ) {
1070
1071                                                foreach( $this->filters as $filtername => $filtervalue ) {
1072
1073                                                        if( $filtername!=null && $filtername!='' ) {
1074
1075                                                                if( $filtername == 'jobid' && !$node->hasJob( $filtervalue) ) {
1076                                                                        $addhost = 0;
1077                                                                } else if( $filtername != 'jobid' ) {
1078                                                                        if( $jobs[$myjob][$filtername] != $filtervalue ) {
1079                                                                                $addhost = 0;
1080                                                                        }
1081                                                                }
1082                                                        }
1083                                                }
1084                                        }
1085                                } else
1086                                        $addhost = 0;
1087                        }
1088
1089                        if( $addhost )
1090                                $filtered_nodes[] = $hostname;
1091                }
1092
1093                return $filtered_nodes;
1094        }
1095
1096        function draw() {
1097
1098                global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH;
1099                global $BIG_CLUSTERIMAGE_MAXWIDTH, $BIG_CLUSTERIMAGE_NODEWIDTH;
1100                global $CLUSTER_CONFS, $confcluster;
1101
1102                global $SORTBY_HOSTNAME, $SORT_ORDER, $skan_str;
1103                global $x_first, $y_first;
1104
1105                foreach( $CLUSTER_CONFS as $confcluster => $conffile )
1106                {
1107                        if( strtolower( trim($this->clustername) ) == strtolower(trim($confcluster)) )
1108                        {
1109                                include_once $conffile;
1110                        }
1111                }
1112
1113                //global $SORTBY_HOSTNAME, $SORT_ORDER;
1114                //global $SORT_XLABEL, $SORT_YLABEL;
1115       
1116                //printf( "SORTBY_HOSTNAME %s SORT_YLABEL %s\n", $SORTBY_HOSTNAME, $SORT_YLABEL );
1117
1118                $mydatag = $this->dataget;
1119                $mydatag->parseXML( $this->data );
1120
1121                if( $this->isSmall() ) {
1122                        $max_width = $SMALL_CLUSTERIMAGE_MAXWIDTH;
1123                        $node_width = $SMALL_CLUSTERIMAGE_NODEWIDTH;
1124                } else if( $this->isBig() ) {
1125                        $max_width = $BIG_CLUSTERIMAGE_MAXWIDTH;
1126                        $node_width = $BIG_CLUSTERIMAGE_NODEWIDTH;
1127                }
1128
1129                $nodes = $mydatag->getNodes();
1130                $nodes_hosts = array_keys( $nodes );
1131
1132                $nodes_nr = count( $nodes );
1133
1134                $nodes_size = $nodes_nr*$node_width;
1135                $node_rows = 0;
1136
1137                if( $nodes_size > $max_width ) {
1138                        $nodes_per_row = ( (int) ($max_width/$node_width) );
1139                } else {
1140                        $nodes_per_row = $nodes_size;
1141                        $node_rows = 1;
1142                }
1143
1144                if( $nodes_per_row < $nodes_nr ) {
1145                        $node_rows = ( (int) ($nodes_nr/$nodes_per_row) );
1146                        $node_rest = fmod( $nodes_nr, $nodes_per_row );
1147                        //printf( "nodesnr %d noderest %f\n", $nodes_nr, $node_rest );
1148                        if( $node_rest > 0 ) {
1149                                $node_rows++;
1150                                //printf( "noderows %d\n", $node_rows );
1151                        }
1152                }
1153
1154                $y_offset       = 0;
1155                $font           = 2;
1156                $fontwidth      = ImageFontWidth( $font );
1157                $fontheight     = ImageFontHeight( $font );
1158                $fontspaceing   = 2;
1159                $y_offset       = $fontheight + (2 * $fontspaceing);
1160
1161                $this->width    = $max_width;
1162                $this->height   = ($y_offset + (($node_rows*$node_width)+1) );
1163
1164                //$image = imageCreateTrueColor( $max_width, ($y_offset + (($node_rows*$node_width)+1) ) );
1165                //$colorwhite = imageColorAllocate( $image, 255, 255, 255 );
1166                //imageFill( $image, 0, 0, $colorwhite );
1167
1168                //if( $this->isSmall() ) {
1169
1170                //      $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1171
1172                //      imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1173                //}
1174
1175                $jobs = $mydatag->getJobs();
1176                //printf("filtername = %s\n", $filtername );
1177                $filtered_nodes = $this->filterNodes( $jobs, $nodes );
1178
1179                //print_r($filtered_nodes);
1180
1181                if( $SORTBY_HOSTNAME != "" )
1182                {
1183
1184                        $sorted         = array();
1185
1186                        $x_first        = 0;
1187                        $y_first        = 0;
1188
1189                        $skan_str       = $SORTBY_HOSTNAME;
1190
1191                        global $x_present, $y_present;
1192                        $x_present      = false;
1193                        $y_present      = false;
1194
1195                        // Should we scan by X, Y or both
1196                        //
1197                        if(strpos( $SORTBY_HOSTNAME, "{x}" ) != false )
1198                        {
1199                                $x_str          = "{x}";
1200                                $x_present      = true;
1201                        }
1202                        else if(strpos( $SORTBY_HOSTNAME, "{X}" ) != false )
1203                        {
1204                                $x_str          = "{X}";
1205                                $x_present      = true;
1206                        }
1207                        if(strpos( $SORTBY_HOSTNAME, "{y}" ) != false )
1208                        {
1209                                $y_str          = "{y}";
1210                                $y_present      = true;
1211                        }
1212                        else if(strpos( $SORTBY_HOSTNAME, "{Y}" ) != false )
1213                        {
1214                                $y_str          = "{Y}";
1215                                $y_present      = true;
1216                        }
1217
1218                        // If we should scan for both X and Y: see which one is first
1219                        //
1220                        if(( strpos( $SORTBY_HOSTNAME, $x_str ) < strpos( $SORTBY_HOSTNAME, $y_str ) ) && ( $x_present && $y_present ))
1221                        {
1222                       
1223                                $x_first        = 1;
1224                        }
1225                        else if(( strpos( $SORTBY_HOSTNAME, $x_str ) > strpos( $SORTBY_HOSTNAME, $y_str ) ) && ( $x_present && $y_present ))
1226                        {
1227                                $y_first        = 1;
1228               
1229                        }
1230                        else if( $x_present )
1231                        {
1232                                $x_first        = 1;
1233                        }
1234                        else if( $y_present )
1235                        {
1236                                $y_first        = 1;
1237                        }
1238
1239                        // Now replace our {x} and {y} with %d for sscanf parsing
1240                        //
1241                        if(( $x_first ) && ( $x_present && $y_present ) )
1242                        {
1243                                $skan_str       = str_replace( $x_str, "%d", $skan_str );
1244                                $skan_str       = str_replace( $y_str, "%d", $skan_str );
1245                        } 
1246                        else if( $x_present)
1247                        {
1248                                $skan_str       = str_replace( $x_str, "%d", $skan_str );
1249                        }
1250                        else if( $y_present)
1251                        {
1252                                $skan_str       = str_replace( $y_str, "%d", $skan_str );
1253                        }
1254
1255                        $x_min          = null;
1256                        $x_max          = null;
1257                        $y_min          = null;
1258                        $y_max          = null;
1259
1260                        // Now let's walk through all our nodes and see which one are valid for our scan pattern
1261                        //
1262                        foreach( $nodes as $hostname => $node )
1263                        {
1264                                $x      = null;
1265                                $y      = null;
1266
1267                                if( $x_present && $y_present )
1268                                {
1269                                        if( $x_first )
1270                                        {
1271                                                $n = sscanf( $hostname, $skan_str, $x, $y );
1272                                        }
1273                                        else if( $y_first )
1274                                        {
1275                                                $n = sscanf( $hostname, $skan_str, $y, $x );
1276                                        }
1277
1278                                        // Remove nodes that don't match
1279                                        //
1280                                        if( $n < 2 )
1281                                        {
1282                                                // This node hostname has no match for: {x} and {y}
1283                                                //
1284                                                unset( $nodes[$hostname] );
1285                                        }
1286                                }
1287                                else if( $x_present && !$y_present )
1288                                {
1289                                        $n = sscanf( $hostname, $skan_str, $x );
1290
1291                                        // Remove nodes that don't match
1292                                        //
1293                                        if( $n < 1 )
1294                                        {
1295                                                // This node hostname has no match for: {x}
1296                                                //
1297                                                unset( $nodes[$hostname] );
1298                                        }
1299                                        $y      = 1;
1300                                }
1301                                else if( $y_present && !$x_present )
1302                                {
1303                                        $n = sscanf( $hostname, $skan_str, $y );
1304
1305                                        // Remove nodes that don't match
1306                                        //
1307                                        if( $n < 1 )
1308                                        {
1309                                                // This node hostname has no match for: {y}
1310                                                //
1311                                                unset( $nodes[$hostname] );
1312                                        }
1313                                        $x      = 1;
1314                                }
1315
1316                                // Determine the lowest value of {x} that exists in all node hostnames
1317                                //
1318                                if( !$x_min && $x != null )
1319                                {
1320                                        $x_min  = $x;
1321                                }
1322                                else if( $x < $x_min && $x != null )
1323                                {
1324                                        $x_min  = $x;
1325                                }
1326
1327                                // Determine the highest value of {x} that exists in all node hostnames
1328                                //
1329                                if( !$x_max && $x != null )
1330                                {
1331                                        $x_max  = $x;
1332                                }
1333                                else if( $x > $x_max && $x != null )
1334                                {
1335                                        $x_max  = $x;
1336                                }
1337
1338                                // Determine the lowest value of {y} that exists in all node hostnames
1339                                //
1340                                if( !$y_min && $y != null )
1341                                {
1342                                        $y_min  = $y;
1343                                }
1344                                else if( $y < $y_min && $y != null )
1345                                {
1346                                        $y_min  = $y;
1347                                }
1348
1349                                // Determine the highest value of {y} that exists in all node hostnames
1350                                //
1351                                if( !$y_max && $y != null )
1352                                {
1353                                        $y_max  = $y;
1354                                }
1355                                else if( $y > $y_max && $y != null )
1356                                {
1357                                        $y_max  = $y;
1358                                }
1359                        }
1360
1361                        // Sort all the nodes (alpha and numerically)
1362                        // 1: gb-r1n1, 2: gb-r1n2, 3: gb-r2n1, etc
1363                        //
1364                        $sorted_nodes   = usort( $nodes, "cmp" );
1365
1366                        $cur_node       = 0;
1367
1368                        $x_offset       = 0;
1369                        $y_offset       = 0;
1370                        $font           = 2;
1371                        $fontwidth      = ImageFontWidth( $font );
1372                        $fontheight     = ImageFontHeight( $font );
1373                        $fontspaceing   = 2;
1374
1375                        if( $this->isSmall() ) 
1376                        {
1377                                $y_offset       = $y_offset + (2 * $fontspaceing) + $fontheight;
1378                        }
1379
1380                        if( $this->isBig() ) 
1381                        {
1382
1383                                $y_offset       = ($fontheight * (1 + strlen( $x_max) ) ) + ((2 + strlen( $x_max)) * $fontspaceing);
1384                                $x_offset       = ($fontwidth * (1 + strlen( $y_max) ) ) + ((2 + strlen( $y_max)) * $fontspaceing);
1385
1386                        }
1387                        //$x_offset     = ($fontwidth * 3) + (5 * $fontspaceing);
1388
1389                        //printf( "xmin %s xmax %s\n", $x_min, $x_max );
1390                        //printf( "ymin %s ymax %s\n", $y_min, $y_max );
1391
1392                        // werkt
1393                        //print_r( $nodes );
1394
1395                        $image_width    = $x_offset + ($node_width * ($x_max-$x_min+2));
1396
1397                        if( $this->isSmall() ) 
1398                        {
1399                                $image_width    = $max_width;
1400                        } else if( $this->isBig() ) 
1401                        {
1402                                $image_width    = ($image_width < $max_width) ? $image_width : $max_width;
1403                        }
1404                        //else if( $this->isSmall() )
1405                        //{
1406                        //      $image_width    = $this->width;
1407                        //}
1408                        $image_height   = $y_offset + ($node_width * ($y_max-$y_min+2));
1409
1410                        $this->width    = $image_width;
1411                        $this->heigth   = $image_heigth;
1412
1413                        $image          = imageCreateTrueColor( $image_width, $image_height );
1414                        $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1415
1416                        imageFill( $image, 0, 0, $colorwhite );
1417
1418                        if( $this->isSmall() ) {
1419
1420                                // Draw a fancy little header text to explain what it is
1421                                //
1422                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1423
1424                                imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1425                        }
1426
1427                        if( $this->isBig() && ( isset( $SORT_XLABEL ) || isset( $SORT_YLABEL ) ) )
1428                        {
1429                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1430
1431                                if( isset( $SORT_XLABEL ) )
1432                                {
1433                                        // Print the {x} label: rack
1434                                        //
1435                                        imageString( $image, $font, $x_offset, $fontspaceing, $SORT_XLABEL, $colorblue );
1436                                }
1437
1438                                if( isset( $SORT_YLABEL ) )
1439                                {
1440                                        // Stupid php without imageStringDown function... we'll make one ourself
1441                                        //
1442
1443                                        // Print the {y} label: node
1444                                        //
1445                                        imageStringDown( $image, $font, $fontspaceing, $y_offset, $SORT_YLABEL, $colorblue );
1446                                }
1447                        }
1448
1449                        for( $n = $x_min; $n <= $x_max; $n++ )
1450                        {
1451                                for( $m = $y_min; $m <= $y_max; $m++ )
1452                                {
1453
1454                                        if( $x_min > 0 )
1455                                        {
1456                                                $x      = $x_offset + ( ($n-$x_min) * $node_width );
1457                                        }
1458                                        if( $y_min > 0 )
1459                                        {
1460                                                $y      = $y_offset + ( ($m-$y_min) * $node_width );
1461                                        }
1462
1463                                        if( $this->isBig() ) 
1464                                        {
1465                                                // Draw y(node) column number header
1466                                                //
1467                                                if(( $n == $x_min ) && ( isset($SORT_YLABEL) ) )
1468                                                {
1469                                                        $mfontspacing   = 1;
1470
1471                                                        $ylabel_x       = $x - ( $fontwidth * strlen( $y_max ) ) - $mfontspacing;
1472                                                        $ylabel_y       = $y;
1473
1474                                                        imageString( $image, $font, $ylabel_x, $ylabel_y, strval( $m ), $colorblue );
1475
1476                                                        $xmin_hit[$n]   = true;
1477                                                }
1478
1479                                                // Draw x(rack) column number header
1480                                                //
1481                                                if(( $m == $y_min ) && ( isset($SORT_XLABEL) ) )
1482                                                {
1483                                                        $mfontspacing   = 2;
1484                                                        $xlabel_y       = $y - ( $fontheight * strlen( $x_max ) );
1485                                                        $xlabel_x       = $x + $mfontspacing; 
1486
1487                                                        imageStringDown( $image, $font, $xlabel_x, $xlabel_y, strval( $n ), $colorblue );
1488                                                }
1489                                        }
1490
1491                                        if( isset( $nodes[$cur_node] ) ) 
1492                                        {
1493                                                $host   = $nodes[$cur_node]->getHostname();
1494
1495                                                if( $x_present && $y_present )
1496                                                {
1497                                                        if( $x_first )
1498                                                        {
1499                                                                $nn = sscanf( $host, $skan_str, $rx, $ry );
1500                                                        }
1501                                                        else if( $y_first )
1502                                                        {
1503                                                                $nn = sscanf( $host, $skan_str, $ry, $rx );
1504                                                        }
1505                                                        if ( $nn < 2 )
1506                                                        {
1507                                                                //printf( "skipping node %s - y present & x present + <2 x,y matchs\n", $host);
1508                                                                continue;
1509                                                        }
1510                                                        if( intval( $rx ) > $n )
1511                                                        {
1512                                                                // If x(rack) is higher than current x, skip to next x(rack)
1513                                                                //
1514                                                                $m              = $y_max + 1;
1515
1516                                                                continue;
1517                                                        }
1518                                                        if( intval( $ry ) > $m )
1519                                                        {
1520                                                                // If y(node) is higher than current y, skip to next y(node)
1521                                                                //
1522                                                                continue;
1523                                                        }
1524                                                }
1525                                                else if( $x_present )
1526                                                {
1527                                                        $nn = sscanf( $host, $skan_str, $rx );
1528                                                }
1529                                                else if( $y_present )
1530                                                {
1531                                                        $nn = sscanf( $host, $skan_str, $ry );
1532                                                }
1533
1534                                                if( !in_array( $host, $filtered_nodes ) )
1535                                                {
1536                                                        // This node has been filtered out: we only want to see certain nodes
1537                                                        //
1538                                                        $nodes[$cur_node]->setShowinfo( 0 );
1539                                                }
1540
1541                                                $nodes[$cur_node]->setCoords( $x, $y );
1542                                                $nodes[$cur_node]->setImage( $image );
1543
1544                                                //print_r( $nodes[$cur_node] );
1545
1546                                                if( $this->isSmall() )
1547                                                        $nodes[$cur_node]->drawSmall();
1548                                                else if( $this->isBig() )
1549                                                        $nodes[$cur_node]->drawBig();
1550
1551                                                $cur_node++;
1552                                        }
1553                                }
1554                        }
1555
1556                }
1557                else
1558                {
1559                        if( $this->isSmall() ) {
1560                                $image          = imageCreateTrueColor( $max_width, ($y_offset + (($node_rows*$node_width)+1) ) );
1561                        } else if( $this->isBig() ) {
1562                                $image_width    = ($node_width * $nodes_nr) + 2;
1563                                $image_width    = ($image_width < $max_width) ? $image_width : $max_width;
1564                                $image          = imageCreateTrueColor( $image_width, ($y_offset + (($node_rows*$node_width)+1) ) );
1565                        }
1566                        $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1567
1568                        imageFill( $image, 0, 0, $colorwhite );
1569
1570                        if( $this->isSmall() ) {
1571
1572                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1573
1574                                imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1575                        }
1576
1577                        for( $n = 0; $n < $node_rows; $n++ ) {
1578                       
1579                                for( $m = 0; $m < $nodes_per_row; $m++ ) {
1580                       
1581                                        $x = ($m * $node_width);
1582                                        $y = $y_offset + ($n * $node_width);
1583
1584                                        $cur_node = ($n * $nodes_per_row) + ($m);
1585                                        $host = $nodes_hosts[$cur_node];
1586
1587                                        if( isset( $nodes[$host] ) ) {
1588
1589                                                $nodes[$host]->setCoords( $x, $y );
1590                                                $nodes[$host]->setImage( $image );
1591
1592                                                if( !in_array( $host, $filtered_nodes ) )
1593                                                {
1594                                                        $nodes[$host]->setShowinfo( 0 );
1595                                                }
1596
1597                                                if( $this->isSmall() )
1598                                                        $nodes[$host]->drawSmall();
1599                                                else if( $this->isBig() )
1600                                                        $nodes[$host]->drawBig();
1601                                        }
1602                                }
1603                        }
1604                }
1605       
1606                $this->nodes    = &$nodes;
1607
1608                if ($this->output) {
1609                        header( 'Content-type: image/png' );
1610                        imagePNG( $image );
1611                        imageDestroy( $image );
1612                }
1613        }
1614
1615        function getImagemapArea() {
1616
1617                $clusterimage_map       = "";
1618
1619                foreach( $this->nodes as $hostname => $node ) {
1620
1621                        $node_map               = $node->getImagemapArea();
1622                        $clusterimage_map       .= $node_map;
1623                }
1624
1625                return $clusterimage_map;
1626        }
1627}
1628
1629class EmptyImage {
1630
1631        function draw() {
1632                $image          = imageCreateTrueColor( 1, 1 );
1633                $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1634                imageFill( $image, 0, 0, $colorwhite );                         
1635
1636                header( 'Content-type: image/png' );
1637                imagePNG( $image );
1638                imageDestroy( $image );
1639        }
1640}
1641
1642class HostImage {
1643
1644        var $data_gather, $cluster, $host, $node, $image;
1645        var $headerstrlen;
1646
1647        function HostImage( $data_gather, $cluster, $host ) {
1648
1649                $this->data_gather      = $data_gather;
1650                $this->cluster          = $cluster;
1651                $this->host             = $host;
1652                $this->y_offset         = 0;
1653                $this->font             = 2;
1654                $this->fontspaceing     = 2;
1655                $this->headerstrlen     = array();
1656
1657                $this->fontheight       = ImageFontHeight( $this->font );
1658                $this->fontwidth        = ImageFontWidth( $this->font );
1659
1660                $dg                     = &$this->data_gather;
1661                $this->node             = &$dg->getNode( $this->host );
1662                $n                      = &$this->node;
1663                $this->njobs            = $n->getJobs();
1664        }
1665
1666        function drawJobs() {
1667
1668                $dg                     = &$this->data_gather;
1669                $colorblack             = imageColorAllocate( $this->image, 0, 0, 0 );
1670
1671                for( $n = 0; $n < count( $this->njobs ); $n++ ) {
1672
1673                        $jobid                  = $this->njobs[$n];
1674                        $jobinfo                = $dg->getJob( $jobid );
1675
1676                        $xoffset                = 5;
1677                        imageString( $this->image, $this->font, $xoffset, $this->y_offset, strval( $jobid ), $colorblack );
1678
1679                        foreach( $this->headerstrlen as $headername => $headerlen ) {
1680
1681                                if( $headername == 'nodes' ) {
1682                                        $attrval        = strval( count( $jobinfo[nodes] ) );
1683                                } else if( $headername == 'cpus' ) {
1684
1685                                        if( !isset( $jobinfo[ppn] ) )
1686                                                $jobinfo[ppn] = 1;
1687
1688                                        $attrval        = strval( count( $jobinfo[nodes] ) * intval( $jobinfo[ppn] ) );
1689
1690                                } else if( $headername == 'runningtime' ) {
1691                                        $attrval        = makeTime( intval( $jobinfo[reported] ) - intval( $jobinfo[start_timestamp] ) );
1692                                } else {
1693                                        $attrval        = strval( $jobinfo[$headername] );
1694                                }
1695
1696                                imageString( $this->image, $this->font, $xoffset, $this->y_offset, $attrval, $colorblack );
1697               
1698                                $xoffset        = $xoffset + ($this->fontwidth * ( $headerlen + 1 ) );
1699
1700                        }
1701                       
1702                        $this->newLineOffset();
1703                }
1704        }
1705
1706        function drawHeader() {
1707
1708                $dg                     = &$this->data_gather;
1709
1710                for( $n = 0; $n < count( $this->njobs ); $n++ ) {
1711
1712                        $jobid                  = $this->njobs[$n];
1713                        $jobinfo                = $dg->getJob( $jobid );
1714
1715                        if( !isset( $this->headerstrlen[id] ) )
1716                                $this->headerstrlen[id] = strlen( strval( $jobid ) );
1717                        else
1718                                if( strlen( strval( $jobid ) ) > $this->headerstrlen[id] )
1719                                        $this->headerstrlen[id] = strlen( strval( $jobid ) );
1720
1721                        if( !isset( $this->headerstrlen[owner] ) )
1722                                $this->headerstrlen[owner]      = strlen( strval( $jobinfo[owner] ) );
1723                        else
1724                                if( strlen( strval( $jobinfo[owner] ) ) > $this->headerstrlen[owner] )
1725                                        $this->headerstrlen[owner]      = strlen( strval( $jobinfo[owner] ) );
1726
1727                        if( !isset( $this->headerstrlen[queue] ) )
1728                                $this->headerstrlen[queue]      = strlen( strval( $jobinfo[queue] ) );
1729                        else
1730                                if( strlen( strval( $jobinfo[queue] ) ) > $this->headerstrlen[queue] )
1731                                        $this->headerstrlen[queue]      = strlen( strval( $jobinfo[queue] ) );
1732
1733                        if( !isset( $jobinfo[ppn] ) )
1734                                $jobinfo[ppn] = 1;
1735
1736                        $cpus                   = count( $jobinfo[nodes] ) * intval( $jobinfo[ppn] );
1737
1738                        if( !isset( $this->headerstrlen[cpus] ) )
1739                                $this->headerstrlen[cpus]       = strlen( strval( $cpus ) );
1740                        else
1741                                if( strlen( strval( $cpus ) ) > $this->headerstrlen[cpus] )
1742                                        $this->headerstrlen[cpus]       = strlen( strval( $cpus ) );
1743
1744                        $nodes                  = count( $jobinfo[nodes] );
1745
1746                        if( !isset( $this->headerstrlen[nodes] ) )
1747                                $this->headerstrlen[nodes]      = strlen( strval( $nodes ) );
1748                        else
1749                                if( strlen( strval( $nodes) ) > $this->headerstrlen[nodes] )
1750                                        $this->headerstrlen[nodes]      = strlen( strval( $nodes ) );
1751
1752                        $runningtime            = makeTime( intval( $jobinfo[reported] ) - intval( $jobinfo[start_timestamp] ) );
1753
1754                        if( !isset( $this->headerstrlen[runningtime] ) )
1755                                $this->headerstrlen[runningtime]        = strlen( strval( $runningtime) );
1756                        else
1757                                if( strlen( strval( $runningtime) ) > $this->headerstrlen[runningtime] )
1758                                        $this->headerstrlen[runningtime]        = strlen( strval( $runningtime) );
1759
1760                        if( !isset( $this->headerstrlen[name] ) )
1761                                $this->headerstrlen[name]       = strlen( strval( $jobinfo[name] ) );
1762                        else
1763                                if( strlen( strval( $jobinfo[name] ) ) > $this->headerstrlen[name] )
1764                                        $this->headerstrlen[name]       = strlen( strval( $jobinfo[name] ) );
1765
1766                }
1767
1768                $xoffset        = 5;
1769
1770                foreach( $this->headerstrlen as $headername => $headerlen ) {
1771
1772                        $colorgreen     = imageColorAllocate( $this->image, 0, 200, 0 );
1773
1774                        if( $headerlen < strlen( $headername ) )
1775                                $this->headerstrlen[$headername]        = strlen( $headername );
1776
1777                        imageString( $this->image, $this->font, $xoffset, $this->y_offset, ucfirst( $headername ), $colorgreen );
1778
1779                        $xoffset        = $xoffset + ($this->fontwidth * ( $this->headerstrlen[$headername] + 1 ) );
1780
1781                }
1782                $this->newLineOffset();
1783        }
1784
1785        function newLineOffset() {
1786
1787                $this->y_offset         = $this->y_offset + $this->fontheight + $this->fontspaceing;
1788        }
1789
1790        function draw() {
1791
1792                $xlen           = 450;
1793                $ylen           = ( count( $this->njobs ) * ( $this->fontheight + $this->fontspaceing ) ) + (3 * $this->fontheight);
1794
1795                $this->image    = imageCreateTrueColor( $xlen, $ylen );
1796                $colorwhite     = imageColorAllocate( $this->image, 255, 255, 255 );
1797                imageFill( $this->image, 0, 0, $colorwhite );                         
1798
1799                $colorblue      = imageColorAllocate( $this->image, 0, 0, 255 );
1800
1801                imageString( $this->image, $this->font, 1, $this->y_offset, "Monarch Joblist - host: ".$this->host, $colorblue );
1802                $this->newLineOffset();
1803
1804                $this->drawHeader();
1805                $this->drawJobs();
1806
1807                header( 'Content-type: image/png' );
1808                imagePNG( $this->image );
1809                imageDestroy( $this->image );
1810        }
1811}
1812
1813function imageStringDown( &$image, $font, $x, $y, &$s, &$col )
1814{
1815        $fw     = imagefontwidth( $font);
1816        $fh     = imagefontheight( $font);
1817       
1818        $fontspacing = 0;
1819
1820        $fx     = $x;
1821        $fy     = $y;
1822
1823        for( $n=0; $n<strlen( $s ); $n++ )
1824        {
1825                $myc    = $s{$n};
1826
1827                imagestring( $image, $font, $fx, $fy, $myc, $col );
1828
1829                $fy     += ($fontspacing + $fh );
1830        }
1831}
1832
1833function array_rem( $val, &$arr )
1834{
1835        // Delete val from arr
1836        //
1837        $i      = array_search( $val, $arr );
1838
1839        if( $i == false ) return false;
1840
1841        $arr    = array_merge( array_slice( $arr, 0, $i ), array_slice( $arr, $i+1, count( $arr ) ) );
1842
1843        return true;
1844}
1845
1846function cmp( $a, $b ) 
1847{
1848        global $SORT_ORDER;
1849        global $skan_str;
1850        global $x_first, $y_first;
1851        global $x_present, $y_present;
1852
1853        //printf("ppoep = %s\n", $skan_str);
1854        $a_node         = $a;
1855        $b_node         = $b;
1856        $a              = $a_node->getHostname();
1857        $b              = $b_node->getHostname();
1858
1859        if( $a == $b ) return 0;
1860
1861        $a_x            = 0;
1862        $b_x            = 0;
1863        $a_y            = 0;
1864        $b_y            = 0;
1865
1866        if( $x_present && $y_present )
1867        {
1868                if( $x_first )
1869                {
1870                        $n = sscanf( $a, $skan_str, $a_x, $a_y );
1871                        $n = sscanf( $b, $skan_str, $b_x, $b_y );
1872                }
1873                else if( $y_first )
1874                {
1875                        $n = sscanf( $a, $skan_str, $a_y, $a_x );
1876                        $n = sscanf( $b, $skan_str, $b_y, $b_x );
1877                }
1878        } 
1879        else if( $x_present && !$y_present )
1880        {
1881                $n = sscanf( $a, $skan_str, $a_x );
1882                $n = sscanf( $b, $skan_str, $b_x );
1883        }
1884        else if( $y_present && !$x_present )
1885        {
1886                $n = sscanf( $a, $skan_str, $a_y );
1887                $n = sscanf( $b, $skan_str, $b_y );
1888        }
1889
1890        if ( $SORT_ORDER=="desc" )
1891        {
1892
1893                if( $x_present && $y_present )
1894                {
1895                        // 1  = a < b
1896                        // -1 = a > b
1897                        //
1898                        if ($a_x == $b_x)
1899                        {
1900                                if ($a_y < $b_y)
1901                                {
1902                                        return 1;
1903                                }
1904                                else if ($a_y > $b_y)
1905                                {
1906                                        return -1;
1907                                }
1908                        }
1909                        else if ($a_x < $b_x)
1910                        {
1911                                return 1;
1912                        }
1913                        else if ($a_x > $b_x)
1914                        {
1915                                return -1;
1916                        }
1917                } 
1918                else if( $x_present && !$y_present )
1919                {
1920                        if ($a_x < $b_x)
1921                        {
1922                                return 1;
1923                        }
1924                        else if ($a_x > $b_x)
1925                        {
1926                                return -1;
1927                        }
1928                }
1929                else if( $y_present && !$x_present )
1930                {
1931                        if ($a_y < $b_y)
1932                        {
1933                                return 1;
1934                        }
1935                        else if ($a_y > $b_y)
1936                        {
1937                                return -1;
1938                        }
1939                }
1940        }
1941        else if ( $SORT_ORDER == "asc" )
1942        {
1943
1944                if( $x_present && $y_present )
1945                {
1946                        // 1  = a > b
1947                        // -1 = a < b
1948                        //
1949                        if ($a_x == $b_x)
1950                        {
1951                                if ($a_y > $b_y)
1952                                {
1953                                        return 1;
1954                                }
1955                                else if ($a_y < $b_y)
1956                                {
1957                                        return -1;
1958                                }
1959                        }
1960                        else if ($a_x > $b_x)
1961                        {
1962                                return 1;
1963                        }
1964                        else if ($a_x < $b_x)
1965                        {
1966                                return -1;
1967                        }
1968                }
1969                else if( $x_present && !$y_present )
1970                {
1971                        if ($a_x > $b_x)
1972                        {
1973                                return 1;
1974                        }
1975                        else if ($a_x < $b_x)
1976                        {
1977                                return -1;
1978                        }
1979                }
1980                else if( $y_present && !$x_present )
1981                {
1982                        if ($a_y > $b_y)
1983                        {
1984                                return 1;
1985                        }
1986                        else if ($a_y < $b_y)
1987                        {
1988                                return -1;
1989                        }
1990                }
1991        }
1992}
1993function makeTime( $time ) {
1994
1995        $days = intval( $time / 86400 );
1996        $time = ($days>0) ? $time % ($days * 86400) : $time;
1997
1998        //printf( "time = %s, days = %s\n", $time, $days );
1999
2000        $date_str = '';
2001        $day_str = '';
2002
2003        if( $days > 0 ) {
2004                if( $days > 1 )
2005                        $day_str .= $days . ' days';
2006                else
2007                        $day_str .= $days . ' day';
2008        }
2009
2010        $hours = intval( $time / 3600 );
2011        $time = $hours ? $time % ($hours * 3600) : $time;
2012
2013        //printf( "time = %s, days = %s, hours = %s\n", $time, $days, $hours );
2014        if( $hours > 0 ) {
2015                $date_str .= $hours . ':';
2016                $date_unit = 'hours'; 
2017        }
2018
2019        $minutes = intval( $time / 60 );
2020        $seconds = $minutes ? $time % ($minutes * 60) : $time;
2021
2022        if( $minutes > 0 ) {
2023
2024                if( $minutes >= 10 )
2025                        $date_str .= $minutes . ':';
2026                else
2027                        $date_str .= '0' . $minutes . ':';
2028
2029                $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
2030        } else {
2031                if($hours > 0 ) {
2032                        $date_str .= '00:';
2033                        $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
2034                }
2035        }
2036
2037
2038        $date_unit = (!isset($date_unit)) ? 'seconds' : $date_unit;
2039
2040        if( $seconds > 0 ) {
2041
2042                if( $seconds >= 10 )
2043                        $date_str .= $seconds . ' ' . $date_unit;
2044                else
2045                        $date_str .= '0' . $seconds . ' ' . $date_unit;
2046
2047        } else if ( $hours > 0 or $minutes > 0 )
2048
2049                $date_str .= '00 ' . $date_unit;
2050
2051        if( $days > 0) {
2052
2053                if( $hours > 0 or $minutes > 0 or $seconds > 0 )
2054                        $date_str = $day_str . ' - ' . $date_str;
2055                else
2056                        $date_str = $day_str;
2057        }
2058
2059        return $date_str;
2060}
2061?>
Note: See TracBrowser for help on using the repository browser.