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

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

libtoga.php:

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