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

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

libtoga.php:

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