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

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

web/addons/job_monarch/overview.php:

  • make our own datasource
  • store the XML data in a session for the clusterimage
  • assign a image map of clickable nodes for the clusterimage

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

  • add node_clustermap imagemap block

web/addons/job_monarch/libtoga.php:

web/addons/job_monarch/image.php:

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