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

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

job_monarch/libtoga.php:

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