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

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

job_monarch/libtoga.php

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