source: trunk/web/addons/toga/toga-functions.php @ 105

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

toga/toga-functions.php:

  • Job and Node relations
File size: 7.4 KB
Line 
1<PRE>
2<?php
3$GANGLIA_PATH = "/var/www/ganglia";
4
5include_once "$GANGLIA_PATH/conf.php";
6include_once "$GANGLIA_PATH/functions.php";
7
8if ( !empty( $_GET ) ) {
9        extract( $_GET );
10}
11
12class HTTPVariables {
13
14        var $clustername, $metricname;
15
16        function HTTPVariables() {
17
18                global $HTTP_GET_VARS;
19
20                $this->clustername = $HTTP_GET_VARS["c"] ? $HTTP_GET_VARS["c"] : null;
21                $this->metricname = $HTTP_GET_VARS["m"] ? $HTTP_GET_VARS["m"] : null;
22        }
23
24        function getClusterName() {
25                return $this->clustername;
26        }
27
28        function getMetricName() {
29                return $this->metricname;
30        }
31}
32
33class DataSource {
34
35        var $data, $ip, $port;
36
37        function DataSource( $ip = '127.0.0.1', $port = 8649 ) {
38                $this->ip = $ip;
39                $this->port = $port;
40        }
41
42        function getData() {
43
44                $errstr;
45                $errno = 0;
46                $timeout = 3;
47
48                $fp = fsockopen( $this->ip, $this->port, &$errno, &$errstr, $timeout );
49
50                if( ! $fp ) {
51                        echo 'Unable to connect to '.$this->ip.':'.$this->port; // printf( 'Unable to connect to [%s:%.0f]', $this->ip, $this->port );
52                        return;
53                }
54
55                while ( !feof( $fp ) ) {
56                       
57                        $data .= fread( $fp, 16384 );
58                }
59
60                fclose( $fp );
61
62                return $data;
63        }
64}
65
66class DataGatherer {
67
68        var $xmlhandler, $data, $httpvars;
69
70        function DataGatherer() {
71
72                $this->parser = xml_parser_create();
73                $this->source = new DataSource();
74                $this->httpvars = new HTTPVariables();
75                $this->xmlhandler = new TorqueXMLHandler();
76                xml_set_element_handler( $this->parser, array( &$this->xmlhandler, 'startElement' ), array( &$this->xmlhandler, 'stopElement' ) );
77        }
78
79        function parseXML() {
80
81                $src = &$this->source;
82                $this->data = $src->getData();
83
84                if ( !xml_parse( &$this->parser, $this->data ) ) {
85                        $error = sprintf( 'XML error: %s at %d', xml_error_string( xml_get_error_code( &$this->parser ) ), xml_get_current_line_number( &$this->parser ) );
86                        // die( $error );
87                }
88        }
89
90        function printInfo() {
91                $handler = $this->xmlhandler;
92                $handler->printInfo();
93        }
94
95}
96
97class TorqueXMLHandler {
98
99        var $clusters, $heartbeat, $nodes, $jobs;
100
101        function TorqueXMLHandler() {
102                $jobs = array();
103                $clusters = array();
104                $nodes = array();
105                $heartbeat = array();
106        }
107
108        function gotNode( $hostname, $jobid = null, $location = 'unspecified' ) {
109
110                $nodes = &$this->nodes;
111
112                if( !isset( $nodes[$hostname] ) ) {
113
114                        $nodes[$hostname] = new Node( $hostname );
115                }
116
117                if( $location != 'unspecified' ) {
118
119                        $nodes[$hostname]->setLocation( $location );
120                //} else {
121
122                        // Return pointer to this node if location was not set
123                        // Which means that this is for a jobinfo
124                        //$mynode = &$nodes[$hostname];
125                        //return $mynode;
126                }
127
128                if( $jobid ) {
129                        $nodes[$hostname]->addJob( $jobid );
130                }
131        }
132
133        function startElement( $parser, $name, $attrs ) {
134
135                $jobs = &$this->jobs;
136
137                if ( $attrs[TN] ) {
138
139                        // Ignore dead metrics. Detect and mask failures.
140                        if ( $attrs[TN] > $attrs[TMAX] * 4 )
141                                return;
142                }
143
144                $jobid = null;
145
146                // printf( '%s=%s', $attrs[NAME], $attrs[VAL] );
147
148                if( $name == 'CLUSTER' ) {
149
150                        $clustername = $attrs[VAL];
151
152                        if( !isset( $clusters[$clustername] ) )
153                                $clusters[$clustername] = array();
154
155                } else if( $name == 'HOST' ) {
156
157                        $hostname = $attrs[NAME];
158                        $location = $attrs[LOCATION];
159
160                        $this->gotNode( $hostname, null,$location );
161
162                } else if( $name == 'METRIC' and strstr( $attrs[NAME], 'TOGA' ) ) {
163
164                        if( strstr( $attrs[NAME], 'TOGA-HEARTBEAT' ) ) {
165
166                                $heartbeat['time'] = $attrs[VAL];
167                                printf( "heartbeat %s\n", $heartbeat['time'] );
168
169                        } else if( strstr( $attrs[NAME], 'TOGA-JOB' ) ) {
170
171                                sscanf( $attrs[NAME], 'TOGA-JOB-%d', $jobid );
172
173                                printf( "jobid %s\n", $jobid );
174
175                                if( !isset( $jobs[$jobid] ) )
176                                        $jobs[$jobid] = array();
177
178                                $fields = explode( ' ', $attrs[VAL] );
179
180                                foreach( $fields as $f ) {
181                                        $togavalues = explode( '=', $f );
182
183                                        $toganame = $togavalues[0];
184                                        $togavalue = $togavalues[1];
185
186                                        printf( "\t%s\t= %s\n", $toganame, $togavalue );
187
188                                        if( $toganame == 'nodes' ) {
189
190                                                if( !isset( $jobs[$toganame] ) )
191                                                        $jobs[$jobid][$toganame] = array();
192
193                                                $nodes = explode( ';', $togavalue );
194
195                                                foreach( $nodes as $node ) {
196
197                                                        // printf( "node %s\n", $node );
198
199                                                        $hostname = $node.'.'.$jobs[$jobid][domain];
200                                                        $jobs[$jobid][$toganame][] = $hostname;
201                                                        $this->gotNode( $hostname, $jobid );
202
203                                                        //$jobs[$jobid][$toganame][$hostname] = $this->gotNode( $hostname );
204                                                        // $jobs[$toganame][$node] = new Node( $node );
205                                                }
206
207                                        } else {
208
209                                                $jobs[$jobid][$toganame] = $togavalue;
210
211                                        }
212                                }
213                        }
214                }
215        }
216
217        function stopElement( $parser, $name ) {
218        }
219
220        function printInfo() {
221
222                $jobs = &$this->jobs;
223
224                printf( "---jobs---\n" );
225
226                foreach( $jobs as $jobid => $job ) {
227
228                        printf( "job %s\n", $jobid );
229
230                        if( isset( $job[nodes] ) ) {
231
232                                foreach( $job[nodes] as $node ) {
233
234                                        $mynode = $this->nodes[$node];
235                                        $hostname = $mynode->getHostname();
236                                        $location = $mynode->getLocation();
237
238                                        printf( "\t- node %s\tlocation %s\n", $hostname, $location );
239                                        //$this->nodes[$hostname]->setLocation( "hier draait job ".$jobid );
240                                }
241                        }
242                }
243
244                printf( "---nodes---\n" );
245
246                $nodes = &$this->nodes;
247
248                foreach( $nodes as $node ) {
249
250                        $hostname = $node->getHostname();
251                        $location = $node->getLocation();
252                        $jobs = implode( ' ', $node->getJobs() );
253                        printf( "* node %s\tlocation %s\tjobs %s\n", $hostname, $location, $jobs );
254                }
255        }
256}
257
258class Node {
259
260        var $img, $hostname, $location, $jobs;
261
262        function Node( $hostname ) {
263
264                $this->hostname = $hostname;
265                $this->img = new NodeImg();
266                $this->jobs = array();
267        }
268
269        function addJob( $jobid ) {
270                $jobs = &$this->jobs;
271
272                $jobs[] = $jobid;
273        }
274
275        function setLocation( $location ) {
276                $this->location = $location;
277        }
278
279        function setHostname( $hostname ) {
280                $this->hostname = $hostname;
281        }
282
283        function setCpus( $cpus ) {
284                $this->cpus = $cpus;
285        }
286
287        function getHostname() {
288                return $this->hostname;
289        }
290
291        function getLocation() {
292                return $this->location;
293        }
294
295        function getCpus() {
296                return $this->cpus;
297        }
298
299        function getJobs() {
300                return $this->jobs;
301        }
302}
303
304class NodeImg {
305
306        var $image;
307
308        function NodeImg( $image = null ) {
309
310                $imageWidth = 100;
311                $imageHeight = 100;
312
313                if( !$image ) {
314                        $this->image = imageCreate( $imageWidth, $imageHeight );
315                                // or die( 'Cannot initialize new image stream: is GD installed?' );
316                } else {
317                        $this->image = $image;
318                }
319
320                $background_color = imageColorAllocate( $this->image, 255, 255, 255 );
321                $black_color = imageColorAllocate( $this->image, 0, 0, 0 );
322                imageRectangle( $this->image, 0, 0, $imageWidth-1, $imageHeight-1, $black_color );
323        }
324
325        function colorHex( $color ) {
326       
327                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 4 )), hexdec( substr( $color, 4, 6 )) );
328
329                return $my_color;
330        }
331
332        function drawNode( $x, $y, &$queuecolor, $load, &$jobcolor ) {
333
334                // Convert Ganglias Hexadecimal load color to a Decimal one
335                $my_loadcolor = $this->colorHex( load_color($load) );
336
337                imageFilledRectangle( $this->image, $x, $y, $x+12, $y+12, $queuecolor );
338                imageFilledRectangle( $this->image, $x+2, $y+2, $x+10, $y+10, $my_loadcolor );
339                //imageFilledEllipse( $this->image, ($x+9)/2, ($y+9)/2, 6, 6, $jobcolor );
340        }
341
342        function drawImage() {
343
344                $queue_color = imageColorAllocate( $this->image, 0, 102, 304 );
345                $job_color = imageColorAllocate( $this->image, 204, 204, 0 );
346
347                $this->drawNode( 1, 1, $queue_color, 0.1, $job_color );
348                header( 'Content-type: image/png' );
349                imagePNG( $this->image );
350                imageDestroy( $this->image );
351        }
352}
353
354//$my_node = new NodeImg();
355//$my_node->drawImage();
356
357$my_data = new DataGatherer();
358$my_data->parseXML();
359$my_data->printInfo();
360$my_data->printInfo();
361?>
362</PRE>
Note: See TracBrowser for help on using the repository browser.