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

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

toga/toga-functions.php:

  • Added XML objects and handlers
File size: 4.2 KB
Line 
1<?php
2$GANGLIA_PATH = "/var/www/ganglia";
3
4include_once "$GANGLIA_PATH/conf.php";
5include_once "$GANGLIA_PATH/functions.php";
6
7if ( !empty( $_GET ) ) {
8        extract( $_GET );
9}
10
11class HTTPVariables {
12
13        var $clustername, $metricname;
14
15        function HTTPVariables() {
16
17                global $HTTP_GET_VARS;
18
19                $this->clustername = $HTTP_GET_VARS["c"] ? $HTTP_GET_VARS["c"] : null;
20                $this->metricname = $HTTP_GET_VARS["m"] ? $HTTP_GET_VARS["m"] : null;
21        }
22
23        function getClusterName() {
24                return $this->clustername;
25        }
26
27        function getMetricName() {
28                return $this->metricname;
29        }
30}
31
32class DataSource {
33
34        var $data, $ip, $port;
35
36        function DataSource( $ip = '127.0.0.1', $port = 8649 ) {
37                $this->ip = $ip;
38                $this->port = $port;
39        }
40
41        function getData() {
42
43                $errstr;
44                $errno = 0;
45                $timeout = 3;
46
47                $fp = fsockopen( $this->ip, $this->port, &$errno, &$errstr, $timeout );
48
49                if( ! $fp ) {
50                        echo 'Unable to connect to '.$this->ip.':'.$this->port; // printf( 'Unable to connect to [%s:%.0f]', $this->ip, $this->port );
51                        return;
52                }
53
54                while ( !feof( $fp ) ) {
55                       
56                        $data .= fread( $fp, 16384 );
57                }
58
59                fclose( $fp );
60
61                return $data;
62        }
63}
64
65class DataGatherer {
66
67        var $xmlhandler, $data;
68
69        function DataGatherer() {
70
71                $this->parser = xml_parser_create();
72                $this->source = new DataSource();
73                $this->xmlhandler = new TorqueXMLHandler();
74                xml_set_element_handler( $this->parser, array( &$this->xmlhandler, 'startElement' ), array( &$this->xmlhandler, 'stopElement' ) );
75        }
76
77        function parseXML() {
78
79                $src = &$this->source;
80                $this->data = $src->getData();
81
82                if ( !xml_parse( &$this->parser, $this->data ) ) {
83                        $error = sprintf( 'XML error: %s at %d', xml_error_string( xml_get_error_code( &$this->parser ) ), xml_get_current_line_number( &$this->parser ) );
84                        // die( $error );
85                }
86        }
87
88}
89
90class TorqueXMLHandler {
91
92        function TorqueXMLHandler() {
93        }
94
95        function startElement( $parser, $name, $attrs ) {
96
97                if ( $attrs[TN] ) {
98
99                        // Ignore dead metrics. Detect and mask failures.
100                        if ( $attrs[TN] > $attrs[TMAX] * 4 )
101                                return;
102                }
103
104                $jobs = array();
105                $jobid = null;
106
107                printf( '%s=%s', $attrs[NAME], $attrs[VAL] );
108
109                sscanf( $attrs[NAME], 'TOGA-JOB-%d', $jobid );
110
111                if( $jobid ) {
112
113                        if( !isset( $jobs[$jobid] ) )
114                                $jobs[$jobid] = array();
115
116                        $fields = explode( ' ', $attrs[VAL] );
117
118                        foreach( $fields as $f ) {
119                                $togavalues = explode( '=', $f );
120
121                                foreach( $togavalues as $toganame => $togavalue ) {
122
123                                        if( $toganame == 'nodes' ) {
124
125                                                $nodes = explode( ';', $togavalue );
126
127                                                foreach( $nodes as $node ) {
128
129                                                        // Doe iets koels met $node
130                                                }
131
132                                        }
133
134                                        $jobs[$toganame] = $togavalue;
135
136                                }
137                        }
138
139                }
140        }
141
142        function stopElement( $parser, $name ) {
143        }
144}
145
146class Node {
147
148        var $img;
149
150        function Node() {
151
152                $this->img = new NodeImg();
153        }
154}
155
156class NodeImg {
157
158        var $image;
159
160        function NodeImg( $image = null ) {
161
162                $imageWidth = 100;
163                $imageHeight = 100;
164
165                if( !$image ) {
166                        $this->image = imageCreate( $imageWidth, $imageHeight );
167                                // or die( 'Cannot initialize new image stream: is GD installed?' );
168                } else {
169                        $this->image = $image;
170                }
171
172                $background_color = imageColorAllocate( $this->image, 255, 255, 255 );
173                $black_color = imageColorAllocate( $this->image, 0, 0, 0 );
174                imageRectangle( $this->image, 0, 0, $imageWidth-1, $imageHeight-1, $black_color );
175        }
176
177        function colorHex( $color ) {
178       
179                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 4 )), hexdec( substr( $color, 4, 6 )) );
180
181                return $my_color;
182        }
183
184        function drawNode( $x, $y, &$queuecolor, $load, &$jobcolor ) {
185
186                // Convert Ganglias Hexadecimal load color to a Decimal one
187                $my_loadcolor = $this->colorHex( load_color($load) );
188
189                imageFilledRectangle( $this->image, $x, $y, $x+12, $y+12, $queuecolor );
190                imageFilledRectangle( $this->image, $x+2, $y+2, $x+10, $y+10, $my_loadcolor );
191                //imageFilledEllipse( $this->image, ($x+9)/2, ($y+9)/2, 6, 6, $jobcolor );
192        }
193
194        function drawImage() {
195
196                $queue_color = imageColorAllocate( $this->image, 0, 102, 304 );
197                $job_color = imageColorAllocate( $this->image, 204, 204, 0 );
198
199                $this->drawNode( 1, 1, $queue_color, 0.1, $job_color );
200                header( 'Content-type: image/png' );
201                imagePNG( $this->image );
202                imageDestroy( $this->image );
203        }
204}
205
206//$my_node = new NodeImg();
207//$my_node->drawImage();
208
209$my_data = new DataGatherer();
210$my_data->parseXML();
211?>
Note: See TracBrowser for help on using the repository browser.