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

Last change on this file since 246 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
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 *
[231]22 * SVN $Id: libtoga.php 246 2006-04-11 09:46:19Z bastiaans $
23 *
[225]24 */
25
26
[103]27class HTTPVariables {
28
29        var $clustername, $metricname;
[112]30        var $restvars, $httpvars;
[103]31
[117]32        function HTTPVariables( $httpvars, $getvars ) {
[103]33
[110]34                $this->restvars = array();
35
[117]36                $this->clustername = $httpvars["c"] ? $httpvars["c"] : null;
37                $this->metricname = $httpvars["m"] ? $httpvars["m"] : null;
[103]38
[117]39                foreach( $httpvars as $httpvar => $httpval ) {
[110]40                       
41                        if( $httpval ) {
42                                $this->restvars[$httpvar] = $httpval;
43                        }
44                }
[117]45
46                foreach( $getvars as $getvar => $getval ) {
47
48                        if( $getval ) {
49                                $this->restvars[$getvar] = $getval;
50                        }
51                }
[103]52        }
53
54        function getClusterName() {
55                return $this->clustername;
56        }
57
58        function getMetricName() {
59                return $this->metricname;
60        }
[110]61
62        function getHttpVar( $var ) {
63                if( isset( $this->restvars[$var] ) )
[112]64                        return $this->restvars[$var];
[110]65                else
66                        return null;
67        }
[103]68}
69
[112]70// Toga's conf
71//
72include_once "./conf.php";
[195]73include_once "./version.php";
[112]74
[117]75global $GANGLIA_PATH;
[206]76global $RRDTOOL;
77global $JOB_ARCHIVE_DIR;
[207]78global $JOB_ARCHIVE_DBASE;
[112]79
[117]80$my_dir = getcwd();
[112]81
[117]82// Load Ganglia's PHP
83chdir( $GANGLIA_PATH );
[112]84
[117]85include_once "./conf.php";
86include_once "./functions.php";
87include_once "./ganglia.php";
[145]88include_once "./get_context.php";
89unset( $start );
90$context = 'cluster';
[117]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
[112]99// Set cluster context so that Ganglia will
100// provide us with the correct metrics array
101//
[126]102global $context, $clustername, $reports;
[129]103
[117]104//$clustername = $httpvars->getClusterName();
[112]105
[126]106global $default_metric;
107
[112]108// Ganglia's array of host metrics
109//
[126]110global $metrics, $hosts_up;
[112]111
[246]112global $DATETIME_FORMAT;
113
114function makeDate( $time ) {
115        global $DATETIME_FORMAT;
116        return strftime( $DATETIME_FORMAT, $time );
117}
118
[130]119class TarchDbase {
120
[138]121        var $ip, $dbase, $conn;
[130]122
[204]123        function TarchDbase( $ip = null, $dbase = 'jobarch' ) {
[207]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];
[138]131                $this->conn = null;
[130]132        }
133
134        function connect() {
135
136                if( $this->ip == null and $this->dbase == 'toga' )
[138]137                        $this->conn = pg_connect( "dbname=".$this->dbase );
[130]138                else
[204]139                        $this->conn = pg_connect( "host=".$this->ip." dbname=".$this->dbase );
[130]140        }
[138]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 ) 
[152]145                        $query = "SELECT job_id FROM jobs WHERE job_id = '$id' AND job_status = 'F'";
[138]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";
[142]157                        if( $start_to_time )
[138]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
[152]164                        $query = "SELECT job_id FROM jobs WHERE job_status = 'F' AND ";
[138]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 );
[140]178
179                $ret = array();
180
181                foreach( $ids as $crow)
182                        $ret[] = $crow[job_id];
183
184                return $ret;
[138]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
[141]195                        $nodes[] = $this->getNodeArray( $result_row[node_id] );
[138]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
[141]208                        $jobs[] = $this->getJobArray( $result_row[job_id] );
[138]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
[141]234                        $rmap_key = array_reverse( $map_key );
235                        array_pop( $rmap_key );
236                        $map_key = array_reverse( $rmap_key );
[138]237                       
[141]238                        $newkey = implode( '_', $map_key );
239                       
[138]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
[143]253                //printf( "query = [%s]\n", $query );
[138]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        }
[130]261}
262
[138]263class TarchRrdGraph {
[130]264        var $rrdbin, $rrdvalues, $clustername, $hostname, $tempdir, $tarchdir, $metrics;
265
[206]266        function TarchRrdGraph( $clustername, $hostname ) {
267
268                global $RRDTOOL;
269                global $JOB_ARCHIVE_DIR;
270
271                $this->rrdbin = $RRDTOOL;
[130]272                $this->rrdvalues = array();
[206]273                $this->tarchdir = $JOB_ARCHIVE_DIR;
[145]274                $this->clustername = $clustername;
275                $this->hostname = $hostname;
[130]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 );
[135]295                //print_r( $output );
[130]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
[145]317                //printf("start = %s end = %s\n", $start, $end );
[130]318                $times = array();
319                $dirlist = $this->dirList( $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname );
[145]320
321                //print_r( $dirlist );
322
[130]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
[145]334                //printf( "first = %s last = %s\n", $first, $last );
335
[130]336                foreach( $dirlist as $dir ) {
337
[145]338                        //printf( "dir %s ", $dir );
339
340                        if( $dir >= $first and $dir <= $last and !array_key_exists( $dir, $times ) ) {
341                       
[130]342                                $times[] = $dir;
[145]343                                //printf("newtime %s ", $dir );
344
345                        }
[130]346                }
347
[145]348                //print_r( $times );
349
[130]350                sort( $times );
351
[145]352                //print_r( $times );
353
[130]354                return $times;
355        }
356
[145]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
[130]387        function graph( $descr ) {
[141]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#
[130]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;
[145]398                return 0;
[130]399        }
400}
401
[103]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
[106]419                if( !$fp ) {
[103]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
[177]424                stream_set_timeout( $fp, 30 );
425
[103]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
[105]439        var $xmlhandler, $data, $httpvars;
[103]440
[163]441        function DataGatherer( $cluster ) {
[103]442
[110]443                global $DATA_SOURCE;
[163]444       
445                //printf("dg cluster = %s\n", $cluster );
[110]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
[103]452                $this->parser = xml_parser_create();
[112]453                $this->httpvars = $httpvars;
[163]454                $this->xmlhandler = new TorqueXMLHandler( $cluster );
[103]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
[112]463                if ( !xml_parse( &$this->parser, $this->data ) )
[103]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
[105]467        function printInfo() {
468                $handler = $this->xmlhandler;
469                $handler->printInfo();
470        }
471
[106]472        function getNodes() {
473                $handler = $this->xmlhandler;
474                return $handler->getNodes();
475        }
476
[124]477        function getCpus() {
478                $handler = $this->xmlhandler;
479                return $handler->getCpus();
480        }
481
[106]482        function getJobs() {
483                $handler = $this->xmlhandler;
484                return $handler->getJobs();
485        }
486
[114]487        function getHeartbeat() {
488                $handler = $this->xmlhandler;
489                return $handler->getHeartbeat();
490        }
[103]491}
492
493class TorqueXMLHandler {
494
[163]495        var $clusters, $heartbeat, $nodes, $jobs, $clustername, $proc_cluster;
[104]496
[163]497        function TorqueXMLHandler( $clustername ) {
[105]498                $jobs = array();
[104]499                $clusters = array();
[163]500                $this->nodes = array();
[104]501                $heartbeat = array();
[163]502                $this->clustername = $clustername;
503                //printf(" cluster set to %s \n", $this->clustername );
[103]504        }
505
[124]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
[103]520        function startElement( $parser, $name, $attrs ) {
521
[105]522                $jobs = &$this->jobs;
[111]523                $nodes = &$this->nodes;
[105]524
[103]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
[163]534                //printf( '%s=%s', $attrs[NAME], $attrs[VAL] );
[103]535
[163]536                //printf( "clustername = %s proc_cluster = %s\n", $this->clustername, $this->proc_cluster );
537
[104]538                if( $name == 'CLUSTER' ) {
[103]539
[163]540                        $this->proc_cluster = $attrs[NAME];
541                        //printf( "Found cluster %s\n", $attrs[NAME] );
542                        //print_r( $attrs );
[103]543
[163]544                        //if( !isset( $clusters[$clustername] ) )
545                        //      $clusters[$clustername] = array();
[103]546
[163]547                } else if( $name == 'HOST' and $this->proc_cluster == $this->clustername) {
[105]548
549                        $hostname = $attrs[NAME];
550                        $location = $attrs[LOCATION];
[163]551                        //printf( "Found node %s\n", $hostname );
[105]552
[200]553                        if( !isset( $nodes[$hostname] ) )
[163]554                                $nodes[$hostname] = new NodeImage( $hostname );
[105]555
[219]556                } else if( $name == 'METRIC' and strstr( $attrs[NAME], 'MONARCH' ) and $this->proc_cluster == $this->clustername ) {
[103]557
[219]558                        if( strstr( $attrs[NAME], 'MONARCH-HEARTBEAT' ) ) {
[103]559
[114]560                                $this->heartbeat['time'] = $attrs[VAL];
[106]561                                //printf( "heartbeat %s\n", $heartbeat['time'] );
[104]562
[219]563                        } else if( strstr( $attrs[NAME], 'MONARCH-JOB' ) ) {
[104]564
[219]565                                sscanf( $attrs[NAME], 'MONARCH-JOB-%d', $jobid );
[104]566
[106]567                                //printf( "jobid %s\n", $jobid );
[104]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
[106]580                                        //printf( "\t%s\t= %s\n", $toganame, $togavalue );
[104]581
[103]582                                        if( $toganame == 'nodes' ) {
583
[135]584                                                if( $jobs[$jobid][status] == 'R' ) {
585                                               
586                                                        if( !isset( $jobs[$jobid][$toganame] ) )
587                                                                $jobs[$jobid][$toganame] = array();
[104]588
[135]589                                                        $mynodes = explode( ';', $togavalue );
[103]590
[135]591                                                        foreach( $mynodes as $node )
[103]592
[135]593                                                                $jobs[$jobid][$toganame][] = $node;
594
595                                                } else if( $jobs[$jobid][status] == 'Q' ) {
596
597                                                        $jobs[$jobid][$toganame] = $togavalue;
598                                                }
599                                               
[104]600                                        } else {
[103]601
[105]602                                                $jobs[$jobid][$toganame] = $togavalue;
[104]603                                        }
[103]604                                }
[111]605
606                                if( isset( $jobs[$jobid][domain] ) and isset( $jobs[$jobid][nodes] ) ) {
[112]607                       
608                                        $nr_nodes = count( $jobs[$jobid][nodes] );
609                       
[111]610                                        foreach( $jobs[$jobid][nodes] as $node ) {
611
[200]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];
[111]622                               
[200]623                                                if( !isset( $nodes[$host] ) )
[111]624                                                        $my_node = new NodeImage( $host );
625                                                else
[200]626                                                        $my_node = $nodes[$host];
[111]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
[163]635                                                $nodes[$host] = $my_node;
[111]636                                        }
637                                }
[103]638                        }
639                }
[114]640                $this->jobs = $jobs;
[163]641                //print_r( $nodes );
642                $this->nodes = $nodes;
[200]643                //print_r( $this->nodes );
[103]644        }
645
646        function stopElement( $parser, $name ) {
647        }
[105]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        }
[106]685
686        function getNodes() {
[163]687                //print_r( $this->nodes );
[106]688                return $this->nodes;
689        }
690
691        function getJobs() {
692                return $this->jobs;
693        }
[114]694
695        function getHeartbeat() {
696                return $this->heartbeat['time'];
697        }
[103]698}
699
[107]700class NodeImage {
[102]701
[122]702        var $image, $x, $y, $hostname, $jobs, $tasks, $showinfo;
[102]703
[111]704        function NodeImage( $hostname ) {
[102]705
[111]706                $this->jobs = array();
707                //$this->image = $image;
708                //$this->x = $x;
709                //$this->y = $y;
710                $this->tasks = 0;
[110]711                $this->hostname = $hostname;
[111]712                $this->cpus = $this->determineCpus();
[122]713                $this->showinfo = 1;
[102]714        }
715
[111]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
[106]748        function setCoords( $x, $y ) {
[107]749
[106]750                $this->x = $x;
751                $this->y = $y;
752        }
753
[102]754        function colorHex( $color ) {
755       
[109]756                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) );
[102]757
758                return $my_color;
759        }
760
[106]761        function setLoad( $load ) {
762                $this->load = $load;
763        }
764
[108]765        function setHostname( $hostname ) {
766                $this->hostname = $hostname;
767        }
768
[122]769        function getHostname() {
770                return $this->hostname;
771        }
772
[114]773        function getJobs() {
774                return $this->jobs;
775        }
776
[122]777        function setShowinfo( $showinfo ) {
778                $this->showinfo = $showinfo;
779        }
780
[111]781        function draw() {
[106]782
[122]783                $this->drawSmall();
784        }
[110]785
[122]786        function drawBig() {
[111]787
[122]788        }
[106]789
[122]790        function drawSmall() {
[106]791
[122]792                global $SMALL_CLUSTERIMAGE_NODEWIDTH;
793                global $JOB_NODE_MARKING;
[102]794
[122]795                $black_color = imageColorAllocate( $this->image, 0, 0, 0 );
[110]796                $size = $SMALL_CLUSTERIMAGE_NODEWIDTH;
[107]797
[110]798                imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+($size), $this->y+($size), $black_color );
799
[122]800                if( $this->showinfo) {
801               
802                        $this->load = $this->determineLoad();
[111]803
[122]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                        }
[111]809
810
[122]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 );
[111]818
[122]819                } else {
[111]820
[122]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
[102]827        }
828
[111]829        function determineCpus() {
[102]830
[108]831                global $metrics;
832
833                $cpus = $metrics[$this->hostname][cpu_num][VAL];
[107]834                if (!$cpus) $cpus=1;
[111]835
836                return $cpus;
837        }
838
839        function determineLoad() {
840
841                global $metrics;
842
[108]843                $load_one = $metrics[$this->hostname][load_one][VAL];
[111]844                $load = ((float) $load_one)/$this->cpus;
[102]845
[111]846                return $load;
[106]847        }
848}
849
[107]850class ClusterImage {
[106]851
[111]852        var $dataget, $image, $clustername;
[124]853        var $filtername, $filters;
[106]854
[122]855        function ClusterImage( $clustername ) {
[114]856
[163]857                //printf( "image cluster = %s\n", $clustername );
858                $this->dataget = new DataGatherer( $clustername );
[111]859                $this->clustername = $clustername;
[124]860                $this->filters = array();
[106]861        }
862
[122]863        function setFilter( $filtername, $filtervalue ) {
864
[124]865                //printf("filter %s = %s\n", $filtername, $filtervalue );
[122]866                //printf( "filter set to %s = %s\n", $filtername, $filtervalue );
[124]867                $this->filters[$filtername] = $filtervalue;
868                //print_r($this->filters);
[122]869        }
870
871        function filterNodes( $jobs, $nodes ) {
872
873                $filtered_nodes = array();
874
875                foreach( $nodes as $node ) {
876
877                        $hostname = $node->getHostname();
878
[124]879                        $addhost = 1;
[122]880
[124]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 )
[122]913                                $filtered_nodes[] = $hostname;
914                }
915
916                return $filtered_nodes;
917        }
918
[106]919        function draw() {
[110]920
[124]921                //printf("stopt met uitvoer");
922                //return;
923
[110]924                global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH;
925       
[107]926                $mydatag = $this->dataget;
[106]927                $mydatag->parseXML();
928
[110]929                //$max_width = 250;
930                //$node_width = 11;
[106]931
[110]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
[106]937                $nodes = $mydatag->getNodes();
[111]938                $nodes_hosts = array_keys( $nodes );
[106]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 ) {
[107]946                        $nodes_per_row = ( (int) ($max_width/$node_width) );
[106]947                } else {
948                        $nodes_per_row = $nodes_size;
949                        $node_rows = 1;
950                }
951
952                if( $nodes_per_row < $nodes_nr ) {
[107]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 ) {
[106]957                                $node_rows++;
[107]958                                //printf( "noderows %d\n", $node_rows );
[106]959                        }
960                }
961
[107]962                //printf( "imagecreate: %dx%d", ($nodes_per_row*$node_width), ($node_rows*$node_width) );
[171]963                //$image = imageCreateTrueColor( ($nodes_per_row*$node_width)+1, ($node_rows*$node_width)+1 );
964                $image = imageCreateTrueColor( $max_width, ($node_rows*$node_width)+1 );
[107]965                $colorwhite = imageColorAllocate( $image, 255, 255, 255 );
966                imageFill( $image, 0, 0, $colorwhite );
[106]967
[122]968                $jobs = $mydatag->getJobs();
969                //printf("filtername = %s\n", $filtername );
970                $filtered_nodes = $this->filterNodes( $jobs, $nodes );
971
972                //print_r($filtered_nodes);
973
[106]974                for( $n = 0; $n < $node_rows; $n++ ) {
975                       
976                        for( $m = 0; $m < $nodes_per_row; $m++ ) {
[107]977                       
978                                $x = ($m * $node_width);
979                                $y = ($n * $node_width);
980
[109]981                                $cur_node = ($n * $nodes_per_row) + ($m);
[111]982                                $host = $nodes_hosts[$cur_node];
[107]983
[122]984
[111]985                                if( isset( $nodes[$host] ) ) {
986
987                                        $nodes[$host]->setCoords( $x, $y );
988                                        $nodes[$host]->setImage( $image );
[122]989
990                                        if( !in_array( $host, $filtered_nodes ) )
991                                                $nodes[$host]->setShowinfo( 0 );
992
[111]993                                        $nodes[$host]->draw();
994                                }
[106]995                        }
996                }
997               
[103]998                header( 'Content-type: image/png' );
[106]999                imagePNG( $image );
1000                imageDestroy( $image );
[102]1001        }
1002}
1003
[106]1004//$my_data = new DataGatherer();
1005//$my_data->parseXML();
1006//$my_data->printInfo();
1007
[112]1008//$ic = new ClusterImage( "LISA Cluster" );
1009//$ic->draw();
[102]1010?>
Note: See TracBrowser for help on using the repository browser.