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

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

job_monarch/libtoga.php,
job_monarch/conf.php:

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