Changeset 362


Ignore:
Timestamp:
06/08/07 16:25:07 (17 years ago)
Author:
bastiaans
Message:

jobmond/jobmond.py:

  • integrated native gmetric thanks to: Nick Galbreath

jobmond/gmetric.py:

  • moved into jobmond.py

jobmond/LICENSE.gmetric:

  • added gmetric LICENSE
Location:
trunk/jobmond
Files:
1 added
1 deleted
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/jobmond/jobmond.py

    r359 r362  
    2424import sys, getopt, ConfigParser
    2525import time, os, socket, string, re
     26import xdrlib, socket
    2627import xml, xml.sax
    2728from xml.sax import saxutils, make_parser
     
    826827                return str_list
    827828
     829GMETRIC_DEFAULT_TYPE    = 'string'
     830GMETRIC_DEFAULT_HOST    = '127.0.0.1'
     831GMETRIC_DEFAULT_PORT    = '8649'
     832
     833class Gmetric:
     834
     835        global GMETRIC_DEFAULT_HOST, GMETRIC_DEFAULT_PORT
     836
     837        slope           = { 'zero' : 0, 'positive' : 1, 'negative' : 2, 'both' : 3, 'unspecified' : 4 }
     838        type            = ( '', 'string', 'uint16', 'int16', 'uint32', 'int32', 'float', 'double', 'timestamp' )
     839        protocol        = ( 'udp', 'multicast' )
     840
     841        def __init__( self, host=GMETRIC_DEFAULT_HOST, port=GMETRIC_DEFAULT_PORT ):
     842               
     843                global GMETRIC_DEFAULT_TYPE
     844
     845                self.prot       = self.checkHostProtocol( host )
     846                self.msg        = xdrlib.Packer()
     847                self.socket     = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
     848
     849                if self.prot not in self.protocol:
     850
     851                        raise ValueError( "Protocol must be one of: " + str( self.protocol ) )
     852
     853                if self.prot == 'multicast':
     854
     855                        self.socket.setsockopt( socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 20 )
     856
     857                self.hostport   = ( host, int( port ) )
     858                self.type       = GMETRIC_DEFAULT_TYPE
     859                self.unitstr    = ''
     860                self.slopestr   = 'both'
     861                self.tmax       = 60
     862
     863        def checkHostProtocol( self, ip ):
     864
     865                MULTICAST_ADDRESS_MIN   = ( "224", "0", "0", "0" )
     866                MULTICAST_ADDRESS_MAX   = ( "239", "255", "255", "255" )
     867
     868                ip_fields               = ip.split( '.' )
     869
     870                if ip_fields >= MULTICAST_ADDRESS_MIN and ip_fields <= MULTICAST_ADDRESS_MAX:
     871
     872                        return 'multicast'
     873                else:
     874                        return 'udp'
     875
     876        def send( self, name, value, dmax ):
     877
     878                msg             = self.makexdr( name, value, self.type, self.unitstr, self.slopestr, self.tmax, dmax )
     879
     880                return self.socket.sendto( msg, self.hostport )
     881
     882        def makexdr( self, name, value, typestr, unitstr, slopestr, tmax, dmax ):
     883
     884                if slopestr not in self.slope:
     885
     886                        raise ValueError( "Slope must be one of: " + str( self.slope.keys() ) )
     887
     888                if typestr not in self.type:
     889
     890                        raise ValueError( "Type must be one of: " + str( self.type ) )
     891
     892                if len( name ) == 0:
     893
     894                        raise ValueError( "Name must be non-empty" )
     895
     896                self.msg.reset()
     897                self.msg.pack_int( 0 )
     898                self.msg.pack_string( typestr )
     899                self.msg.pack_string( name )
     900                self.msg.pack_string( str( value ) )
     901                self.msg.pack_string( unitstr )
     902                self.msg.pack_int( self.slope[ slopestr ] )
     903                self.msg.pack_uint( int( tmax ) )
     904                self.msg.pack_uint( int( dmax ) )
     905
     906                return self.msg.get_buffer()
     907
    828908def printTime( ):
    829909
Note: See TracChangeset for help on using the changeset viewer.