source: trunk/web2/addons/job_monarch/libtoga.php @ 582

Last change on this file since 582 was 582, checked in by ramonb, 15 years ago

job_monarch/libtoga.php:

  • fixed clusterimage filtering

job_monarch/image.php:

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