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

Last change on this file since 648 was 648, checked in by ramonb, 14 years ago

job_monarch/overview.php,
job_monarch/index.php:

  • old code cleanup

job_monarch/index.php,
job_monarch/image.php,
job_monarch/jobstore.php

  • rewrote session handling

job_monarch/image.php:

job_monarch/templates/header.tpl:

  • add session to clusterimage
  • (temp?) disabled pie chart

job_monarch/libtoga.php:

  • wrote new SessionHandler? class to handle all sessions
  • DataSource? now uses interactive port so can retrieve only relevant XML
  • disable XML_OPTION_CASE_FOLDING to speed up xml parsing
  • use jobmond poll interval to only reload XML when useful
  • use strpos instead of strstr, should be faster xml parsing
  • dont parse XML in ClusterImage? anymore, instead supply DataGather? object


job_monarch/conf.php:

  • use interactive gmetad port now
File size: 54.8 KB
RevLine 
[532]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 531 2008-07-08 09:04:51Z ramonb $
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
[578]36                $this->clustername      = $httpvars['c'] ? $httpvars['c'] : $getvars['c'];
37                $this->metricname       = $httpvars['m'] ? $httpvars['m'] : $getvars['m'];
[532]38
39                if( count( $httpvars ) > 0 )
40                {
41                        foreach( $httpvars as $httpvar => $httpval )
42                        {
43                                if( $httpval )
44                                {
45                                        $this->restvars[$httpvar] = $httpval;
46                                }
47                        }
48                }
49
50                if( count( $getvars ) > 0 )
51                {
52                        foreach( $getvars as $getvar => $getval )
53                        {
54                                if( $getval )
55                                {
56                                        $this->restvars[$getvar] = $getval;
57                                }
58                        }
59                }
60        }
61
62        function getClusterName()
63        {
64                return $this->clustername;
65        }
66
67        function getMetricName()
68        {
69                return $this->metricname;
70        }
71
72        function getHttpVar( $var )
73        {
74                if( isset( $this->restvars[$var] ) )
75                {
76                        return $this->restvars[$var];
77                }
78                else
79                {
80                        return null;
81                }
82        }
83}
84
85$CLUSTER_CONFS  = array();
86
87// Monarch's conf
88//
89include_once "./conf.php";
90include_once "./version.php";
91
92global $GANGLIA_PATH;
93global $RRDTOOL;
94global $JOB_ARCHIVE_DIR;
95global $JOB_ARCHIVE_DBASE;
96global $skan_str;
97global $x_first, $y_first;
98global $CLUSTER_CONFS;
99
100$my_dir = getcwd();
101
102// Load Ganglia's PHP
103chdir( $GANGLIA_PATH );
104
105include_once "./conf.php";
106include_once "./functions.php";
107include_once "./ganglia.php";
108include_once "./get_context.php";
109
110$context = 'cluster';
111include_once "./get_ganglia.php";
112
113// Back to our PHP
114chdir( $my_dir );
115
116global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH, $DATA_SOURCE, $HTTP_GET_VARS, $_GET;
117$httpvars = new HTTPVariables( $HTTP_GET_VARS, $_GET );
118
119// Set cluster context so that Ganglia will
120// provide us with the correct metrics array
121//
[602]122global $context, $clustername, $reports, $always_timestamp, $always_constant;
[532]123
124global $default_metric;
125
126// Ganglia's array of host metrics
127//
128global $metrics, $hosts_up;
129global $range, $start;
130
131global $DATETIME_FORMAT;
132
133function makeDate( $time )
134{
135        global $DATETIME_FORMAT;
136        return strftime( $DATETIME_FORMAT, $time );
137}
138
[648]139class SessionHandler
140{
141        var $cluster, $poll_stored;
[532]142
[648]143        function SessionHandler( $cluster )
144        {
145                $this->cluster          = $cluster;
146                $this->ds               = new DataSource( $this->cluster );
147
148                //ob_start();
149                session_start();
150        }
151
152        function refreshSession()
153        {
154                $ds                     = &$this->ds;
155                $myxml_data             = $ds->getData();
156
157                $_SESSION['data']               = &$myxml_data;
158                $_SESSION['gather_time']        = time();
159        }
160
161        function hasSession()
162        {
163                return ( $this->have_session );
164        }
165
166        function getSession()
167        {
168                return $_SESSION;
169
170        }
171
172        function endSession()
173        {
174                session_write_close();
175                //ob_end_flush();
176                //session_unlock();
177                //session_destroy();
178        }
179
180        function updatePollInterval( $poll_interval )
181        {
182                //if( ! isset( $_SESSION['poll_interval'] ) )
183                //{
184                        $_SESSION['poll_interval']      = $poll_interval;
185                //}
186        }
187
188        function checkSession()
189        {
190                // I got nothing; create session
191                //
192                //if( ! $this->have_session )
193                //{
194                //      $this->refreshSession();
195                //
196                //      return 0;
197                //}
198
199                //if( $this->poll_stored )
200                //{
201                //      session_start();
202
203                        if( isset( $_SESSION['gather_time'] ) && isset( $_SESSION['poll_interval'] ) )
204                        {
205
206                                $gather_time    = $_SESSION['gather_time'];
207                                $poll_interval  = $_SESSION['poll_interval'];
208
209                                // If poll_interval time elapsed since last update; recreate session
210                                //
211                                if( (time() - $gather_time) >= $poll_interval )
212                                {
213                                        $this->refreshSession();
214                                }
215                        }
216                        else
217                        {
218                                $this->refreshSession();
219                        }
220                //}
221        }
222}
223
[532]224class TarchDbase
225{
226        var $ip, $dbase, $conn;
227
228        function TarchDbase( $ip = null, $dbase = null )
229        {
230                global $CLUSTER_CONFS, $clustername;
231                global $JOB_ARCHIVE_DBASE;
232
233                // Import cluster specific settings
234                //
235                foreach( $CLUSTER_CONFS as $confcluster => $conffile )
236                {
237                        if( strtolower( trim($this->clustername) ) == strtolower(trim($confcluster)) )
238                        {
239                                include_once $conffile;
240                        }
241                }
242
243                $db_fields = explode( '/', $JOB_ARCHIVE_DBASE );
244
245                $this->ip       = $db_fields[0];
246                $this->dbase    = $db_fields[1];
247                $this->conn     = null;
248        }
249
250        function connect()
251        {
252                if( $this->ip == null )
253                        $this->conn = pg_connect( "dbname=".$this->dbase );
254                else
255                        $this->conn = pg_connect( "host=".$this->ip." dbname=".$this->dbase );
256        }
257
258        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 )
259        {
260                global $SEARCH_RESULT_LIMIT;
261
262                if( $id )
263                {
264                        $select_query = "SELECT job_id FROM jobs WHERE job_id = '$id' AND job_status = 'F'";
265                        $this->resultcount = 1;
266                }
267                else
268                {
269                        $query_args = array();
270                       
271                        if( $queue )
272                        {
273                                $query_args[] = "job_queue ='$queue'";
274                        }
275                        if( $user )
276                        {
277                                $query_args[] = "job_owner ='$user'";
278                        }
279                        if( $name )
280                        {
281                                $query_args[] = "job_name = '$name'";
282                        }
283                        if( $start_from_time )
284                        {
285                                $query_args[] = "job_start_timestamp >= $start_from_time";
286                        }
287                        if( $start_to_time )
288                        {
289                                $query_args[] = "job_start_timestamp <= $start_to_time";
290                        }
291                        if( $end_from_time )
292                        {
293                                $query_args[] = "job_stop_timestamp >= $end_from_time";
294                        }
295                        if( $end_to_time )
296                        {
297                                $query_args[] = "job_stop_timestamp <= $end_to_time";
298                        }
299
300                        $query = "FROM jobs WHERE job_status = 'F' AND ";
301                        $extra_query_args = '';
302
303                        foreach( $query_args as $myquery )
304                        {
305                                if( $extra_query_args == '' )
306                                {
307                                        $extra_query_args = $myquery;
308                                }
309                                else
310                                {
311                                        $extra_query_args .= " AND ".$myquery;
312                                }
313                        }
314                        $query .= $extra_query_args;
315
316                        $count_result_idname = "COUNT(job_id)";
317                        $select_result_idname = "job_id";
318
319                        $count_query = "SELECT " . $count_result_idname . " " . $query;
320
321                        $count_result = $this->queryDbase( $count_query );
[577]322                        $this->resultcount = (int) $count_result[0]['count'];
[532]323
324                        $select_query = "SELECT " . $select_result_idname . " " . $query . " ORDER BY job_id DESC LIMIT " . $SEARCH_RESULT_LIMIT;
325                }
326
327                $ids = $this->queryDbase( $select_query );
328
329                $ret = array();
330
331                foreach( $ids as $crow)
332                {
[577]333                        $ret[] = $crow['job_id'];
[532]334                }
335
336                return $ret;
337        }
338
339        function getNodesForJob( $jobid )
340        {
341                $result = $this->queryDbase( "SELECT node_id FROM job_nodes WHERE job_id = '$jobid'" );
342
343                $nodes = array();
344
345                foreach( $result as $result_row ) 
346                {
[577]347                        $nodes[] = $this->getNodeArray( $result_row['node_id'] );
[532]348                }
349
350                return $nodes;
351        }
352
353        function getJobsForNode( $nodeid )
354        {
355                $result = $this->queryDbase( "SELECT job_id FROM job_nodes WHERE node_id = '$nodeid'" );
356
357                $jobs = array();
358
359                foreach( $result as $result_row )
360                {
[577]361                        $jobs[] = $this->getJobArray( $result_row['job_id'] );
[532]362                }
363                return $jobs;
364        }
365
366        function getJobArray( $id )
367        {
368                $result = $this->queryDbase( "SELECT * FROM jobs WHERE job_id = '$id'" );
369
370                return ( $this->makeArray( $result[0] ) );
371        }
372
373        function getNodeArray( $id )
374        {
375                $result = $this->queryDbase( "SELECT * FROM nodes WHERE node_id = '$id'" );
376
377                return ( $this->makeArray( $result[0] ) );
378        }
379
380        function makeArray( $result_row )
381        {
382                $myar = array();
383
384                foreach( $result_row as $mykey => $myval )
385                {
386                        $map_key = explode( '_', $mykey );
387
388                        $rmap_key = array_reverse( $map_key );
389                        array_pop( $rmap_key );
390                        $map_key = array_reverse( $rmap_key );
391                       
392                        $newkey = implode( '_', $map_key );
393                       
394                        $myar[$newkey] = $result_row[$mykey];
395                }
396
397                return $myar;
398        }
399
400        function queryDbase( $query )
401        {
402                $result_rows = array();
403       
404                if( !$this->conn )
405                {
406                        $this->connect();
407                }
408
409                $result = pg_query( $this->conn, $query );
410
411                while ($row = pg_fetch_assoc($result))
412                {
413                        $result_rows[] = $row;
414                }
415
416                return $result_rows;
417        }
418}
419
420class TarchRrdGraph
421{
422        var $rrdbin, $rrdvalues, $clustername, $hostname, $tempdir, $tarchdir, $metrics;
423
424        function TarchRrdGraph( $clustername, $hostname )
425        {
426                global $RRDTOOL;
427                global $JOB_ARCHIVE_DIR;
428
429                $this->rrdbin           = $RRDTOOL;
430                $this->rrdvalues        = array();
431                $this->tarchdir         = $JOB_ARCHIVE_DIR;
432                $this->clustername      = $clustername;
433                $this->hostname         = $hostname;
434        }
435
436        function doCmd( $command )
437        {
438                $pipe = popen( $command . ' 2>&1', 'r' );
439
440                if (!$pipe)
441                {
442                        print "pipe failed.";
443                        return "";
444                }
445
446                $output = '';
447
448                while(!feof($pipe))
449                {
450                        $output .= fread($pipe, 1024);
451                }
452
453                pclose($pipe);
454
455                $output = explode( "\n", $output );
456
457                return $output;
458        }
459
460        function dirList( $dir )
461        {
462                $dirlist = array();
463
464                if ($handle = opendir( $dir ))
465                {
466                        while (false !== ($file = readdir($handle)))
467                        {
468                                if ($file != "." && $file != "..")
469                                {
470                                        $dirlist[] = $file;
471                                }
472                        }
473                        closedir($handle);
474                }
475
476                return $dirlist;
477        }
478
479        function getTimePeriods( $start, $end )
480        {
481                $times = array();
482                $dirlist = $this->dirList( $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname );
483
484                $first = 0;
485                $last = 9999999999999;
486
487                foreach( $dirlist as $dir )
488                {
489                        if( $dir > $first and $dir <= $start )
490                        {
491                                $first = $dir;
492                        }
493                        if( $dir < $last and $dir >= $end )
494                        {
495                                $last = $dir;
496                        }
497                }
498
499                foreach( $dirlist as $dir )
500                {
501                        if( $dir >= $first and $dir <= $last and !array_key_exists( $dir, $times ) )
502                        {
503                                $times[] = $dir;
504                        }
505                }
506
507                sort( $times );
508
509                return $times;
510        }
511
512        function getRrdDirs( $start, $stop )
513        {
514                $timess = $this->getTimePeriods( $start, $stop );
515
516                $rrd_files = array();
517
518                foreach( $timess as $time )
519                {
520                        $rrd_files[] = $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname. '/'.$time;
521                }
522
523                return $rrd_files;
524        }
525
526        function getRrdFiles( $metric, $start, $stop )
527        {
528                $times = $this->getTimePeriods( $start, $stop );
529
530                $rrd_files = array();
531
532                foreach( $times as $time )
533                {
534                        $rrd_files[] = $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname . '/' .$time. '/' . $metric. '.rrd';
535                }
536
537                return $rrd_files;
538        }
539}
540
541class DataSource
542{
543        var $data, $ip, $port;
544
[648]545        function DataSource( $cluster )
[532]546        {
547                global $DATA_SOURCE;
548
549                $ds_fields      = explode( ':', $DATA_SOURCE );
550
551                $ds_ip          = $ds_fields[0];
552                $ds_port        = $ds_fields[1];
553
554                $this->ip       = $ds_ip;
555                $this->port     = $ds_port;
[648]556                $this->cluster  = $cluster;
[532]557        }
558
559        function getData()
560        {
561                $errstr;
562                $errno = 0;
563                $timeout = 3;
564
565                $fp = fsockopen( $this->ip, $this->port, $errno, $errstr, $timeout );
566
567                if( !$fp )
568                {
569                        echo 'Unable to connect to '.$this->ip.':'.$this->port; // printf( 'Unable to connect to [%s:%.0f]', $this->ip, $this->port );
570                        return;
571                }
572
[648]573                $clustername    = $this->cluster;
574
575                $rc = fputs($fp, "/$clustername"."\n" );
576
577                if (!$rc)
578                {
579                        echo "Could not sent request to gmetad: $errstr";
580                        return;
581                }
582
[532]583                stream_set_timeout( $fp, 30 );
584
585                while ( !feof( $fp ) )
586                {
587                        $data .= fread( $fp, 16384 );
588                }
589
590                fclose( $fp );
591
592                return $data;
593        }
594}
595
596class DataGatherer
597{
598        var $xmlhandler, $data, $httpvars;
599
600        function DataGatherer( $cluster )
601        {
602                $this->cluster  = $cluster;
603                $this->httpvars = $httpvars;
604        }
605
606        function parseXML( $data )
607        {
608                $this->parser           = xml_parser_create();
609                $this->xmlhandler       = new TorqueXMLHandler( $this->cluster );
610
[648]611                xml_parser_set_option( $this->parser, XML_OPTION_CASE_FOLDING, 0 );
[532]612                xml_set_element_handler( $this->parser, array( &$this->xmlhandler, 'startElement' ), array( &$this->xmlhandler, 'stopElement' ) );
613
614                if ( !xml_parse( $this->parser, $data ) )
615                {
616                        $error = sprintf( 'XML error: %s at %d', xml_error_string( xml_get_error_code( $this->parser ) ), xml_get_current_line_number( $this->parser ) );
617                }
618        }
619
[648]620        function getPollInterval()
621        {
622                $handler = $this->xmlhandler;
623                //printf("d i %s\n", $handler->getPollInterval() );
624
625                return $handler->getPollInterval();
626        }
627
[532]628        function printInfo()
629        {
630                $handler = $this->xmlhandler;
631                $handler->printInfo();
632        }
633
634        function getUsingFQDN()
635        {
636                $handler = $this->xmlhandler;
637                return $handler->getUsingFQDN();
638        }
639
640        function getNodes()
641        {
642                $handler = $this->xmlhandler;
643                return $handler->getNodes();
644        }
645
646        function getNode( $node )
647        {
648                $handler = $this->xmlhandler;
649                return $handler->getNode( $node );
650        }
651
652        function getCpus()
653        {
654                $handler = $this->xmlhandler;
655                return $handler->getCpus();
656        }
657
658        function getJobs()
659        {
[648]660                $handler = &$this->xmlhandler;
661                return ( $handler->getJobs() );
[532]662        }
663
664        function getJob( $job )
665        {
666                $handler = $this->xmlhandler;
667                return $handler->getJob( $job );
668        }
669
670        function getHeartbeat()
671        {
672                $handler = $this->xmlhandler;
673                return $handler->getHeartbeat();
674        }
675
676        function isJobmonRunning()
677        {
678                $handler = $this->xmlhandler;
679                return $handler->isJobmonRunning();
680        }
681}
682
683class TorqueXMLHandler
684{
685        var $clusters, $heartbeat, $nodes, $jobs, $clustername, $proc_cluster;
686
687        function TorqueXMLHandler( $clustername )
688        {
689                $jobs                   = array();
690                $clusters               = array();
691                $this->nodes            = array();
692                $heartbeat              = array();
693                $down_nodes             = array();
694                $offline_nodes          = array();
695                $this->clustername      = $clustername;
[562]696                $this->fqdn             = 0;
697                $this->fqdnFound        = 0;
[648]698                $this->pollinterval     = 0;
[532]699        }
700
[648]701        function getPollInterval()
702        {
703                //printf( "t pi %s\n", $this->pollinterval );
704                return ( $this->pollinterval );
705        }
706
[532]707        function getUsingFQDN()
708        {
709                return $this->fqdn;
710        }
711
712        function getCpus()
713        {
714                $cpus = 0;
715
716                if( isset( $this->jobs ) && count( $this->jobs ) > 0 )
717                {
718                        foreach( $this->jobs as $jobid=>$jobattrs )
719                        {
[577]720                                $nodes  = count( $jobattrs['nodes'] );
721                                $ppn    = (int) $jobattrs['ppn'] ? $jobattrs['ppn'] : 1;
[532]722                                $mycpus = $nodes * $ppn;
723
724                                $cpus   = $cpus + $mycpus;
725                        }
726                }
727        }
728
729        function isJobmonRunning()
730        {
731                if (isset( $this->heartbeat['time'] ))
732                {
[648]733                        return true;
[532]734                }
735                else
736                {
[648]737                        return false;
[532]738                }
739        }
740
741        function makeHostname( $thostname, $tdomain=null )
742        {
743                // Should hostname be FQDN or short w/o domain
744                //
[648]745                //$nodes = &$this->nodes;
[532]746
[562]747                $domain_len     = 0 - strlen( $tdomain );
[532]748       
[562]749                if( $tdomain && $this->fqdn )
[532]750                {
751                        if( substr( $thostname, $domain_len ) != $tdomain )
[648]752                        //if( strpos( $thostname, $tdomain ) !== false )
[532]753                        {
754                                $thostname = $thostname . '.'.$tdomain;
755                        } 
756                }
757
758                return $thostname;
759        }
760
761        function startElement( $parser, $name, $attrs )
762        {
[648]763                $jobs   = $this->jobs;
764                $nodes  = $this->nodes;
[532]765
[577]766                if ( $attrs['TN'] )
[532]767                {
768                        // Ignore dead metrics. Detect and mask failures.
[577]769                        if ( $attrs['TN'] > $attrs['TMAX'] * 4 )
[532]770                        {
771                                return;
772                        }
773                }
774
775                $jobid = null;
776
[648]777                //if( $name == 'CLUSTER' )
778                //{
779                //      $this->proc_cluster = $attrs['NAME'];
780                //}
781                //else if( $name == 'HOST' and $this->proc_cluster == $this->clustername)
782                if( $name == 'HOST' )
[532]783                {
[577]784                        $hostname = $attrs['NAME'];
[532]785
[562]786                        // Assume to use FQDN if we find a '.' in the hostname
787                        //
788                        if( strpos( $hostname, '.' ) !== false )
789                        {
790                                if( !$this->fqdnFound )
791                                {
792                                        $this->fqdn             = 1;
793                                        $this->fqdnFound        = 1;
794                                }
795                        }
796                        else
797                        {
798                                if( !$this->fqdnFound )
799                                {
800                                        $this->fqdn             = 0;
801                                        $this->fqdnFound        = 1;
802                                }
803                        }
804
[577]805                        $location = $attrs['LOCATION'];
[532]806
807                        if( !isset( $nodes[$hostname] ) )
808                        {
809                                $nodes[$hostname] = new NodeImage( $this->proc_cluster, $hostname );
810                        }
811                }
[648]812                //else if( $name == 'METRIC' and strstr( $attrs['NAME'], 'MONARCH' ) and $this->proc_cluster == $this->clustername )
813                else if( ($name == 'METRIC') && (strpos( $attrs['NAME'], 'MONARCH' ) !== false) )
[532]814                {
[648]815                        if( strpos( $attrs['NAME'], 'MONARCH-HEARTBEAT' ) !== false )
[532]816                        {
[577]817                                $this->heartbeat['time'] = $attrs['VAL'];
[532]818                        }
[648]819                        else if( strpos( $attrs['NAME'], 'MONARCH-DOWN' ) !== false)
[532]820                        {
[577]821                                $fields         = explode( ' ', $attrs['VAL'] );
[532]822
823                                $nodes_down     = array();
824                                $down_domain    = null;
825
826                                foreach( $fields as $f )
827                                {
828                                        $togavalues     = explode( '=', $f );
829
830                                        $toganame       = $togavalues[0];
831                                        $togavalue      = $togavalues[1];
832
[648]833
[532]834                                        if( $toganame == 'nodes' )
835                                        {
836                                                $mynodes = explode( ';', $togavalue );
837
838                                                foreach( $mynodes as $node )
839                                                {
840                                                        $nodes_down[] = $node;
841                                                }
842                                        }
843                                        else if( $toganame == 'domain' )
844                                        {
845                                                $down_domain = $togavalue;
846                                        }
847                                        else if( $toganame == 'reported' )
848                                        {
849                                                if( !isset( $this->down_nodes['heartbeat'] ) )
850                                                {
851                                                        $this->down_nodes[$togavalue]   = array( $nodes_down, $down_domain );
852                                                }
853                                        }
854                                }
[562]855                                $down_nodes     = $nodes_down;
856
857                                foreach( $down_nodes as $node )
858                                {
859                                        $nodes_down[] = $this->makeHostname( $node, $down_domain );
860                                }
[532]861                        }
[648]862                        else if( strpos( $attrs['NAME'], 'MONARCH-OFFLINE' ) !== false )
[532]863                        {
[577]864                                $fields         = explode( ' ', $attrs['VAL'] );
[532]865
866                                $nodes_offline  = array();
867                                $offline_domain = null;
868
869                                foreach( $fields as $f )
870                                {
871                                        $togavalues     = explode( '=', $f );
872
873                                        $toganame       = $togavalues[0];
874                                        $togavalue      = $togavalues[1];
875
876                                        if( $toganame == 'nodes' )
877                                        {
878                                                $mynodes = explode( ';', $togavalue );
879
880                                                foreach( $mynodes as $node )
881                                                {
882                                                        $nodes_offline[] = $node;
883                                                }
884                                        }
885                                        else if( $toganame == 'domain' )
886                                        {
887                                                $offline_domain = $togavalue;
888                                        }
889                                        else if( $toganame == 'reported' )
890                                        {
891                                                if( !isset( $this->offline_nodes['heartbeat'] ) )
892                                                {
893                                                        $this->offline_nodes[$togavalue] = array( $nodes_offline, $offline_domain );
894                                                }
895                                        }
896                                }
[562]897                                $offline_nodes  = $nodes_offline;
898
899                                foreach( $offline_nodes as $node )
900                                {
901                                        $nodes_offline[] = $this->makeHostname( $node, $offline_domain );
902                                }
[532]903                        }
[648]904                        else if( strpos( $attrs['NAME'], 'MONARCH-JOB' ) !== false )
[532]905                        {
[577]906                                sscanf( $attrs['NAME'], 'MONARCH-JOB-%d-%d', $jobid, $monincr );
[532]907
908                                if( !isset( $jobs[$jobid] ) )
909                                {
910                                        $jobs[$jobid] = array();
911                                }
912
[577]913                                $fields = explode( ' ', $attrs['VAL'] );
[532]914
915                                foreach( $fields as $f )
916                                {
917                                        $togavalues = explode( '=', $f );
918
919                                        $toganame = $togavalues[0];
920                                        $togavalue = $togavalues[1];
921
[648]922                                        //printf( "tn %s\n", $toganame );
923
[532]924                                        if( $toganame == 'nodes' )
925                                        {
[577]926                                                if( $jobs[$jobid]['status'] == 'R' )
[532]927                                                {
928                                                        if( !isset( $jobs[$jobid][$toganame] ) )
929                                                        {
930                                                                $jobs[$jobid][$toganame] = array();
931                                                        }
932
933                                                        $mynodes = explode( ';', $togavalue );
934
935                                                        foreach( $mynodes as $node )
936                                                        {
937                                                                if( !in_array( $node, $jobs[$jobid][$toganame] ) )
938                                                                {
939                                                                        $jobs[$jobid][$toganame][] = $node;
940                                                                }
941                                                        }
942
943                                                }
[577]944                                                else if( $jobs[$jobid]['status'] == 'Q' )
[532]945                                                {
946                                                        $jobs[$jobid][$toganame] = $togavalue;
947                                                }
948                                        }
[648]949                                        else if( $toganame == 'poll_interval' )
950                                        {
951                                                $this->pollinterval     = $togavalue;
952                                                //printf("u pi %s\n", $togavalue );
953                                        }
[532]954                                        else
955                                        {
956                                                $jobs[$jobid][$toganame] = $togavalue;
957                                        }
958                                }
959
[577]960                                if( isset( $jobs[$jobid]['nodes'] ) )
[532]961                                {
[577]962                                        $nr_nodes = count( $jobs[$jobid]['nodes'] );
[532]963               
[577]964                                        if( $jobs[$jobid]['status'] == 'R' )
[532]965                                        {
[577]966                                                if( isset( $jobs[$jobid]['domain'] ) )
[532]967                                                {
[577]968                                                        $domain         = $jobs[$jobid]['domain'];
[532]969                                                }
[562]970                                                $job_nodes      = array();
[532]971
[577]972                                                foreach( $jobs[$jobid]['nodes'] as $node )
[532]973                                                {
974                                                        // Only add domain name to the hostname if Ganglia is doing that too
975                                                        //
[562]976                                                        $host   = $this->makeHostname( $node, $domain );
[532]977
978                                                        if( !isset( $nodes[$host] ) )
979                                                        {
980                                                                $my_node = new NodeImage( $this->proc_cluster, $host );
981                                                        }
982                                                        else
983                                                        {
984                                                                $my_node = $nodes[$host];
985                                                        }
986
987                                                        if( !$my_node->hasJob( $jobid ) )
988                                                        {
[577]989                                                                if( isset( $jobs[$jobid]['ppn'] ) )
[532]990                                                                {
[577]991                                                                        $my_node->addJob( $jobid, ((int) $jobs[$jobid]['ppn']) );
[532]992                                                                }
993                                                                else
994                                                                {
995                                                                        $my_node->addJob( $jobid, 1 );
996                                                                }
997                                                        }
998
[562]999                                                        $nodes[$host]   = $my_node;
1000                                                        $job_nodes[]    = $host;
[532]1001                                                }
[577]1002                                                $jobs[$jobid]['nodes']  = $job_nodes;
[532]1003                                        }
1004                                }
1005                        }
1006                }
1007                $this->jobs     = $jobs;
1008                $this->nodes    = $nodes;
1009        }
1010
1011        function stopElement( $parser, $name )
1012        {
1013                $nodes  = $this->nodes;
1014
1015                if( $name == "GANGLIA_XML" )
1016                {
1017                        if( sizeof( $this->down_nodes ) > 0 )
1018                        {
1019                                foreach( $this->down_nodes as $reported => $dnodes )
1020                                {
1021                                        if( $reported == $this->heartbeat['time'] )
1022                                        {
1023                                                $domain = $dnodes[1];
1024
1025                                                foreach( $dnodes[0] as $downhost )
1026                                                {
1027                                                        $downhost = $this->makeHostname( $downhost, $domain );
1028
1029                                                        if( isset( $nodes[$downhost] ) )
1030                                                        {
1031                                                                // OMG PHP4 is fking stupid!
1032                                                                // $nodes[$downhost]->setDown( 1 ) won't work here..
1033                                                                //
1034                                                                $mynode = $nodes[$downhost];
1035                                                                $mynode->setDown( 1 );
1036                                                                $nodes[$downhost] = $mynode;
1037                                                        }
1038                                                }
1039                                        }
1040                                }
1041                        }
1042
1043                        if( sizeof( $this->offline_nodes ) > 0 )
1044                        {
1045                                foreach( $this->offline_nodes as $reported => $onodes )
1046                                {
1047                                        if( $reported == $this->heartbeat['time'] )
1048                                        {
1049                                                $domain = $onodes[1];
1050
1051                                                foreach( $onodes[0] as $offlinehost )
1052                                                {
1053                                                        $offlinehost = $this->makeHostname( $offlinehost, $domain );
1054
1055                                                        if( isset( $nodes[$offlinehost] ) )
1056                                                        {
1057                                                                // OMG PHP4 is fking stupid!
1058                                                                // $nodes[$offlinehost]->setDown( 1 ) won't work here..
1059                                                                //
1060                                                                $mynode = $nodes[$offlinehost];
1061                                                                $mynode->setOffline( 1 );
1062                                                                $nodes[$offlinehost] = $mynode;
1063                                                        }
1064                                                }
1065                                        }
1066                                }
1067                        }
1068                }
1069
1070                $this->nodes = $nodes;
1071        }
1072
1073        function printInfo()
1074        {
1075                $jobs = &$this->jobs;
1076
1077                printf( "---jobs---\n" );
1078
1079                foreach( $jobs as $jobid => $job )
1080                {
1081                        printf( "job %s\n", $jobid );
1082
[577]1083                        if( isset( $job['nodes'] ) )
[532]1084                        {
[577]1085                                foreach( $job['nodes'] as $node )
[532]1086                                {
1087                                        $mynode = $this->nodes[$node];
1088                                        $hostname = $mynode->getHostname();
1089                                        $location = $mynode->getLocation();
1090
1091                                        printf( "\t- node %s\tlocation %s\n", $hostname, $location );
1092                                }
1093                        }
1094                }
1095
1096                printf( "---nodes---\n" );
1097
1098                $nodes = &$this->nodes;
1099
1100                foreach( $nodes as $node )
1101                {
1102                        $hostname = $node->getHostname();
1103                        $location = $node->getLocation();
1104                        $jobs = implode( ' ', $node->getJobs() );
1105                        printf( "* node %s\tlocation %s\tjobs %s\n", $hostname, $location, $jobs );
1106                }
1107        }
1108
1109        function getNodes()
1110        {
1111                return $this->nodes;
1112        }
1113
1114        function getNode( $node )
1115        {
1116                $nodes = &$this->nodes;
1117
1118                if( isset( $nodes[$node] ) )
1119                {
1120                        return $nodes[$node];
1121                }
1122                else
1123                {
1124                        return NULL;
1125                }
1126        }
1127
1128        function getJobs()
1129        {
1130                return $this->jobs;
1131        }
1132
1133        function getJob( $job )
1134        {
1135                $jobs = &$this->jobs;
1136
1137                if( isset( $jobs[$job] ) )
1138                {
1139                        return $jobs[$job];
1140                }
1141                else
1142                {
1143                        return NULL;
1144                }
1145        }
1146
1147        function getHeartbeat()
1148        {
1149                return $this->heartbeat['time'];
1150        }
1151}
1152
1153class NodeImage
1154{
1155        var $image, $x, $y, $hostname, $jobs, $tasks, $showinfo;
1156
1157        function NodeImage( $cluster, $hostname )
1158        {
1159                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
1160
1161                $this->jobs             = array();
1162                $this->tasks            = 0;
1163                $this->hostname         = $hostname;
1164                $this->cpus             = $this->determineCpus();
1165                $this->clustername      = $cluster;
1166                $this->showinfo         = 1;
1167                $this->size             = $SMALL_CLUSTERIMAGE_NODEWIDTH;
1168                $this->down             = 0;
1169                $this->offline          = 0;
[574]1170                $this->small            = false;
1171                $this->big              = false;
[575]1172                $this->selected         = false;
[532]1173        }
1174
1175        function addJob( $jobid, $cpus )
1176        {
1177                $jobs           = &$this->jobs;
1178                $jobs[]         = $jobid;
1179                $this->jobs     = $jobs;
1180
1181                $this->addTask( $cpus );
1182        }
1183
1184        function hasJob( $jobid )
1185        {
1186                $jobfound = 0;
1187
1188                if( count( $this->jobs ) > 0 )
1189                {
1190                        foreach( $this->jobs as $job )
1191                        {
1192                                if( $job == $jobid )
1193                                {
1194                                        $jobfound = 1;
1195                                }
1196                        }
1197                }
1198
1199                return $jobfound;
1200        }
1201
1202        function addTask( $cpus )
1203        {
1204                $this->tasks = $this->tasks + $cpus;
1205        }
1206        function setDown( $down )
1207        {
1208                $this->down = $down;
1209        }
1210        function isDown()
1211        {
1212                return $this->down;
1213        }
1214        function setOffline( $offline )
1215        {
1216                $this->offline = $offline;
1217        }
1218        function isOffline()
1219        {
1220                return $this->offline;
1221        }
1222        function setImage( $image )
1223        {
1224                $this->image = $image;
1225        }
1226        function setCoords( $x, $y )
1227        {
1228                $this->x = $x;
1229                $this->y = $y;
1230        }
1231        function getX()
1232        {
1233                return $this->x;
1234        }
1235        function getY()
1236        {
1237                return $this->y;
1238        }
1239
1240        function getImagemapArea()
1241        {
1242                $area_topleft           = $this->x . "," . $this->y;
1243                $area_bottomright       = ($this->x + $this->size) . "," . ($this->y + $this->size);
1244                $area_coords            = $area_topleft . "," . $area_bottomright;
1245
1246                $area_href              = "./?c=" . $this->clustername . "&h=" . $this->hostname;
1247
1248                $area_tooltip           = $this->hostname;
[548]1249                $area_onclick           = "return ClusterImageSelectHost( '" . $this->hostname . "' )";
[532]1250
1251                if( $this->down)
1252                {
1253                        $area_tooltip           = $area_tooltip . ": DOWN";
1254                }
1255                else if( $this->offline )
1256                {
1257                        $area_tooltip           = $area_tooltip . ": OFFLINE";
1258                }
1259
1260                $area_tooltip           = $area_tooltip . ": " . implode( " ", $this->jobs );
1261
[547]1262                $tag_onclick            = "ONCLICK=\"" . $area_onclick . "\"";
[532]1263                $tag_href               = "HREF=\"" . $area_href . "\"";
1264                $tag_coords             = "COORDS=\"" . $area_coords . "\"";
1265                $tag_tooltip1           = "ALT=\"" . $area_tooltip . "\"";
1266                $tag_tooltip2           = "TITLE=\"" . $area_tooltip . "\"";
1267
[547]1268                return ("<AREA SHAPE=\"RECT\" " . $tag_coords . " " . $tag_onclick . " " . $tag_href . " " . $tag_tooltip1 . " " . $tag_tooltip2 . ">");
[532]1269        }
1270
1271        function colorHex( $color )
1272        {
1273                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) );
1274
1275                return $my_color;
1276        }
1277
1278        function setLoad( $load )
1279        {
1280                $this->load = $load;
1281        }
1282
1283        function setHostname( $hostname )
1284        {
1285                $this->hostname = $hostname;
1286        }
1287
1288        function getHostname()
1289        {
1290                return $this->hostname;
1291        }
1292
1293        function getJobs()
1294        {
1295                return $this->jobs;
1296        }
1297
1298        function setShowinfo( $showinfo )
1299        {
1300                $this->showinfo = $showinfo;
1301        }
1302
[575]1303        function setSelected()
1304        {
1305                $this->selected = true;
1306        }
1307
[532]1308        function drawSmall()
1309        {
1310                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
1311
1312                $this->size     = $SMALL_CLUSTERIMAGE_NODEWIDTH;
[574]1313                $this->small    = true;
1314                $this->big      = false;
[532]1315
1316                $this->draw();
1317        }
1318
1319        function drawBig()
1320        {
1321                global $BIG_CLUSTERIMAGE_NODEWIDTH;
1322
1323                $this->size     = $BIG_CLUSTERIMAGE_NODEWIDTH;
[574]1324                $this->small    = false;
1325                $this->big      = true;
[532]1326
[573]1327                $this->drawShadow();
[532]1328                $this->draw();
1329        }
1330
[573]1331        function drawShadow()
1332        {
1333                // offset of drop shadow from top left
1334                //
1335                $ds_offset      = 5;
1336 
1337                // number of steps from black to background color
1338                //
1339                $ds_steps       = 15;
1340
1341                // distance between steps
1342                //
1343                $ds_spread = 1;
1344
1345                // define the background color
1346                //
1347                $background = array("r" => 255, "g" => 255, "b" => 255);
1348
1349                // create a new canvas.  New canvas dimensions should be larger than the original's
1350                //
1351                $width  = $this->size + $ds_offset;
1352                $height = $this->size + $ds_offset;
1353
1354                // determine the offset between colors
1355                //
[578]1356                $step_offset = array("r" => ($background['r'] / $ds_steps), "g" => ($background['g'] / $ds_steps), "b" => ($background['b'] / $ds_steps));
[573]1357
1358                // calculate and allocate the needed colors
1359                //
1360                $current_color = $background;
1361
1362                for ($i = 0; $i <= $ds_steps ; $i++)
1363                {
[578]1364                        $colors[$i] = imagecolorallocate($this->image, round($current_color['r']), round($current_color['g']), round($current_color['b']));
[573]1365
[578]1366                        $current_color['r'] -= $step_offset['r'];
1367                        $current_color['g'] -= $step_offset['g'];
1368                        $current_color['b'] -= $step_offset['b'];
[573]1369                }
1370
1371                // draw overlapping rectangles to create a drop shadow effect
1372                //
1373                for ($i = 3; $i < count($colors); $i++)
1374                {
1375                        imagefilledrectangle( $this->image, ($this->x + $ds_offset), ($this->y + $ds_offset), ($this->x + $width), ($this->y + $height), $colors[$i] );
1376                        $width -= $ds_spread;
1377                        $height -= $ds_spread;
1378                }
1379        }
1380
[532]1381        function draw()
1382        {
1383                global $JOB_NODE_MARKING, $NODE_DOWN_MARKING, $NODE_OFFLINE_MARKING;
1384
1385                $black_color = imageColorAllocate( $this->image, 0, 0, 0 );
1386                $size = $this->size;
1387
[574]1388                // seperator pixel row
1389                //
1390                $sep    = 0;
[532]1391
[574]1392                if( $this->big )
1393                {
1394                        $sep    = 2;
1395                }
1396
[575]1397                $select_color   = imageColorAllocate( $this->image, 91, 255, 41 );
[574]1398
[575]1399                if( $this->selected )
1400                {
1401                        imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+($size), $this->y+($size), $select_color );
1402                }
1403                else
1404                {
1405                        imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+($size-$sep), $this->y+($size-$sep), $black_color );
1406                }
1407
[532]1408                if( $this->showinfo)
1409                {
1410                        $this->load = $this->determineLoad();
1411
1412                        if( !isset( $this->image ) or !isset( $this->x ) or !isset( $this->y ) )
1413                        {
1414                                printf( "aborting\n" );
1415                                printf( "x %d y %d load %f\n", $this->x, $this->y, $load );
1416                                return;
1417                        }
1418
1419                        // Convert Ganglias Hexadecimal load color to a Decimal one
1420                        //
1421                        $load = $this->determineLoad(); 
1422                        $usecolor = $this->colorHex( load_color($load) );
[574]1423                        imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-(1+$sep)), $this->y+($size-(1+$sep)), $usecolor );
[532]1424                        if( $this->down )
1425                        {
1426                                imageString( $this->image, 1, $this->x+(($size/2)-1), $this->y+(($size/2)-4), $NODE_DOWN_MARKING, $black_color );
1427                        }
1428                        else if( $this->offline )
1429                        {
1430                                imageString( $this->image, 1, $this->x+(($size/2)-1), $this->y+(($size/2)-4), $NODE_OFFLINE_MARKING, $black_color );
1431                        }
1432                        else if( count( $this->jobs ) > 0 )
1433                        {
1434                                imageString( $this->image, 1, $this->x+(($size/2)-1), $this->y+(($size/2)-4), $JOB_NODE_MARKING, $black_color );
1435                        }
1436                }
1437                else
1438                {
1439                        // White
1440                        $usecolor = imageColorAllocate( $this->image, 255, 255, 255 );
1441                        imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor );
1442                }
1443        }
1444
1445        function determineCpus()
1446        {
1447                global $metrics;
1448
[577]1449                $cpus = $metrics[$this->hostname][cpu_num]['VAL'];
[532]1450
1451                if (!$cpus)
1452                {
1453                        $cpus=1;
1454                }
1455
1456                return $cpus;
1457        }
1458
1459        function determineLoad()
1460        {
1461                global $metrics;
1462
[577]1463                $load_one       = $metrics[$this->hostname][load_one]['VAL'];
[532]1464                $load           = ((float) $load_one)/$this->cpus;
1465
1466                return $load;
1467        }
1468}
1469
1470class ClusterImage
1471{
1472        var $dataget, $image, $clustername;
1473        var $filtername, $filters;
1474
[648]1475        function ClusterImage( $datag, $clustername )
[532]1476        {
[648]1477                //$this->dataget                = new DataGatherer( $clustername );
1478                $this->dataget          = $datag;
1479                //$this->data           = $data;
[532]1480                $this->clustername      = $clustername;
1481                $this->filters          = array();
1482                $this->size             = 's';
1483                $this->width            = 0;
1484                $this->height           = 0;
1485                $this->output           = 1;
[575]1486                $this->selected         = '';
[532]1487        }
1488
1489        function getWidth()
1490        {
1491                return $this->width;
1492        }
1493        function getHeight()
1494        {
1495                return $this->height;
1496        }
1497        function setSmall()
1498        {
1499                $this->size     = 's';
1500        }
1501        function setBig()
1502        {
1503                $this->size     = 'b';
1504        }
1505        function setNoimage()
1506        {
1507                $this->output   = 0;
1508        }
1509        function isSmall()
1510        {
1511                return ($this->size == 's');
1512        }
1513        function isBig()
1514        {
1515                return ($this->size == 'b');
1516        }
1517        function setFilter( $filtername, $filtervalue )
1518        {
1519                $this->filters[$filtername] = $filtervalue;
1520        }
1521
1522        function filterNodes( $jobs, $nodes )
1523        {
1524                $filtered_nodes = array();
1525
[582]1526                $filter_checks  = array();
1527
[532]1528                foreach( $nodes as $node )
1529                {
[582]1530                        $hostname       = $node->getHostname();
1531                        $mynjobs        = $node->getJobs();
[532]1532
[582]1533                        foreach( $this->filters as $filtername => $filtervalue )
1534                        {
1535                                $filter_checks[$filtername] = false;
1536                        }
[532]1537
[582]1538                        if( array_key_exists( 'host', $this->filters ) )
[532]1539                        {
[582]1540                                if( $hostname == $this->filters['host'] )
[532]1541                                {
[582]1542                                        $this->selected         = $hostname;
[586]1543                                        $filtered_nodes[]       = $hostname;
[575]1544                                }
[582]1545                        }
1546
1547                        foreach( $mynjobs as $myjob )
1548                        {
1549                                foreach( $this->filters as $filtername => $filtervalue )
[575]1550                                {
[582]1551                                        if( $filtername == 'jid' )
[532]1552                                        {
[582]1553                                                if( $myjob == $filtervalue )
[532]1554                                                {
[582]1555                                                        $filter_checks['jid'] = true;
1556                                                }
1557                                        }
1558                                        if( $filtername == 'query' )
1559                                        {
1560                                                foreach( $jobs[$myjob] as $myj_attr => $myj_val )
1561                                                {
1562                                                        if( is_array( $myj_val ) )
[532]1563                                                        {
[582]1564                                                                foreach( $myj_val as $myj_v )
[532]1565                                                                {
[582]1566                                                                        if( strpos( $myj_v, $filtervalue ) !== false )
[532]1567                                                                        {
[582]1568                                                                                $filter_checks['query'] = true;
[562]1569                                                                        }
[532]1570                                                                }
[582]1571                                                        }
1572                                                        else
1573                                                        {
[586]1574                                                                if( strpos( $myj_val, $filtervalue ) !== false )
[563]1575                                                                {
[582]1576                                                                        $filter_checks['query'] = true;
[563]1577                                                                }
[532]1578                                                        }
1579                                                }
1580                                        }
[582]1581                                        if( $filtername != 'host' )
1582                                        {
1583                                                if( $jobs[$myjob][$filtername] == $filtervalue )
1584                                                {
1585                                                        $filter_checks[$filtername] = true;
1586                                                }
1587                                        }
[532]1588                                }
[582]1589                        }
1590
1591                        $addhost        = true;
1592
1593                        foreach( $filter_checks as $c_filtername => $c_filterfound )
1594                        {
1595                                if( ! $c_filterfound )
[532]1596                                {
[582]1597                                        $addhost        = false;
[532]1598                                }
1599                        }
1600
[582]1601                        if( $addhost == true )
[532]1602                        {
1603                                $filtered_nodes[] = $hostname;
1604                        }
1605                }
1606
1607                return $filtered_nodes;
1608        }
1609
1610        function draw()
1611        {
1612                global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH;
1613                global $BIG_CLUSTERIMAGE_MAXWIDTH, $BIG_CLUSTERIMAGE_NODEWIDTH;
1614                global $CLUSTER_CONFS, $confcluster, $SHOW_EMPTY_COLUMN, $SHOW_EMPTY_ROW;
1615
1616                global $SORTBY_HOSTNAME, $SORT_ORDER, $skan_str;
1617                global $x_first, $y_first;
1618
1619                foreach( $CLUSTER_CONFS as $confcluster => $conffile )
1620                {
1621                        if( strtolower( trim($this->clustername) ) == strtolower(trim($confcluster)) )
1622                        {
1623                                include_once $conffile;
1624                        }
1625                }
1626
[648]1627                $mydatag                = &$this->dataget;
1628                //$mydatag->parseXML( $this->data );
[532]1629
1630                if( $this->isSmall() )
1631                {
1632                        $max_width      = $SMALL_CLUSTERIMAGE_MAXWIDTH;
1633                        $node_width     = $SMALL_CLUSTERIMAGE_NODEWIDTH;
1634                }
1635                else if( $this->isBig() )
1636                {
1637                        $max_width      = $BIG_CLUSTERIMAGE_MAXWIDTH;
1638                        $node_width     = $BIG_CLUSTERIMAGE_NODEWIDTH;
1639                }
1640
1641                $nodes          = $mydatag->getNodes();
[648]1642                //print_r( $nodes);
[532]1643                $nodes_hosts    = array_keys( $nodes );
1644
1645                $nodes_nr       = count( $nodes );
1646
1647                $nodes_size     = $nodes_nr*$node_width;
1648                $node_rows      = 0;
1649
1650                if( $nodes_size > $max_width )
1651                {
1652                        $nodes_per_row = ( (int) ($max_width/$node_width) );
1653                }
1654                else
1655                {
1656                        $nodes_per_row = $nodes_size;
1657                        $node_rows = 1;
1658                }
1659
1660                if( $nodes_per_row < $nodes_nr )
1661                {
1662                        $node_rows = ( (int) ($nodes_nr/$nodes_per_row) );
1663                        $node_rest = fmod( $nodes_nr, $nodes_per_row );
1664
1665                        if( $node_rest > 0 )
1666                        {
1667                                $node_rows++;
1668                        }
1669                }
1670
1671                $y_offset       = 0;
1672                $font           = 2;
1673                $fontwidth      = ImageFontWidth( $font );
1674                $fontheight     = ImageFontHeight( $font );
1675                $fontspaceing   = 2;
1676                $y_offset       = $fontheight + (2 * $fontspaceing);
1677
1678                $this->width    = $max_width;
1679                $this->height   = ($y_offset + (($node_rows*$node_width)+1) );
1680
[648]1681                $jobs           = $mydatag->getJobs();
[532]1682                $filtered_nodes = $this->filterNodes( $jobs, $nodes );
[575]1683                $selected_host  = $this->selected;
[532]1684
1685                if( $SORTBY_HOSTNAME != "" )
1686                {
1687                        $sorted         = array();
1688
1689                        $x_first        = 0;
1690                        $y_first        = 0;
1691
1692                        $skan_str       = $SORTBY_HOSTNAME;
1693
1694                        global $x_present, $y_present;
1695                        $x_present      = false;
1696                        $y_present      = false;
1697
1698                        // Should we scan by X, Y or both
1699                        //
1700                        if(strpos( $SORTBY_HOSTNAME, "{x}" ) != false )
1701                        {
1702                                $x_str          = "{x}";
1703                                $x_present      = true;
1704                        }
1705                        else if(strpos( $SORTBY_HOSTNAME, "{X}" ) != false )
1706                        {
1707                                $x_str          = "{X}";
1708                                $x_present      = true;
1709                        }
1710                        if(strpos( $SORTBY_HOSTNAME, "{y}" ) != false )
1711                        {
1712                                $y_str          = "{y}";
1713                                $y_present      = true;
1714                        }
1715                        else if(strpos( $SORTBY_HOSTNAME, "{Y}" ) != false )
1716                        {
1717                                $y_str          = "{Y}";
1718                                $y_present      = true;
1719                        }
1720
1721                        // If we should scan for both X and Y: see which one is first
1722                        //
1723                        if(( strpos( $SORTBY_HOSTNAME, $x_str ) < strpos( $SORTBY_HOSTNAME, $y_str ) ) && ( $x_present && $y_present ))
1724                        {
1725                                $x_first        = 1;
1726                        }
1727                        else if(( strpos( $SORTBY_HOSTNAME, $x_str ) > strpos( $SORTBY_HOSTNAME, $y_str ) ) && ( $x_present && $y_present ))
1728                        {
1729                                $y_first        = 1;
1730               
1731                        }
1732                        else if( $x_present )
1733                        {
1734                                $x_first        = 1;
1735                        }
1736                        else if( $y_present )
1737                        {
1738                                $y_first        = 1;
1739                        }
1740
1741                        // Now replace our {x} and {y} with %d for sscanf parsing
1742                        //
1743                        if(( $x_first ) && ( $x_present && $y_present ) )
1744                        {
1745                                $skan_str       = str_replace( $x_str, "%d", $skan_str );
1746                                $skan_str       = str_replace( $y_str, "%d", $skan_str );
1747                        } 
1748                        else if( $x_present)
1749                        {
1750                                $skan_str       = str_replace( $x_str, "%d", $skan_str );
1751                        }
1752                        else if( $y_present)
1753                        {
1754                                $skan_str       = str_replace( $y_str, "%d", $skan_str );
1755                        }
1756
1757                        $x_min          = null;
1758                        $x_max          = null;
1759                        $y_min          = null;
1760                        $y_max          = null;
1761
1762                        $x_columns      = array();
1763                        $y_rows         = array();
1764
1765                        // Now let's walk through all our nodes and see which one are valid for our scan pattern
1766                        //
1767                        foreach( $nodes as $hostname => $node )
1768                        {
1769                                $x      = null;
1770                                $y      = null;
1771
1772                                if( $x_present && $y_present )
1773                                {
1774                                        if( $x_first )
1775                                        {
1776                                                $n = sscanf( $hostname, $skan_str, $x, $y );
1777                                        }
1778                                        else if( $y_first )
1779                                        {
1780                                                $n = sscanf( $hostname, $skan_str, $y, $x );
1781                                        }
1782
1783                                        // Remove nodes that don't match
1784                                        //
1785                                        if( $n < 2 )
1786                                        {
1787                                                // This node hostname has no match for: {x} and {y}
1788                                                //
1789                                                unset( $nodes[$hostname] );
1790                                        }
1791                                }
1792                                else if( $x_present && !$y_present )
1793                                {
1794                                        $n = sscanf( $hostname, $skan_str, $x );
1795
1796                                        // Remove nodes that don't match
1797                                        //
1798                                        if( $n < 1 )
1799                                        {
1800                                                // This node hostname has no match for: {x}
1801                                                //
1802                                                unset( $nodes[$hostname] );
1803                                        }
1804                                        $y      = 1;
1805                                }
1806                                else if( $y_present && !$x_present )
1807                                {
1808                                        $n = sscanf( $hostname, $skan_str, $y );
1809
1810                                        // Remove nodes that don't match
1811                                        //
1812                                        if( $n < 1 )
1813                                        {
1814                                                // This node hostname has no match for: {y}
1815                                                //
1816                                                unset( $nodes[$hostname] );
1817                                        }
1818                                        $x      = 1;
1819                                }
1820
1821                                // Determine the lowest value of {x} that exists in all node hostnames
1822                                //
1823                                if( !$x_min && $x != null )
1824                                {
1825                                        $x_min  = $x;
1826                                }
1827                                else if( $x < $x_min && $x != null )
1828                                {
1829                                        $x_min  = $x;
1830                                }
1831
1832                                // Determine the highest value of {x} that exists in all node hostnames
1833                                //
1834                                if( !$x_max && $x != null )
1835                                {
1836                                        $x_max  = $x;
1837                                }
1838                                else if( $x > $x_max && $x != null )
1839                                {
1840                                        $x_max  = $x;
1841                                }
1842
1843                                // Determine the lowest value of {y} that exists in all node hostnames
1844                                //
1845                                if( !$y_min && $y != null )
1846                                {
1847                                        $y_min  = $y;
1848                                }
1849                                else if( $y < $y_min && $y != null )
1850                                {
1851                                        $y_min  = $y;
1852                                }
1853
1854                                // Determine the highest value of {y} that exists in all node hostnames
1855                                //
1856                                if( !$y_max && $y != null )
1857                                {
1858                                        $y_max  = $y;
1859                                }
1860                                else if( $y > $y_max && $y != null )
1861                                {
1862                                        $y_max  = $y;
1863                                }
1864
1865                                // Store which non-empty columns and rows we found
1866                                //
1867                                if( !in_array( $x, $x_columns ) )
1868                                {
1869                                        $x_columns[] = $x;
1870                                }
1871                                if( !in_array( $y, $y_rows ) )
1872                                {
1873                                        $y_rows[] = $y;
1874                                }
1875                        }
1876
1877                        // Sort all the nodes (alpha and numerically)
1878                        // 1: gb-r1n1, 2: gb-r1n2, 3: gb-r2n1, etc
1879                        //
1880                        $sorted_nodes   = usort( $nodes, "cmp" );
1881
1882                        //print_r( $x_columns ) ;
1883
1884                        $cur_node       = 0;
1885
1886                        $x_offset       = 0;
1887                        $y_offset       = 0;
1888                        $font           = 2;
1889                        $fontwidth      = ImageFontWidth( $font );
1890                        $fontheight     = ImageFontHeight( $font );
1891                        $fontspaceing   = 2;
1892
1893                        if( $this->isSmall() ) 
1894                        {
1895                                $y_offset       = $y_offset + (2 * $fontspaceing) + $fontheight;
1896                        }
1897
1898                        if( $this->isBig() ) 
1899                        {
1900                                $y_offset       = ($fontheight * (1 + strlen( $x_max) ) ) + ((2 + strlen( $x_max)) * $fontspaceing);
1901                                $x_offset       = ($fontwidth * (1 + strlen( $y_max) ) ) + ((2 + strlen( $y_max)) * $fontspaceing);
1902                        }
1903
1904                        $image_width    = $x_offset + ($node_width * ($x_max-$x_min+2));
1905
1906                        if( $this->isSmall() ) 
1907                        {
1908                                $image_width    = $max_width;
1909                        }
1910                        else if( $this->isBig() ) 
1911                        {
1912                                $image_width    = ($image_width < $max_width) ? $image_width : $max_width;
1913                        }
1914                        $image_height   = $y_offset + ($node_width * ($y_max-$y_min+2));
1915
1916                        $this->width    = $image_width;
1917                        $this->heigth   = $image_heigth;
1918
1919                        $image          = imageCreateTrueColor( $image_width, $image_height );
1920                        $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1921
1922                        imageFill( $image, 0, 0, $colorwhite );
1923
1924                        if( $this->isSmall() )
1925                        {
1926                                // Draw a fancy little header text to explain what it is
1927                                //
1928                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1929
1930                                imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1931                        }
1932
1933                        if( $this->isBig() && ( isset( $SORT_XLABEL ) || isset( $SORT_YLABEL ) ) )
1934                        {
1935                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1936
1937                                if( isset( $SORT_XLABEL ) )
1938                                {
1939                                        // Print the {x} label: rack
1940                                        //
1941                                        imageString( $image, $font, $x_offset, $fontspaceing, $SORT_XLABEL, $colorblue );
1942                                }
1943
1944                                if( isset( $SORT_YLABEL ) )
1945                                {
1946                                        // Stupid php without imageStringDown function... we'll make one ourself
1947                                        //
1948
1949                                        // Print the {y} label: node
1950                                        //
1951                                        imageStringDown( $image, $font, $fontspaceing, $y_offset, $SORT_YLABEL, $colorblue );
1952                                }
1953                        }
1954
1955                        $previous_n     = 0;
1956                        $previous_m     = 0;
1957                        $x_empty_count  = 0;
1958                        $y_empty_count  = 0;
1959
1960                        // Let's start assigning x,y coordinates now
1961                        //
1962                        for( $n = $x_min; $n <= $x_max; $n++ )
1963                        {
1964                                for( $m = $y_min; $m <= $y_max; $m++ )
1965                                {
1966                                        if( $x_min > 0 )
1967                                        {
1968                                                $x      = $x_offset + ( ($n-$x_min) * $node_width ) - ($x_empty_count * $node_width);
1969                                        }
1970                                        if( $y_min > 0 )
1971                                        {
1972                                                $y      = $y_offset + ( ($m-$y_min) * $node_width ) - ($y_empty_count * $node_width);
1973                                        }
1974
1975                                        // Don't show empty rows/columns if option enabled
1976                                        //
1977                                        if( !in_array( $n, $x_columns ) && !$SHOW_EMPTY_COLUMN )
1978                                        {
1979                                                // Skip to next iteration: we don't want a empty column
1980                                                //
1981                                                if( $n > $previous_n )
1982                                                {
1983                                                        $previous_n = $n;
1984                                                        $x_empty_count++;
1985                                                }
1986                                                continue;
1987                                        }
1988                                        if( !in_array( $m, $y_rows ) && !$SHOW_EMPTY_ROW )
1989
1990                                        {
1991                                                // Skip to next iteration: we don't want a empty column
1992                                                //
1993                                                if( $m > $previous_m )
1994                                                {
1995                                                        $previous_m = $m;
1996                                                        $y_empty_count++;
1997                                                }
1998                                                continue;
1999                                        }
2000
2001                                        if( $this->isBig() ) 
2002                                        {
2003                                                // Draw y(node) column number header
2004                                                //
2005                                                if(( $n == $x_min ) && ( isset($SORT_YLABEL) ) )
2006                                                {
2007                                                        $mfontspacing   = 1;
2008
2009                                                        $ylabel_x       = $x - ( $fontwidth * strlen( $y_max ) ) - $mfontspacing;
2010                                                        $ylabel_y       = $y;
2011
2012                                                        imageString( $image, $font, $ylabel_x, $ylabel_y, strval( $m ), $colorblue );
2013
2014                                                        $xmin_hit[$n]   = true;
2015                                                }
2016
2017                                                // Draw x(rack) column number header
2018                                                //
2019                                                if(( $m == $y_min ) && ( isset($SORT_XLABEL) ) )
2020                                                {
2021                                                        $mfontspacing   = 2;
2022                                                        $xlabel_y       = $y - ( $fontheight * strlen( $x_max ) );
2023                                                        $xlabel_x       = $x + $mfontspacing; 
2024
2025                                                        imageStringDown( $image, $font, $xlabel_x, $xlabel_y, strval( $n ), $colorblue );
2026                                                }
2027                                        }
2028
2029                                        if( isset( $nodes[$cur_node] ) ) 
2030                                        {
2031                                                $host   = $nodes[$cur_node]->getHostname();
2032
2033                                                if( $x_present && $y_present )
2034                                                {
2035                                                        if( $x_first )
2036                                                        {
2037                                                                $nn = sscanf( $host, $skan_str, $rx, $ry );
2038                                                        }
2039                                                        else if( $y_first )
2040                                                        {
2041                                                                $nn = sscanf( $host, $skan_str, $ry, $rx );
2042                                                        }
2043                                                        if ( $nn < 2 )
2044                                                        {
2045                                                                //printf( "skipping node %s - y present & x present + <2 x,y matchs\n", $host);
2046                                                                continue;
2047                                                        }
2048                                                        if( intval( $rx ) > $n )
2049                                                        {
2050                                                                // If x(rack) is higher than current x, skip to next x(rack)
2051                                                                //
2052                                                                $m              = $y_max + 1;
2053
2054                                                                continue;
2055                                                        }
2056                                                        if( intval( $ry ) > $m )
2057                                                        {
2058                                                                // If y(node) is higher than current y, skip to next y(node)
2059                                                                //
2060                                                                continue;
2061                                                        }
2062                                                }
2063                                                else if( $x_present )
2064                                                {
2065                                                        $nn = sscanf( $host, $skan_str, $rx );
2066                                                }
2067                                                else if( $y_present )
2068                                                {
2069                                                        $nn = sscanf( $host, $skan_str, $ry );
2070                                                }
2071
2072                                                if( !in_array( $host, $filtered_nodes ) )
2073                                                {
2074                                                        // This node has been filtered out: we only want to see certain nodes
2075                                                        //
2076                                                        $nodes[$cur_node]->setShowinfo( 0 );
2077                                                }
[575]2078                                                else
2079                                                {
2080                                                        if( $selected_host != '' && $host == $selected_host )
2081                                                        {
2082                                                                $nodes[$cur_node]->setSelected();
2083                                                        }
2084                                                }
[532]2085
2086                                                $nodes[$cur_node]->setCoords( $x, $y );
2087                                                $nodes[$cur_node]->setImage( $image );
2088
2089                                                if( $this->isSmall() )
2090                                                {
2091                                                        $nodes[$cur_node]->drawSmall();
2092                                                }
2093                                                else if( $this->isBig() )
2094                                                {
2095                                                        $nodes[$cur_node]->drawBig();
2096                                                }
2097
2098                                                $cur_node++;
2099                                        }
2100                                }
2101                        }
2102
2103                }
2104                else
2105                {
2106                        if( $this->isSmall() )
2107                        {
2108                                $image          = imageCreateTrueColor( $max_width, ($y_offset + (($node_rows*$node_width)+1) ) );
2109                        }
2110                        else if( $this->isBig() )
2111                        {
2112                                $image_width    = ($node_width * $nodes_nr) + 2;
2113                                $image_width    = ($image_width < $max_width) ? $image_width : $max_width;
2114                                $image          = imageCreateTrueColor( $image_width, ($y_offset + (($node_rows*$node_width)+1) ) );
2115                        }
2116                        $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
2117
2118                        imageFill( $image, 0, 0, $colorwhite );
2119
2120                        if( $this->isSmall() )
2121                        {
2122                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
2123
2124                                imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
2125                        }
2126
2127                        for( $n = 0; $n < $node_rows; $n++ )
2128                        {
2129                                for( $m = 0; $m < $nodes_per_row; $m++ )
2130                                {
2131                                        $x = ($m * $node_width);
2132                                        $y = $y_offset + ($n * $node_width);
2133
2134                                        $cur_node = ($n * $nodes_per_row) + ($m);
2135                                        $host = $nodes_hosts[$cur_node];
2136
2137                                        if( isset( $nodes[$host] ) )
2138                                        {
2139                                                $nodes[$host]->setCoords( $x, $y );
2140                                                $nodes[$host]->setImage( $image );
2141
2142                                                if( !in_array( $host, $filtered_nodes ) )
2143                                                {
2144                                                        $nodes[$host]->setShowinfo( 0 );
2145                                                }
2146
2147                                                if( $this->isSmall() )
2148                                                {
2149                                                        $nodes[$host]->drawSmall();
2150                                                }
2151                                                else if( $this->isBig() )
2152                                                {
2153                                                        $nodes[$host]->drawBig();
2154                                                }
2155                                        }
2156                                }
2157                        }
2158                }
2159       
2160                $this->nodes    = &$nodes;
2161
[573]2162                imageColorTransparent( $image, $colorwhite );
2163
[532]2164                if ($this->output)
2165                {
2166                        header( 'Content-type: image/png' );
2167                        imagePNG( $image );
2168                        imageDestroy( $image );
2169                }
2170        }
2171
2172        function getImagemapArea()
2173        {
2174                $clusterimage_map       = "";
2175
2176                foreach( $this->nodes as $hostname => $node )
2177                {
2178                        $node_map               = $node->getImagemapArea();
2179                        $clusterimage_map       .= $node_map;
2180                }
2181
2182                return $clusterimage_map;
2183        }
2184}
2185
2186class EmptyImage
2187{
2188        function draw()
2189        {
2190                $image          = imageCreateTrueColor( 1, 1 );
2191                $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
2192                imageFill( $image, 0, 0, $colorwhite );                         
2193
2194                header( 'Content-type: image/png' );
2195                imagePNG( $image );
2196                imageDestroy( $image );
2197        }
2198}
2199
2200class HostImage
2201{
2202        var $data_gather, $cluster, $host, $node, $image;
2203        var $headerstrlen;
2204
2205        function HostImage( $data_gather, $cluster, $host )
2206        {
2207                $this->data_gather      = $data_gather;
2208                $this->cluster          = $cluster;
2209                $this->host             = $host;
2210                $this->y_offset         = 0;
2211                $this->font             = 2;
2212                $this->fontspaceing     = 2;
2213                $this->headerstrlen     = array();
2214
2215                $this->fontheight       = ImageFontHeight( $this->font );
2216                $this->fontwidth        = ImageFontWidth( $this->font );
2217
2218                $dg                     = &$this->data_gather;
2219                $this->node             = &$dg->getNode( $this->host );
2220                $n                      = &$this->node;
2221                $this->njobs            = $n->getJobs();
2222        }
2223
2224        function drawJobs()
2225        {
2226                $dg                     = &$this->data_gather;
2227                $colorblack             = imageColorAllocate( $this->image, 0, 0, 0 );
2228
2229                for( $n = 0; $n < count( $this->njobs ); $n++ )
2230                {
2231                        $jobid                  = $this->njobs[$n];
2232                        $jobinfo                = $dg->getJob( $jobid );
2233
2234                        $xoffset                = 5;
2235                        imageString( $this->image, $this->font, $xoffset, $this->y_offset, strval( $jobid ), $colorblack );
2236
2237                        foreach( $this->headerstrlen as $headername => $headerlen )
2238                        {
2239                                if( $headername == 'nodes' )
2240                                {
[577]2241                                        $attrval        = strval( count( $jobinfo['nodes'] ) );
[532]2242                                }
2243                                else if( $headername == 'cpus' )
2244                                {
[577]2245                                        if( !isset( $jobinfo['ppn'] ) )
[532]2246                                        {
[577]2247                                                $jobinfo['ppn'] = 1;
[532]2248                                        }
2249
[577]2250                                        $attrval        = strval( count( $jobinfo['nodes'] ) * intval( $jobinfo['ppn'] ) );
[532]2251                                }
2252                                else if( $headername == 'runningtime' )
2253                                {
[577]2254                                        $attrval        = makeTime( intval( $jobinfo['reported'] ) - intval( $jobinfo['start_timestamp'] ) );
[532]2255                                }
2256                                else
2257                                {
2258                                        $attrval        = strval( $jobinfo[$headername] );
2259                                }
2260
2261                                imageString( $this->image, $this->font, $xoffset, $this->y_offset, $attrval, $colorblack );
2262               
2263                                $xoffset        = $xoffset + ($this->fontwidth * ( $headerlen + 1 ) );
2264                        }
2265                       
2266                        $this->newLineOffset();
2267                }
2268        }
2269
2270        function drawHeader()
2271        {
2272                $dg                     = &$this->data_gather;
2273
2274                for( $n = 0; $n < count( $this->njobs ); $n++ )
2275                {
2276                        $jobid                  = $this->njobs[$n];
2277                        $jobinfo                = $dg->getJob( $jobid );
2278
[577]2279                        if( !isset( $this->headerstrlen['id'] ) )
[532]2280                        {
[577]2281                                $this->headerstrlen['id']       = strlen( strval( $jobid ) );
[532]2282                        }
[577]2283                        else if( strlen( strval( $jobid ) ) > $this->headerstrlen['id'] )
[532]2284                        {
[577]2285                                $this->headerstrlen['id']       = strlen( strval( $jobid ) );
[532]2286                        }
2287
[577]2288                        if( !isset( $this->headerstrlen['owner'] ) )
[532]2289                        {
[577]2290                                $this->headerstrlen['owner']    = strlen( strval( $jobinfo['owner'] ) );
[532]2291                        }
[577]2292                        else if( strlen( strval( $jobinfo['owner'] ) ) > $this->headerstrlen['owner'] )
[532]2293                        {
[577]2294                                $this->headerstrlen['owner']    = strlen( strval( $jobinfo['owner'] ) );
[532]2295                        }
2296
[577]2297                        if( !isset( $this->headerstrlen['queue'] ) )
[532]2298                        {
[577]2299                                $this->headerstrlen['queue']    = strlen( strval( $jobinfo['queue'] ) );
[532]2300                        }
[577]2301                        else if( strlen( strval( $jobinfo['queue'] ) ) > $this->headerstrlen['queue'] )
[532]2302                        {
[577]2303                                $this->headerstrlen['queue']    = strlen( strval( $jobinfo['queue'] ) );
[532]2304                        }
2305
[577]2306                        if( !isset( $jobinfo['ppn'] ) )
[532]2307                        {
[577]2308                                $jobinfo['ppn'] = 1;
[532]2309                        }
2310
[577]2311                        $cpus                   = count( $jobinfo['nodes'] ) * intval( $jobinfo['ppn'] );
[532]2312
[577]2313                        if( !isset( $this->headerstrlen['cpus'] ) )
[532]2314                        {
[577]2315                                $this->headerstrlen['cpus']     = strlen( strval( $cpus ) );
[532]2316                        }
[577]2317                        else if( strlen( strval( $cpus ) ) > $this->headerstrlen['cpus'] )
[532]2318                        {
[577]2319                                $this->headerstrlen['cpus']     = strlen( strval( $cpus ) );
[532]2320                        }
2321
[577]2322                        $nodes                  = count( $jobinfo['nodes'] );
[532]2323
[577]2324                        if( !isset( $this->headerstrlen['nodes'] ) )
[532]2325                        {
[577]2326                                $this->headerstrlen['nodes']    = strlen( strval( $nodes ) );
[532]2327                        }
[577]2328                        else if( strlen( strval( $nodes) ) > $this->headerstrlen['nodes'] )
[532]2329                        {
[577]2330                                $this->headerstrlen['nodes']    = strlen( strval( $nodes ) );
[532]2331                        }
2332
[577]2333                        $runningtime            = makeTime( intval( $jobinfo['reported'] ) - intval( $jobinfo['start_timestamp'] ) );
[532]2334
[577]2335                        if( !isset( $this->headerstrlen['runningtime'] ) )
[532]2336                        {
[577]2337                                $this->headerstrlen['runningtime']      = strlen( strval( $runningtime) );
[532]2338                        }
[577]2339                        else if( strlen( strval( $runningtime) ) > $this->headerstrlen['runningtime'] )
[532]2340                        {
[577]2341                                $this->headerstrlen['runningtime']      = strlen( strval( $runningtime) );
[532]2342                        }
2343
[577]2344                        if( !isset( $this->headerstrlen['name'] ) )
[532]2345                        {
[577]2346                                $this->headerstrlen['name']     = strlen( strval( $jobinfo['name'] ) );
[532]2347                        }
[577]2348                        else if( strlen( strval( $jobinfo['name'] ) ) > $this->headerstrlen['name'] )
[532]2349                        {
[577]2350                                $this->headerstrlen['name']     = strlen( strval( $jobinfo['name'] ) );
[532]2351                        }
2352                }
2353
2354                $xoffset        = 5;
2355
2356                foreach( $this->headerstrlen as $headername => $headerlen )
2357                {
2358                        $colorgreen     = imageColorAllocate( $this->image, 0, 200, 0 );
2359
2360                        if( $headerlen < strlen( $headername ) )
2361                        {
2362                                $this->headerstrlen[$headername]        = strlen( $headername );
2363                        }
2364
2365                        imageString( $this->image, $this->font, $xoffset, $this->y_offset, ucfirst( $headername ), $colorgreen );
2366
2367                        $xoffset        = $xoffset + ($this->fontwidth * ( $this->headerstrlen[$headername] + 1 ) );
2368                }
2369                $this->newLineOffset();
2370        }
2371
2372        function newLineOffset()
2373        {
2374                $this->y_offset         = $this->y_offset + $this->fontheight + $this->fontspaceing;
2375        }
2376
2377        function draw()
2378        {
2379                $xlen           = 450;
2380                $ylen           = ( count( $this->njobs ) * ( $this->fontheight + $this->fontspaceing ) ) + (3 * $this->fontheight);
2381
2382                $this->image    = imageCreateTrueColor( $xlen, $ylen );
2383                $colorwhite     = imageColorAllocate( $this->image, 255, 255, 255 );
2384                imageFill( $this->image, 0, 0, $colorwhite );                         
2385
2386                $colorblue      = imageColorAllocate( $this->image, 0, 0, 255 );
2387
2388                imageString( $this->image, $this->font, 1, $this->y_offset, "Monarch Joblist - host: ".$this->host, $colorblue );
2389                $this->newLineOffset();
2390
2391                $this->drawHeader();
2392                $this->drawJobs();
2393
[576]2394                header("Cache-Control: no-cache, must-revalidate");
2395                header("Pragma: no-cache");
[532]2396                header( 'Content-type: image/png' );
2397                imagePNG( $this->image );
2398                imageDestroy( $this->image );
2399        }
2400}
2401
2402function imageStringDown( &$image, $font, $x, $y, &$s, &$col )
2403{
2404        $fw     = imagefontwidth( $font);
2405        $fh     = imagefontheight( $font);
2406       
2407        $fontspacing = 0;
2408
2409        $fx     = $x;
2410        $fy     = $y;
2411
2412        for( $n=0; $n<strlen( $s ); $n++ )
2413        {
2414                $myc    = $s{$n};
2415
2416                imagestring( $image, $font, $fx, $fy, $myc, $col );
2417
2418                $fy     += ($fontspacing + $fh );
2419        }
2420}
2421
2422function array_rem( $val, &$arr )
2423{
2424        // Delete val from arr
2425        //
2426        $i      = array_search( $val, $arr );
2427
2428        if( $i == false ) return false;
2429
2430        $arr    = array_merge( array_slice( $arr, 0, $i ), array_slice( $arr, $i+1, count( $arr ) ) );
2431
2432        return true;
2433}
2434
2435function cmp( $a, $b ) 
2436{
2437        global $SORT_ORDER;
2438        global $skan_str;
2439        global $x_first, $y_first;
2440        global $x_present, $y_present;
2441
2442        $a_node         = $a;
2443        $b_node         = $b;
2444        $a              = $a_node->getHostname();
2445        $b              = $b_node->getHostname();
2446
2447        if( $a == $b ) return 0;
2448
2449        $a_x            = 0;
2450        $b_x            = 0;
2451        $a_y            = 0;
2452        $b_y            = 0;
2453
2454        if( $x_present && $y_present )
2455        {
2456                if( $x_first )
2457                {
2458                        $n = sscanf( $a, $skan_str, $a_x, $a_y );
2459                        $n = sscanf( $b, $skan_str, $b_x, $b_y );
2460                }
2461                else if( $y_first )
2462                {
2463                        $n = sscanf( $a, $skan_str, $a_y, $a_x );
2464                        $n = sscanf( $b, $skan_str, $b_y, $b_x );
2465                }
2466        } 
2467        else if( $x_present && !$y_present )
2468        {
2469                $n = sscanf( $a, $skan_str, $a_x );
2470                $n = sscanf( $b, $skan_str, $b_x );
2471        }
2472        else if( $y_present && !$x_present )
2473        {
2474                $n = sscanf( $a, $skan_str, $a_y );
2475                $n = sscanf( $b, $skan_str, $b_y );
2476        }
2477
2478        if ( $SORT_ORDER=="desc" )
2479        {
2480
2481                if( $x_present && $y_present )
2482                {
2483                        // 1  = a < b
2484                        // -1 = a > b
2485                        //
2486                        if ($a_x == $b_x)
2487                        {
2488                                if ($a_y < $b_y)
2489                                {
2490                                        return 1;
2491                                }
2492                                else if ($a_y > $b_y)
2493                                {
2494                                        return -1;
2495                                }
2496                        }
2497                        else if ($a_x < $b_x)
2498                        {
2499                                return 1;
2500                        }
2501                        else if ($a_x > $b_x)
2502                        {
2503                                return -1;
2504                        }
2505                } 
2506                else if( $x_present && !$y_present )
2507                {
2508                        if ($a_x < $b_x)
2509                        {
2510                                return 1;
2511                        }
2512                        else if ($a_x > $b_x)
2513                        {
2514                                return -1;
2515                        }
2516                }
2517                else if( $y_present && !$x_present )
2518                {
2519                        if ($a_y < $b_y)
2520                        {
2521                                return 1;
2522                        }
2523                        else if ($a_y > $b_y)
2524                        {
2525                                return -1;
2526                        }
2527                }
2528        }
2529        else if ( $SORT_ORDER == "asc" )
2530        {
2531
2532                if( $x_present && $y_present )
2533                {
2534                        // 1  = a > b
2535                        // -1 = a < b
2536                        //
2537                        if ($a_x == $b_x)
2538                        {
2539                                if ($a_y > $b_y)
2540                                {
2541                                        return 1;
2542                                }
2543                                else if ($a_y < $b_y)
2544                                {
2545                                        return -1;
2546                                }
2547                        }
2548                        else if ($a_x > $b_x)
2549                        {
2550                                return 1;
2551                        }
2552                        else if ($a_x < $b_x)
2553                        {
2554                                return -1;
2555                        }
2556                }
2557                else if( $x_present && !$y_present )
2558                {
2559                        if ($a_x > $b_x)
2560                        {
2561                                return 1;
2562                        }
2563                        else if ($a_x < $b_x)
2564                        {
2565                                return -1;
2566                        }
2567                }
2568                else if( $y_present && !$x_present )
2569                {
2570                        if ($a_y > $b_y)
2571                        {
2572                                return 1;
2573                        }
2574                        else if ($a_y < $b_y)
2575                        {
2576                                return -1;
2577                        }
2578                }
2579        }
2580}
[533]2581
2582function timeToEpoch( $time )
2583{
2584        $time_fields    = explode( ':', $time );
2585
2586        if( count( $time_fields ) == 3 )
2587        {
2588                $hours          = $time_fields[0];
2589                $minutes        = $time_fields[1];
2590                $seconds        = $time_fields[2];
2591
2592        }
2593        else if( count( $time_fields ) == 2 )
2594        {
2595                $hours          = 0;
2596                $minutes        = $time_fields[0];
2597                $seconds        = $time_fields[1];
2598
2599        }
2600        else if( count( $time_fields ) == 1 )
2601        {
2602                $hours          = 0;
2603                $minutes        = 0;
2604                $seconds        = $time_fields[0];
2605        }
2606
2607        $myepoch        = intval( $seconds + (intval( $minutes * 60 )) + (intval( $hours * 3600 )) );
2608
2609        return $myepoch;
2610}
2611
[532]2612function makeTime( $time )
2613{
2614        $days = intval( $time / 86400 );
2615        $time = ($days>0) ? $time % ($days * 86400) : $time;
2616
2617        $date_str = '';
2618        $day_str = '';
2619
2620        if( $days > 0 )
2621        {
[534]2622                if( $days > 1 )
[532]2623                {
[534]2624                        $date_str .= $days . ' days';
[532]2625                }
[534]2626                else
[532]2627                {
[534]2628                        $date_str .= $days . ' day';
[532]2629                }
2630        }
2631
2632        $hours = intval( $time / 3600 );
2633        $time = $hours ? $time % ($hours * 3600) : $time;
2634
2635        if( $hours > 0 )
2636        {
[534]2637                if( $hours > 1 )
2638                {
2639                        $date_unit      = 'hours';
2640                }
2641                else
2642                {
2643                        $date_unit      = 'hour';
2644                }
2645
2646                if( $date_str != '' )
2647                {
2648                        $date_str .= ' ' . $hours . ' ' . $date_unit;
2649                }
2650                else
2651                {
2652                        $date_str .= $hours . ' ' . $date_unit;
2653                }
[532]2654        }
2655
2656        $minutes = intval( $time / 60 );
2657        $seconds = $minutes ? $time % ($minutes * 60) : $time;
2658
2659        if( $minutes > 0 )
2660        {
[534]2661                if( $minutes > 1 )
[532]2662                {
[534]2663                        $date_unit      = 'minutes';
[532]2664                }
[534]2665                else
[532]2666                {
[534]2667                        $date_unit      = 'minute';
[532]2668                }
2669
[534]2670                if( $date_str != '' )
[532]2671                {
[534]2672                        $date_str .= ' ' . $minutes . ' ' . $date_unit;
2673                }
2674                else
2675                {
2676                        $date_str .= $minutes . ' ' . $date_unit;
2677                }
[532]2678        }
2679
2680        if( $seconds > 0 )
2681        {
[534]2682                if( $seconds > 1 )
[532]2683                {
[534]2684                        $date_unit      = 'seconds';
[532]2685                }
[534]2686                else
[532]2687                {
[534]2688                        $date_unit      = 'second';
[532]2689                }
2690
[534]2691                if( $date_str != '' )
[532]2692                {
[534]2693                        $date_str .= ' ' . $seconds . ' ' . $date_unit;
[532]2694                }
[534]2695                else
[532]2696                {
[534]2697                        $date_str .= $seconds . ' ' . $date_unit;
[532]2698                }
2699        }
[534]2700
[532]2701        return $date_str;
2702}
2703?>
Note: See TracBrowser for help on using the repository browser.