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

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

web/addons/job_monarch/overview.php:

  • removed size setting of clusterimage in html assignment

web/addons/job_monarch/libtoga.php:

  • changed clusterimage width/height determination
  • Property svn:keywords set to Id
File size: 37.7 KB
RevLine 
[102]1<?php
[225]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 *
[231]22 * SVN $Id: libtoga.php 330 2007-04-22 13:51:37Z bastiaans $
23 *
[225]24 */
25
26
[103]27class HTTPVariables {
28
29        var $clustername, $metricname;
[112]30        var $restvars, $httpvars;
[103]31
[117]32        function HTTPVariables( $httpvars, $getvars ) {
[103]33
[110]34                $this->restvars = array();
35
[280]36                $this->clustername = $httpvars["c"] ? $httpvars["c"] : $getvars["c"];
37                $this->metricname = $httpvars["m"] ? $httpvars["m"] : $getvars["m"];
[103]38
[117]39                foreach( $httpvars as $httpvar => $httpval ) {
[110]40                       
41                        if( $httpval ) {
42                                $this->restvars[$httpvar] = $httpval;
43                        }
44                }
[117]45
46                foreach( $getvars as $getvar => $getval ) {
47
48                        if( $getval ) {
49                                $this->restvars[$getvar] = $getval;
50                        }
51                }
[103]52        }
53
54        function getClusterName() {
55                return $this->clustername;
56        }
57
58        function getMetricName() {
59                return $this->metricname;
60        }
[110]61
62        function getHttpVar( $var ) {
63                if( isset( $this->restvars[$var] ) )
[112]64                        return $this->restvars[$var];
[110]65                else
66                        return null;
67        }
[103]68}
69
[112]70// Toga's conf
71//
72include_once "./conf.php";
[195]73include_once "./version.php";
[112]74
[117]75global $GANGLIA_PATH;
[206]76global $RRDTOOL;
77global $JOB_ARCHIVE_DIR;
[207]78global $JOB_ARCHIVE_DBASE;
[329]79global $SORTBY_HOSTNAME;
80global $SORT_ORDER;
81global $skan_str;
82global $x_first, $y_first;
[112]83
[117]84$my_dir = getcwd();
[112]85
[117]86// Load Ganglia's PHP
87chdir( $GANGLIA_PATH );
[112]88
[117]89include_once "./conf.php";
90include_once "./functions.php";
91include_once "./ganglia.php";
[145]92include_once "./get_context.php";
93unset( $start );
94$context = 'cluster';
[117]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
[112]103// Set cluster context so that Ganglia will
104// provide us with the correct metrics array
105//
[126]106global $context, $clustername, $reports;
[129]107
[117]108//$clustername = $httpvars->getClusterName();
[112]109
[126]110global $default_metric;
111
[112]112// Ganglia's array of host metrics
113//
[126]114global $metrics, $hosts_up;
[112]115
[246]116global $DATETIME_FORMAT;
117
118function makeDate( $time ) {
119        global $DATETIME_FORMAT;
120        return strftime( $DATETIME_FORMAT, $time );
121}
122
[130]123class TarchDbase {
124
[138]125        var $ip, $dbase, $conn;
[130]126
[294]127        function TarchDbase( $ip = null, $dbase = null ) {
[207]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];
[138]135                $this->conn = null;
[130]136        }
137
138        function connect() {
139
[294]140                if( $this->ip == null )
[138]141                        $this->conn = pg_connect( "dbname=".$this->dbase );
[130]142                else
[204]143                        $this->conn = pg_connect( "host=".$this->ip." dbname=".$this->dbase );
[130]144        }
[138]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
[248]148                global $SEARCH_RESULT_LIMIT;
149
[251]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 {
[138]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";
[142]164                        if( $start_to_time )
[138]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
[248]171                        $query = "FROM jobs WHERE job_status = 'F' AND ";
[138]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
[251]183                        $count_result_idname = "COUNT(job_id)";
184                        $select_result_idname = "job_id";
[140]185
[251]186                        $count_query = "SELECT " . $count_result_idname . " " . $query;
[248]187
[251]188                        $count_result = $this->queryDbase( $count_query );
189                        $this->resultcount = (int) $count_result[0][count];
[248]190
[251]191                        $select_query = "SELECT " . $select_result_idname . " " . $query . " ORDER BY job_id LIMIT " . $SEARCH_RESULT_LIMIT;
192                }
193
[248]194                $ids = $this->queryDbase( $select_query );
195
[140]196                $ret = array();
197
198                foreach( $ids as $crow)
199                        $ret[] = $crow[job_id];
200
201                return $ret;
[138]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
[141]212                        $nodes[] = $this->getNodeArray( $result_row[node_id] );
[138]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
[141]225                        $jobs[] = $this->getJobArray( $result_row[job_id] );
[138]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
[141]251                        $rmap_key = array_reverse( $map_key );
252                        array_pop( $rmap_key );
253                        $map_key = array_reverse( $rmap_key );
[138]254                       
[141]255                        $newkey = implode( '_', $map_key );
256                       
[138]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
[143]270                //printf( "query = [%s]\n", $query );
[138]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        }
[130]278}
279
[138]280class TarchRrdGraph {
[130]281        var $rrdbin, $rrdvalues, $clustername, $hostname, $tempdir, $tarchdir, $metrics;
282
[206]283        function TarchRrdGraph( $clustername, $hostname ) {
284
285                global $RRDTOOL;
286                global $JOB_ARCHIVE_DIR;
287
288                $this->rrdbin = $RRDTOOL;
[130]289                $this->rrdvalues = array();
[206]290                $this->tarchdir = $JOB_ARCHIVE_DIR;
[145]291                $this->clustername = $clustername;
292                $this->hostname = $hostname;
[130]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 );
[135]312                //print_r( $output );
[130]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
[145]334                //printf("start = %s end = %s\n", $start, $end );
[130]335                $times = array();
336                $dirlist = $this->dirList( $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname );
[145]337
338                //print_r( $dirlist );
339
[130]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
[145]351                //printf( "first = %s last = %s\n", $first, $last );
352
[130]353                foreach( $dirlist as $dir ) {
354
[145]355                        //printf( "dir %s ", $dir );
356
357                        if( $dir >= $first and $dir <= $last and !array_key_exists( $dir, $times ) ) {
358                       
[130]359                                $times[] = $dir;
[145]360                                //printf("newtime %s ", $dir );
361
362                        }
[130]363                }
364
[145]365                //print_r( $times );
366
[130]367                sort( $times );
368
[145]369                //print_r( $times );
370
[130]371                return $times;
372        }
373
[145]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
[130]404        function graph( $descr ) {
[141]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#
[130]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;
[145]415                return 0;
[130]416        }
417}
418
[103]419class DataSource {
420
421        var $data, $ip, $port;
422
[326]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
[103]436        }
437
438        function getData() {
439
440                $errstr;
441                $errno = 0;
442                $timeout = 3;
443
[313]444                $fp = fsockopen( $this->ip, $this->port, $errno, $errstr, $timeout );
[103]445
[106]446                if( !$fp ) {
[103]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
[177]451                stream_set_timeout( $fp, 30 );
452
[103]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
[105]466        var $xmlhandler, $data, $httpvars;
[103]467
[163]468        function DataGatherer( $cluster ) {
[103]469
[326]470                //global $DATA_SOURCE;
[163]471       
472                //printf("dg cluster = %s\n", $cluster );
[326]473                //$ds_fields = explode( ':', $DATA_SOURCE );
474                //$ds_ip = $ds_fields[0];
475                //$ds_port = $ds_fields[1];
[110]476
[326]477                //$this->source = new DataSource( $ds_ip, $ds_port );
[110]478
[326]479                $this->cluster  = $cluster;
[112]480                $this->httpvars = $httpvars;
[103]481        }
482
[326]483        function parseXML( $data ) {
[103]484
[326]485                //$src = &$this->source;
486                //$this->data = $src->getData();
[103]487
[326]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 ) )
[313]494                        $error = sprintf( 'XML error: %s at %d', xml_error_string( xml_get_error_code( $this->parser ) ), xml_get_current_line_number( $this->parser ) );
[103]495        }
496
[105]497        function printInfo() {
498                $handler = $this->xmlhandler;
499                $handler->printInfo();
500        }
501
[106]502        function getNodes() {
503                $handler = $this->xmlhandler;
504                return $handler->getNodes();
505        }
506
[303]507        function getNode( $node ) {
508                $handler = $this->xmlhandler;
509                return $handler->getNode( $node );
510        }
511
[124]512        function getCpus() {
513                $handler = $this->xmlhandler;
514                return $handler->getCpus();
515        }
516
[106]517        function getJobs() {
518                $handler = $this->xmlhandler;
519                return $handler->getJobs();
520        }
521
[303]522        function getJob( $job ) {
523                $handler = $this->xmlhandler;
524                return $handler->getJob( $job );
525        }
526
[114]527        function getHeartbeat() {
528                $handler = $this->xmlhandler;
529                return $handler->getHeartbeat();
530        }
[298]531
532        function isJobmonRunning() {
533                $handler = $this->xmlhandler;
534                return $handler->isJobmonRunning();
535        }
[103]536}
537
538class TorqueXMLHandler {
539
[163]540        var $clusters, $heartbeat, $nodes, $jobs, $clustername, $proc_cluster;
[104]541
[163]542        function TorqueXMLHandler( $clustername ) {
[105]543                $jobs = array();
[104]544                $clusters = array();
[163]545                $this->nodes = array();
[104]546                $heartbeat = array();
[163]547                $this->clustername = $clustername;
548                //printf(" cluster set to %s \n", $this->clustername );
[103]549        }
550
[124]551        function getCpus() {
552
553                $cpus = 0;
554
[301]555                if( isset( $this->jobs ) && count( $this->jobs ) > 0 ) {
[124]556
[301]557                        foreach( $this->jobs as $jobid=>$jobattrs ) {
[124]558
[301]559                                $nodes = count( $jobattrs[nodes] );
560                                $ppn = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
561                                $mycpus = $nodes * $ppn;
562
563                                $cpus = $cpus + $mycpus;
564                        }
[124]565                }
566        }
567
[298]568        function isJobmonRunning() {
569
570                if (isset( $this->heartbeat['time'] ))
571                        return 1;
572                else
573                        return 0;
574        }
575
[103]576        function startElement( $parser, $name, $attrs ) {
577
[262]578                $jobs = $this->jobs;
579                $nodes = $this->nodes;
[105]580
[103]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
[163]590                //printf( '%s=%s', $attrs[NAME], $attrs[VAL] );
[103]591
[163]592                //printf( "clustername = %s proc_cluster = %s\n", $this->clustername, $this->proc_cluster );
593
[104]594                if( $name == 'CLUSTER' ) {
[103]595
[163]596                        $this->proc_cluster = $attrs[NAME];
597                        //printf( "Found cluster %s\n", $attrs[NAME] );
598                        //print_r( $attrs );
[103]599
[163]600                        //if( !isset( $clusters[$clustername] ) )
601                        //      $clusters[$clustername] = array();
[103]602
[163]603                } else if( $name == 'HOST' and $this->proc_cluster == $this->clustername) {
[105]604
605                        $hostname = $attrs[NAME];
606                        $location = $attrs[LOCATION];
[163]607                        //printf( "Found node %s\n", $hostname );
[105]608
[200]609                        if( !isset( $nodes[$hostname] ) )
[326]610                                $nodes[$hostname] = new NodeImage( $this->proc_cluster, $hostname );
[105]611
[219]612                } else if( $name == 'METRIC' and strstr( $attrs[NAME], 'MONARCH' ) and $this->proc_cluster == $this->clustername ) {
[103]613
[219]614                        if( strstr( $attrs[NAME], 'MONARCH-HEARTBEAT' ) ) {
[103]615
[114]616                                $this->heartbeat['time'] = $attrs[VAL];
[106]617                                //printf( "heartbeat %s\n", $heartbeat['time'] );
[104]618
[219]619                        } else if( strstr( $attrs[NAME], 'MONARCH-JOB' ) ) {
[104]620
[253]621                                sscanf( $attrs[NAME], 'MONARCH-JOB-%d-%d', $jobid, $monincr );
[104]622
[106]623                                //printf( "jobid %s\n", $jobid );
[104]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
[106]636                                        //printf( "\t%s\t= %s\n", $toganame, $togavalue );
[104]637
[103]638                                        if( $toganame == 'nodes' ) {
639
[135]640                                                if( $jobs[$jobid][status] == 'R' ) {
641                                               
642                                                        if( !isset( $jobs[$jobid][$toganame] ) )
643                                                                $jobs[$jobid][$toganame] = array();
[104]644
[135]645                                                        $mynodes = explode( ';', $togavalue );
[103]646
[300]647                                                        //print_r($mynodes);
648
[262]649                                                        foreach( $mynodes as $node ) {
[103]650
[262]651                                                                if( !in_array( $node, $jobs[$jobid][$toganame] ) ) {
[253]652                                                                        $jobs[$jobid][$toganame][] = $node;
[262]653                                                                }
654                                                        }
[135]655
656                                                } else if( $jobs[$jobid][status] == 'Q' ) {
657
658                                                        $jobs[$jobid][$toganame] = $togavalue;
659                                                }
660                                               
[104]661                                        } else {
[103]662
[105]663                                                $jobs[$jobid][$toganame] = $togavalue;
[104]664                                        }
[103]665                                }
[111]666
667                                if( isset( $jobs[$jobid][domain] ) and isset( $jobs[$jobid][nodes] ) ) {
[112]668                       
669                                        $nr_nodes = count( $jobs[$jobid][nodes] );
[300]670               
671                                        if( $jobs[$jobid][status] == 'R' ) {
[111]672
[300]673                                                foreach( $jobs[$jobid][nodes] as $node ) {
[200]674
[300]675                                                        $domain = $jobs[$jobid][domain];
676                                                        $domain_len = 0 - strlen( $domain );
[200]677
[300]678                                                        if( substr( $node, $domain_len ) != $domain ) {
679                                                                $host = $node. '.'.$domain;
680                                                        } else {
681                                                                $host = $node;
682                                                        }
683
684                                                        //$host = $node.'.'.$jobs[$jobid][domain];
[111]685                               
[300]686                                                        if( !isset( $nodes[$host] ) )
[326]687                                                                $my_node = new NodeImage( $this->proc_cluster, $host );
[300]688                                                        else
689                                                                $my_node = $nodes[$host];
[111]690
[300]691                                                        if( !$my_node->hasJob( $jobid ) )
[111]692
[300]693                                                                if( isset( $jobs[$jobid][ppn] ) )
694                                                                        $my_node->addJob( $jobid, ((int) $jobs[$jobid][ppn]) );
695                                                                else
696                                                                        $my_node->addJob( $jobid, 1 );
[111]697
[300]698                                                        $nodes[$host] = $my_node;
699                                                }
[111]700                                        }
701                                }
[103]702                        }
703                }
[114]704                $this->jobs = $jobs;
[163]705                //print_r( $nodes );
706                $this->nodes = $nodes;
[200]707                //print_r( $this->nodes );
[103]708        }
709
710        function stopElement( $parser, $name ) {
711        }
[105]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        }
[106]749
750        function getNodes() {
[163]751                //print_r( $this->nodes );
[106]752                return $this->nodes;
753        }
754
[303]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
[106]764        function getJobs() {
765                return $this->jobs;
766        }
[114]767
[303]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
[114]777        function getHeartbeat() {
778                return $this->heartbeat['time'];
779        }
[103]780}
781
[107]782class NodeImage {
[102]783
[122]784        var $image, $x, $y, $hostname, $jobs, $tasks, $showinfo;
[102]785
[326]786        function NodeImage( $cluster, $hostname ) {
[102]787
[305]788                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
789
[111]790                $this->jobs = array();
791                //$this->image = $image;
792                //$this->x = $x;
793                //$this->y = $y;
794                $this->tasks = 0;
[110]795                $this->hostname = $hostname;
[111]796                $this->cpus = $this->determineCpus();
[326]797                $this->clustername = $cluster;
[122]798                $this->showinfo = 1;
[305]799                $this->size = $SMALL_CLUSTERIMAGE_NODEWIDTH;
[102]800        }
801
[111]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
[106]834        function setCoords( $x, $y ) {
[107]835
[106]836                $this->x = $x;
837                $this->y = $y;
838        }
839
[326]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
[102]857        function colorHex( $color ) {
858       
[109]859                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) );
[102]860
861                return $my_color;
862        }
863
[106]864        function setLoad( $load ) {
865                $this->load = $load;
866        }
867
[108]868        function setHostname( $hostname ) {
869                $this->hostname = $hostname;
870        }
871
[122]872        function getHostname() {
873                return $this->hostname;
874        }
875
[114]876        function getJobs() {
877                return $this->jobs;
878        }
879
[122]880        function setShowinfo( $showinfo ) {
881                $this->showinfo = $showinfo;
882        }
883
[305]884        function drawSmall() {
[106]885
[305]886                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
887
888                $this->size     = $SMALL_CLUSTERIMAGE_NODEWIDTH;
889
890                $this->draw();
[122]891        }
[110]892
[122]893        function drawBig() {
[111]894
[305]895                global $BIG_CLUSTERIMAGE_NODEWIDTH;
896
897                $this->size     = $BIG_CLUSTERIMAGE_NODEWIDTH;
898
899                $this->draw();
[122]900        }
[106]901
[305]902        function draw() {
[106]903
[122]904                global $JOB_NODE_MARKING;
[102]905
[122]906                $black_color = imageColorAllocate( $this->image, 0, 0, 0 );
[305]907                $size = $this->size;
[107]908
[110]909                imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+($size), $this->y+($size), $black_color );
910
[122]911                if( $this->showinfo) {
912               
913                        $this->load = $this->determineLoad();
[111]914
[122]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                        }
[111]920
921
[122]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 )
[280]928                                imageString( $this->image, 1, $this->x+(($size/2)-1), $this->y+(($size/2)-4), $JOB_NODE_MARKING, $black_color );
[111]929
[122]930                } else {
[111]931
[122]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
[102]938        }
939
[111]940        function determineCpus() {
[102]941
[108]942                global $metrics;
943
944                $cpus = $metrics[$this->hostname][cpu_num][VAL];
[107]945                if (!$cpus) $cpus=1;
[111]946
947                return $cpus;
948        }
949
950        function determineLoad() {
951
952                global $metrics;
953
[108]954                $load_one = $metrics[$this->hostname][load_one][VAL];
[111]955                $load = ((float) $load_one)/$this->cpus;
[102]956
[111]957                return $load;
[106]958        }
959}
960
[107]961class ClusterImage {
[106]962
[111]963        var $dataget, $image, $clustername;
[124]964        var $filtername, $filters;
[106]965
[326]966        //function ClusterImage( $clustername ) {
967        function ClusterImage( $data, $clustername ) {
[114]968
[326]969                //$this->dataget                = $dataget;
[305]970                $this->dataget          = new DataGatherer( $clustername );
[326]971                $this->data             = $data;
[305]972                $this->clustername      = $clustername;
973                $this->filters          = array();
974                $this->size             = 's';
[326]975                $this->width            = 0;
976                $this->height           = 0;
977                $this->output           = 1;
[106]978        }
979
[326]980        function getWidth() {
981                return $this->width;
982        }
983        function getHeight() {
984                return $this->height;
985        }
986
[305]987        function setSmall() {
988                $this->size     = 's';
989        }
990
991        function setBig() {
992                $this->size     = 'b';
993        }
994
[326]995        function setNoimage() {
996                $this->output   = 0;
997        }
998
[305]999        function isSmall() {
1000                return ($this->size == 's');
1001        }
1002
1003        function isBig() {
1004                return ($this->size == 'b');
1005        }
1006
[122]1007        function setFilter( $filtername, $filtervalue ) {
1008
[124]1009                $this->filters[$filtername] = $filtervalue;
[122]1010        }
1011
1012        function filterNodes( $jobs, $nodes ) {
1013
1014                $filtered_nodes = array();
1015
1016                foreach( $nodes as $node ) {
1017
1018                        $hostname = $node->getHostname();
1019
[124]1020                        $addhost = 1;
[122]1021
[124]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 )
[122]1049                                $filtered_nodes[] = $hostname;
1050                }
1051
1052                return $filtered_nodes;
1053        }
1054
[106]1055        function draw() {
[110]1056
1057                global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH;
[305]1058                global $BIG_CLUSTERIMAGE_MAXWIDTH, $BIG_CLUSTERIMAGE_NODEWIDTH;
[329]1059
1060                global $SORTBY_HOSTNAME, $SORT_ORDER, $skan_str;
1061                global $x_first, $y_first;
[110]1062       
[107]1063                $mydatag = $this->dataget;
[326]1064                $mydatag->parseXML( $this->data );
[106]1065
[305]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                }
[106]1073
1074                $nodes = $mydatag->getNodes();
[111]1075                $nodes_hosts = array_keys( $nodes );
[106]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 ) {
[107]1083                        $nodes_per_row = ( (int) ($max_width/$node_width) );
[106]1084                } else {
1085                        $nodes_per_row = $nodes_size;
1086                        $node_rows = 1;
1087                }
1088
1089                if( $nodes_per_row < $nodes_nr ) {
[107]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 ) {
[106]1094                                $node_rows++;
[107]1095                                //printf( "noderows %d\n", $node_rows );
[106]1096                        }
1097                }
1098
[306]1099                $y_offset       = 0;
1100                $font           = 2;
[329]1101                $fontwidth      = ImageFontWidth( $font );
[306]1102                $fontheight     = ImageFontHeight( $font );
1103                $fontspaceing   = 2;
1104                $y_offset       = $fontheight + (2 * $fontspaceing);
1105
[326]1106                $this->width    = $max_width;
1107                $this->height   = ($y_offset + (($node_rows*$node_width)+1) );
1108
[329]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 );
[106]1112
[329]1113                //if( $this->isSmall() ) {
[306]1114
[329]1115                //      $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
[306]1116
[329]1117                //      imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1118                //}
[306]1119
[122]1120                $jobs = $mydatag->getJobs();
1121                //printf("filtername = %s\n", $filtername );
1122                $filtered_nodes = $this->filterNodes( $jobs, $nodes );
1123
1124                //print_r($filtered_nodes);
1125
[329]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                        {
[106]1137                       
[329]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
[330]1221                        $image_width    = $x_offset + ($node_width * ($x_max-$x_min+2));
1222                        $image_width    = ($image_width < $this->width) ? $image_width : $this->width;
1223                        $image_height   = $y_offset + ($node_width * ($y_max-$y_min+2));
1224
1225                        $this->width    = $image_width;
1226                        $this->heigth   = $image_heigth;
1227
1228                        $image          = imageCreateTrueColor( $image_width, $image_height );
[329]1229                        $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1230
1231                        imageFill( $image, 0, 0, $colorwhite );
1232
1233                        //if( $this->isSmall() ) {
1234
1235                        //      $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1236
1237                        //      imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1238                        //}
1239
1240                        for( $n = $x_min; $n <= $x_max; $n++ )
1241                        {
1242                                for( $m = $y_min; $m <= $y_max; $m++ )
1243                                {
1244                                        if( $x_min > 0 )
1245                                        {
1246                                                $x      = $x_offset + ( ($n-$x_min) * $node_width );
1247                                        }
1248                                        if( $y_min > 0 )
1249                                        {
1250                                                $y      = $y_offset + ( ($m-$y_min) * $node_width );
1251                                        }
1252
[330]1253                                        if( isset( $nodes[$cur_node] ) ) 
1254                                        {
[329]1255                                                $host   = $nodes[$cur_node]->getHostname();
1256
1257                                                if( $x_first )
1258                                                {
1259                                                        $nn = sscanf( $host, $skan_str, $rx, $ry );
1260                                                }
1261                                                else if( $y_first )
1262                                                {
1263                                                        $nn = sscanf( $host, $skan_str, $ry, $rx );
1264                                                }
1265                                                if ( $nn < 2 )
1266                                                {
1267                                                        continue;
1268                                                }
1269                                                if( ( $rx - $x_min ) > $n )
1270                                                {
1271                                                        $m      = $y_max;
1272                                                        continue;
1273                                                }
1274
1275                                                if( !in_array( $host, $filtered_nodes ) )
1276                                                        $nodes[$cur_node]->setShowinfo( 0 );
1277
1278                                                $nodes[$cur_node]->setCoords( $x, $y );
1279                                                $nodes[$cur_node]->setImage( $image );
1280
1281                                                //print_r( $nodes[$cur_node] );
1282
1283                                                if( $this->isSmall() )
1284                                                        $nodes[$cur_node]->drawSmall();
1285                                                else if( $this->isBig() )
1286                                                        $nodes[$cur_node]->drawBig();
1287                                        }
1288                                        $cur_node++;
1289                                }
1290                        }
1291
1292                }
1293                else
1294                {
1295                        $image          = imageCreateTrueColor( $max_width, ($y_offset + (($node_rows*$node_width)+1) ) );
1296                        $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1297
1298                        imageFill( $image, 0, 0, $colorwhite );
1299
1300                        if( $this->isSmall() ) {
1301
1302                                $colorblue      = imageColorAllocate( $image, 0, 0, 255 );
1303
1304                                imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue );
1305                        }
1306
1307                        for( $n = 0; $n < $node_rows; $n++ ) {
[107]1308                       
[329]1309                                for( $m = 0; $m < $nodes_per_row; $m++ ) {
1310                       
1311                                        $x = ($m * $node_width);
1312                                        $y = $y_offset + ($n * $node_width);
[107]1313
[329]1314                                        $cur_node = ($n * $nodes_per_row) + ($m);
1315                                        $host = $nodes_hosts[$cur_node];
[107]1316
[329]1317                                        if( isset( $nodes[$host] ) ) {
[111]1318
[329]1319                                                $nodes[$host]->setCoords( $x, $y );
1320                                                $nodes[$host]->setImage( $image );
[122]1321
[329]1322                                                if( !in_array( $host, $filtered_nodes ) )
1323                                                        $nodes[$host]->setShowinfo( 0 );
[122]1324
[329]1325                                                if( $this->isSmall() )
1326                                                        $nodes[$host]->drawSmall();
1327                                                else if( $this->isBig() )
1328                                                        $nodes[$host]->drawBig();
1329                                        }
[111]1330                                }
[106]1331                        }
1332                }
[326]1333       
1334                $this->nodes    = &$nodes;
1335
1336                if ($this->output) {
1337                        header( 'Content-type: image/png' );
1338                        imagePNG( $image );
1339                        imageDestroy( $image );
1340                }
[102]1341        }
[326]1342
1343        function getImagemapArea() {
1344
1345                $clusterimage_map       = "";
1346
1347                foreach( $this->nodes as $hostname => $node ) {
1348
1349                        $node_map               = $node->getImagemapArea();
1350                        $clusterimage_map       .= $node_map;
1351                }
1352
1353                return $clusterimage_map;
1354        }
[102]1355}
1356
[298]1357class EmptyImage {
1358
1359        function draw() {
1360                $image          = imageCreateTrueColor( 1, 1 );
1361                $colorwhite     = imageColorAllocate( $image, 255, 255, 255 );
1362                imageFill( $image, 0, 0, $colorwhite );                         
1363
1364                header( 'Content-type: image/png' );
1365                imagePNG( $image );
1366                imageDestroy( $image );
1367        }
1368}
1369
[303]1370class HostImage {
[106]1371
[303]1372        var $data_gather, $cluster, $host, $node, $image;
1373        var $headerstrlen;
1374
1375        function HostImage( $data_gather, $cluster, $host ) {
1376
1377                $this->data_gather      = $data_gather;
1378                $this->cluster          = $cluster;
1379                $this->host             = $host;
1380                $this->y_offset         = 0;
1381                $this->font             = 2;
1382                $this->fontspaceing     = 2;
1383                $this->headerstrlen     = array();
1384
1385                $this->fontheight       = ImageFontHeight( $this->font );
1386                $this->fontwidth        = ImageFontWidth( $this->font );
1387
1388                $dg                     = &$this->data_gather;
1389                $this->node             = &$dg->getNode( $this->host );
1390                $n                      = &$this->node;
1391                $this->njobs            = $n->getJobs();
1392        }
1393
1394        function drawJobs() {
1395
1396                $dg                     = &$this->data_gather;
1397                $colorblack             = imageColorAllocate( $this->image, 0, 0, 0 );
1398
1399                for( $n = 0; $n < count( $this->njobs ); $n++ ) {
1400
1401                        $jobid                  = $this->njobs[$n];
1402                        $jobinfo                = $dg->getJob( $jobid );
1403
1404                        $xoffset                = 5;
1405                        imageString( $this->image, $this->font, $xoffset, $this->y_offset, strval( $jobid ), $colorblack );
1406
1407                        foreach( $this->headerstrlen as $headername => $headerlen ) {
1408
1409                                if( $headername == 'nodes' ) {
1410                                        $attrval        = strval( count( $jobinfo[nodes] ) );
1411                                } else if( $headername == 'cpus' ) {
1412
1413                                        if( !isset( $jobinfo[ppn] ) )
1414                                                $jobinfo[ppn] = 1;
1415
1416                                        $attrval        = strval( count( $jobinfo[nodes] ) * intval( $jobinfo[ppn] ) );
1417
1418                                } else if( $headername == 'runningtime' ) {
1419                                        $attrval        = makeTime( intval( $jobinfo[reported] ) - intval( $jobinfo[start_timestamp] ) );
1420                                } else {
1421                                        $attrval        = strval( $jobinfo[$headername] );
1422                                }
1423
1424                                imageString( $this->image, $this->font, $xoffset, $this->y_offset, $attrval, $colorblack );
1425               
1426                                $xoffset        = $xoffset + ($this->fontwidth * ( $headerlen + 1 ) );
1427
1428                        }
1429                       
1430                        $this->newLineOffset();
1431                }
1432        }
1433
1434        function drawHeader() {
1435
1436                $dg                     = &$this->data_gather;
1437
1438                for( $n = 0; $n < count( $this->njobs ); $n++ ) {
1439
1440                        $jobid                  = $this->njobs[$n];
1441                        $jobinfo                = $dg->getJob( $jobid );
1442
1443                        if( !isset( $this->headerstrlen[id] ) )
1444                                $this->headerstrlen[id] = strlen( strval( $jobid ) );
1445                        else
1446                                if( strlen( strval( $jobid ) ) > $this->headerstrlen[id] )
1447                                        $this->headerstrlen[id] = strlen( strval( $jobid ) );
1448
1449                        if( !isset( $this->headerstrlen[owner] ) )
1450                                $this->headerstrlen[owner]      = strlen( strval( $jobinfo[owner] ) );
1451                        else
1452                                if( strlen( strval( $jobinfo[owner] ) ) > $this->headerstrlen[owner] )
1453                                        $this->headerstrlen[owner]      = strlen( strval( $jobinfo[owner] ) );
1454
1455                        if( !isset( $this->headerstrlen[queue] ) )
1456                                $this->headerstrlen[queue]      = strlen( strval( $jobinfo[queue] ) );
1457                        else
1458                                if( strlen( strval( $jobinfo[queue] ) ) > $this->headerstrlen[queue] )
1459                                        $this->headerstrlen[queue]      = strlen( strval( $jobinfo[queue] ) );
1460
1461                        if( !isset( $jobinfo[ppn] ) )
1462                                $jobinfo[ppn] = 1;
1463
1464                        $cpus                   = count( $jobinfo[nodes] ) * intval( $jobinfo[ppn] );
1465
1466                        if( !isset( $this->headerstrlen[cpus] ) )
1467                                $this->headerstrlen[cpus]       = strlen( strval( $cpus ) );
1468                        else
1469                                if( strlen( strval( $cpus ) ) > $this->headerstrlen[cpus] )
1470                                        $this->headerstrlen[cpus]       = strlen( strval( $cpus ) );
1471
1472                        $nodes                  = count( $jobinfo[nodes] );
1473
1474                        if( !isset( $this->headerstrlen[nodes] ) )
1475                                $this->headerstrlen[nodes]      = strlen( strval( $nodes ) );
1476                        else
1477                                if( strlen( strval( $nodes) ) > $this->headerstrlen[nodes] )
1478                                        $this->headerstrlen[nodes]      = strlen( strval( $nodes ) );
1479
1480                        $runningtime            = makeTime( intval( $jobinfo[reported] ) - intval( $jobinfo[start_timestamp] ) );
1481
1482                        if( !isset( $this->headerstrlen[runningtime] ) )
1483                                $this->headerstrlen[runningtime]        = strlen( strval( $runningtime) );
1484                        else
1485                                if( strlen( strval( $runningtime) ) > $this->headerstrlen[runningtime] )
1486                                        $this->headerstrlen[runningtime]        = strlen( strval( $runningtime) );
1487
1488                        if( !isset( $this->headerstrlen[name] ) )
1489                                $this->headerstrlen[name]       = strlen( strval( $jobinfo[name] ) );
1490                        else
1491                                if( strlen( strval( $jobinfo[name] ) ) > $this->headerstrlen[name] )
1492                                        $this->headerstrlen[name]       = strlen( strval( $jobinfo[name] ) );
1493
1494                }
1495
1496                $xoffset        = 5;
1497
1498                foreach( $this->headerstrlen as $headername => &$headerlen ) {
1499
1500                        $colorgreen     = imageColorAllocate( $this->image, 0, 200, 0 );
1501
1502                        imageString( $this->image, $this->font, $xoffset, $this->y_offset, ucfirst( $headername ), $colorgreen );
1503               
1504                        if( $headerlen < strlen( $headername ) )
1505                                $headerlen      = strlen( $headername );
1506
1507                        $xoffset        = $xoffset + ($this->fontwidth * ( $headerlen + 1 ) );
1508
1509                }
1510                $this->newLineOffset();
1511        }
1512
1513        function newLineOffset() {
1514
1515                $this->y_offset         = $this->y_offset + $this->fontheight + $this->fontspaceing;
1516        }
1517
1518        function draw() {
1519
1520                $xlen           = 450;
1521                $ylen           = ( count( $this->njobs ) * ( $this->fontheight + $this->fontspaceing ) ) + (3 * $this->fontheight);
1522
1523                $this->image    = imageCreateTrueColor( $xlen, $ylen );
1524                $colorwhite     = imageColorAllocate( $this->image, 255, 255, 255 );
1525                imageFill( $this->image, 0, 0, $colorwhite );                         
1526
1527                $colorblue      = imageColorAllocate( $this->image, 0, 0, 255 );
1528
1529                imageString( $this->image, $this->font, 1, $this->y_offset, "Monarch Joblist - host: ".$this->host, $colorblue );
1530                $this->newLineOffset();
1531
1532                $this->drawHeader();
1533                $this->drawJobs();
1534
1535                header( 'Content-type: image/png' );
1536                imagePNG( $this->image );
1537                imageDestroy( $this->image );
1538        }
1539}
1540
[329]1541function array_rem( $val, &$arr )
1542{
1543        // Delete val from arr
1544        //
1545        $i      = array_search( $val, $arr );
1546
1547        if( $i == false ) return false;
1548
1549        $arr    = array_merge( array_slice( $arr, 0, $i ), array_slice( $arr, $i+1, count( $arr ) ) );
1550
1551        return true;
1552}
1553
1554function cmp( $a, $b ) 
1555{
1556        global $SORT_ORDER;
1557        global $skan_str;
1558        global $x_first, $y_first;
1559
1560        $a_node         = $a;
1561        $b_node         = $b;
1562        $a              = $a_node->getHostname();
1563        $b              = $b_node->getHostname();
1564
1565        if( $a == $b ) return 0;
1566
1567        if( $x_first )
1568        {
1569                $n = sscanf( $a, $skan_str, $a_x, $a_y );
1570                $n = sscanf( $b, $skan_str, $b_x, $b_y );
1571        }
1572        else if( $y_first )
1573        {
1574                $n = sscanf( $a, $skan_str, $a_y, $a_x );
1575                $n = sscanf( $b, $skan_str, $b_y, $b_x );
1576        }
1577
1578        if ( $SORT_ORDER=="desc" )
1579        {
1580
1581                // 1  = a < b
1582                // -1 = a > b
1583                //
1584                if ($a_x == $b_x)
1585                {
1586                        if ($a_y < $b_y)
1587                        {
1588                                return 1;
1589                        }
1590                        else if ($a_y > $b_y)
1591                        {
1592                                return -1;
1593                        }
1594                }
1595                else if ($a_x < $b_x)
1596                {
1597                        return 1;
1598                }
1599                else if ($a_x > $b_x)
1600                {
1601                        return -1;
1602                }
1603        }
1604        else if ( $SORT_ORDER == "asc" )
1605        {
1606
1607                // 1  = a > b
1608                // -1 = a < b
1609                //
1610                if ($a_x == $b_x)
1611                {
1612                        if ($a_y > $b_y)
1613                        {
1614                                return 1;
1615                        }
1616                        else if ($a_y < $b_y)
1617                        {
1618                                return -1;
1619                        }
1620                }
1621                else if ($a_x > $b_x)
1622                {
1623                        return 1;
1624                }
1625                else if ($a_x < $b_x)
1626                {
1627                        return -1;
1628                }
1629        }
1630}
[303]1631function makeTime( $time ) {
1632
1633        $days = intval( $time / 86400 );
1634        $time = ($days>0) ? $time % ($days * 86400) : $time;
1635
1636        //printf( "time = %s, days = %s\n", $time, $days );
1637
1638        $date_str = '';
1639        $day_str = '';
1640
1641        if( $days > 0 ) {
1642                if( $days > 1 )
1643                        $day_str .= $days . ' days';
1644                else
1645                        $day_str .= $days . ' day';
1646        }
1647
1648        $hours = intval( $time / 3600 );
1649        $time = $hours ? $time % ($hours * 3600) : $time;
1650
1651        //printf( "time = %s, days = %s, hours = %s\n", $time, $days, $hours );
1652        if( $hours > 0 ) {
1653                $date_str .= $hours . ':';
1654                $date_unit = 'hours'; 
1655        }
1656
1657        $minutes = intval( $time / 60 );
1658        $seconds = $minutes ? $time % ($minutes * 60) : $time;
1659
1660        if( $minutes > 0 ) {
1661
1662                if( $minutes >= 10 )
1663                        $date_str .= $minutes . ':';
1664                else
1665                        $date_str .= '0' . $minutes . ':';
1666
1667                $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
1668        } else {
1669                if($hours > 0 ) {
1670                        $date_str .= '00:';
1671                        $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
1672                }
1673        }
1674
1675
1676        $date_unit = (!isset($date_unit)) ? 'seconds' : $date_unit;
1677
1678        if( $seconds > 0 ) {
1679
1680                if( $seconds >= 10 )
1681                        $date_str .= $seconds . ' ' . $date_unit;
1682                else
1683                        $date_str .= '0' . $seconds . ' ' . $date_unit;
1684
1685        } else if ( $hours > 0 or $minutes > 0 )
1686
1687                $date_str .= '00 ' . $date_unit;
1688
1689        if( $days > 0) {
1690
1691                if( $hours > 0 or $minutes > 0 or $seconds > 0 )
1692                        $date_str = $day_str . ' - ' . $date_str;
1693                else
1694                        $date_str = $day_str;
1695        }
1696
1697        return $date_str;
1698}
[102]1699?>
Note: See TracBrowser for help on using the repository browser.