source: branches/0.3/web/addons/job_monarch/libtoga.php @ 650

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

web/addons/job_monarch/libtoga.php:

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