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

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

jobmond/jobmond.py:

  • added report time to down/offline nodes
  • added domain name to down/offline nodes

web/addons/job_monarch/libtoga.php:

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