source: trunk/web/addons/toga/libtoga.php @ 152

Last change on this file since 152 was 152, checked in by bastiaans, 19 years ago

toga/libtoga.php:

  • Only search jobs in 'F' state

toga/search.php:

  • Fixed form validation

web/addons/toga/graph.php:

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