[104] | 1 | <PRE> |
---|
[102] | 2 | <?php |
---|
| 3 | $GANGLIA_PATH = "/var/www/ganglia"; |
---|
| 4 | |
---|
| 5 | include_once "$GANGLIA_PATH/conf.php"; |
---|
| 6 | include_once "$GANGLIA_PATH/functions.php"; |
---|
| 7 | |
---|
[103] | 8 | if ( !empty( $_GET ) ) { |
---|
| 9 | extract( $_GET ); |
---|
| 10 | } |
---|
| 11 | |
---|
| 12 | class 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 | |
---|
| 33 | class 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 | |
---|
| 66 | class DataGatherer { |
---|
| 67 | |
---|
[105] | 68 | var $xmlhandler, $data, $httpvars; |
---|
[103] | 69 | |
---|
| 70 | function DataGatherer() { |
---|
| 71 | |
---|
| 72 | $this->parser = xml_parser_create(); |
---|
| 73 | $this->source = new DataSource(); |
---|
[105] | 74 | $this->httpvars = new HTTPVariables(); |
---|
[103] | 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 | |
---|
[105] | 90 | function printInfo() { |
---|
| 91 | $handler = $this->xmlhandler; |
---|
| 92 | $handler->printInfo(); |
---|
| 93 | } |
---|
| 94 | |
---|
[103] | 95 | } |
---|
| 96 | |
---|
| 97 | class TorqueXMLHandler { |
---|
| 98 | |
---|
[105] | 99 | var $clusters, $heartbeat, $nodes, $jobs; |
---|
[104] | 100 | |
---|
[103] | 101 | function TorqueXMLHandler() { |
---|
[105] | 102 | $jobs = array(); |
---|
[104] | 103 | $clusters = array(); |
---|
[105] | 104 | $nodes = array(); |
---|
[104] | 105 | $heartbeat = array(); |
---|
[103] | 106 | } |
---|
| 107 | |
---|
[105] | 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 | |
---|
[103] | 133 | function startElement( $parser, $name, $attrs ) { |
---|
| 134 | |
---|
[105] | 135 | $jobs = &$this->jobs; |
---|
| 136 | |
---|
[103] | 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 | |
---|
[104] | 146 | // printf( '%s=%s', $attrs[NAME], $attrs[VAL] ); |
---|
[103] | 147 | |
---|
[104] | 148 | if( $name == 'CLUSTER' ) { |
---|
[103] | 149 | |
---|
[104] | 150 | $clustername = $attrs[VAL]; |
---|
[103] | 151 | |
---|
[104] | 152 | if( !isset( $clusters[$clustername] ) ) |
---|
| 153 | $clusters[$clustername] = array(); |
---|
[103] | 154 | |
---|
[105] | 155 | } else if( $name == 'HOST' ) { |
---|
| 156 | |
---|
| 157 | $hostname = $attrs[NAME]; |
---|
| 158 | $location = $attrs[LOCATION]; |
---|
| 159 | |
---|
| 160 | $this->gotNode( $hostname, null,$location ); |
---|
| 161 | |
---|
[104] | 162 | } else if( $name == 'METRIC' and strstr( $attrs[NAME], 'TOGA' ) ) { |
---|
[103] | 163 | |
---|
[104] | 164 | if( strstr( $attrs[NAME], 'TOGA-HEARTBEAT' ) ) { |
---|
[103] | 165 | |
---|
[104] | 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 | |
---|
[105] | 186 | printf( "\t%s\t= %s\n", $toganame, $togavalue ); |
---|
[104] | 187 | |
---|
[103] | 188 | if( $toganame == 'nodes' ) { |
---|
| 189 | |
---|
[104] | 190 | if( !isset( $jobs[$toganame] ) ) |
---|
[105] | 191 | $jobs[$jobid][$toganame] = array(); |
---|
[104] | 192 | |
---|
[103] | 193 | $nodes = explode( ';', $togavalue ); |
---|
| 194 | |
---|
| 195 | foreach( $nodes as $node ) { |
---|
| 196 | |
---|
[105] | 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 ); |
---|
[103] | 205 | } |
---|
| 206 | |
---|
[104] | 207 | } else { |
---|
[103] | 208 | |
---|
[105] | 209 | $jobs[$jobid][$toganame] = $togavalue; |
---|
[103] | 210 | |
---|
[104] | 211 | } |
---|
[103] | 212 | } |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | function stopElement( $parser, $name ) { |
---|
| 218 | } |
---|
[105] | 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 | } |
---|
[103] | 256 | } |
---|
| 257 | |
---|
| 258 | class Node { |
---|
| 259 | |
---|
[105] | 260 | var $img, $hostname, $location, $jobs; |
---|
[103] | 261 | |
---|
[104] | 262 | function Node( $hostname ) { |
---|
[103] | 263 | |
---|
[104] | 264 | $this->hostname = $hostname; |
---|
[103] | 265 | $this->img = new NodeImg(); |
---|
[105] | 266 | $this->jobs = array(); |
---|
[103] | 267 | } |
---|
[104] | 268 | |
---|
[105] | 269 | function addJob( $jobid ) { |
---|
| 270 | $jobs = &$this->jobs; |
---|
| 271 | |
---|
| 272 | $jobs[] = $jobid; |
---|
| 273 | } |
---|
| 274 | |
---|
[104] | 275 | function setLocation( $location ) { |
---|
| 276 | $this->location = $location; |
---|
| 277 | } |
---|
| 278 | |
---|
[105] | 279 | function setHostname( $hostname ) { |
---|
| 280 | $this->hostname = $hostname; |
---|
| 281 | } |
---|
| 282 | |
---|
[104] | 283 | function setCpus( $cpus ) { |
---|
| 284 | $this->cpus = $cpus; |
---|
| 285 | } |
---|
[105] | 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 | } |
---|
[103] | 302 | } |
---|
| 303 | |
---|
[102] | 304 | class NodeImg { |
---|
| 305 | |
---|
| 306 | var $image; |
---|
| 307 | |
---|
| 308 | function NodeImg( $image = null ) { |
---|
| 309 | |
---|
| 310 | $imageWidth = 100; |
---|
| 311 | $imageHeight = 100; |
---|
| 312 | |
---|
[103] | 313 | if( !$image ) { |
---|
| 314 | $this->image = imageCreate( $imageWidth, $imageHeight ); |
---|
| 315 | // or die( 'Cannot initialize new image stream: is GD installed?' ); |
---|
[102] | 316 | } else { |
---|
[103] | 317 | $this->image = $image; |
---|
[102] | 318 | } |
---|
| 319 | |
---|
[103] | 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 ); |
---|
[102] | 323 | } |
---|
| 324 | |
---|
| 325 | function colorHex( $color ) { |
---|
| 326 | |
---|
[103] | 327 | $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 4 )), hexdec( substr( $color, 4, 6 )) ); |
---|
[102] | 328 | |
---|
| 329 | return $my_color; |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | function drawNode( $x, $y, &$queuecolor, $load, &$jobcolor ) { |
---|
| 333 | |
---|
[103] | 334 | // Convert Ganglias Hexadecimal load color to a Decimal one |
---|
[102] | 335 | $my_loadcolor = $this->colorHex( load_color($load) ); |
---|
| 336 | |
---|
[103] | 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 ); |
---|
[102] | 340 | } |
---|
| 341 | |
---|
| 342 | function drawImage() { |
---|
| 343 | |
---|
[103] | 344 | $queue_color = imageColorAllocate( $this->image, 0, 102, 304 ); |
---|
| 345 | $job_color = imageColorAllocate( $this->image, 204, 204, 0 ); |
---|
[102] | 346 | |
---|
| 347 | $this->drawNode( 1, 1, $queue_color, 0.1, $job_color ); |
---|
[103] | 348 | header( 'Content-type: image/png' ); |
---|
| 349 | imagePNG( $this->image ); |
---|
| 350 | imageDestroy( $this->image ); |
---|
[102] | 351 | } |
---|
| 352 | } |
---|
| 353 | |
---|
[103] | 354 | //$my_node = new NodeImg(); |
---|
| 355 | //$my_node->drawImage(); |
---|
[102] | 356 | |
---|
[103] | 357 | $my_data = new DataGatherer(); |
---|
| 358 | $my_data->parseXML(); |
---|
[105] | 359 | $my_data->printInfo(); |
---|
| 360 | $my_data->printInfo(); |
---|
[102] | 361 | ?> |
---|
[104] | 362 | </PRE> |
---|