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

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

job_monarch/libtoga.php,
job_monarch/clusterconf/gina-example.php:

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