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

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

toga/toga-functions.php:

  • XML parsing actually working
  • More attributes for Node class


File size: 5.2 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;
69
70        function DataGatherer() {
71
72                $this->parser = xml_parser_create();
73                $this->source = new DataSource();
74                $this->xmlhandler = new TorqueXMLHandler();
75                xml_set_element_handler( $this->parser, array( &$this->xmlhandler, 'startElement' ), array( &$this->xmlhandler, 'stopElement' ) );
76        }
77
78        function parseXML() {
79
80                $src = &$this->source;
81                $this->data = $src->getData();
82
83                if ( !xml_parse( &$this->parser, $this->data ) ) {
84                        $error = sprintf( 'XML error: %s at %d', xml_error_string( xml_get_error_code( &$this->parser ) ), xml_get_current_line_number( &$this->parser ) );
85                        // die( $error );
86                }
87        }
88
89}
90
91class TorqueXMLHandler {
92
93        var $clusters, $heartbeat;
94
95        function TorqueXMLHandler() {
96                $clusters = array();
97                $heartbeat = array();
98        }
99
100        function startElement( $parser, $name, $attrs ) {
101
102                if ( $attrs[TN] ) {
103
104                        // Ignore dead metrics. Detect and mask failures.
105                        if ( $attrs[TN] > $attrs[TMAX] * 4 )
106                                return;
107                }
108
109                $jobs = array();
110                $jobid = null;
111
112                // printf( '%s=%s', $attrs[NAME], $attrs[VAL] );
113
114
115                if( $name == 'CLUSTER' ) {
116
117                        $clustername = $attrs[VAL];
118
119                        if( !isset( $clusters[$clustername] ) )
120                                $clusters[$clustername] = array();
121
122                } else if( $name == 'METRIC' and strstr( $attrs[NAME], 'TOGA' ) ) {
123
124                        if( strstr( $attrs[NAME], 'TOGA-HEARTBEAT' ) ) {
125
126                                $heartbeat['time'] = $attrs[VAL];
127                                printf( "heartbeat %s\n", $heartbeat['time'] );
128
129                        } else if( strstr( $attrs[NAME], 'TOGA-JOB' ) ) {
130
131                                sscanf( $attrs[NAME], 'TOGA-JOB-%d', $jobid );
132
133                                printf( "jobid %s\n", $jobid );
134
135                                if( !isset( $jobs[$jobid] ) )
136                                        $jobs[$jobid] = array();
137
138                                $fields = explode( ' ', $attrs[VAL] );
139
140                                foreach( $fields as $f ) {
141                                        $togavalues = explode( '=', $f );
142
143                                        $toganame = $togavalues[0];
144                                        $togavalue = $togavalues[1];
145
146                                        printf( "toganame %s, togavalue %s\n", $toganame, $togavalue );
147
148                                        if( $toganame == 'nodes' ) {
149
150                                                if( !isset( $jobs[$toganame] ) )
151                                                        $jobs[$toganame] = array();
152
153                                                $nodes = explode( ';', $togavalue );
154
155                                                foreach( $nodes as $node ) {
156
157                                                        printf( "node %s\n", $node );
158                                                        $jobs[$toganame][] = new Node( $node );
159                                                }
160
161                                        } else {
162
163                                                $jobs[$toganame] = $togavalue;
164
165                                        }
166                                }
167                        }
168                }
169        }
170
171        function stopElement( $parser, $name ) {
172        }
173}
174
175class Node {
176
177        var $img, $hostname, $location;
178
179        function Node( $hostname ) {
180
181                $this->hostname = $hostname;
182                $this->img = new NodeImg();
183        }
184
185        function setLocation( $location ) {
186                $this->location = $location;
187        }
188
189        function setCpus( $cpus ) {
190                $this->cpus = $cpus;
191        }
192}
193
194class NodeImg {
195
196        var $image;
197
198        function NodeImg( $image = null ) {
199
200                $imageWidth = 100;
201                $imageHeight = 100;
202
203                if( !$image ) {
204                        $this->image = imageCreate( $imageWidth, $imageHeight );
205                                // or die( 'Cannot initialize new image stream: is GD installed?' );
206                } else {
207                        $this->image = $image;
208                }
209
210                $background_color = imageColorAllocate( $this->image, 255, 255, 255 );
211                $black_color = imageColorAllocate( $this->image, 0, 0, 0 );
212                imageRectangle( $this->image, 0, 0, $imageWidth-1, $imageHeight-1, $black_color );
213        }
214
215        function colorHex( $color ) {
216       
217                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 4 )), hexdec( substr( $color, 4, 6 )) );
218
219                return $my_color;
220        }
221
222        function drawNode( $x, $y, &$queuecolor, $load, &$jobcolor ) {
223
224                // Convert Ganglias Hexadecimal load color to a Decimal one
225                $my_loadcolor = $this->colorHex( load_color($load) );
226
227                imageFilledRectangle( $this->image, $x, $y, $x+12, $y+12, $queuecolor );
228                imageFilledRectangle( $this->image, $x+2, $y+2, $x+10, $y+10, $my_loadcolor );
229                //imageFilledEllipse( $this->image, ($x+9)/2, ($y+9)/2, 6, 6, $jobcolor );
230        }
231
232        function drawImage() {
233
234                $queue_color = imageColorAllocate( $this->image, 0, 102, 304 );
235                $job_color = imageColorAllocate( $this->image, 204, 204, 0 );
236
237                $this->drawNode( 1, 1, $queue_color, 0.1, $job_color );
238                header( 'Content-type: image/png' );
239                imagePNG( $this->image );
240                imageDestroy( $this->image );
241        }
242}
243
244//$my_node = new NodeImg();
245//$my_node->drawImage();
246
247$my_data = new DataGatherer();
248$my_data->parseXML();
249?>
250</PRE>
Note: See TracBrowser for help on using the repository browser.