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

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

web/addons/job_monarch/conf.php:

  • added DATETIME_FORMAT

web/addons/job_monarch/libtoga.php:

  • added makeDate()

web/addons/job_monarch/search.php,
web/addons/job_monarch/overview.php:

  • disable local makeDate, use from libtoga

web/addons/job_monarch/search.php

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