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
RevLine 
[102]1<?php
[225]2/*
3 *
4 * This file is part of Jobmonarch
5 *
6 * Copyright (C) 2006  Ramon Bastiaans
7 *
8 * Jobmonarch is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * Jobmonarch is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 *
22 */
23
24
[117]25// If php is compiled without globals
26//
[145]27//if ( !empty( $_GET ) ) {
28//        extract( $_GET );
29//}
[117]30
[103]31class HTTPVariables {
32
33        var $clustername, $metricname;
[112]34        var $restvars, $httpvars;
[103]35
[117]36        function HTTPVariables( $httpvars, $getvars ) {
[103]37
[110]38                $this->restvars = array();
39
[117]40                $this->clustername = $httpvars["c"] ? $httpvars["c"] : null;
41                $this->metricname = $httpvars["m"] ? $httpvars["m"] : null;
[103]42
[117]43                foreach( $httpvars as $httpvar => $httpval ) {
[110]44                       
45                        if( $httpval ) {
46                                $this->restvars[$httpvar] = $httpval;
47                        }
48                }
[117]49
50                foreach( $getvars as $getvar => $getval ) {
51
52                        if( $getval ) {
53                                $this->restvars[$getvar] = $getval;
54                        }
55                }
[103]56        }
57
58        function getClusterName() {
59                return $this->clustername;
60        }
61
62        function getMetricName() {
63                return $this->metricname;
64        }
[110]65
66        function getHttpVar( $var ) {
67                if( isset( $this->restvars[$var] ) )
[112]68                        return $this->restvars[$var];
[110]69                else
70                        return null;
71        }
[103]72}
73
[112]74// Toga's conf
75//
76include_once "./conf.php";
[195]77include_once "./version.php";
[112]78
[117]79global $GANGLIA_PATH;
[206]80global $RRDTOOL;
81global $JOB_ARCHIVE_DIR;
[207]82global $JOB_ARCHIVE_DBASE;
[112]83
[117]84$my_dir = getcwd();
[112]85
[117]86// Load Ganglia's PHP
87chdir( $GANGLIA_PATH );
[112]88
[117]89include_once "./conf.php";
90include_once "./functions.php";
91include_once "./ganglia.php";
[145]92include_once "./get_context.php";
93unset( $start );
94$context = 'cluster';
[117]95include_once "./get_ganglia.php";
96
97// Back to our PHP
98chdir( $my_dir );
99
100global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH, $DATA_SOURCE, $HTTP_GET_VARS, $_GET;
101$httpvars = new HTTPVariables( $HTTP_GET_VARS, $_GET );
102
[112]103// Set cluster context so that Ganglia will
104// provide us with the correct metrics array
105//
[126]106global $context, $clustername, $reports;
[129]107
[117]108//$clustername = $httpvars->getClusterName();
[112]109
[126]110global $default_metric;
111
[112]112// Ganglia's array of host metrics
113//
[126]114global $metrics, $hosts_up;
[112]115
[130]116class TarchDbase {
117
[138]118        var $ip, $dbase, $conn;
[130]119
[204]120        function TarchDbase( $ip = null, $dbase = 'jobarch' ) {
[207]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];
[138]128                $this->conn = null;
[130]129        }
130
131        function connect() {
132
133                if( $this->ip == null and $this->dbase == 'toga' )
[138]134                        $this->conn = pg_connect( "dbname=".$this->dbase );
[130]135                else
[204]136                        $this->conn = pg_connect( "host=".$this->ip." dbname=".$this->dbase );
[130]137        }
[138]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 ) 
[152]142                        $query = "SELECT job_id FROM jobs WHERE job_id = '$id' AND job_status = 'F'";
[138]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";
[142]154                        if( $start_to_time )
[138]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
[152]161                        $query = "SELECT job_id FROM jobs WHERE job_status = 'F' AND ";
[138]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 );
[140]175
176                $ret = array();
177
178                foreach( $ids as $crow)
179                        $ret[] = $crow[job_id];
180
181                return $ret;
[138]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
[141]192                        $nodes[] = $this->getNodeArray( $result_row[node_id] );
[138]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
[141]205                        $jobs[] = $this->getJobArray( $result_row[job_id] );
[138]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
[141]231                        $rmap_key = array_reverse( $map_key );
232                        array_pop( $rmap_key );
233                        $map_key = array_reverse( $rmap_key );
[138]234                       
[141]235                        $newkey = implode( '_', $map_key );
236                       
[138]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
[143]250                //printf( "query = [%s]\n", $query );
[138]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        }
[130]258}
259
[138]260class TarchRrdGraph {
[130]261        var $rrdbin, $rrdvalues, $clustername, $hostname, $tempdir, $tarchdir, $metrics;
262
[206]263        function TarchRrdGraph( $clustername, $hostname ) {
264
265                global $RRDTOOL;
266                global $JOB_ARCHIVE_DIR;
267
268                $this->rrdbin = $RRDTOOL;
[130]269                $this->rrdvalues = array();
[206]270                $this->tarchdir = $JOB_ARCHIVE_DIR;
[145]271                $this->clustername = $clustername;
272                $this->hostname = $hostname;
[130]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 );
[135]292                //print_r( $output );
[130]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
[145]314                //printf("start = %s end = %s\n", $start, $end );
[130]315                $times = array();
316                $dirlist = $this->dirList( $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname );
[145]317
318                //print_r( $dirlist );
319
[130]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
[145]331                //printf( "first = %s last = %s\n", $first, $last );
332
[130]333                foreach( $dirlist as $dir ) {
334
[145]335                        //printf( "dir %s ", $dir );
336
337                        if( $dir >= $first and $dir <= $last and !array_key_exists( $dir, $times ) ) {
338                       
[130]339                                $times[] = $dir;
[145]340                                //printf("newtime %s ", $dir );
341
342                        }
[130]343                }
344
[145]345                //print_r( $times );
346
[130]347                sort( $times );
348
[145]349                //print_r( $times );
350
[130]351                return $times;
352        }
353
[145]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
[130]384        function graph( $descr ) {
[141]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#
[130]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;
[145]395                return 0;
[130]396        }
397}
398
[103]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
[106]416                if( !$fp ) {
[103]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
[177]421                stream_set_timeout( $fp, 30 );
422
[103]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
[105]436        var $xmlhandler, $data, $httpvars;
[103]437
[163]438        function DataGatherer( $cluster ) {
[103]439
[110]440                global $DATA_SOURCE;
[163]441       
442                //printf("dg cluster = %s\n", $cluster );
[110]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
[103]449                $this->parser = xml_parser_create();
[112]450                $this->httpvars = $httpvars;
[163]451                $this->xmlhandler = new TorqueXMLHandler( $cluster );
[103]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
[112]460                if ( !xml_parse( &$this->parser, $this->data ) )
[103]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
[105]464        function printInfo() {
465                $handler = $this->xmlhandler;
466                $handler->printInfo();
467        }
468
[106]469        function getNodes() {
470                $handler = $this->xmlhandler;
471                return $handler->getNodes();
472        }
473
[124]474        function getCpus() {
475                $handler = $this->xmlhandler;
476                return $handler->getCpus();
477        }
478
[106]479        function getJobs() {
480                $handler = $this->xmlhandler;
481                return $handler->getJobs();
482        }
483
[114]484        function getHeartbeat() {
485                $handler = $this->xmlhandler;
486                return $handler->getHeartbeat();
487        }
[103]488}
489
490class TorqueXMLHandler {
491
[163]492        var $clusters, $heartbeat, $nodes, $jobs, $clustername, $proc_cluster;
[104]493
[163]494        function TorqueXMLHandler( $clustername ) {
[105]495                $jobs = array();
[104]496                $clusters = array();
[163]497                $this->nodes = array();
[104]498                $heartbeat = array();
[163]499                $this->clustername = $clustername;
500                //printf(" cluster set to %s \n", $this->clustername );
[103]501        }
502
[124]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
[103]517        function startElement( $parser, $name, $attrs ) {
518
[105]519                $jobs = &$this->jobs;
[111]520                $nodes = &$this->nodes;
[105]521
[103]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
[163]531                //printf( '%s=%s', $attrs[NAME], $attrs[VAL] );
[103]532
[163]533                //printf( "clustername = %s proc_cluster = %s\n", $this->clustername, $this->proc_cluster );
534
[104]535                if( $name == 'CLUSTER' ) {
[103]536
[163]537                        $this->proc_cluster = $attrs[NAME];
538                        //printf( "Found cluster %s\n", $attrs[NAME] );
539                        //print_r( $attrs );
[103]540
[163]541                        //if( !isset( $clusters[$clustername] ) )
542                        //      $clusters[$clustername] = array();
[103]543
[163]544                } else if( $name == 'HOST' and $this->proc_cluster == $this->clustername) {
[105]545
546                        $hostname = $attrs[NAME];
547                        $location = $attrs[LOCATION];
[163]548                        //printf( "Found node %s\n", $hostname );
[105]549
[200]550                        if( !isset( $nodes[$hostname] ) )
[163]551                                $nodes[$hostname] = new NodeImage( $hostname );
[105]552
[219]553                } else if( $name == 'METRIC' and strstr( $attrs[NAME], 'MONARCH' ) and $this->proc_cluster == $this->clustername ) {
[103]554
[219]555                        if( strstr( $attrs[NAME], 'MONARCH-HEARTBEAT' ) ) {
[103]556
[114]557                                $this->heartbeat['time'] = $attrs[VAL];
[106]558                                //printf( "heartbeat %s\n", $heartbeat['time'] );
[104]559
[219]560                        } else if( strstr( $attrs[NAME], 'MONARCH-JOB' ) ) {
[104]561
[219]562                                sscanf( $attrs[NAME], 'MONARCH-JOB-%d', $jobid );
[104]563
[106]564                                //printf( "jobid %s\n", $jobid );
[104]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
[106]577                                        //printf( "\t%s\t= %s\n", $toganame, $togavalue );
[104]578
[103]579                                        if( $toganame == 'nodes' ) {
580
[135]581                                                if( $jobs[$jobid][status] == 'R' ) {
582                                               
583                                                        if( !isset( $jobs[$jobid][$toganame] ) )
584                                                                $jobs[$jobid][$toganame] = array();
[104]585
[135]586                                                        $mynodes = explode( ';', $togavalue );
[103]587
[135]588                                                        foreach( $mynodes as $node )
[103]589
[135]590                                                                $jobs[$jobid][$toganame][] = $node;
591
592                                                } else if( $jobs[$jobid][status] == 'Q' ) {
593
594                                                        $jobs[$jobid][$toganame] = $togavalue;
595                                                }
596                                               
[104]597                                        } else {
[103]598
[105]599                                                $jobs[$jobid][$toganame] = $togavalue;
[104]600                                        }
[103]601                                }
[111]602
603                                if( isset( $jobs[$jobid][domain] ) and isset( $jobs[$jobid][nodes] ) ) {
[112]604                       
605                                        $nr_nodes = count( $jobs[$jobid][nodes] );
606                       
[111]607                                        foreach( $jobs[$jobid][nodes] as $node ) {
608
[200]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];
[111]619                               
[200]620                                                if( !isset( $nodes[$host] ) )
[111]621                                                        $my_node = new NodeImage( $host );
622                                                else
[200]623                                                        $my_node = $nodes[$host];
[111]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
[163]632                                                $nodes[$host] = $my_node;
[111]633                                        }
634                                }
[103]635                        }
636                }
[114]637                $this->jobs = $jobs;
[163]638                //print_r( $nodes );
639                $this->nodes = $nodes;
[200]640                //print_r( $this->nodes );
[103]641        }
642
643        function stopElement( $parser, $name ) {
644        }
[105]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        }
[106]682
683        function getNodes() {
[163]684                //print_r( $this->nodes );
[106]685                return $this->nodes;
686        }
687
688        function getJobs() {
689                return $this->jobs;
690        }
[114]691
692        function getHeartbeat() {
693                return $this->heartbeat['time'];
694        }
[103]695}
696
[107]697class NodeImage {
[102]698
[122]699        var $image, $x, $y, $hostname, $jobs, $tasks, $showinfo;
[102]700
[111]701        function NodeImage( $hostname ) {
[102]702
[111]703                $this->jobs = array();
704                //$this->image = $image;
705                //$this->x = $x;
706                //$this->y = $y;
707                $this->tasks = 0;
[110]708                $this->hostname = $hostname;
[111]709                $this->cpus = $this->determineCpus();
[122]710                $this->showinfo = 1;
[102]711        }
712
[111]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
[106]745        function setCoords( $x, $y ) {
[107]746
[106]747                $this->x = $x;
748                $this->y = $y;
749        }
750
[102]751        function colorHex( $color ) {
752       
[109]753                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) );
[102]754
755                return $my_color;
756        }
757
[106]758        function setLoad( $load ) {
759                $this->load = $load;
760        }
761
[108]762        function setHostname( $hostname ) {
763                $this->hostname = $hostname;
764        }
765
[122]766        function getHostname() {
767                return $this->hostname;
768        }
769
[114]770        function getJobs() {
771                return $this->jobs;
772        }
773
[122]774        function setShowinfo( $showinfo ) {
775                $this->showinfo = $showinfo;
776        }
777
[111]778        function draw() {
[106]779
[122]780                $this->drawSmall();
781        }
[110]782
[122]783        function drawBig() {
[111]784
[122]785        }
[106]786
[122]787        function drawSmall() {
[106]788
[122]789                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
790                global $JOB_NODE_MARKING;
[102]791
[122]792                $black_color = imageColorAllocate( $this->image, 0, 0, 0 );
[110]793                $size = $SMALL_CLUSTERIMAGE_NODEWIDTH;
[107]794
[110]795                imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+($size), $this->y+($size), $black_color );
796
[122]797                if( $this->showinfo) {
798               
799                        $this->load = $this->determineLoad();
[111]800
[122]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                        }
[111]806
807
[122]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 );
[111]815
[122]816                } else {
[111]817
[122]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
[102]824        }
825
[111]826        function determineCpus() {
[102]827
[108]828                global $metrics;
829
830                $cpus = $metrics[$this->hostname][cpu_num][VAL];
[107]831                if (!$cpus) $cpus=1;
[111]832
833                return $cpus;
834        }
835
836        function determineLoad() {
837
838                global $metrics;
839
[108]840                $load_one = $metrics[$this->hostname][load_one][VAL];
[111]841                $load = ((float) $load_one)/$this->cpus;
[102]842
[111]843                return $load;
[106]844        }
845}
846
[107]847class ClusterImage {
[106]848
[111]849        var $dataget, $image, $clustername;
[124]850        var $filtername, $filters;
[106]851
[122]852        function ClusterImage( $clustername ) {
[114]853
[163]854                //printf( "image cluster = %s\n", $clustername );
855                $this->dataget = new DataGatherer( $clustername );
[111]856                $this->clustername = $clustername;
[124]857                $this->filters = array();
[106]858        }
859
[122]860        function setFilter( $filtername, $filtervalue ) {
861
[124]862                //printf("filter %s = %s\n", $filtername, $filtervalue );
[122]863                //printf( "filter set to %s = %s\n", $filtername, $filtervalue );
[124]864                $this->filters[$filtername] = $filtervalue;
865                //print_r($this->filters);
[122]866        }
867
868        function filterNodes( $jobs, $nodes ) {
869
870                $filtered_nodes = array();
871
872                foreach( $nodes as $node ) {
873
874                        $hostname = $node->getHostname();
875
[124]876                        $addhost = 1;
[122]877
[124]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 )
[122]910                                $filtered_nodes[] = $hostname;
911                }
912
913                return $filtered_nodes;
914        }
915
[106]916        function draw() {
[110]917
[124]918                //printf("stopt met uitvoer");
919                //return;
920
[110]921                global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH;
922       
[107]923                $mydatag = $this->dataget;
[106]924                $mydatag->parseXML();
925
[110]926                //$max_width = 250;
927                //$node_width = 11;
[106]928
[110]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
[106]934                $nodes = $mydatag->getNodes();
[111]935                $nodes_hosts = array_keys( $nodes );
[106]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 ) {
[107]943                        $nodes_per_row = ( (int) ($max_width/$node_width) );
[106]944                } else {
945                        $nodes_per_row = $nodes_size;
946                        $node_rows = 1;
947                }
948
949                if( $nodes_per_row < $nodes_nr ) {
[107]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 ) {
[106]954                                $node_rows++;
[107]955                                //printf( "noderows %d\n", $node_rows );
[106]956                        }
957                }
958
[107]959                //printf( "imagecreate: %dx%d", ($nodes_per_row*$node_width), ($node_rows*$node_width) );
[171]960                //$image = imageCreateTrueColor( ($nodes_per_row*$node_width)+1, ($node_rows*$node_width)+1 );
961                $image = imageCreateTrueColor( $max_width, ($node_rows*$node_width)+1 );
[107]962                $colorwhite = imageColorAllocate( $image, 255, 255, 255 );
963                imageFill( $image, 0, 0, $colorwhite );
[106]964
[122]965                $jobs = $mydatag->getJobs();
966                //printf("filtername = %s\n", $filtername );
967                $filtered_nodes = $this->filterNodes( $jobs, $nodes );
968
969                //print_r($filtered_nodes);
970
[106]971                for( $n = 0; $n < $node_rows; $n++ ) {
972                       
973                        for( $m = 0; $m < $nodes_per_row; $m++ ) {
[107]974                       
975                                $x = ($m * $node_width);
976                                $y = ($n * $node_width);
977
[109]978                                $cur_node = ($n * $nodes_per_row) + ($m);
[111]979                                $host = $nodes_hosts[$cur_node];
[107]980
[122]981
[111]982                                if( isset( $nodes[$host] ) ) {
983
984                                        $nodes[$host]->setCoords( $x, $y );
985                                        $nodes[$host]->setImage( $image );
[122]986
987                                        if( !in_array( $host, $filtered_nodes ) )
988                                                $nodes[$host]->setShowinfo( 0 );
989
[111]990                                        $nodes[$host]->draw();
991                                }
[106]992                        }
993                }
994               
[103]995                header( 'Content-type: image/png' );
[106]996                imagePNG( $image );
997                imageDestroy( $image );
[102]998        }
999}
1000
[106]1001//$my_data = new DataGatherer();
1002//$my_data->parseXML();
1003//$my_data->printInfo();
1004
[112]1005//$ic = new ClusterImage( "LISA Cluster" );
1006//$ic->draw();
[102]1007?>
Note: See TracBrowser for help on using the repository browser.