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

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

web/addons/job_monarch/conf.php:

  • added SEARCH_RESULT_LIMIT option to prevent HUGE result lists

web/addons/job_monarch/libtoga.php:

  • added a resultcount
  • changed sql query to count first and limit the results

web/addons/job_monarch/search.php:

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