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

Last change on this file since 329 was 329, checked in by bastiaans, 17 years ago

web/addons/job_monarch/conf.php:

  • added SORTBY_HOSTNAME, SORT_ORDER, SORT_XLABEL, SORT_YLABEL

web/addons/job_monarch/image.php:

  • removed debug statement

web/addons/job_monarch/libtoga.php:

  • added x, y sorting if SORTBY_HOSTNAME is set
  • added array_rem to remove value from array
  • added cmp function for sorting of nodes according to SORTBY_HOSTNAME x, y setting
  • Property svn:keywords set to Id
File size: 37.5 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 329 2007-04-22 13:36:26Z bastiaans $
23 *
24 */
25
26
27class HTTPVariables {
28
29        var $clustername, $metricname;
30        var $restvars, $httpvars;
31
32        function HTTPVariables( $httpvars, $getvars ) {
33
34                $this->restvars = array();
35
36                $this->clustername = $httpvars["c"] ? $httpvars["c"] : $getvars["c"];
37                $this->metricname = $httpvars["m"] ? $httpvars["m"] : $getvars["m"];
38
39                foreach( $httpvars as $httpvar => $httpval ) {
40                       
41                        if( $httpval ) {
42                                $this->restvars[$httpvar] = $httpval;
43                        }
44                }
45
46                foreach( $getvars as $getvar => $getval ) {
47
48                        if( $getval ) {
49                                $this->restvars[$getvar] = $getval;
50                        }
51                }
52        }
53
54        function getClusterName() {
55                return $this->clustername;
56        }
57
58        function getMetricName() {
59                return $this->metricname;
60        }
61
62        function getHttpVar( $var ) {
63                if( isset( $this->restvars[$var] ) )
64                        return $this->restvars[$var];
65                else
66                        return null;
67        }
68}
69
70// Toga's conf
71//
72include_once "./conf.php";
73include_once "./version.php";
74
75global $GANGLIA_PATH;
76global $RRDTOOL;
77global $JOB_ARCHIVE_DIR;
78global $JOB_ARCHIVE_DBASE;
79global $SORTBY_HOSTNAME;
80global $SORT_ORDER;
81global $skan_str;
82global $x_first, $y_first;
83
84$my_dir = getcwd();
85
86// Load Ganglia's PHP
87chdir( $GANGLIA_PATH );
88
89include_once "./conf.php";
90include_once "./functions.php";
91include_once "./ganglia.php";
92include_once "./get_context.php";
93unset( $start );
94$context = 'cluster';
95include_once "./get_ganglia.php";
96
97// Back to our PHP
98chdir( $my_dir );
99
100global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH, $DATA_SOURCE, $HTTP_GET_VARS, $_GET;
101$httpvars = new HTTPVariables( $HTTP_GET_VARS, $_GET );
102
103// Set cluster context so that Ganglia will
104// provide us with the correct metrics array
105//
106global $context, $clustername, $reports;
107
108//$clustername = $httpvars->getClusterName();
109
110global $default_metric;
111
112// Ganglia's array of host metrics
113//
114global $metrics, $hosts_up;
115
116global $DATETIME_FORMAT;
117
118function makeDate( $time ) {
119        global $DATETIME_FORMAT;
120        return strftime( $DATETIME_FORMAT, $time );
121}
122
123class TarchDbase {
124
125        var $ip, $dbase, $conn;
126
127        function TarchDbase( $ip = null, $dbase = null ) {
128
129                global $JOB_ARCHIVE_DBASE;
130
131                $db_fields = explode( '/', $JOB_ARCHIVE_DBASE );
132
133                $this->ip = $db_fields[0];
134                $this->dbase = $db_fields[1];
135                $this->conn = null;
136        }
137
138        function connect() {
139
140                if( $this->ip == null )
141                        $this->conn = pg_connect( "dbname=".$this->dbase );
142                else
143                        $this->conn = pg_connect( "host=".$this->ip." dbname=".$this->dbase );
144        }
145
146        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 ) {
147
148                global $SEARCH_RESULT_LIMIT;
149
150                if( $id ) {
151                        $select_query = "SELECT job_id FROM jobs WHERE job_id = '$id' AND job_status = 'F'";
152                        $this->resultcount = 1;
153                } else {
154                        $query_args = array();
155                       
156                        if( $queue )
157                                $query_args[] = "job_queue ='$queue'";
158                        if( $user )
159                                $query_args[] = "job_owner ='$user'";
160                        if( $name )
161                                $query_args[] = "job_name = '$name'";
162                        if( $start_from_time )
163                                $query_args[] = "job_start_timestamp >= $start_from_time";
164                        if( $start_to_time )
165                                $query_args[] = "job_start_timestamp <= $start_to_time";
166                        if( $end_from_time )
167                                $query_args[] = "job_stop_timestamp >= $end_from_time";
168                        if( $end_to_time )
169                                $query_args[] = "job_stop_timestamp <= $end_to_time";
170
171                        $query = "FROM jobs WHERE job_status = 'F' AND ";
172                        $extra_query_args = '';
173
174                        foreach( $query_args as $myquery ) {
175
176                                if( $extra_query_args == '' )
177                                        $extra_query_args = $myquery;
178                                else
179                                        $extra_query_args .= " AND ".$myquery;
180                        }
181                        $query .= $extra_query_args;
182
183                        $count_result_idname = "COUNT(job_id)";
184                        $select_result_idname = "job_id";
185
186                        $count_query = "SELECT " . $count_result_idname . " " . $query;
187
188                        $count_result = $this->queryDbase( $count_query );
189                        $this->resultcount = (int) $count_result[0][count];
190
191                        $select_query = "SELECT " . $select_result_idname . " " . $query . " ORDER BY job_id LIMIT " . $SEARCH_RESULT_LIMIT;
192                }
193
194                $ids = $this->queryDbase( $select_query );
195
196                $ret = array();
197
198                foreach( $ids as $crow)
199                        $ret[] = $crow[job_id];
200
201                return $ret;
202        }
203
204        function getNodesForJob( $jobid ) {
205
206                $result = $this->queryDbase( "SELECT node_id FROM job_nodes WHERE job_id = '$jobid'" );
207
208                $nodes = array();
209
210                foreach( $result as $result_row ) 
211
212                        $nodes[] = $this->getNodeArray( $result_row[node_id] );
213
214                return $nodes;
215        }
216
217        function getJobsForNode( $nodeid ) {
218
219                $result = $this->queryDbase( "SELECT job_id FROM job_nodes WHERE node_id = '$nodeid'" );
220
221                $jobs = array();
222
223                foreach( $result as $result_row )
224
225                        $jobs[] = $this->getJobArray( $result_row[job_id] );
226
227                return $jobs;
228        }
229
230        function getJobArray( $id ) {
231                $result = $this->queryDbase( "SELECT * FROM jobs WHERE job_id = '$id'" );
232
233                return ( $this->makeArray( $result[0] ) );
234        }
235
236        function getNodeArray( $id ) {
237
238                $result = $this->queryDbase( "SELECT * FROM nodes WHERE node_id = '$id'" );
239
240                return ( $this->makeArray( $result[0] ) );
241        }
242
243        function makeArray( $result_row ) {
244
245                $myar = array();
246
247                foreach( $result_row as $mykey => $myval ) {
248
249                        $map_key = explode( '_', $mykey );
250
251                        $rmap_key = array_reverse( $map_key );
252                        array_pop( $rmap_key );
253                        $map_key = array_reverse( $rmap_key );
254                       
255                        $newkey = implode( '_', $map_key );
256                       
257                        $myar[$newkey] = $result_row[$mykey];
258                }
259
260                return $myar;
261        }
262
263        function queryDbase( $query ) {
264
265                $result_rows = array();
266       
267                if( !$this->conn )
268                        $this->connect();
269
270                //printf( "query = [%s]\n", $query );
271                $result = pg_query( $this->conn, $query );
272
273                while ($row = pg_fetch_assoc($result))
274                        $result_rows[] = $row;
275
276                return $result_rows;
277        }
278}
279
280class TarchRrdGraph {
281        var $rrdbin, $rrdvalues, $clustername, $hostname, $tempdir, $tarchdir, $metrics;
282
283        function TarchRrdGraph( $clustername, $hostname ) {
284
285                global $RRDTOOL;
286                global $JOB_ARCHIVE_DIR;
287
288                $this->rrdbin = $RRDTOOL;
289                $this->rrdvalues = array();
290                $this->tarchdir = $JOB_ARCHIVE_DIR;
291                $this->clustername = $clustername;
292                $this->hostname = $hostname;
293        }
294
295        function doCmd( $command ) {
296
297                printf( "command = %s\n", $command );
298                $pipe = popen( $command . ' 2>&1', 'r' );
299
300                if (!$pipe) {
301                        print "pipe failed.";
302                        return "";
303                }
304
305                $output = '';
306                while(!feof($pipe))
307                        $output .= fread($pipe, 1024);
308
309                pclose($pipe);
310
311                $output = explode( "\n", $output );
312                //print_r( $output );
313                return $output;
314        }
315
316        function dirList( $dir ) {
317
318                $dirlist = array();
319
320                if ($handle = opendir( $dir )) {
321                        while (false !== ($file = readdir($handle))) {
322                                if ($file != "." && $file != "..") {
323                                        $dirlist[] = $file;
324                                }
325                        }
326                        closedir($handle);
327                }
328
329                return $dirlist;
330        }
331
332        function getTimePeriods( $start, $end ) {
333
334                //printf("start = %s end = %s\n", $start, $end );
335                $times = array();
336                $dirlist = $this->dirList( $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname );
337
338                //print_r( $dirlist );
339
340                $first = 0;
341                $last = 9999999999999;
342
343                foreach( $dirlist as $dir ) {
344
345                        if( $dir > $first and $dir <= $start )
346                                $first = $dir;
347                        if( $dir < $last and $dir >= $end )
348                                $last = $dir;
349                }
350
351                //printf( "first = %s last = %s\n", $first, $last );
352
353                foreach( $dirlist as $dir ) {
354
355                        //printf( "dir %s ", $dir );
356
357                        if( $dir >= $first and $dir <= $last and !array_key_exists( $dir, $times ) ) {
358                       
359                                $times[] = $dir;
360                                //printf("newtime %s ", $dir );
361
362                        }
363                }
364
365                //print_r( $times );
366
367                sort( $times );
368
369                //print_r( $times );
370
371                return $times;
372        }
373
374        function getRrdDirs( $start, $stop ) {
375
376                //printf( "tarchdir = %s\n", $this->tarchdir );
377                $timess = $this->getTimePeriods( $start, $stop );
378                //print_r( $timess );
379
380                $rrd_files = array();
381
382                foreach( $timess as $time ) {
383
384                        $rrd_files[] = $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname. '/'.$time;
385                }
386
387                return $rrd_files;
388        }
389
390        function getRrdFiles( $metric, $start, $stop ) {
391
392                $times = $this->getTimePeriods( $start, $stop );
393
394                $rrd_files = array();
395
396                foreach( $times as $time ) {
397
398                        $rrd_files[] = $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname . '/' .$time. '/' . $metric. '.rrd';
399                }
400
401                return $rrd_files;
402        }
403
404        function graph( $descr ) {
405//      monitor2:/data/toga/rrds/LISA Cluster/gb-r15n11.irc.sara.nl# rrdtool graph /var/www/ganglia/test1.png --start 1118683231 --end 1118750431 --width 300 --height 400 DEF:'1'='./1118647515/load_one.rrd':'sum':AVERAGE DEF:'2'='./1118690723/load_one.rrd':'sum':AVERAGE DEF:'3'='./1118733925/load_one.rrd':'sum':AVERAGE AREA:1#555555:"load_one" AREA:2#555555 AREA:3#555555
406//      380x461
407//      monitor2:/data/toga/rrds/LISA Cluster/gb-r15n11.irc.sara.nl#
408                //$command = $this->rrdbin . " graph - --start $start --end $end ".
409                        "--width $width --height $height $upper_limit $lower_limit ".
410                        "--title '$title' $vertical_label $extras $background ". $series;
411
412                //$graph = $this->doCmd( $command );
413
414                //return $graph;
415                return 0;
416        }
417}
418
419class DataSource {
420
421        var $data, $ip, $port;
422
423        //function DataSource( $ip = '127.0.0.1', $port = 8649 ) {
424        function DataSource() {
425
426                global $DATA_SOURCE;
427
428                $ds_fields      = explode( ':', $DATA_SOURCE );
429
430                $ds_ip          = $ds_fields[0];
431                $ds_port        = $ds_fields[1];
432
433                $this->ip       = $ds_ip;
434                $this->port     = $ds_port;
435
436        }
437
438        function getData() {
439
440                $errstr;
441                $errno = 0;
442                $timeout = 3;
443
444                $fp = fsockopen( $this->ip, $this->port, $errno, $errstr, $timeout );
445
446                if( !$fp ) {
447                        echo 'Unable to connect to '.$this->ip.':'.$this->port; // printf( 'Unable to connect to [%s:%.0f]', $this->ip, $this->port );
448                        return;
449                }
450
451                stream_set_timeout( $fp, 30 );
452
453                while ( !feof( $fp ) ) {
454                       
455                        $data .= fread( $fp, 16384 );
456                }
457
458                fclose( $fp );
459
460                return $data;
461        }
462}
463
464class DataGatherer {
465
466        var $xmlhandler, $data, $httpvars;
467
468        function DataGatherer( $cluster ) {
469
470                //global $DATA_SOURCE;
471       
472                //printf("dg cluster = %s\n", $cluster );
473                //$ds_fields = explode( ':', $DATA_SOURCE );
474                //$ds_ip = $ds_fields[0];
475                //$ds_port = $ds_fields[1];
476
477                //$this->source = new DataSource( $ds_ip, $ds_port );
478
479                $this->cluster  = $cluster;
480                $this->httpvars = $httpvars;
481        }
482
483        function parseXML( $data ) {
484
485                //$src = &$this->source;
486                //$this->data = $src->getData();
487
488                $this->parser           = xml_parser_create();
489                $this->xmlhandler       = new TorqueXMLHandler( $this->cluster );
490
491                xml_set_element_handler( $this->parser, array( &$this->xmlhandler, 'startElement' ), array( &$this->xmlhandler, 'stopElement' ) );
492                //if ( !xml_parse( $this->parser, $this->data ) )
493                if ( !xml_parse( $this->parser, $data ) )
494                        $error = sprintf( 'XML error: %s at %d', xml_error_string( xml_get_error_code( $this->parser ) ), xml_get_current_line_number( $this->parser ) );
495        }
496
497        function printInfo() {
498                $handler = $this->xmlhandler;
499                $handler->printInfo();
500        }
501
502        function getNodes() {
503                $handler = $this->xmlhandler;
504                return $handler->getNodes();
505        }
506
507        function getNode( $node ) {
508                $handler = $this->xmlhandler;
509                return $handler->getNode( $node );
510        }
511
512        function getCpus() {
513                $handler = $this->xmlhandler;
514                return $handler->getCpus();
515        }
516
517        function getJobs() {
518                $handler = $this->xmlhandler;
519                return $handler->getJobs();
520        }
521
522        function getJob( $job ) {
523                $handler = $this->xmlhandler;
524                return $handler->getJob( $job );
525        }
526
527        function getHeartbeat() {
528                $handler = $this->xmlhandler;
529                return $handler->getHeartbeat();
530        }
531
532        function isJobmonRunning() {
533                $handler = $this->xmlhandler;
534                return $handler->isJobmonRunning();
535        }
536}
537
538class TorqueXMLHandler {
539
540        var $clusters, $heartbeat, $nodes, $jobs, $clustername, $proc_cluster;
541
542        function TorqueXMLHandler( $clustername ) {
543                $jobs = array();
544                $clusters = array();
545                $this->nodes = array();
546                $heartbeat = array();
547                $this->clustername = $clustername;
548                //printf(" cluster set to %s \n", $this->clustername );
549        }
550
551        function getCpus() {
552
553                $cpus = 0;
554
555                if( isset( $this->jobs ) && count( $this->jobs ) > 0 ) {
556
557                        foreach( $this->jobs as $jobid=>$jobattrs ) {
558
559                                $nodes = count( $jobattrs[nodes] );
560                                $ppn = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
561                                $mycpus = $nodes * $ppn;
562
563                                $cpus = $cpus + $mycpus;
564                        }
565                }
566        }
567
568        function isJobmonRunning() {
569
570                if (isset( $this->heartbeat['time'] ))
571                        return 1;
572                else
573                        return 0;
574        }
575
576        function startElement( $parser, $name, $attrs ) {
577
578                $jobs = $this->jobs;
579                $nodes = $this->nodes;
580
581                if ( $attrs[TN] ) {
582
583                        // Ignore dead metrics. Detect and mask failures.
584                        if ( $attrs[TN] > $attrs[TMAX] * 4 )
585                                return;
586                }
587
588                $jobid = null;
589
590                //printf( '%s=%s', $attrs[NAME], $attrs[VAL] );
591
592                //printf( "clustername = %s proc_cluster = %s\n", $this->clustername, $this->proc_cluster );
593
594                if( $name == 'CLUSTER' ) {
595
596                        $this->proc_cluster = $attrs[NAME];
597                        //printf( "Found cluster %s\n", $attrs[NAME] );
598                        //print_r( $attrs );
599
600                        //if( !isset( $clusters[$clustername] ) )
601                        //      $clusters[$clustername] = array();
602
603                } else if( $name == 'HOST' and $this->proc_cluster == $this->clustername) {
604
605                        $hostname = $attrs[NAME];
606                        $location = $attrs[LOCATION];
607                        //printf( "Found node %s\n", $hostname );
608
609                        if( !isset( $nodes[$hostname] ) )
610                                $nodes[$hostname] = new NodeImage( $this->proc_cluster, $hostname );
611
612                } else if( $name == 'METRIC' and strstr( $attrs[NAME], 'MONARCH' ) and $this->proc_cluster == $this->clustername ) {
613
614                        if( strstr( $attrs[NAME], 'MONARCH-HEARTBEAT' ) ) {
615
616                                $this->heartbeat['time'] = $attrs[VAL];
617                                //printf( "heartbeat %s\n", $heartbeat['time'] );
618
619                        } else if( strstr( $attrs[NAME], 'MONARCH-JOB' ) ) {
620
621                                sscanf( $attrs[NAME], 'MONARCH-JOB-%d-%d', $jobid, $monincr );
622
623                                //printf( "jobid %s\n", $jobid );
624
625                                if( !isset( $jobs[$jobid] ) )
626                                        $jobs[$jobid] = array();
627
628                                $fields = explode( ' ', $attrs[VAL] );
629
630                                foreach( $fields as $f ) {
631                                        $togavalues = explode( '=', $f );
632
633                                        $toganame = $togavalues[0];
634                                        $togavalue = $togavalues[1];
635
636                                        //printf( "\t%s\t= %s\n", $toganame, $togavalue );
637
638                                        if( $toganame == 'nodes' ) {
639
640                                                if( $jobs[$jobid][status] == 'R' ) {
641                                               
642                                                        if( !isset( $jobs[$jobid][$toganame] ) )
643                                                                $jobs[$jobid][$toganame] = array();
644
645                                                        $mynodes = explode( ';', $togavalue );
646
647                                                        //print_r($mynodes);
648
649                                                        foreach( $mynodes as $node ) {
650
651                                                                if( !in_array( $node, $jobs[$jobid][$toganame] ) ) {
652                                                                        $jobs[$jobid][$toganame][] = $node;
653                                                                }
654                                                        }
655
656                                                } else if( $jobs[$jobid][status] == 'Q' ) {
657
658                                                        $jobs[$jobid][$toganame] = $togavalue;
659                                                }
660                                               
661                                        } else {
662
663                                                $jobs[$jobid][$toganame] = $togavalue;
664                                        }
665                                }
666
667                                if( isset( $jobs[$jobid][domain] ) and isset( $jobs[$jobid][nodes] ) ) {
668                       
669                                        $nr_nodes = count( $jobs[$jobid][nodes] );
670               
671                                        if( $jobs[$jobid][status] == 'R' ) {
672
673                                                foreach( $jobs[$jobid][nodes] as $node ) {
674
675                                                        $domain = $jobs[$jobid][domain];
676                                                        $domain_len = 0 - strlen( $domain );
677
678                                                        if( substr( $node, $domain_len ) != $domain ) {
679                                                                $host = $node. '.'.$domain;
680                                                        } else {
681                                                                $host = $node;
682                                                        }
683
684                                                        //$host = $node.'.'.$jobs[$jobid][domain];
685                               
686                                                        if( !isset( $nodes[$host] ) )
687                                                                $my_node = new NodeImage( $this->proc_cluster, $host );
688                                                        else
689                                                                $my_node = $nodes[$host];
690
691                                                        if( !$my_node->hasJob( $jobid ) )
692
693                                                                if( isset( $jobs[$jobid][ppn] ) )
694                                                                        $my_node->addJob( $jobid, ((int) $jobs[$jobid][ppn]) );
695                                                                else
696                                                                        $my_node->addJob( $jobid, 1 );
697
698                                                        $nodes[$host] = $my_node;
699                                                }
700                                        }
701                                }
702                        }
703                }
704                $this->jobs = $jobs;
705                //print_r( $nodes );
706                $this->nodes = $nodes;
707                //print_r( $this->nodes );
708        }
709
710        function stopElement( $parser, $name ) {
711        }
712
713        function printInfo() {
714
715                $jobs = &$this->jobs;
716
717                printf( "---jobs---\n" );
718
719                foreach( $jobs as $jobid => $job ) {
720
721                        printf( "job %s\n", $jobid );
722
723                        if( isset( $job[nodes] ) ) {
724
725                                foreach( $job[nodes] as $node ) {
726
727                                        $mynode = $this->nodes[$node];
728                                        $hostname = $mynode->getHostname();
729                                        $location = $mynode->getLocation();
730
731                                        printf( "\t- node %s\tlocation %s\n", $hostname, $location );
732                                        //$this->nodes[$hostname]->setLocation( "hier draait job ".$jobid );
733                                }
734                        }
735                }
736
737                printf( "---nodes---\n" );
738
739                $nodes = &$this->nodes;
740
741                foreach( $nodes as $node ) {
742
743                        $hostname = $node->getHostname();
744                        $location = $node->getLocation();
745                        $jobs = implode( ' ', $node->getJobs() );
746                        printf( "* node %s\tlocation %s\tjobs %s\n", $hostname, $location, $jobs );
747                }
748        }
749
750        function getNodes() {
751                //print_r( $this->nodes );
752                return $this->nodes;
753        }
754
755        function getNode( $node ) {
756
757                $nodes = &$this->nodes;
758                if( isset( $nodes[$node] ) )
759                        return $nodes[$node];
760                else
761                        return NULL;
762        }
763
764        function getJobs() {
765                return $this->jobs;
766        }
767
768        function getJob( $job ) {
769
770                $jobs = &$this->jobs;
771                if( isset( $jobs[$job] ) )
772                        return $jobs[$job];
773                else
774                        return NULL;
775        }
776
777        function getHeartbeat() {
778                return $this->heartbeat['time'];
779        }
780}
781
782class NodeImage {
783
784        var $image, $x, $y, $hostname, $jobs, $tasks, $showinfo;
785
786        function NodeImage( $cluster, $hostname ) {
787
788                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
789
790                $this->jobs = array();
791                //$this->image = $image;
792                //$this->x = $x;
793                //$this->y = $y;
794                $this->tasks = 0;
795                $this->hostname = $hostname;
796                $this->cpus = $this->determineCpus();
797                $this->clustername = $cluster;
798                $this->showinfo = 1;
799                $this->size = $SMALL_CLUSTERIMAGE_NODEWIDTH;
800        }
801
802        function addJob( $jobid, $cpus ) {
803                $jobs = &$this->jobs;
804
805                $jobs[] = $jobid;
806                $this->jobs = $jobs;
807
808                $this->addTask( $cpus );
809        }
810
811        function hasJob( $jobid ) {
812
813                $jobfound = 0;
814
815                if( count( $this->jobs ) > 0 )
816                        foreach( $this->jobs as $job )
817
818                                if( $job == $jobid )
819                                        $jobfound = 1;
820
821                return $jobfound;
822        }
823
824        function addTask( $cpus ) {
825
826                $this->tasks = $this->tasks + $cpus;
827        }
828
829        function setImage( $image ) {
830
831                $this->image = $image;
832        }
833
834        function setCoords( $x, $y ) {
835
836                $this->x = $x;
837                $this->y = $y;
838        }
839
840        function getImagemapArea() {
841
842                $area_topleft           = $this->x . "," . $this->y;
843                $area_bottomright       = ($this->x + $this->size) . "," . ($this->y + $this->size);
844                $area_coords            = $area_topleft . "," . $area_bottomright;
845
846                $area_href              = "./?c=" . $this->clustername . "&h=" . $this->hostname;
847                $area_tooltip           = $this->hostname . ": " . implode( " ", $this->jobs );
848
849                $tag_href               = "HREF=\"" . $area_href . "\"";
850                $tag_coords             = "COORDS=\"" . $area_coords . "\"";
851                $tag_tooltip1           = "ALT=\"" . $area_tooltip . "\"";
852                $tag_tooltip2           = "TITLE=\"" . $area_tooltip . "\"";
853
854                return ("<AREA SHAPE=\"RECT\" " . $tag_coords . " " . $tag_href . " " . $tag_tooltip1 . " " . $tag_tooltip2 . ">");
855        }
856
857        function colorHex( $color ) {
858       
859                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) );
860
861                return $my_color;
862        }
863
864        function setLoad( $load ) {
865                $this->load = $load;
866        }
867
868        function setHostname( $hostname ) {
869                $this->hostname = $hostname;
870        }
871
872        function getHostname() {
873                return $this->hostname;
874        }
875
876        function getJobs() {
877                return $this->jobs;
878        }
879
880        function setShowinfo( $showinfo ) {
881                $this->showinfo = $showinfo;
882        }
883
884        function drawSmall() {
885
886                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
887
888                $this->size     = $SMALL_CLUSTERIMAGE_NODEWIDTH;
889
890                $this->draw();
891        }
892
893        function drawBig() {
894
895                global $BIG_CLUSTERIMAGE_NODEWIDTH;
896
897                $this->size     = $BIG_CLUSTERIMAGE_NODEWIDTH;
898
899                $this->draw();
900        }
901
902        function draw() {
903
904                global $JOB_NODE_MARKING;
905
906                $black_color = imageColorAllocate( $this->image, 0, 0, 0 );
907                $size = $this->size;
908
909                imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+($size), $this->y+($size), $black_color );
910
911                if( $this->showinfo) {
912               
913                        $this->load = $this->determineLoad();
914
915                        if( !isset( $this->image ) or !isset( $this->x ) or !isset( $this->y ) ) {
916                                printf( "aborting\n" );
917                                printf( "x %d y %d load %f\n", $this->x, $this->y, $load );
918                                return;
919                        }
920
921
922                        // Convert Ganglias Hexadecimal load color to a Decimal one
923                        //
924                        $load = $this->determineLoad(); 
925                        $usecolor = $this->colorHex( load_color($load) );
926                        imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor );
927                        if( count( $this->jobs ) > 0 )
928                                imageString( $this->image, 1, $this->x+(($size/2)-1), $this->y+(($size/2)-4), $JOB_NODE_MARKING, $black_color );
929
930                } else {
931
932                        // White
933                        $usecolor = imageColorAllocate( $this->image, 255, 255, 255 );
934                        imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor );
935                }
936
937
938        }
939
940        function determineCpus() {
941
942                global $metrics;
943
944                $cpus = $metrics[$this->hostname][cpu_num][VAL];
945                if (!$cpus) $cpus=1;
946
947                return $cpus;
948        }
949
950        function determineLoad() {
951
952                global $metrics;
953
954                $load_one = $metrics[$this->hostname][load_one][VAL];
955                $load = ((float) $load_one)/$this->cpus;
956
957                return $load;
958        }
959}
960
961class ClusterImage {
962
963        var $dataget, $image, $clustername;
964        var $filtername, $filters;
965
966        //function ClusterImage( $clustername ) {
967        function ClusterImage( $data, $clustername ) {
968
969                //$this->dataget                = $dataget;
970                $this->dataget          = new DataGatherer( $clustername );
971                $this->data             = $data;
972                $this->clustername      = $clustername;
973                $this->filters          = array();
974                $this->size             = 's';
975                $this->width            = 0;
976                $this->height           = 0;
977                $this->output           = 1;
978        }
979
980        function getWidth() {
981                return $this->width;
982        }
983        function getHeight() {
984                return $this->height;
985        }
986
987        function setSmall() {
988                $this->size     = 's';
989        }
990
991        function setBig() {
992                $this->size     = 'b';
993        }
994
995        function setNoimage() {
996                $this->output   = 0;
997        }
998
999        function isSmall() {
1000                return ($this->size == 's');
1001        }
1002
1003        function isBig() {
1004                return ($this->size == 'b');
1005        }
1006
1007        function setFilter( $filtername, $filtervalue ) {
1008
1009                $this->filters[$filtername] = $filtervalue;
1010        }
1011
1012        function filterNodes( $jobs, $nodes ) {
1013
1014                $filtered_nodes = array();
1015
1016                foreach( $nodes as $node ) {
1017
1018                        $hostname = $node->getHostname();
1019
1020                        $addhost = 1;
1021
1022                        if( count( $this->filters ) > 0 ) {
1023
1024                                $mynjobs = $node->getJobs();
1025
1026                                if( count( $mynjobs ) > 0 ) {
1027
1028                                        foreach( $mynjobs as $myjob ) {
1029
1030                                                foreach( $this->filters as $filtername => $filtervalue ) {
1031
1032                                                        if( $filtername!=null && $filtername!='' ) {
1033
1034                                                                if( $filtername == 'jobid' && !$node->hasJob( $filtervalue) ) {
1035                                                                        $addhost = 0;
1036                                                                } else if( $filtername != 'jobid' ) {
1037                                                                        if( $jobs[$myjob][$filtername] != $filtervalue ) {
1038                                                                                $addhost = 0;
1039                                                                        }
1040                                                                }
1041                                                        }
1042                                                }
1043                                        }
1044                                } else
1045                                        $addhost = 0;
1046                        }
1047
1048                        if( $addhost )
1049                                $filtered_nodes[] = $hostname;
1050                }
1051
1052                return $filtered_nodes;
1053        }
1054
1055        function draw() {
1056
1057                global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH;
1058                global $BIG_CLUSTERIMAGE_MAXWIDTH, $BIG_CLUSTERIMAGE_NODEWIDTH;
1059
1060                global $SORTBY_HOSTNAME, $SORT_ORDER, $skan_str;
1061                global $x_first, $y_first;
1062       
1063                $mydatag = $this->dataget;
1064                $mydatag->parseXML( $this->data );
1065
1066                if( $this->isSmall() ) {
1067                        $max_width = $SMALL_CLUSTERIMAGE_MAXWIDTH;
1068                        $node_width = $SMALL_CLUSTERIMAGE_NODEWIDTH;
1069                } else if( $this->isBig() ) {
1070                        $max_width = $BIG_CLUSTERIMAGE_MAXWIDTH;
1071                        $node_width = $BIG_CLUSTERIMAGE_NODEWIDTH;
1072                }
1073
1074                $nodes = $mydatag->getNodes();
1075                $nodes_hosts = array_keys( $nodes );
1076
1077                $nodes_nr = count( $nodes );
1078
1079                $nodes_size = $nodes_nr*$node_width;
1080                $node_rows = 0;
1081
1082                if( $nodes_size > $max_width ) {
1083                        $nodes_per_row = ( (int) ($max_width/$node_width) );
1084                } else {
1085                        $nodes_per_row = $nodes_size;
1086                        $node_rows = 1;
1087                }
1088
1089                if( $nodes_per_row < $nodes_nr ) {
1090                        $node_rows = ( (int) ($nodes_nr/$nodes_per_row) );
1091                        $node_rest = fmod( $nodes_nr, $nodes_per_row );
1092                        //printf( "nodesnr %d noderest %f\n", $nodes_nr, $node_rest );
1093                        if( $node_rest > 0 ) {
1094                                $node_rows++;
1095                                //printf( "noderows %d\n", $node_rows );
1096                        }
1097                }
1098
1099                $y_offset       = 0;
1100                $font           = 2;
1101                $fontwidth      = ImageFontWidth( $font );
1102                $fontheight     = ImageFontHeight( $font );
1103                $fontspaceing   = 2;
1104                $y_offset       = $fontheight + (2 * $fontspaceing);
1105
1106                $this->width    = $max_width;
1107                $this->height   = ($y_offset + (($node_rows*$node_width)+1) );
1108
1109                //$image = imageCreateTrueColor( $max_width, ($y_offset + (($node_rows*$node_width)+1) ) );
1110                //$colorwhite = imageColorAllocate( $image, 255, 255, 255 );
1111                //imageFill( $image, 0, 0, $colorwhite );
1112
1113                //if( $this->isSmall() ) {
1114
1115                //      $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1116
1117                //      imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1118                //}
1119
1120                $jobs = $mydatag->getJobs();
1121                //printf("filtername = %s\n", $filtername );
1122                $filtered_nodes = $this->filterNodes( $jobs, $nodes );
1123
1124                //print_r($filtered_nodes);
1125
1126                if( $SORTBY_HOSTNAME != "" )
1127                {
1128
1129                        $sorted         = array();
1130
1131                        $x_first        = 0;
1132                        $y_first        = 0;
1133
1134
1135                        if( strpos( $SORTBY_HOSTNAME, "{x}" ) < strpos( $SORTBY_HOSTNAME, "{y}" ) )
1136                        {
1137                       
1138                                $x_first        = 1;
1139                        }
1140                        else
1141                        {
1142                                $y_first        = 1;
1143               
1144                        }
1145
1146                        $skan_str       = str_replace( "{x}", "%d", $SORTBY_HOSTNAME );
1147                        $skan_str       = str_replace( "{y}", "%d", $skan_str );
1148
1149                        $x_min          = null;
1150                        $x_max          = null;
1151                        $y_min          = null;
1152                        $y_max          = null;
1153
1154                        foreach( $nodes as $hostname => $node )
1155                        {
1156                                //$n    = sscanf( $hostname, $skan_str, $i, $j );
1157                                if( $x_first )
1158                                {
1159                                        $n = sscanf( $hostname, $skan_str, $x, $y );
1160                                }
1161                                else if( $y_first )
1162                                {
1163                                        $n = sscanf( $hostname, $skan_str, $y, $x );
1164                                }
1165
1166                                //printf( "n %s\n", $n );
1167
1168                                // Remove nodes that don't match
1169                                //
1170                                if( $n < 2 )
1171                                {
1172                                        unset( $nodes[$hostname] );
1173                                }
1174
1175                                if( !$x_min )
1176                                {
1177                                        $x_min  = $x;
1178                                }
1179                                else if( $x < $x_min )
1180                                {
1181                                        $x_min  = $x;
1182                                }
1183                                if( !$x_max )
1184                                {
1185                                        $x_max  = $x;
1186                                }
1187                                else if( $x > $x_max )
1188                                {
1189                                        $x_max  = $x;
1190                                }
1191                                if( !$y_min )
1192                                {
1193                                        $y_min  = $y;
1194                                }
1195                                else if( $y < $y_min )
1196                                {
1197                                        $y_min  = $y;
1198                                }
1199                                if( !$y_max )
1200                                {
1201                                        $y_max  = $y;
1202                                }
1203                                else if( $y > $y_max )
1204                                {
1205                                        $y_max  = $y;
1206                                }
1207                        }
1208
1209                        //printf( "ss %s\n", $skan_str);
1210                        $sorted_nodes   = usort( $nodes, "cmp" );
1211
1212                        $x_offset       = 0;
1213                        $cur_node       = 0;
1214
1215                        //printf( "xmin %s xmax %s\n", $x_min, $x_max );
1216                        //printf( "ymin %s ymax %s\n", $y_min, $y_max );
1217
1218                        // werkt
1219                        //print_r( $nodes );
1220
1221                        $image          = imageCreateTrueColor( $this->width, ($y_offset + (($node_rows*$node_width)+1) ) );
1222                        $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1223
1224                        imageFill( $image, 0, 0, $colorwhite );
1225
1226                        //if( $this->isSmall() ) {
1227
1228                        //      $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1229
1230                        //      imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1231                        //}
1232
1233                        for( $n = $x_min; $n <= $x_max; $n++ )
1234                        {
1235                                for( $m = $y_min; $m <= $y_max; $m++ )
1236                                {
1237                                        if( $x_min > 0 )
1238                                        {
1239                                                $x      = $x_offset + ( ($n-$x_min) * $node_width );
1240                                        }
1241                                        if( $y_min > 0 )
1242                                        {
1243                                                $y      = $y_offset + ( ($m-$y_min) * $node_width );
1244                                        }
1245
1246                                        if( isset( $nodes[$cur_node] ) ) {
1247
1248
1249                                                $host   = $nodes[$cur_node]->getHostname();
1250
1251                                                if( $x_first )
1252                                                {
1253                                                        $nn = sscanf( $host, $skan_str, $rx, $ry );
1254                                                }
1255                                                else if( $y_first )
1256                                                {
1257                                                        $nn = sscanf( $host, $skan_str, $ry, $rx );
1258                                                }
1259                                                if ( $nn < 2 )
1260                                                {
1261                                                        continue;
1262                                                }
1263                                                if( ( $rx - $x_min ) > $n )
1264                                                {
1265                                                        $m      = $y_max;
1266                                                        continue;
1267                                                }
1268
1269                                                if( !in_array( $host, $filtered_nodes ) )
1270                                                        $nodes[$cur_node]->setShowinfo( 0 );
1271
1272                                                $nodes[$cur_node]->setCoords( $x, $y );
1273                                                $nodes[$cur_node]->setImage( $image );
1274
1275                                                //print_r( $nodes[$cur_node] );
1276
1277                                                if( $this->isSmall() )
1278                                                        $nodes[$cur_node]->drawSmall();
1279                                                else if( $this->isBig() )
1280                                                        $nodes[$cur_node]->drawBig();
1281                                        }
1282                                        $cur_node++;
1283                                }
1284                        }
1285
1286                }
1287                else
1288                {
1289                        $image          = imageCreateTrueColor( $max_width, ($y_offset + (($node_rows*$node_width)+1) ) );
1290                        $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1291
1292                        imageFill( $image, 0, 0, $colorwhite );
1293
1294                        if( $this->isSmall() ) {
1295
1296                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1297
1298                                imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1299                        }
1300
1301                        for( $n = 0; $n < $node_rows; $n++ ) {
1302                       
1303                                for( $m = 0; $m < $nodes_per_row; $m++ ) {
1304                       
1305                                        $x = ($m * $node_width);
1306                                        $y = $y_offset + ($n * $node_width);
1307
1308                                        $cur_node = ($n * $nodes_per_row) + ($m);
1309                                        $host = $nodes_hosts[$cur_node];
1310
1311                                        if( isset( $nodes[$host] ) ) {
1312
1313                                                $nodes[$host]->setCoords( $x, $y );
1314                                                $nodes[$host]->setImage( $image );
1315
1316                                                if( !in_array( $host, $filtered_nodes ) )
1317                                                        $nodes[$host]->setShowinfo( 0 );
1318
1319                                                if( $this->isSmall() )
1320                                                        $nodes[$host]->drawSmall();
1321                                                else if( $this->isBig() )
1322                                                        $nodes[$host]->drawBig();
1323                                        }
1324                                }
1325                        }
1326                }
1327       
1328                $this->nodes    = &$nodes;
1329
1330                if ($this->output) {
1331                        header( 'Content-type: image/png' );
1332                        imagePNG( $image );
1333                        imageDestroy( $image );
1334                }
1335        }
1336
1337        function getImagemapArea() {
1338
1339                $clusterimage_map       = "";
1340
1341                foreach( $this->nodes as $hostname => $node ) {
1342
1343                        $node_map               = $node->getImagemapArea();
1344                        $clusterimage_map       .= $node_map;
1345                }
1346
1347                return $clusterimage_map;
1348        }
1349}
1350
1351class EmptyImage {
1352
1353        function draw() {
1354                $image          = imageCreateTrueColor( 1, 1 );
1355                $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1356                imageFill( $image, 0, 0, $colorwhite );                         
1357
1358                header( 'Content-type: image/png' );
1359                imagePNG( $image );
1360                imageDestroy( $image );
1361        }
1362}
1363
1364class HostImage {
1365
1366        var $data_gather, $cluster, $host, $node, $image;
1367        var $headerstrlen;
1368
1369        function HostImage( $data_gather, $cluster, $host ) {
1370
1371                $this->data_gather      = $data_gather;
1372                $this->cluster          = $cluster;
1373                $this->host             = $host;
1374                $this->y_offset         = 0;
1375                $this->font             = 2;
1376                $this->fontspaceing     = 2;
1377                $this->headerstrlen     = array();
1378
1379                $this->fontheight       = ImageFontHeight( $this->font );
1380                $this->fontwidth        = ImageFontWidth( $this->font );
1381
1382                $dg                     = &$this->data_gather;
1383                $this->node             = &$dg->getNode( $this->host );
1384                $n                      = &$this->node;
1385                $this->njobs            = $n->getJobs();
1386        }
1387
1388        function drawJobs() {
1389
1390                $dg                     = &$this->data_gather;
1391                $colorblack             = imageColorAllocate( $this->image, 0, 0, 0 );
1392
1393                for( $n = 0; $n < count( $this->njobs ); $n++ ) {
1394
1395                        $jobid                  = $this->njobs[$n];
1396                        $jobinfo                = $dg->getJob( $jobid );
1397
1398                        $xoffset                = 5;
1399                        imageString( $this->image, $this->font, $xoffset, $this->y_offset, strval( $jobid ), $colorblack );
1400
1401                        foreach( $this->headerstrlen as $headername => $headerlen ) {
1402
1403                                if( $headername == 'nodes' ) {
1404                                        $attrval        = strval( count( $jobinfo[nodes] ) );
1405                                } else if( $headername == 'cpus' ) {
1406
1407                                        if( !isset( $jobinfo[ppn] ) )
1408                                                $jobinfo[ppn] = 1;
1409
1410                                        $attrval        = strval( count( $jobinfo[nodes] ) * intval( $jobinfo[ppn] ) );
1411
1412                                } else if( $headername == 'runningtime' ) {
1413                                        $attrval        = makeTime( intval( $jobinfo[reported] ) - intval( $jobinfo[start_timestamp] ) );
1414                                } else {
1415                                        $attrval        = strval( $jobinfo[$headername] );
1416                                }
1417
1418                                imageString( $this->image, $this->font, $xoffset, $this->y_offset, $attrval, $colorblack );
1419               
1420                                $xoffset        = $xoffset + ($this->fontwidth * ( $headerlen + 1 ) );
1421
1422                        }
1423                       
1424                        $this->newLineOffset();
1425                }
1426        }
1427
1428        function drawHeader() {
1429
1430                $dg                     = &$this->data_gather;
1431
1432                for( $n = 0; $n < count( $this->njobs ); $n++ ) {
1433
1434                        $jobid                  = $this->njobs[$n];
1435                        $jobinfo                = $dg->getJob( $jobid );
1436
1437                        if( !isset( $this->headerstrlen[id] ) )
1438                                $this->headerstrlen[id] = strlen( strval( $jobid ) );
1439                        else
1440                                if( strlen( strval( $jobid ) ) > $this->headerstrlen[id] )
1441                                        $this->headerstrlen[id] = strlen( strval( $jobid ) );
1442
1443                        if( !isset( $this->headerstrlen[owner] ) )
1444                                $this->headerstrlen[owner]      = strlen( strval( $jobinfo[owner] ) );
1445                        else
1446                                if( strlen( strval( $jobinfo[owner] ) ) > $this->headerstrlen[owner] )
1447                                        $this->headerstrlen[owner]      = strlen( strval( $jobinfo[owner] ) );
1448
1449                        if( !isset( $this->headerstrlen[queue] ) )
1450                                $this->headerstrlen[queue]      = strlen( strval( $jobinfo[queue] ) );
1451                        else
1452                                if( strlen( strval( $jobinfo[queue] ) ) > $this->headerstrlen[queue] )
1453                                        $this->headerstrlen[queue]      = strlen( strval( $jobinfo[queue] ) );
1454
1455                        if( !isset( $jobinfo[ppn] ) )
1456                                $jobinfo[ppn] = 1;
1457
1458                        $cpus                   = count( $jobinfo[nodes] ) * intval( $jobinfo[ppn] );
1459
1460                        if( !isset( $this->headerstrlen[cpus] ) )
1461                                $this->headerstrlen[cpus]       = strlen( strval( $cpus ) );
1462                        else
1463                                if( strlen( strval( $cpus ) ) > $this->headerstrlen[cpus] )
1464                                        $this->headerstrlen[cpus]       = strlen( strval( $cpus ) );
1465
1466                        $nodes                  = count( $jobinfo[nodes] );
1467
1468                        if( !isset( $this->headerstrlen[nodes] ) )
1469                                $this->headerstrlen[nodes]      = strlen( strval( $nodes ) );
1470                        else
1471                                if( strlen( strval( $nodes) ) > $this->headerstrlen[nodes] )
1472                                        $this->headerstrlen[nodes]      = strlen( strval( $nodes ) );
1473
1474                        $runningtime            = makeTime( intval( $jobinfo[reported] ) - intval( $jobinfo[start_timestamp] ) );
1475
1476                        if( !isset( $this->headerstrlen[runningtime] ) )
1477                                $this->headerstrlen[runningtime]        = strlen( strval( $runningtime) );
1478                        else
1479                                if( strlen( strval( $runningtime) ) > $this->headerstrlen[runningtime] )
1480                                        $this->headerstrlen[runningtime]        = strlen( strval( $runningtime) );
1481
1482                        if( !isset( $this->headerstrlen[name] ) )
1483                                $this->headerstrlen[name]       = strlen( strval( $jobinfo[name] ) );
1484                        else
1485                                if( strlen( strval( $jobinfo[name] ) ) > $this->headerstrlen[name] )
1486                                        $this->headerstrlen[name]       = strlen( strval( $jobinfo[name] ) );
1487
1488                }
1489
1490                $xoffset        = 5;
1491
1492                foreach( $this->headerstrlen as $headername => &$headerlen ) {
1493
1494                        $colorgreen     = imageColorAllocate( $this->image, 0, 200, 0 );
1495
1496                        imageString( $this->image, $this->font, $xoffset, $this->y_offset, ucfirst( $headername ), $colorgreen );
1497               
1498                        if( $headerlen < strlen( $headername ) )
1499                                $headerlen      = strlen( $headername );
1500
1501                        $xoffset        = $xoffset + ($this->fontwidth * ( $headerlen + 1 ) );
1502
1503                }
1504                $this->newLineOffset();
1505        }
1506
1507        function newLineOffset() {
1508
1509                $this->y_offset         = $this->y_offset + $this->fontheight + $this->fontspaceing;
1510        }
1511
1512        function draw() {
1513
1514                $xlen           = 450;
1515                $ylen           = ( count( $this->njobs ) * ( $this->fontheight + $this->fontspaceing ) ) + (3 * $this->fontheight);
1516
1517                $this->image    = imageCreateTrueColor( $xlen, $ylen );
1518                $colorwhite     = imageColorAllocate( $this->image, 255, 255, 255 );
1519                imageFill( $this->image, 0, 0, $colorwhite );                         
1520
1521                $colorblue      = imageColorAllocate( $this->image, 0, 0, 255 );
1522
1523                imageString( $this->image, $this->font, 1, $this->y_offset, "Monarch Joblist - host: ".$this->host, $colorblue );
1524                $this->newLineOffset();
1525
1526                $this->drawHeader();
1527                $this->drawJobs();
1528
1529                header( 'Content-type: image/png' );
1530                imagePNG( $this->image );
1531                imageDestroy( $this->image );
1532        }
1533}
1534
1535function array_rem( $val, &$arr )
1536{
1537        // Delete val from arr
1538        //
1539        $i      = array_search( $val, $arr );
1540
1541        if( $i == false ) return false;
1542
1543        $arr    = array_merge( array_slice( $arr, 0, $i ), array_slice( $arr, $i+1, count( $arr ) ) );
1544
1545        return true;
1546}
1547
1548function cmp( $a, $b ) 
1549{
1550        global $SORT_ORDER;
1551        global $skan_str;
1552        global $x_first, $y_first;
1553
1554        $a_node         = $a;
1555        $b_node         = $b;
1556        $a              = $a_node->getHostname();
1557        $b              = $b_node->getHostname();
1558
1559        if( $a == $b ) return 0;
1560
1561        if( $x_first )
1562        {
1563                $n = sscanf( $a, $skan_str, $a_x, $a_y );
1564                $n = sscanf( $b, $skan_str, $b_x, $b_y );
1565        }
1566        else if( $y_first )
1567        {
1568                $n = sscanf( $a, $skan_str, $a_y, $a_x );
1569                $n = sscanf( $b, $skan_str, $b_y, $b_x );
1570        }
1571
1572        if ( $SORT_ORDER=="desc" )
1573        {
1574
1575                // 1  = a < b
1576                // -1 = a > b
1577                //
1578                if ($a_x == $b_x)
1579                {
1580                        if ($a_y < $b_y)
1581                        {
1582                                return 1;
1583                        }
1584                        else if ($a_y > $b_y)
1585                        {
1586                                return -1;
1587                        }
1588                }
1589                else if ($a_x < $b_x)
1590                {
1591                        return 1;
1592                }
1593                else if ($a_x > $b_x)
1594                {
1595                        return -1;
1596                }
1597        }
1598        else if ( $SORT_ORDER == "asc" )
1599        {
1600
1601                // 1  = a > b
1602                // -1 = a < b
1603                //
1604                if ($a_x == $b_x)
1605                {
1606                        if ($a_y > $b_y)
1607                        {
1608                                return 1;
1609                        }
1610                        else if ($a_y < $b_y)
1611                        {
1612                                return -1;
1613                        }
1614                }
1615                else if ($a_x > $b_x)
1616                {
1617                        return 1;
1618                }
1619                else if ($a_x < $b_x)
1620                {
1621                        return -1;
1622                }
1623        }
1624}
1625function makeTime( $time ) {
1626
1627        $days = intval( $time / 86400 );
1628        $time = ($days>0) ? $time % ($days * 86400) : $time;
1629
1630        //printf( "time = %s, days = %s\n", $time, $days );
1631
1632        $date_str = '';
1633        $day_str = '';
1634
1635        if( $days > 0 ) {
1636                if( $days > 1 )
1637                        $day_str .= $days . ' days';
1638                else
1639                        $day_str .= $days . ' day';
1640        }
1641
1642        $hours = intval( $time / 3600 );
1643        $time = $hours ? $time % ($hours * 3600) : $time;
1644
1645        //printf( "time = %s, days = %s, hours = %s\n", $time, $days, $hours );
1646        if( $hours > 0 ) {
1647                $date_str .= $hours . ':';
1648                $date_unit = 'hours'; 
1649        }
1650
1651        $minutes = intval( $time / 60 );
1652        $seconds = $minutes ? $time % ($minutes * 60) : $time;
1653
1654        if( $minutes > 0 ) {
1655
1656                if( $minutes >= 10 )
1657                        $date_str .= $minutes . ':';
1658                else
1659                        $date_str .= '0' . $minutes . ':';
1660
1661                $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
1662        } else {
1663                if($hours > 0 ) {
1664                        $date_str .= '00:';
1665                        $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
1666                }
1667        }
1668
1669
1670        $date_unit = (!isset($date_unit)) ? 'seconds' : $date_unit;
1671
1672        if( $seconds > 0 ) {
1673
1674                if( $seconds >= 10 )
1675                        $date_str .= $seconds . ' ' . $date_unit;
1676                else
1677                        $date_str .= '0' . $seconds . ' ' . $date_unit;
1678
1679        } else if ( $hours > 0 or $minutes > 0 )
1680
1681                $date_str .= '00 ' . $date_unit;
1682
1683        if( $days > 0) {
1684
1685                if( $hours > 0 or $minutes > 0 or $seconds > 0 )
1686                        $date_str = $day_str . ' - ' . $date_str;
1687                else
1688                        $date_str = $day_str;
1689        }
1690
1691        return $date_str;
1692}
1693?>
Note: See TracBrowser for help on using the repository browser.