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

Last change on this file since 225 was 225, checked in by bastiaans, 18 years ago

ALL:

  • added GPL
File size: 22.1 KB
Line 
1<?php
2/*
3 *
4 * This file is part of Jobmonarch
5 *
6 * Copyright (C) 2006  Ramon Bastiaans
7 *
8 * Jobmonarch is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * Jobmonarch is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 *
22 */
23
24
25// If php is compiled without globals
26//
27//if ( !empty( $_GET ) ) {
28//        extract( $_GET );
29//}
30
31class HTTPVariables {
32
33        var $clustername, $metricname;
34        var $restvars, $httpvars;
35
36        function HTTPVariables( $httpvars, $getvars ) {
37
38                $this->restvars = array();
39
40                $this->clustername = $httpvars["c"] ? $httpvars["c"] : null;
41                $this->metricname = $httpvars["m"] ? $httpvars["m"] : null;
42
43                foreach( $httpvars as $httpvar => $httpval ) {
44                       
45                        if( $httpval ) {
46                                $this->restvars[$httpvar] = $httpval;
47                        }
48                }
49
50                foreach( $getvars as $getvar => $getval ) {
51
52                        if( $getval ) {
53                                $this->restvars[$getvar] = $getval;
54                        }
55                }
56        }
57
58        function getClusterName() {
59                return $this->clustername;
60        }
61
62        function getMetricName() {
63                return $this->metricname;
64        }
65
66        function getHttpVar( $var ) {
67                if( isset( $this->restvars[$var] ) )
68                        return $this->restvars[$var];
69                else
70                        return null;
71        }
72}
73
74// Toga's conf
75//
76include_once "./conf.php";
77include_once "./version.php";
78
79global $GANGLIA_PATH;
80global $RRDTOOL;
81global $JOB_ARCHIVE_DIR;
82global $JOB_ARCHIVE_DBASE;
83
84$my_dir = getcwd();
85
86// Load Ganglia's PHP
87chdir( $GANGLIA_PATH );
88
89include_once "./conf.php";
90include_once "./functions.php";
91include_once "./ganglia.php";
92include_once "./get_context.php";
93unset( $start );
94$context = 'cluster';
95include_once "./get_ganglia.php";
96
97// Back to our PHP
98chdir( $my_dir );
99
100global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH, $DATA_SOURCE, $HTTP_GET_VARS, $_GET;
101$httpvars = new HTTPVariables( $HTTP_GET_VARS, $_GET );
102
103// Set cluster context so that Ganglia will
104// provide us with the correct metrics array
105//
106global $context, $clustername, $reports;
107
108//$clustername = $httpvars->getClusterName();
109
110global $default_metric;
111
112// Ganglia's array of host metrics
113//
114global $metrics, $hosts_up;
115
116class TarchDbase {
117
118        var $ip, $dbase, $conn;
119
120        function TarchDbase( $ip = null, $dbase = 'jobarch' ) {
121
122                global $JOB_ARCHIVE_DBASE;
123
124                $db_fields = explode( '/', $JOB_ARCHIVE_DBASE );
125
126                $this->ip = $db_fields[0];
127                $this->dbase = $db_fields[1];
128                $this->conn = null;
129        }
130
131        function connect() {
132
133                if( $this->ip == null and $this->dbase == 'toga' )
134                        $this->conn = pg_connect( "dbname=".$this->dbase );
135                else
136                        $this->conn = pg_connect( "host=".$this->ip." dbname=".$this->dbase );
137        }
138
139        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 ) {
140
141                if( $id ) 
142                        $query = "SELECT job_id FROM jobs WHERE job_id = '$id' AND job_status = 'F'";
143                else {
144                        $query_args = array();
145                       
146                        if( $queue )
147                                $query_args[] = "job_queue ='$queue'";
148                        if( $user )
149                                $query_args[] = "job_owner ='$user'";
150                        if( $name )
151                                $query_args[] = "job_name = '$name'";
152                        if( $start_from_time )
153                                $query_args[] = "job_start_timestamp >= $start_from_time";
154                        if( $start_to_time )
155                                $query_args[] = "job_start_timestamp <= $start_to_time";
156                        if( $end_from_time )
157                                $query_args[] = "job_stop_timestamp >= $end_from_time";
158                        if( $end_to_time )
159                                $query_args[] = "job_stop_timestamp <= $end_to_time";
160
161                        $query = "SELECT job_id FROM jobs WHERE job_status = 'F' AND ";
162                        $extra_query_args = '';
163
164                        foreach( $query_args as $myquery ) {
165
166                                if( $extra_query_args == '' )
167                                        $extra_query_args = $myquery;
168                                else
169                                        $extra_query_args .= " AND ".$myquery;
170                        }
171                        $query .= $extra_query_args;
172                }
173
174                $ids = $this->queryDbase( $query );
175
176                $ret = array();
177
178                foreach( $ids as $crow)
179                        $ret[] = $crow[job_id];
180
181                return $ret;
182        }
183
184        function getNodesForJob( $jobid ) {
185
186                $result = $this->queryDbase( "SELECT node_id FROM job_nodes WHERE job_id = '$jobid'" );
187
188                $nodes = array();
189
190                foreach( $result as $result_row ) 
191
192                        $nodes[] = $this->getNodeArray( $result_row[node_id] );
193
194                return $nodes;
195        }
196
197        function getJobsForNode( $nodeid ) {
198
199                $result = $this->queryDbase( "SELECT job_id FROM job_nodes WHERE node_id = '$nodeid'" );
200
201                $jobs = array();
202
203                foreach( $result as $result_row )
204
205                        $jobs[] = $this->getJobArray( $result_row[job_id] );
206
207                return $jobs;
208        }
209
210        function getJobArray( $id ) {
211                $result = $this->queryDbase( "SELECT * FROM jobs WHERE job_id = '$id'" );
212
213                return ( $this->makeArray( $result[0] ) );
214        }
215
216        function getNodeArray( $id ) {
217
218                $result = $this->queryDbase( "SELECT * FROM nodes WHERE node_id = '$id'" );
219
220                return ( $this->makeArray( $result[0] ) );
221        }
222
223        function makeArray( $result_row ) {
224
225                $myar = array();
226
227                foreach( $result_row as $mykey => $myval ) {
228
229                        $map_key = explode( '_', $mykey );
230
231                        $rmap_key = array_reverse( $map_key );
232                        array_pop( $rmap_key );
233                        $map_key = array_reverse( $rmap_key );
234                       
235                        $newkey = implode( '_', $map_key );
236                       
237                        $myar[$newkey] = $result_row[$mykey];
238                }
239
240                return $myar;
241        }
242
243        function queryDbase( $query ) {
244
245                $result_rows = array();
246       
247                if( !$this->conn )
248                        $this->connect();
249
250                //printf( "query = [%s]\n", $query );
251                $result = pg_query( $this->conn, $query );
252
253                while ($row = pg_fetch_assoc($result))
254                        $result_rows[] = $row;
255
256                return $result_rows;
257        }
258}
259
260class TarchRrdGraph {
261        var $rrdbin, $rrdvalues, $clustername, $hostname, $tempdir, $tarchdir, $metrics;
262
263        function TarchRrdGraph( $clustername, $hostname ) {
264
265                global $RRDTOOL;
266                global $JOB_ARCHIVE_DIR;
267
268                $this->rrdbin = $RRDTOOL;
269                $this->rrdvalues = array();
270                $this->tarchdir = $JOB_ARCHIVE_DIR;
271                $this->clustername = $clustername;
272                $this->hostname = $hostname;
273        }
274
275        function doCmd( $command ) {
276
277                printf( "command = %s\n", $command );
278                $pipe = popen( $command . ' 2>&1', 'r' );
279
280                if (!$pipe) {
281                        print "pipe failed.";
282                        return "";
283                }
284
285                $output = '';
286                while(!feof($pipe))
287                        $output .= fread($pipe, 1024);
288
289                pclose($pipe);
290
291                $output = explode( "\n", $output );
292                //print_r( $output );
293                return $output;
294        }
295
296        function dirList( $dir ) {
297
298                $dirlist = array();
299
300                if ($handle = opendir( $dir )) {
301                        while (false !== ($file = readdir($handle))) {
302                                if ($file != "." && $file != "..") {
303                                        $dirlist[] = $file;
304                                }
305                        }
306                        closedir($handle);
307                }
308
309                return $dirlist;
310        }
311
312        function getTimePeriods( $start, $end ) {
313
314                //printf("start = %s end = %s\n", $start, $end );
315                $times = array();
316                $dirlist = $this->dirList( $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname );
317
318                //print_r( $dirlist );
319
320                $first = 0;
321                $last = 9999999999999;
322
323                foreach( $dirlist as $dir ) {
324
325                        if( $dir > $first and $dir <= $start )
326                                $first = $dir;
327                        if( $dir < $last and $dir >= $end )
328                                $last = $dir;
329                }
330
331                //printf( "first = %s last = %s\n", $first, $last );
332
333                foreach( $dirlist as $dir ) {
334
335                        //printf( "dir %s ", $dir );
336
337                        if( $dir >= $first and $dir <= $last and !array_key_exists( $dir, $times ) ) {
338                       
339                                $times[] = $dir;
340                                //printf("newtime %s ", $dir );
341
342                        }
343                }
344
345                //print_r( $times );
346
347                sort( $times );
348
349                //print_r( $times );
350
351                return $times;
352        }
353
354        function getRrdDirs( $start, $stop ) {
355
356                //printf( "tarchdir = %s\n", $this->tarchdir );
357                $timess = $this->getTimePeriods( $start, $stop );
358                //print_r( $timess );
359
360                $rrd_files = array();
361
362                foreach( $timess as $time ) {
363
364                        $rrd_files[] = $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname. '/'.$time;
365                }
366
367                return $rrd_files;
368        }
369
370        function getRrdFiles( $metric, $start, $stop ) {
371
372                $times = $this->getTimePeriods( $start, $stop );
373
374                $rrd_files = array();
375
376                foreach( $times as $time ) {
377
378                        $rrd_files[] = $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname . '/' .$time. '/' . $metric. '.rrd';
379                }
380
381                return $rrd_files;
382        }
383
384        function graph( $descr ) {
385//      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
386//      380x461
387//      monitor2:/data/toga/rrds/LISA Cluster/gb-r15n11.irc.sara.nl#
388                //$command = $this->rrdbin . " graph - --start $start --end $end ".
389                        "--width $width --height $height $upper_limit $lower_limit ".
390                        "--title '$title' $vertical_label $extras $background ". $series;
391
392                //$graph = $this->doCmd( $command );
393
394                //return $graph;
395                return 0;
396        }
397}
398
399class DataSource {
400
401        var $data, $ip, $port;
402
403        function DataSource( $ip = '127.0.0.1', $port = 8649 ) {
404                $this->ip = $ip;
405                $this->port = $port;
406        }
407
408        function getData() {
409
410                $errstr;
411                $errno = 0;
412                $timeout = 3;
413
414                $fp = fsockopen( $this->ip, $this->port, &$errno, &$errstr, $timeout );
415
416                if( !$fp ) {
417                        echo 'Unable to connect to '.$this->ip.':'.$this->port; // printf( 'Unable to connect to [%s:%.0f]', $this->ip, $this->port );
418                        return;
419                }
420
421                stream_set_timeout( $fp, 30 );
422
423                while ( !feof( $fp ) ) {
424                       
425                        $data .= fread( $fp, 16384 );
426                }
427
428                fclose( $fp );
429
430                return $data;
431        }
432}
433
434class DataGatherer {
435
436        var $xmlhandler, $data, $httpvars;
437
438        function DataGatherer( $cluster ) {
439
440                global $DATA_SOURCE;
441       
442                //printf("dg cluster = %s\n", $cluster );
443                $ds_fields = explode( ':', $DATA_SOURCE );
444                $ds_ip = $ds_fields[0];
445                $ds_port = $ds_fields[1];
446
447                $this->source = new DataSource( $ds_ip, $ds_port );
448
449                $this->parser = xml_parser_create();
450                $this->httpvars = $httpvars;
451                $this->xmlhandler = new TorqueXMLHandler( $cluster );
452                xml_set_element_handler( $this->parser, array( &$this->xmlhandler, 'startElement' ), array( &$this->xmlhandler, 'stopElement' ) );
453        }
454
455        function parseXML() {
456
457                $src = &$this->source;
458                $this->data = $src->getData();
459
460                if ( !xml_parse( &$this->parser, $this->data ) )
461                        $error = sprintf( 'XML error: %s at %d', xml_error_string( xml_get_error_code( &$this->parser ) ), xml_get_current_line_number( &$this->parser ) );
462        }
463
464        function printInfo() {
465                $handler = $this->xmlhandler;
466                $handler->printInfo();
467        }
468
469        function getNodes() {
470                $handler = $this->xmlhandler;
471                return $handler->getNodes();
472        }
473
474        function getCpus() {
475                $handler = $this->xmlhandler;
476                return $handler->getCpus();
477        }
478
479        function getJobs() {
480                $handler = $this->xmlhandler;
481                return $handler->getJobs();
482        }
483
484        function getHeartbeat() {
485                $handler = $this->xmlhandler;
486                return $handler->getHeartbeat();
487        }
488}
489
490class TorqueXMLHandler {
491
492        var $clusters, $heartbeat, $nodes, $jobs, $clustername, $proc_cluster;
493
494        function TorqueXMLHandler( $clustername ) {
495                $jobs = array();
496                $clusters = array();
497                $this->nodes = array();
498                $heartbeat = array();
499                $this->clustername = $clustername;
500                //printf(" cluster set to %s \n", $this->clustername );
501        }
502
503        function getCpus() {
504
505                $cpus = 0;
506
507                foreach( $this->jobs as $jobid=>$jobattrs ) {
508
509                        $nodes = count( $jobattrs[nodes] );
510                        $ppn = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
511                        $mycpus = $nodes * $ppn;
512
513                        $cpus = $cpus + $mycpus;
514                }
515        }
516
517        function startElement( $parser, $name, $attrs ) {
518
519                $jobs = &$this->jobs;
520                $nodes = &$this->nodes;
521
522                if ( $attrs[TN] ) {
523
524                        // Ignore dead metrics. Detect and mask failures.
525                        if ( $attrs[TN] > $attrs[TMAX] * 4 )
526                                return;
527                }
528
529                $jobid = null;
530
531                //printf( '%s=%s', $attrs[NAME], $attrs[VAL] );
532
533                //printf( "clustername = %s proc_cluster = %s\n", $this->clustername, $this->proc_cluster );
534
535                if( $name == 'CLUSTER' ) {
536
537                        $this->proc_cluster = $attrs[NAME];
538                        //printf( "Found cluster %s\n", $attrs[NAME] );
539                        //print_r( $attrs );
540
541                        //if( !isset( $clusters[$clustername] ) )
542                        //      $clusters[$clustername] = array();
543
544                } else if( $name == 'HOST' and $this->proc_cluster == $this->clustername) {
545
546                        $hostname = $attrs[NAME];
547                        $location = $attrs[LOCATION];
548                        //printf( "Found node %s\n", $hostname );
549
550                        if( !isset( $nodes[$hostname] ) )
551                                $nodes[$hostname] = new NodeImage( $hostname );
552
553                } else if( $name == 'METRIC' and strstr( $attrs[NAME], 'MONARCH' ) and $this->proc_cluster == $this->clustername ) {
554
555                        if( strstr( $attrs[NAME], 'MONARCH-HEARTBEAT' ) ) {
556
557                                $this->heartbeat['time'] = $attrs[VAL];
558                                //printf( "heartbeat %s\n", $heartbeat['time'] );
559
560                        } else if( strstr( $attrs[NAME], 'MONARCH-JOB' ) ) {
561
562                                sscanf( $attrs[NAME], 'MONARCH-JOB-%d', $jobid );
563
564                                //printf( "jobid %s\n", $jobid );
565
566                                if( !isset( $jobs[$jobid] ) )
567                                        $jobs[$jobid] = array();
568
569                                $fields = explode( ' ', $attrs[VAL] );
570
571                                foreach( $fields as $f ) {
572                                        $togavalues = explode( '=', $f );
573
574                                        $toganame = $togavalues[0];
575                                        $togavalue = $togavalues[1];
576
577                                        //printf( "\t%s\t= %s\n", $toganame, $togavalue );
578
579                                        if( $toganame == 'nodes' ) {
580
581                                                if( $jobs[$jobid][status] == 'R' ) {
582                                               
583                                                        if( !isset( $jobs[$jobid][$toganame] ) )
584                                                                $jobs[$jobid][$toganame] = array();
585
586                                                        $mynodes = explode( ';', $togavalue );
587
588                                                        foreach( $mynodes as $node )
589
590                                                                $jobs[$jobid][$toganame][] = $node;
591
592                                                } else if( $jobs[$jobid][status] == 'Q' ) {
593
594                                                        $jobs[$jobid][$toganame] = $togavalue;
595                                                }
596                                               
597                                        } else {
598
599                                                $jobs[$jobid][$toganame] = $togavalue;
600                                        }
601                                }
602
603                                if( isset( $jobs[$jobid][domain] ) and isset( $jobs[$jobid][nodes] ) ) {
604                       
605                                        $nr_nodes = count( $jobs[$jobid][nodes] );
606                       
607                                        foreach( $jobs[$jobid][nodes] as $node ) {
608
609                                                $domain = $jobs[$jobid][domain];
610                                                $domain_len = 0 - strlen( $domain );
611
612                                                if( substr( $node, $domain_len ) != $domain ) {
613                                                        $host = $node. '.'.$domain;
614                                                } else {
615                                                        $host = $node;
616                                                }
617
618                                                //$host = $node.'.'.$jobs[$jobid][domain];
619                               
620                                                if( !isset( $nodes[$host] ) )
621                                                        $my_node = new NodeImage( $host );
622                                                else
623                                                        $my_node = $nodes[$host];
624
625                                                if( !$my_node->hasJob( $jobid ) )
626
627                                                        if( isset( $jobs[$jobid][ppn] ) )
628                                                                $my_node->addJob( $jobid, ((int) $jobs[$jobid][ppn]) );
629                                                        else
630                                                                $my_node->addJob( $jobid, 1 );
631
632                                                $nodes[$host] = $my_node;
633                                        }
634                                }
635                        }
636                }
637                $this->jobs = $jobs;
638                //print_r( $nodes );
639                $this->nodes = $nodes;
640                //print_r( $this->nodes );
641        }
642
643        function stopElement( $parser, $name ) {
644        }
645
646        function printInfo() {
647
648                $jobs = &$this->jobs;
649
650                printf( "---jobs---\n" );
651
652                foreach( $jobs as $jobid => $job ) {
653
654                        printf( "job %s\n", $jobid );
655
656                        if( isset( $job[nodes] ) ) {
657
658                                foreach( $job[nodes] as $node ) {
659
660                                        $mynode = $this->nodes[$node];
661                                        $hostname = $mynode->getHostname();
662                                        $location = $mynode->getLocation();
663
664                                        printf( "\t- node %s\tlocation %s\n", $hostname, $location );
665                                        //$this->nodes[$hostname]->setLocation( "hier draait job ".$jobid );
666                                }
667                        }
668                }
669
670                printf( "---nodes---\n" );
671
672                $nodes = &$this->nodes;
673
674                foreach( $nodes as $node ) {
675
676                        $hostname = $node->getHostname();
677                        $location = $node->getLocation();
678                        $jobs = implode( ' ', $node->getJobs() );
679                        printf( "* node %s\tlocation %s\tjobs %s\n", $hostname, $location, $jobs );
680                }
681        }
682
683        function getNodes() {
684                //print_r( $this->nodes );
685                return $this->nodes;
686        }
687
688        function getJobs() {
689                return $this->jobs;
690        }
691
692        function getHeartbeat() {
693                return $this->heartbeat['time'];
694        }
695}
696
697class NodeImage {
698
699        var $image, $x, $y, $hostname, $jobs, $tasks, $showinfo;
700
701        function NodeImage( $hostname ) {
702
703                $this->jobs = array();
704                //$this->image = $image;
705                //$this->x = $x;
706                //$this->y = $y;
707                $this->tasks = 0;
708                $this->hostname = $hostname;
709                $this->cpus = $this->determineCpus();
710                $this->showinfo = 1;
711        }
712
713        function addJob( $jobid, $cpus ) {
714                $jobs = &$this->jobs;
715
716                $jobs[] = $jobid;
717                $this->jobs = $jobs;
718
719                $this->addTask( $cpus );
720        }
721
722        function hasJob( $jobid ) {
723
724                $jobfound = 0;
725
726                if( count( $this->jobs ) > 0 )
727                        foreach( $this->jobs as $job )
728
729                                if( $job == $jobid )
730                                        $jobfound = 1;
731
732                return $jobfound;
733        }
734
735        function addTask( $cpus ) {
736
737                $this->tasks = $this->tasks + $cpus;
738        }
739
740        function setImage( $image ) {
741
742                $this->image = $image;
743        }
744
745        function setCoords( $x, $y ) {
746
747                $this->x = $x;
748                $this->y = $y;
749        }
750
751        function colorHex( $color ) {
752       
753                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) );
754
755                return $my_color;
756        }
757
758        function setLoad( $load ) {
759                $this->load = $load;
760        }
761
762        function setHostname( $hostname ) {
763                $this->hostname = $hostname;
764        }
765
766        function getHostname() {
767                return $this->hostname;
768        }
769
770        function getJobs() {
771                return $this->jobs;
772        }
773
774        function setShowinfo( $showinfo ) {
775                $this->showinfo = $showinfo;
776        }
777
778        function draw() {
779
780                $this->drawSmall();
781        }
782
783        function drawBig() {
784
785        }
786
787        function drawSmall() {
788
789                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
790                global $JOB_NODE_MARKING;
791
792                $black_color = imageColorAllocate( $this->image, 0, 0, 0 );
793                $size = $SMALL_CLUSTERIMAGE_NODEWIDTH;
794
795                imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+($size), $this->y+($size), $black_color );
796
797                if( $this->showinfo) {
798               
799                        $this->load = $this->determineLoad();
800
801                        if( !isset( $this->image ) or !isset( $this->x ) or !isset( $this->y ) ) {
802                                printf( "aborting\n" );
803                                printf( "x %d y %d load %f\n", $this->x, $this->y, $load );
804                                return;
805                        }
806
807
808                        // Convert Ganglias Hexadecimal load color to a Decimal one
809                        //
810                        $load = $this->determineLoad(); 
811                        $usecolor = $this->colorHex( load_color($load) );
812                        imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor );
813                        if( count( $this->jobs ) > 0 )
814                                imageString( $this->image, 1, $this->x+(($size/2)-2), $this->y+(($size/2)-3), $JOB_NODE_MARKING, $black_color );
815
816                } else {
817
818                        // White
819                        $usecolor = imageColorAllocate( $this->image, 255, 255, 255 );
820                        imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor );
821                }
822
823
824        }
825
826        function determineCpus() {
827
828                global $metrics;
829
830                $cpus = $metrics[$this->hostname][cpu_num][VAL];
831                if (!$cpus) $cpus=1;
832
833                return $cpus;
834        }
835
836        function determineLoad() {
837
838                global $metrics;
839
840                $load_one = $metrics[$this->hostname][load_one][VAL];
841                $load = ((float) $load_one)/$this->cpus;
842
843                return $load;
844        }
845}
846
847class ClusterImage {
848
849        var $dataget, $image, $clustername;
850        var $filtername, $filters;
851
852        function ClusterImage( $clustername ) {
853
854                //printf( "image cluster = %s\n", $clustername );
855                $this->dataget = new DataGatherer( $clustername );
856                $this->clustername = $clustername;
857                $this->filters = array();
858        }
859
860        function setFilter( $filtername, $filtervalue ) {
861
862                //printf("filter %s = %s\n", $filtername, $filtervalue );
863                //printf( "filter set to %s = %s\n", $filtername, $filtervalue );
864                $this->filters[$filtername] = $filtervalue;
865                //print_r($this->filters);
866        }
867
868        function filterNodes( $jobs, $nodes ) {
869
870                $filtered_nodes = array();
871
872                foreach( $nodes as $node ) {
873
874                        $hostname = $node->getHostname();
875
876                        $addhost = 1;
877
878                        if( count( $this->filters ) > 0 ) {
879
880                                $mynjobs = $node->getJobs();
881
882                                if( count( $mynjobs ) > 0 ) {
883
884                                        foreach( $mynjobs as $myjob ) {
885
886                                                foreach( $this->filters as $filtername => $filtervalue ) {
887
888                                                        //printf("filter bla %s = %s\n", $filtername,$filtervalue );
889
890                                                        if( $filtername!=null && $filtername!='' ) {
891
892                                                                if( $filtername == 'jobid' && !$node->hasJob( $filtervalue) ) {
893                                                                        $addhost = 0;
894                                                                        //printf("host %s has no job %s\n", $hostname, $filtervalue);
895                                                                } else if( $filtername != 'jobid' ) {
896                                                                        //printf("myjob is %s\n", $myjob );
897                                                                        if( $jobs[$myjob][$filtername] != $filtervalue ) {
898                                                                                //printf("host %s has no job with %s=%s\n", $hostname, $filtername, $filtervalue);
899                                                                                $addhost = 0;
900                                                                        }
901                                                                }
902                                                        }
903                                                }
904                                        }
905                                } else
906                                        $addhost = 0;
907                        }
908
909                        if( $addhost )
910                                $filtered_nodes[] = $hostname;
911                }
912
913                return $filtered_nodes;
914        }
915
916        function draw() {
917
918                //printf("stopt met uitvoer");
919                //return;
920
921                global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH;
922       
923                $mydatag = $this->dataget;
924                $mydatag->parseXML();
925
926                //$max_width = 250;
927                //$node_width = 11;
928
929                $max_width = $SMALL_CLUSTERIMAGE_MAXWIDTH;
930                $node_width = $SMALL_CLUSTERIMAGE_NODEWIDTH;
931
932                //printf( "cmaxw %s nmaxw %s", $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH );
933
934                $nodes = $mydatag->getNodes();
935                $nodes_hosts = array_keys( $nodes );
936
937                $nodes_nr = count( $nodes );
938
939                $nodes_size = $nodes_nr*$node_width;
940                $node_rows = 0;
941
942                if( $nodes_size > $max_width ) {
943                        $nodes_per_row = ( (int) ($max_width/$node_width) );
944                } else {
945                        $nodes_per_row = $nodes_size;
946                        $node_rows = 1;
947                }
948
949                if( $nodes_per_row < $nodes_nr ) {
950                        $node_rows = ( (int) ($nodes_nr/$nodes_per_row) );
951                        $node_rest = fmod( $nodes_nr, $nodes_per_row );
952                        //printf( "nodesnr %d noderest %f\n", $nodes_nr, $node_rest );
953                        if( $node_rest > 0 ) {
954                                $node_rows++;
955                                //printf( "noderows %d\n", $node_rows );
956                        }
957                }
958
959                //printf( "imagecreate: %dx%d", ($nodes_per_row*$node_width), ($node_rows*$node_width) );
960                //$image = imageCreateTrueColor( ($nodes_per_row*$node_width)+1, ($node_rows*$node_width)+1 );
961                $image = imageCreateTrueColor( $max_width, ($node_rows*$node_width)+1 );
962                $colorwhite = imageColorAllocate( $image, 255, 255, 255 );
963                imageFill( $image, 0, 0, $colorwhite );
964
965                $jobs = $mydatag->getJobs();
966                //printf("filtername = %s\n", $filtername );
967                $filtered_nodes = $this->filterNodes( $jobs, $nodes );
968
969                //print_r($filtered_nodes);
970
971                for( $n = 0; $n < $node_rows; $n++ ) {
972                       
973                        for( $m = 0; $m < $nodes_per_row; $m++ ) {
974                       
975                                $x = ($m * $node_width);
976                                $y = ($n * $node_width);
977
978                                $cur_node = ($n * $nodes_per_row) + ($m);
979                                $host = $nodes_hosts[$cur_node];
980
981
982                                if( isset( $nodes[$host] ) ) {
983
984                                        $nodes[$host]->setCoords( $x, $y );
985                                        $nodes[$host]->setImage( $image );
986
987                                        if( !in_array( $host, $filtered_nodes ) )
988                                                $nodes[$host]->setShowinfo( 0 );
989
990                                        $nodes[$host]->draw();
991                                }
992                        }
993                }
994               
995                header( 'Content-type: image/png' );
996                imagePNG( $image );
997                imageDestroy( $image );
998        }
999}
1000
1001//$my_data = new DataGatherer();
1002//$my_data->parseXML();
1003//$my_data->printInfo();
1004
1005//$ic = new ClusterImage( "LISA Cluster" );
1006//$ic->draw();
1007?>
Note: See TracBrowser for help on using the repository browser.