Changeset 103 for trunk/web/addons/toga


Ignore:
Timestamp:
05/13/05 10:52:39 (19 years ago)
Author:
bastiaans
Message:

toga/toga-functions.php:

  • Added XML objects and handlers
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/web/addons/toga/toga-functions.php

    r102 r103  
    11<?php
    2 
    32$GANGLIA_PATH = "/var/www/ganglia";
    43
     
    65include_once "$GANGLIA_PATH/functions.php";
    76
     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
    8156class NodeImg {
    9157
     
    15163                $imageHeight = 100;
    16164
    17                 if( ! $image ) {
    18                         $this->$image = @imageCreate( $imageWidth, $imageHeight )
    19                                 or die( "Cannot initialize new image stream: is GD installed?" );
     165                if( !$image ) {
     166                        $this->image = imageCreate( $imageWidth, $imageHeight );
     167                                // or die( 'Cannot initialize new image stream: is GD installed?' );
    20168                } else {
    21                         $this->$image = $image;
    22                 }
    23 
    24                 $background_color = imageColorAllocate( $this->$image, 255, 255, 255 );
    25                 $black_color = imageColorAllocate( $this->$image, 0, 0, 0);
    26                 imageRectangle( $this->$image, 0, 0, $imageWidth-1, $imageHeight-1, $black_color );
     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 );
    27175        }
    28176
    29177        function colorHex( $color ) {
    30178       
    31                 $my_color = imageColorAllocate( $this->$image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 4 )), hexdec( substr( $color, 4, 6 ) ) );
     179                $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 4 )), hexdec( substr( $color, 4, 6 )) );
    32180
    33181                return $my_color;
     
    36184        function drawNode( $x, $y, &$queuecolor, $load, &$jobcolor ) {
    37185
    38                 // Convert Ganglia's Hexadecimal load color to a Decimal one
     186                // Convert Ganglias Hexadecimal load color to a Decimal one
    39187                $my_loadcolor = $this->colorHex( load_color($load) );
    40188
    41                 imageFilledRectangle( $this->$image, $x, $y, $x+12, $y+12, $queuecolor );
    42                 imageFilledRectangle( $this->$image, $x+2, $y+2, $x+10, $y+10, $my_loadcolor );
    43                 //imageFilledEllipse( $this->$image, ($x+9)/2, ($y+9)/2, 6, 6, $jobcolor );
     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 );
    44192        }
    45193
    46194        function drawImage() {
    47195
    48                 $queue_color = imageColorAllocate( $this->$image, 0, 102, 304 );
    49                 $job_color = imageColorAllocate( $this->$image, 204, 204, 0 );
     196                $queue_color = imageColorAllocate( $this->image, 0, 102, 304 );
     197                $job_color = imageColorAllocate( $this->image, 204, 204, 0 );
    50198
    51199                $this->drawNode( 1, 1, $queue_color, 0.1, $job_color );
    52                 header( "Content-type: image/png" );
    53                 imagePNG( $this->$image );
    54                 imageDestroy( $this->$image );
    55         }
    56 }
    57 
    58 $my_node = new NodeImg();
    59 $my_node->drawImage();
    60 
     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();
    61211?>
Note: See TracChangeset for help on using the changeset viewer.