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

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

libtoga.php:

  • dont run na code if info unavailable
  • Property svn:keywords set to Id
File size: 50.3 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 518 2008-03-08 20:51:57Z 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                        if( sizeof( $this->down_nodes ) > 0 )
892                        {
893                                foreach( $this->down_nodes as $reported => $dnodes )
894                                {
895
896                                        if( $reported == $this->heartbeat['time'] )
897                                        {
898                                                $domain = $dnodes[1];
899
900                                                foreach( $dnodes[0] as $downhost )
901                                                {
902                                                        $downhost = $this->makeHostname( $downhost, $domain );
903
904                                                        if( isset( $nodes[$downhost] ) )
905                                                        {
906                                                                // OMG PHP4 is fking stupid!
907                                                                // $nodes[$downhost]->setDown( 1 ) won't work here..
908                                                                //
909                                                                $mynode = $nodes[$downhost];
910                                                                $mynode->setDown( 1 );
911                                                                $nodes[$downhost] = $mynode;
912                                                        }
913                                                }
914                                        }
915                                }
916                        }
917
918                        if( sizeof( $this->offline_nodes ) > 0 )
919                        {
920                                foreach( $this->offline_nodes as $reported => $onodes )
921                                {
922                                        if( $reported == $this->heartbeat['time'] )
923                                        {
924                                                $domain = $onodes[1];
925
926                                                foreach( $onodes[0] as $offlinehost )
927                                                {
928                                                        $offlinehost = $this->makeHostname( $offlinehost, $domain );
929
930                                                        if( isset( $nodes[$offlinehost] ) )
931                                                        {
932                                                                // OMG PHP4 is fking stupid!
933                                                                // $nodes[$offlinehost]->setDown( 1 ) won't work here..
934                                                                //
935                                                                $mynode = $nodes[$offlinehost];
936                                                                $mynode->setOffline( 1 );
937                                                                $nodes[$offlinehost] = $mynode;
938                                                        }
939                                                }
940                                        }
941                                }
942                        }
943                }
944
945                $this->nodes = $nodes;
946        }
947
948        function printInfo() {
949
950                $jobs = &$this->jobs;
951
952                printf( "---jobs---\n" );
953
954                foreach( $jobs as $jobid => $job ) {
955
956                        printf( "job %s\n", $jobid );
957
958                        if( isset( $job[nodes] ) ) {
959
960                                foreach( $job[nodes] as $node ) {
961
962                                        $mynode = $this->nodes[$node];
963                                        $hostname = $mynode->getHostname();
964                                        $location = $mynode->getLocation();
965
966                                        printf( "\t- node %s\tlocation %s\n", $hostname, $location );
967                                        //$this->nodes[$hostname]->setLocation( "hier draait job ".$jobid );
968                                }
969                        }
970                }
971
972                printf( "---nodes---\n" );
973
974                $nodes = &$this->nodes;
975
976                foreach( $nodes as $node ) {
977
978                        $hostname = $node->getHostname();
979                        $location = $node->getLocation();
980                        $jobs = implode( ' ', $node->getJobs() );
981                        printf( "* node %s\tlocation %s\tjobs %s\n", $hostname, $location, $jobs );
982                }
983        }
984
985        function getNodes() {
986                //print_r( $this->nodes );
987                return $this->nodes;
988        }
989
990        function getNode( $node ) {
991
992                $nodes = &$this->nodes;
993                if( isset( $nodes[$node] ) )
994                        return $nodes[$node];
995                else
996                        return NULL;
997        }
998
999        function getJobs() {
1000                return $this->jobs;
1001        }
1002
1003        function getJob( $job ) {
1004
1005                $jobs = &$this->jobs;
1006                if( isset( $jobs[$job] ) )
1007                        return $jobs[$job];
1008                else
1009                        return NULL;
1010        }
1011
1012        function getHeartbeat() {
1013                return $this->heartbeat['time'];
1014        }
1015}
1016
1017class NodeImage {
1018
1019        var $image, $x, $y, $hostname, $jobs, $tasks, $showinfo;
1020
1021        function NodeImage( $cluster, $hostname ) {
1022
1023                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
1024
1025                $this->jobs = array();
1026                //$this->image = $image;
1027                //$this->x = $x;
1028                //$this->y = $y;
1029                $this->tasks = 0;
1030                $this->hostname = $hostname;
1031                $this->cpus = $this->determineCpus();
1032                $this->clustername = $cluster;
1033                $this->showinfo = 1;
1034                $this->size = $SMALL_CLUSTERIMAGE_NODEWIDTH;
1035                $this->down = 0;
1036                $this->offline = 0;
1037        }
1038
1039        function addJob( $jobid, $cpus ) {
1040                $jobs = &$this->jobs;
1041
1042                $jobs[] = $jobid;
1043                $this->jobs = $jobs;
1044
1045                $this->addTask( $cpus );
1046        }
1047
1048        function hasJob( $jobid ) {
1049
1050                $jobfound = 0;
1051
1052                if( count( $this->jobs ) > 0 )
1053                        foreach( $this->jobs as $job )
1054
1055                                if( $job == $jobid )
1056                                        $jobfound = 1;
1057
1058                return $jobfound;
1059        }
1060
1061        function addTask( $cpus ) {
1062
1063                $this->tasks = $this->tasks + $cpus;
1064        }
1065
1066        function setDown( $down ) {
1067
1068                $this->down = $down;
1069        }
1070
1071        function isDown() {
1072
1073                return $this->down;
1074        }
1075        function setOffline( $offline ) {
1076
1077                $this->offline = $offline;
1078        }
1079
1080        function isOffline() {
1081
1082                return $this->offline;
1083        }
1084
1085        function setImage( $image ) {
1086
1087                $this->image = $image;
1088        }
1089
1090        function setCoords( $x, $y ) {
1091
1092                $this->x = $x;
1093                $this->y = $y;
1094        }
1095
1096        function getImagemapArea() {
1097
1098                $area_topleft           = $this->x . "," . $this->y;
1099                $area_bottomright       = ($this->x + $this->size) . "," . ($this->y + $this->size);
1100                $area_coords            = $area_topleft . "," . $area_bottomright;
1101
1102                $area_href              = "./?c=" . $this->clustername . "&h=" . $this->hostname;
1103
1104                $area_tooltip           = $this->hostname;
1105
1106                if( $this->down)
1107                {
1108                        $area_tooltip           = $area_tooltip . ": DOWN";
1109                }
1110                else if( $this->offline )
1111                {
1112                        $area_tooltip           = $area_tooltip . ": OFFLINE";
1113                }
1114
1115                $area_tooltip           = $area_tooltip . ": " . implode( " ", $this->jobs );
1116
1117                $tag_href               = "HREF=\"" . $area_href . "\"";
1118                $tag_coords             = "COORDS=\"" . $area_coords . "\"";
1119                $tag_tooltip1           = "ALT=\"" . $area_tooltip . "\"";
1120                $tag_tooltip2           = "TITLE=\"" . $area_tooltip . "\"";
1121
1122                return ("<AREA SHAPE=\"RECT\" " . $tag_coords . " " . $tag_href . " " . $tag_tooltip1 . " " . $tag_tooltip2 . ">");
1123        }
1124
1125        function colorHex( $color ) {
1126       
1127                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) );
1128
1129                return $my_color;
1130        }
1131
1132        function setLoad( $load ) {
1133                $this->load = $load;
1134        }
1135
1136        function setHostname( $hostname ) {
1137                $this->hostname = $hostname;
1138        }
1139
1140        function getHostname() {
1141                return $this->hostname;
1142        }
1143
1144        function getJobs() {
1145                return $this->jobs;
1146        }
1147
1148        function setShowinfo( $showinfo ) {
1149                $this->showinfo = $showinfo;
1150        }
1151
1152        function drawSmall() {
1153
1154                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
1155
1156                $this->size     = $SMALL_CLUSTERIMAGE_NODEWIDTH;
1157
1158                $this->draw();
1159        }
1160
1161        function drawBig() {
1162
1163                global $BIG_CLUSTERIMAGE_NODEWIDTH;
1164
1165                $this->size     = $BIG_CLUSTERIMAGE_NODEWIDTH;
1166
1167                $this->draw();
1168        }
1169
1170        function draw() {
1171
1172                global $JOB_NODE_MARKING, $NODE_DOWN_MARKING, $NODE_OFFLINE_MARKING;
1173
1174                $black_color = imageColorAllocate( $this->image, 0, 0, 0 );
1175                $size = $this->size;
1176
1177                imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+($size), $this->y+($size), $black_color );
1178
1179                if( $this->showinfo) {
1180               
1181                        $this->load = $this->determineLoad();
1182
1183                        if( !isset( $this->image ) or !isset( $this->x ) or !isset( $this->y ) ) {
1184                                printf( "aborting\n" );
1185                                printf( "x %d y %d load %f\n", $this->x, $this->y, $load );
1186                                return;
1187                        }
1188
1189
1190                        // Convert Ganglias Hexadecimal load color to a Decimal one
1191                        //
1192                        $load = $this->determineLoad(); 
1193                        $usecolor = $this->colorHex( load_color($load) );
1194                        imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor );
1195                        if( $this->down )
1196                        {
1197                                imageString( $this->image, 1, $this->x+(($size/2)-1), $this->y+(($size/2)-4), $NODE_DOWN_MARKING, $black_color );
1198                        }
1199                        else if( $this->offline )
1200                        {
1201                                imageString( $this->image, 1, $this->x+(($size/2)-1), $this->y+(($size/2)-4), $NODE_OFFLINE_MARKING, $black_color );
1202                        }
1203                        else if( count( $this->jobs ) > 0 )
1204                        {
1205                                imageString( $this->image, 1, $this->x+(($size/2)-1), $this->y+(($size/2)-4), $JOB_NODE_MARKING, $black_color );
1206                        }
1207
1208                } else {
1209
1210                        // White
1211                        $usecolor = imageColorAllocate( $this->image, 255, 255, 255 );
1212                        imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor );
1213                }
1214
1215
1216        }
1217
1218        function determineCpus() {
1219
1220                global $metrics;
1221
1222                $cpus = $metrics[$this->hostname][cpu_num][VAL];
1223                if (!$cpus) $cpus=1;
1224
1225                return $cpus;
1226        }
1227
1228        function determineLoad() {
1229
1230                global $metrics;
1231
1232                $load_one = $metrics[$this->hostname][load_one][VAL];
1233                $load = ((float) $load_one)/$this->cpus;
1234
1235                return $load;
1236        }
1237}
1238
1239class ClusterImage {
1240
1241        var $dataget, $image, $clustername;
1242        var $filtername, $filters;
1243
1244        //function ClusterImage( $clustername ) {
1245        function ClusterImage( $data, $clustername ) {
1246
1247                //$this->dataget                = $dataget;
1248                $this->dataget          = new DataGatherer( $clustername );
1249                $this->data             = $data;
1250                $this->clustername      = $clustername;
1251                $this->filters          = array();
1252                $this->size             = 's';
1253                $this->width            = 0;
1254                $this->height           = 0;
1255                $this->output           = 1;
1256        }
1257
1258        function getWidth() {
1259                return $this->width;
1260        }
1261        function getHeight() {
1262                return $this->height;
1263        }
1264
1265        function setSmall() {
1266                $this->size     = 's';
1267        }
1268
1269        function setBig() {
1270                $this->size     = 'b';
1271        }
1272
1273        function setNoimage() {
1274                $this->output   = 0;
1275        }
1276
1277        function isSmall() {
1278                return ($this->size == 's');
1279        }
1280
1281        function isBig() {
1282                return ($this->size == 'b');
1283        }
1284
1285        function setFilter( $filtername, $filtervalue ) {
1286
1287                $this->filters[$filtername] = $filtervalue;
1288        }
1289
1290        function filterNodes( $jobs, $nodes ) {
1291
1292                $filtered_nodes = array();
1293
1294                foreach( $nodes as $node ) {
1295
1296                        $hostname = $node->getHostname();
1297
1298                        $addhost = 1;
1299
1300                        if( count( $this->filters ) > 0 ) {
1301
1302                                $mynjobs = $node->getJobs();
1303
1304                                if( count( $mynjobs ) > 0 ) {
1305
1306                                        foreach( $mynjobs as $myjob ) {
1307
1308                                                foreach( $this->filters as $filtername => $filtervalue ) {
1309
1310                                                        if( $filtername!=null && $filtername!='' ) {
1311
1312                                                                if( $filtername == 'jobid' && !$node->hasJob( $filtervalue) ) {
1313                                                                        $addhost = 0;
1314                                                                } else if( $filtername != 'jobid' ) {
1315                                                                        if( $jobs[$myjob][$filtername] != $filtervalue ) {
1316                                                                                $addhost = 0;
1317                                                                        }
1318                                                                }
1319                                                        }
1320                                                }
1321                                        }
1322                                } else
1323                                        $addhost = 0;
1324                        }
1325
1326                        if( $addhost )
1327                                $filtered_nodes[] = $hostname;
1328                }
1329
1330                return $filtered_nodes;
1331        }
1332
1333        function draw() {
1334
1335                global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH;
1336                global $BIG_CLUSTERIMAGE_MAXWIDTH, $BIG_CLUSTERIMAGE_NODEWIDTH;
1337                global $CLUSTER_CONFS, $confcluster;
1338
1339                global $SORTBY_HOSTNAME, $SORT_ORDER, $skan_str;
1340                global $x_first, $y_first;
1341
1342                foreach( $CLUSTER_CONFS as $confcluster => $conffile )
1343                {
1344                        if( strtolower( trim($this->clustername) ) == strtolower(trim($confcluster)) )
1345                        {
1346                                include_once $conffile;
1347                        }
1348                }
1349
1350                //global $SORTBY_HOSTNAME, $SORT_ORDER;
1351                //global $SORT_XLABEL, $SORT_YLABEL;
1352       
1353                //printf( "SORTBY_HOSTNAME %s SORT_YLABEL %s\n", $SORTBY_HOSTNAME, $SORT_YLABEL );
1354
1355                $mydatag = $this->dataget;
1356                $mydatag->parseXML( $this->data );
1357
1358                if( $this->isSmall() ) {
1359                        $max_width = $SMALL_CLUSTERIMAGE_MAXWIDTH;
1360                        $node_width = $SMALL_CLUSTERIMAGE_NODEWIDTH;
1361                } else if( $this->isBig() ) {
1362                        $max_width = $BIG_CLUSTERIMAGE_MAXWIDTH;
1363                        $node_width = $BIG_CLUSTERIMAGE_NODEWIDTH;
1364                }
1365
1366                $nodes = $mydatag->getNodes();
1367                $nodes_hosts = array_keys( $nodes );
1368
1369                $nodes_nr = count( $nodes );
1370
1371                $nodes_size = $nodes_nr*$node_width;
1372                $node_rows = 0;
1373
1374                if( $nodes_size > $max_width ) {
1375                        $nodes_per_row = ( (int) ($max_width/$node_width) );
1376                } else {
1377                        $nodes_per_row = $nodes_size;
1378                        $node_rows = 1;
1379                }
1380
1381                if( $nodes_per_row < $nodes_nr ) {
1382                        $node_rows = ( (int) ($nodes_nr/$nodes_per_row) );
1383                        $node_rest = fmod( $nodes_nr, $nodes_per_row );
1384                        //printf( "nodesnr %d noderest %f\n", $nodes_nr, $node_rest );
1385                        if( $node_rest > 0 ) {
1386                                $node_rows++;
1387                                //printf( "noderows %d\n", $node_rows );
1388                        }
1389                }
1390
1391                $y_offset       = 0;
1392                $font           = 2;
1393                $fontwidth      = ImageFontWidth( $font );
1394                $fontheight     = ImageFontHeight( $font );
1395                $fontspaceing   = 2;
1396                $y_offset       = $fontheight + (2 * $fontspaceing);
1397
1398                $this->width    = $max_width;
1399                $this->height   = ($y_offset + (($node_rows*$node_width)+1) );
1400
1401                //$image = imageCreateTrueColor( $max_width, ($y_offset + (($node_rows*$node_width)+1) ) );
1402                //$colorwhite = imageColorAllocate( $image, 255, 255, 255 );
1403                //imageFill( $image, 0, 0, $colorwhite );
1404
1405                //if( $this->isSmall() ) {
1406
1407                //      $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1408
1409                //      imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1410                //}
1411
1412                $jobs = $mydatag->getJobs();
1413                //printf("filtername = %s\n", $filtername );
1414                $filtered_nodes = $this->filterNodes( $jobs, $nodes );
1415
1416                //print_r($filtered_nodes);
1417
1418                if( $SORTBY_HOSTNAME != "" )
1419                {
1420
1421                        $sorted         = array();
1422
1423                        $x_first        = 0;
1424                        $y_first        = 0;
1425
1426                        $skan_str       = $SORTBY_HOSTNAME;
1427
1428                        global $x_present, $y_present;
1429                        $x_present      = false;
1430                        $y_present      = false;
1431
1432                        // Should we scan by X, Y or both
1433                        //
1434                        if(strpos( $SORTBY_HOSTNAME, "{x}" ) != false )
1435                        {
1436                                $x_str          = "{x}";
1437                                $x_present      = true;
1438                        }
1439                        else if(strpos( $SORTBY_HOSTNAME, "{X}" ) != false )
1440                        {
1441                                $x_str          = "{X}";
1442                                $x_present      = true;
1443                        }
1444                        if(strpos( $SORTBY_HOSTNAME, "{y}" ) != false )
1445                        {
1446                                $y_str          = "{y}";
1447                                $y_present      = true;
1448                        }
1449                        else if(strpos( $SORTBY_HOSTNAME, "{Y}" ) != false )
1450                        {
1451                                $y_str          = "{Y}";
1452                                $y_present      = true;
1453                        }
1454
1455                        // If we should scan for both X and Y: see which one is first
1456                        //
1457                        if(( strpos( $SORTBY_HOSTNAME, $x_str ) < strpos( $SORTBY_HOSTNAME, $y_str ) ) && ( $x_present && $y_present ))
1458                        {
1459                       
1460                                $x_first        = 1;
1461                        }
1462                        else if(( strpos( $SORTBY_HOSTNAME, $x_str ) > strpos( $SORTBY_HOSTNAME, $y_str ) ) && ( $x_present && $y_present ))
1463                        {
1464                                $y_first        = 1;
1465               
1466                        }
1467                        else if( $x_present )
1468                        {
1469                                $x_first        = 1;
1470                        }
1471                        else if( $y_present )
1472                        {
1473                                $y_first        = 1;
1474                        }
1475
1476                        // Now replace our {x} and {y} with %d for sscanf parsing
1477                        //
1478                        if(( $x_first ) && ( $x_present && $y_present ) )
1479                        {
1480                                $skan_str       = str_replace( $x_str, "%d", $skan_str );
1481                                $skan_str       = str_replace( $y_str, "%d", $skan_str );
1482                        } 
1483                        else if( $x_present)
1484                        {
1485                                $skan_str       = str_replace( $x_str, "%d", $skan_str );
1486                        }
1487                        else if( $y_present)
1488                        {
1489                                $skan_str       = str_replace( $y_str, "%d", $skan_str );
1490                        }
1491
1492                        $x_min          = null;
1493                        $x_max          = null;
1494                        $y_min          = null;
1495                        $y_max          = null;
1496
1497                        // Now let's walk through all our nodes and see which one are valid for our scan pattern
1498                        //
1499                        foreach( $nodes as $hostname => $node )
1500                        {
1501                                $x      = null;
1502                                $y      = null;
1503
1504                                if( $x_present && $y_present )
1505                                {
1506                                        if( $x_first )
1507                                        {
1508                                                $n = sscanf( $hostname, $skan_str, $x, $y );
1509                                        }
1510                                        else if( $y_first )
1511                                        {
1512                                                $n = sscanf( $hostname, $skan_str, $y, $x );
1513                                        }
1514
1515                                        // Remove nodes that don't match
1516                                        //
1517                                        if( $n < 2 )
1518                                        {
1519                                                // This node hostname has no match for: {x} and {y}
1520                                                //
1521                                                unset( $nodes[$hostname] );
1522                                        }
1523                                }
1524                                else if( $x_present && !$y_present )
1525                                {
1526                                        $n = sscanf( $hostname, $skan_str, $x );
1527
1528                                        // Remove nodes that don't match
1529                                        //
1530                                        if( $n < 1 )
1531                                        {
1532                                                // This node hostname has no match for: {x}
1533                                                //
1534                                                unset( $nodes[$hostname] );
1535                                        }
1536                                        $y      = 1;
1537                                }
1538                                else if( $y_present && !$x_present )
1539                                {
1540                                        $n = sscanf( $hostname, $skan_str, $y );
1541
1542                                        // Remove nodes that don't match
1543                                        //
1544                                        if( $n < 1 )
1545                                        {
1546                                                // This node hostname has no match for: {y}
1547                                                //
1548                                                unset( $nodes[$hostname] );
1549                                        }
1550                                        $x      = 1;
1551                                }
1552
1553                                // Determine the lowest value of {x} that exists in all node hostnames
1554                                //
1555                                if( !$x_min && $x != null )
1556                                {
1557                                        $x_min  = $x;
1558                                }
1559                                else if( $x < $x_min && $x != null )
1560                                {
1561                                        $x_min  = $x;
1562                                }
1563
1564                                // Determine the highest value of {x} that exists in all node hostnames
1565                                //
1566                                if( !$x_max && $x != null )
1567                                {
1568                                        $x_max  = $x;
1569                                }
1570                                else if( $x > $x_max && $x != null )
1571                                {
1572                                        $x_max  = $x;
1573                                }
1574
1575                                // Determine the lowest value of {y} that exists in all node hostnames
1576                                //
1577                                if( !$y_min && $y != null )
1578                                {
1579                                        $y_min  = $y;
1580                                }
1581                                else if( $y < $y_min && $y != null )
1582                                {
1583                                        $y_min  = $y;
1584                                }
1585
1586                                // Determine the highest value of {y} that exists in all node hostnames
1587                                //
1588                                if( !$y_max && $y != null )
1589                                {
1590                                        $y_max  = $y;
1591                                }
1592                                else if( $y > $y_max && $y != null )
1593                                {
1594                                        $y_max  = $y;
1595                                }
1596                        }
1597
1598                        // Sort all the nodes (alpha and numerically)
1599                        // 1: gb-r1n1, 2: gb-r1n2, 3: gb-r2n1, etc
1600                        //
1601                        $sorted_nodes   = usort( $nodes, "cmp" );
1602
1603                        $cur_node       = 0;
1604
1605                        $x_offset       = 0;
1606                        $y_offset       = 0;
1607                        $font           = 2;
1608                        $fontwidth      = ImageFontWidth( $font );
1609                        $fontheight     = ImageFontHeight( $font );
1610                        $fontspaceing   = 2;
1611
1612                        if( $this->isSmall() ) 
1613                        {
1614                                $y_offset       = $y_offset + (2 * $fontspaceing) + $fontheight;
1615                        }
1616
1617                        if( $this->isBig() ) 
1618                        {
1619
1620                                $y_offset       = ($fontheight * (1 + strlen( $x_max) ) ) + ((2 + strlen( $x_max)) * $fontspaceing);
1621                                $x_offset       = ($fontwidth * (1 + strlen( $y_max) ) ) + ((2 + strlen( $y_max)) * $fontspaceing);
1622
1623                        }
1624                        //$x_offset     = ($fontwidth * 3) + (5 * $fontspaceing);
1625
1626                        //printf( "xmin %s xmax %s\n", $x_min, $x_max );
1627                        //printf( "ymin %s ymax %s\n", $y_min, $y_max );
1628
1629                        // werkt
1630                        //print_r( $nodes );
1631
1632                        $image_width    = $x_offset + ($node_width * ($x_max-$x_min+2));
1633
1634                        if( $this->isSmall() ) 
1635                        {
1636                                $image_width    = $max_width;
1637                        } else if( $this->isBig() ) 
1638                        {
1639                                $image_width    = ($image_width < $max_width) ? $image_width : $max_width;
1640                        }
1641                        //else if( $this->isSmall() )
1642                        //{
1643                        //      $image_width    = $this->width;
1644                        //}
1645                        $image_height   = $y_offset + ($node_width * ($y_max-$y_min+2));
1646
1647                        $this->width    = $image_width;
1648                        $this->heigth   = $image_heigth;
1649
1650                        $image          = imageCreateTrueColor( $image_width, $image_height );
1651                        $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1652
1653                        imageFill( $image, 0, 0, $colorwhite );
1654
1655                        if( $this->isSmall() ) {
1656
1657                                // Draw a fancy little header text to explain what it is
1658                                //
1659                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1660
1661                                imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1662                        }
1663
1664                        if( $this->isBig() && ( isset( $SORT_XLABEL ) || isset( $SORT_YLABEL ) ) )
1665                        {
1666                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1667
1668                                if( isset( $SORT_XLABEL ) )
1669                                {
1670                                        // Print the {x} label: rack
1671                                        //
1672                                        imageString( $image, $font, $x_offset, $fontspaceing, $SORT_XLABEL, $colorblue );
1673                                }
1674
1675                                if( isset( $SORT_YLABEL ) )
1676                                {
1677                                        // Stupid php without imageStringDown function... we'll make one ourself
1678                                        //
1679
1680                                        // Print the {y} label: node
1681                                        //
1682                                        imageStringDown( $image, $font, $fontspaceing, $y_offset, $SORT_YLABEL, $colorblue );
1683                                }
1684                        }
1685
1686                        for( $n = $x_min; $n <= $x_max; $n++ )
1687                        {
1688                                for( $m = $y_min; $m <= $y_max; $m++ )
1689                                {
1690
1691                                        if( $x_min > 0 )
1692                                        {
1693                                                $x      = $x_offset + ( ($n-$x_min) * $node_width );
1694                                        }
1695                                        if( $y_min > 0 )
1696                                        {
1697                                                $y      = $y_offset + ( ($m-$y_min) * $node_width );
1698                                        }
1699
1700                                        if( $this->isBig() ) 
1701                                        {
1702                                                // Draw y(node) column number header
1703                                                //
1704                                                if(( $n == $x_min ) && ( isset($SORT_YLABEL) ) )
1705                                                {
1706                                                        $mfontspacing   = 1;
1707
1708                                                        $ylabel_x       = $x - ( $fontwidth * strlen( $y_max ) ) - $mfontspacing;
1709                                                        $ylabel_y       = $y;
1710
1711                                                        imageString( $image, $font, $ylabel_x, $ylabel_y, strval( $m ), $colorblue );
1712
1713                                                        $xmin_hit[$n]   = true;
1714                                                }
1715
1716                                                // Draw x(rack) column number header
1717                                                //
1718                                                if(( $m == $y_min ) && ( isset($SORT_XLABEL) ) )
1719                                                {
1720                                                        $mfontspacing   = 2;
1721                                                        $xlabel_y       = $y - ( $fontheight * strlen( $x_max ) );
1722                                                        $xlabel_x       = $x + $mfontspacing; 
1723
1724                                                        imageStringDown( $image, $font, $xlabel_x, $xlabel_y, strval( $n ), $colorblue );
1725                                                }
1726                                        }
1727
1728                                        if( isset( $nodes[$cur_node] ) ) 
1729                                        {
1730                                                $host   = $nodes[$cur_node]->getHostname();
1731
1732                                                if( $x_present && $y_present )
1733                                                {
1734                                                        if( $x_first )
1735                                                        {
1736                                                                $nn = sscanf( $host, $skan_str, $rx, $ry );
1737                                                        }
1738                                                        else if( $y_first )
1739                                                        {
1740                                                                $nn = sscanf( $host, $skan_str, $ry, $rx );
1741                                                        }
1742                                                        if ( $nn < 2 )
1743                                                        {
1744                                                                //printf( "skipping node %s - y present & x present + <2 x,y matchs\n", $host);
1745                                                                continue;
1746                                                        }
1747                                                        if( intval( $rx ) > $n )
1748                                                        {
1749                                                                // If x(rack) is higher than current x, skip to next x(rack)
1750                                                                //
1751                                                                $m              = $y_max + 1;
1752
1753                                                                continue;
1754                                                        }
1755                                                        if( intval( $ry ) > $m )
1756                                                        {
1757                                                                // If y(node) is higher than current y, skip to next y(node)
1758                                                                //
1759                                                                continue;
1760                                                        }
1761                                                }
1762                                                else if( $x_present )
1763                                                {
1764                                                        $nn = sscanf( $host, $skan_str, $rx );
1765                                                }
1766                                                else if( $y_present )
1767                                                {
1768                                                        $nn = sscanf( $host, $skan_str, $ry );
1769                                                }
1770
1771                                                if( !in_array( $host, $filtered_nodes ) )
1772                                                {
1773                                                        // This node has been filtered out: we only want to see certain nodes
1774                                                        //
1775                                                        $nodes[$cur_node]->setShowinfo( 0 );
1776                                                }
1777
1778                                                $nodes[$cur_node]->setCoords( $x, $y );
1779                                                $nodes[$cur_node]->setImage( $image );
1780
1781                                                //print_r( $nodes[$cur_node] );
1782
1783                                                if( $this->isSmall() )
1784                                                        $nodes[$cur_node]->drawSmall();
1785                                                else if( $this->isBig() )
1786                                                        $nodes[$cur_node]->drawBig();
1787
1788                                                $cur_node++;
1789                                        }
1790                                }
1791                        }
1792
1793                }
1794                else
1795                {
1796                        if( $this->isSmall() ) {
1797                                $image          = imageCreateTrueColor( $max_width, ($y_offset + (($node_rows*$node_width)+1) ) );
1798                        } else if( $this->isBig() ) {
1799                                $image_width    = ($node_width * $nodes_nr) + 2;
1800                                $image_width    = ($image_width < $max_width) ? $image_width : $max_width;
1801                                $image          = imageCreateTrueColor( $image_width, ($y_offset + (($node_rows*$node_width)+1) ) );
1802                        }
1803                        $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1804
1805                        imageFill( $image, 0, 0, $colorwhite );
1806
1807                        if( $this->isSmall() ) {
1808
1809                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1810
1811                                imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1812                        }
1813
1814                        for( $n = 0; $n < $node_rows; $n++ ) {
1815                       
1816                                for( $m = 0; $m < $nodes_per_row; $m++ ) {
1817                       
1818                                        $x = ($m * $node_width);
1819                                        $y = $y_offset + ($n * $node_width);
1820
1821                                        $cur_node = ($n * $nodes_per_row) + ($m);
1822                                        $host = $nodes_hosts[$cur_node];
1823
1824                                        if( isset( $nodes[$host] ) ) {
1825
1826                                                $nodes[$host]->setCoords( $x, $y );
1827                                                $nodes[$host]->setImage( $image );
1828
1829                                                if( !in_array( $host, $filtered_nodes ) )
1830                                                {
1831                                                        $nodes[$host]->setShowinfo( 0 );
1832                                                }
1833
1834                                                if( $this->isSmall() )
1835                                                        $nodes[$host]->drawSmall();
1836                                                else if( $this->isBig() )
1837                                                        $nodes[$host]->drawBig();
1838                                        }
1839                                }
1840                        }
1841                }
1842       
1843                $this->nodes    = &$nodes;
1844
1845                if ($this->output) {
1846                        header( 'Content-type: image/png' );
1847                        imagePNG( $image );
1848                        imageDestroy( $image );
1849                }
1850        }
1851
1852        function getImagemapArea() {
1853
1854                $clusterimage_map       = "";
1855
1856                foreach( $this->nodes as $hostname => $node ) {
1857
1858                        $node_map               = $node->getImagemapArea();
1859                        $clusterimage_map       .= $node_map;
1860                }
1861
1862                return $clusterimage_map;
1863        }
1864}
1865
1866class EmptyImage {
1867
1868        function draw() {
1869                $image          = imageCreateTrueColor( 1, 1 );
1870                $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1871                imageFill( $image, 0, 0, $colorwhite );                         
1872
1873                header( 'Content-type: image/png' );
1874                imagePNG( $image );
1875                imageDestroy( $image );
1876        }
1877}
1878
1879class HostImage {
1880
1881        var $data_gather, $cluster, $host, $node, $image;
1882        var $headerstrlen;
1883
1884        function HostImage( $data_gather, $cluster, $host ) {
1885
1886                $this->data_gather      = $data_gather;
1887                $this->cluster          = $cluster;
1888                $this->host             = $host;
1889                $this->y_offset         = 0;
1890                $this->font             = 2;
1891                $this->fontspaceing     = 2;
1892                $this->headerstrlen     = array();
1893
1894                $this->fontheight       = ImageFontHeight( $this->font );
1895                $this->fontwidth        = ImageFontWidth( $this->font );
1896
1897                $dg                     = &$this->data_gather;
1898                $this->node             = &$dg->getNode( $this->host );
1899                $n                      = &$this->node;
1900                $this->njobs            = $n->getJobs();
1901        }
1902
1903        function drawJobs() {
1904
1905                $dg                     = &$this->data_gather;
1906                $colorblack             = imageColorAllocate( $this->image, 0, 0, 0 );
1907
1908                for( $n = 0; $n < count( $this->njobs ); $n++ ) {
1909
1910                        $jobid                  = $this->njobs[$n];
1911                        $jobinfo                = $dg->getJob( $jobid );
1912
1913                        $xoffset                = 5;
1914                        imageString( $this->image, $this->font, $xoffset, $this->y_offset, strval( $jobid ), $colorblack );
1915
1916                        foreach( $this->headerstrlen as $headername => $headerlen ) {
1917
1918                                if( $headername == 'nodes' ) {
1919                                        $attrval        = strval( count( $jobinfo[nodes] ) );
1920                                } else if( $headername == 'cpus' ) {
1921
1922                                        if( !isset( $jobinfo[ppn] ) )
1923                                                $jobinfo[ppn] = 1;
1924
1925                                        $attrval        = strval( count( $jobinfo[nodes] ) * intval( $jobinfo[ppn] ) );
1926
1927                                } else if( $headername == 'runningtime' ) {
1928                                        $attrval        = makeTime( intval( $jobinfo[reported] ) - intval( $jobinfo[start_timestamp] ) );
1929                                } else {
1930                                        $attrval        = strval( $jobinfo[$headername] );
1931                                }
1932
1933                                imageString( $this->image, $this->font, $xoffset, $this->y_offset, $attrval, $colorblack );
1934               
1935                                $xoffset        = $xoffset + ($this->fontwidth * ( $headerlen + 1 ) );
1936
1937                        }
1938                       
1939                        $this->newLineOffset();
1940                }
1941        }
1942
1943        function drawHeader() {
1944
1945                $dg                     = &$this->data_gather;
1946
1947                for( $n = 0; $n < count( $this->njobs ); $n++ ) {
1948
1949                        $jobid                  = $this->njobs[$n];
1950                        $jobinfo                = $dg->getJob( $jobid );
1951
1952                        if( !isset( $this->headerstrlen[id] ) )
1953                                $this->headerstrlen[id] = strlen( strval( $jobid ) );
1954                        else
1955                                if( strlen( strval( $jobid ) ) > $this->headerstrlen[id] )
1956                                        $this->headerstrlen[id] = strlen( strval( $jobid ) );
1957
1958                        if( !isset( $this->headerstrlen[owner] ) )
1959                                $this->headerstrlen[owner]      = strlen( strval( $jobinfo[owner] ) );
1960                        else
1961                                if( strlen( strval( $jobinfo[owner] ) ) > $this->headerstrlen[owner] )
1962                                        $this->headerstrlen[owner]      = strlen( strval( $jobinfo[owner] ) );
1963
1964                        if( !isset( $this->headerstrlen[queue] ) )
1965                                $this->headerstrlen[queue]      = strlen( strval( $jobinfo[queue] ) );
1966                        else
1967                                if( strlen( strval( $jobinfo[queue] ) ) > $this->headerstrlen[queue] )
1968                                        $this->headerstrlen[queue]      = strlen( strval( $jobinfo[queue] ) );
1969
1970                        if( !isset( $jobinfo[ppn] ) )
1971                                $jobinfo[ppn] = 1;
1972
1973                        $cpus                   = count( $jobinfo[nodes] ) * intval( $jobinfo[ppn] );
1974
1975                        if( !isset( $this->headerstrlen[cpus] ) )
1976                                $this->headerstrlen[cpus]       = strlen( strval( $cpus ) );
1977                        else
1978                                if( strlen( strval( $cpus ) ) > $this->headerstrlen[cpus] )
1979                                        $this->headerstrlen[cpus]       = strlen( strval( $cpus ) );
1980
1981                        $nodes                  = count( $jobinfo[nodes] );
1982
1983                        if( !isset( $this->headerstrlen[nodes] ) )
1984                                $this->headerstrlen[nodes]      = strlen( strval( $nodes ) );
1985                        else
1986                                if( strlen( strval( $nodes) ) > $this->headerstrlen[nodes] )
1987                                        $this->headerstrlen[nodes]      = strlen( strval( $nodes ) );
1988
1989                        $runningtime            = makeTime( intval( $jobinfo[reported] ) - intval( $jobinfo[start_timestamp] ) );
1990
1991                        if( !isset( $this->headerstrlen[runningtime] ) )
1992                                $this->headerstrlen[runningtime]        = strlen( strval( $runningtime) );
1993                        else
1994                                if( strlen( strval( $runningtime) ) > $this->headerstrlen[runningtime] )
1995                                        $this->headerstrlen[runningtime]        = strlen( strval( $runningtime) );
1996
1997                        if( !isset( $this->headerstrlen[name] ) )
1998                                $this->headerstrlen[name]       = strlen( strval( $jobinfo[name] ) );
1999                        else
2000                                if( strlen( strval( $jobinfo[name] ) ) > $this->headerstrlen[name] )
2001                                        $this->headerstrlen[name]       = strlen( strval( $jobinfo[name] ) );
2002
2003                }
2004
2005                $xoffset        = 5;
2006
2007                foreach( $this->headerstrlen as $headername => $headerlen ) {
2008
2009                        $colorgreen     = imageColorAllocate( $this->image, 0, 200, 0 );
2010
2011                        if( $headerlen < strlen( $headername ) )
2012                                $this->headerstrlen[$headername]        = strlen( $headername );
2013
2014                        imageString( $this->image, $this->font, $xoffset, $this->y_offset, ucfirst( $headername ), $colorgreen );
2015
2016                        $xoffset        = $xoffset + ($this->fontwidth * ( $this->headerstrlen[$headername] + 1 ) );
2017
2018                }
2019                $this->newLineOffset();
2020        }
2021
2022        function newLineOffset() {
2023
2024                $this->y_offset         = $this->y_offset + $this->fontheight + $this->fontspaceing;
2025        }
2026
2027        function draw() {
2028
2029                $xlen           = 450;
2030                $ylen           = ( count( $this->njobs ) * ( $this->fontheight + $this->fontspaceing ) ) + (3 * $this->fontheight);
2031
2032                $this->image    = imageCreateTrueColor( $xlen, $ylen );
2033                $colorwhite     = imageColorAllocate( $this->image, 255, 255, 255 );
2034                imageFill( $this->image, 0, 0, $colorwhite );                         
2035
2036                $colorblue      = imageColorAllocate( $this->image, 0, 0, 255 );
2037
2038                imageString( $this->image, $this->font, 1, $this->y_offset, "Monarch Joblist - host: ".$this->host, $colorblue );
2039                $this->newLineOffset();
2040
2041                $this->drawHeader();
2042                $this->drawJobs();
2043
2044                header( 'Content-type: image/png' );
2045                imagePNG( $this->image );
2046                imageDestroy( $this->image );
2047        }
2048}
2049
2050function imageStringDown( &$image, $font, $x, $y, &$s, &$col )
2051{
2052        $fw     = imagefontwidth( $font);
2053        $fh     = imagefontheight( $font);
2054       
2055        $fontspacing = 0;
2056
2057        $fx     = $x;
2058        $fy     = $y;
2059
2060        for( $n=0; $n<strlen( $s ); $n++ )
2061        {
2062                $myc    = $s{$n};
2063
2064                imagestring( $image, $font, $fx, $fy, $myc, $col );
2065
2066                $fy     += ($fontspacing + $fh );
2067        }
2068}
2069
2070function array_rem( $val, &$arr )
2071{
2072        // Delete val from arr
2073        //
2074        $i      = array_search( $val, $arr );
2075
2076        if( $i == false ) return false;
2077
2078        $arr    = array_merge( array_slice( $arr, 0, $i ), array_slice( $arr, $i+1, count( $arr ) ) );
2079
2080        return true;
2081}
2082
2083function cmp( $a, $b ) 
2084{
2085        global $SORT_ORDER;
2086        global $skan_str;
2087        global $x_first, $y_first;
2088        global $x_present, $y_present;
2089
2090        //printf("ppoep = %s\n", $skan_str);
2091        $a_node         = $a;
2092        $b_node         = $b;
2093        $a              = $a_node->getHostname();
2094        $b              = $b_node->getHostname();
2095
2096        if( $a == $b ) return 0;
2097
2098        $a_x            = 0;
2099        $b_x            = 0;
2100        $a_y            = 0;
2101        $b_y            = 0;
2102
2103        if( $x_present && $y_present )
2104        {
2105                if( $x_first )
2106                {
2107                        $n = sscanf( $a, $skan_str, $a_x, $a_y );
2108                        $n = sscanf( $b, $skan_str, $b_x, $b_y );
2109                }
2110                else if( $y_first )
2111                {
2112                        $n = sscanf( $a, $skan_str, $a_y, $a_x );
2113                        $n = sscanf( $b, $skan_str, $b_y, $b_x );
2114                }
2115        } 
2116        else if( $x_present && !$y_present )
2117        {
2118                $n = sscanf( $a, $skan_str, $a_x );
2119                $n = sscanf( $b, $skan_str, $b_x );
2120        }
2121        else if( $y_present && !$x_present )
2122        {
2123                $n = sscanf( $a, $skan_str, $a_y );
2124                $n = sscanf( $b, $skan_str, $b_y );
2125        }
2126
2127        if ( $SORT_ORDER=="desc" )
2128        {
2129
2130                if( $x_present && $y_present )
2131                {
2132                        // 1  = a < b
2133                        // -1 = a > b
2134                        //
2135                        if ($a_x == $b_x)
2136                        {
2137                                if ($a_y < $b_y)
2138                                {
2139                                        return 1;
2140                                }
2141                                else if ($a_y > $b_y)
2142                                {
2143                                        return -1;
2144                                }
2145                        }
2146                        else if ($a_x < $b_x)
2147                        {
2148                                return 1;
2149                        }
2150                        else if ($a_x > $b_x)
2151                        {
2152                                return -1;
2153                        }
2154                } 
2155                else if( $x_present && !$y_present )
2156                {
2157                        if ($a_x < $b_x)
2158                        {
2159                                return 1;
2160                        }
2161                        else if ($a_x > $b_x)
2162                        {
2163                                return -1;
2164                        }
2165                }
2166                else if( $y_present && !$x_present )
2167                {
2168                        if ($a_y < $b_y)
2169                        {
2170                                return 1;
2171                        }
2172                        else if ($a_y > $b_y)
2173                        {
2174                                return -1;
2175                        }
2176                }
2177        }
2178        else if ( $SORT_ORDER == "asc" )
2179        {
2180
2181                if( $x_present && $y_present )
2182                {
2183                        // 1  = a > b
2184                        // -1 = a < b
2185                        //
2186                        if ($a_x == $b_x)
2187                        {
2188                                if ($a_y > $b_y)
2189                                {
2190                                        return 1;
2191                                }
2192                                else if ($a_y < $b_y)
2193                                {
2194                                        return -1;
2195                                }
2196                        }
2197                        else if ($a_x > $b_x)
2198                        {
2199                                return 1;
2200                        }
2201                        else if ($a_x < $b_x)
2202                        {
2203                                return -1;
2204                        }
2205                }
2206                else if( $x_present && !$y_present )
2207                {
2208                        if ($a_x > $b_x)
2209                        {
2210                                return 1;
2211                        }
2212                        else if ($a_x < $b_x)
2213                        {
2214                                return -1;
2215                        }
2216                }
2217                else if( $y_present && !$x_present )
2218                {
2219                        if ($a_y > $b_y)
2220                        {
2221                                return 1;
2222                        }
2223                        else if ($a_y < $b_y)
2224                        {
2225                                return -1;
2226                        }
2227                }
2228        }
2229}
2230function makeTime( $time ) {
2231
2232        $days = intval( $time / 86400 );
2233        $time = ($days>0) ? $time % ($days * 86400) : $time;
2234
2235        //printf( "time = %s, days = %s\n", $time, $days );
2236
2237        $date_str = '';
2238        $day_str = '';
2239
2240        if( $days > 0 ) {
2241                if( $days > 1 )
2242                        $day_str .= $days . ' days';
2243                else
2244                        $day_str .= $days . ' day';
2245        }
2246
2247        $hours = intval( $time / 3600 );
2248        $time = $hours ? $time % ($hours * 3600) : $time;
2249
2250        //printf( "time = %s, days = %s, hours = %s\n", $time, $days, $hours );
2251        if( $hours > 0 ) {
2252                $date_str .= $hours . ':';
2253                $date_unit = 'hours'; 
2254        }
2255
2256        $minutes = intval( $time / 60 );
2257        $seconds = $minutes ? $time % ($minutes * 60) : $time;
2258
2259        if( $minutes > 0 ) {
2260
2261                if( $minutes >= 10 )
2262                        $date_str .= $minutes . ':';
2263                else
2264                        $date_str .= '0' . $minutes . ':';
2265
2266                $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
2267        } else {
2268                if($hours > 0 ) {
2269                        $date_str .= '00:';
2270                        $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
2271                }
2272        }
2273
2274
2275        $date_unit = (!isset($date_unit)) ? 'seconds' : $date_unit;
2276
2277        if( $seconds > 0 ) {
2278
2279                if( $seconds >= 10 )
2280                        $date_str .= $seconds . ' ' . $date_unit;
2281                else
2282                        $date_str .= '0' . $seconds . ' ' . $date_unit;
2283
2284        } else if ( $hours > 0 or $minutes > 0 )
2285
2286                $date_str .= '00 ' . $date_unit;
2287
2288        if( $days > 0) {
2289
2290                if( $hours > 0 or $minutes > 0 or $seconds > 0 )
2291                        $date_str = $day_str . ' - ' . $date_str;
2292                else
2293                        $date_str = $day_str;
2294        }
2295
2296        return $date_str;
2297}
2298?>
Note: See TracBrowser for help on using the repository browser.