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

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

web/addons/job_monarch/version.php:

  • SVN version

web/addons/job_monarch/libtoga.php:

  • need $start in my graph.php

jobmond/jobmond.py:

  • added number of running and queued jobs metric reporting

web/addons/job_monarch/graph.php:

  • added 'job_report' graph, shows running and queued jobs

web/addons/job_monarch/overview.php:

  • included IMG to job_report graph

web/addons/job_monarch/index.php:

  • added range menu
  • fixed metric_menu selection now properly instantly refreshes graphs

web/addons/job_monarch/templates/overview.tpl:

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