Ignore:
Timestamp:
03/12/08 18:22:46 (16 years ago)
Author:
bastiaans
Message:

jobmond/jobmond.py:

  • added: gmond config parsing: ticket #48
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/jobmond/jobmond.py

    r514 r520  
    9191        return loadConfig( config_filename )
    9292
     93class GangliaConfigParser:
     94
     95        def __init__( self, config_file ):
     96
     97                self.config_file        = config_file
     98
     99                if not os.path.exists( self.config_file ):
     100
     101                        debug_msg( 0, "FATAL ERROR: gmond config '" + self.config_file + "' not found!" )
     102                        sys.exit( 1 )
     103
     104        def removeQuotes( self, value ):
     105
     106                clean_value     = value
     107                clean_value     = clean_value.replace( "'", "" )
     108                clean_value     = clean_value.replace( '"', '' )
     109                clean_value     = clean_value.strip()
     110
     111                return clean_value
     112
     113        def getVal( self, section, valname ):
     114
     115                cfg_fp          = open( self.config_file )
     116                section_start   = False
     117                section_found   = False
     118                value           = None
     119
     120                for line in cfg_fp.readlines():
     121
     122                        if line.find( section ) != -1:
     123
     124                                section_found   = True
     125
     126                        if line.find( '{' ) != -1 and section_found:
     127
     128                                section_start   = True
     129
     130                        if line.find( '}' ) != -1 and section_found:
     131
     132                                section_start   = False
     133                                section_found   = False
     134
     135                        if line.find( valname ) != -1 and section_start:
     136
     137                                value           = string.join( line.split( '=' )[1:], '' ).strip()
     138
     139                cfg_fp.close()
     140
     141                return value
     142
     143        def getInt( self, section, valname ):
     144
     145                value   = self.getVal( section, valname )
     146
     147                if not value:
     148                        return False
     149
     150                value   = self.removeQuotes( value )
     151
     152                return int( value )
     153
     154        def getStr( self, section, valname ):
     155
     156                value   = self.getVal( section, valname )
     157
     158                if not value:
     159                        return False
     160
     161                value   = self.removeQuotes( value )
     162
     163                return str( value )
     164
     165def findGmetric():
     166
     167        for dir in os.path.expandvars( '$PATH' ).split( ':' ):
     168
     169                guess   = '%s/%s' %( dir, 'gmetric' )
     170
     171                if os.path.exists( guess ):
     172
     173                        return guess
     174
     175        return False
     176
    93177def loadConfig( filename ):
    94178
     
    149233
    150234                debug_msg( 0, 'ERROR: no option USE_SYSLOG found: assuming yes' )
    151 
    152 
    153235
    154236        if USE_SYSLOG:
     
    202284        except ConfigParser.NoOptionError:
    203285
    204                 GMOND_CONF              = None
    205 
    206         try:
    207 
    208                 GMETRIC_BINARY          = cfg.get( 'DEFAULT', 'GMETRIC_BINARY' )
    209 
    210         except ConfigParser.NoOptionError:
    211 
    212                 GMETRIC_BINARY          = '/usr/bin/gmetric'
     286                # Not specified: assume /etc/gmond.conf
     287                #
     288                GMOND_CONF              = '/etc/gmond.conf'
     289
     290        ganglia_cfg             = GangliaConfigParser( GMOND_CONF )
     291
     292        # Let's try to find the GMETRIC_TARGET ourselves first from GMOND_CONF
     293        #
     294        gmetric_dest_ip         = ganglia_cfg.getStr( 'udp_send_channel', 'mcast_join' )
     295
     296        if not gmetric_dest_ip:
     297
     298                # Maybe unicast target then
     299                #
     300                gmetric_dest_ip         = ganglia_cfg.getStr( 'udp_send_channel', 'host' )
     301
     302        gmetric_dest_port       = gcp.getStr( 'udp_send_channel', 'port' )
     303
     304        if gmetric_dest_ip and gmetric_dest_port:
     305
     306                GMETRIC_TARGET  = '%s:%s' %( gmetric_dest_ip, gmetric_dest_port )
     307        else:
     308
     309                debug_msg( 0, "WARNING: Can't parse udp_send_channel from: '%s'" %GMOND_CONF )
     310
     311                # Couldn't figure it out: let's see if it's in our jobmond.conf
     312                #
     313                try:
     314
     315                        GMETRIC_TARGET  = cfg.get( 'DEFAULT', 'GMETRIC_TARGET' )
     316
     317                # Guess not: now just give up
     318                #
     319                except ConfigParser.NoOptionError:
     320
     321                        GMETRIC_TARGET  = None
     322
     323                        debug_msg( 0, "ERROR: GMETRIC_TARGET not set: internal Gmetric handling aborted. Failing back to DEPRECATED use of gmond.conf/gmetric binary. This will slow down jobmond significantly!" )
     324
     325        gmetric_bin     = findGmetric()
     326
     327        if gmetric_bin:
     328
     329                GMETRIC_BINARY          = gmetric_bin
     330        else:
     331                debug_msg( 0, "WARNING: Can't find gmetric binary anywhere in $PATH" )
     332
     333                try:
     334
     335                        GMETRIC_BINARY          = cfg.get( 'DEFAULT', 'GMETRIC_BINARY' )
     336
     337                except ConfigParser.NoOptionError:
     338
     339                        debug_msg( 0, "FATAL ERROR: GMETRIC_BINARY not set and not in $PATH" )
     340                        sys.exit( 1 )
    213341
    214342        DETECT_TIME_DIFFS       = cfg.getboolean( 'DEFAULT', 'DETECT_TIME_DIFFS' )
     
    237365                QUEUE           = None
    238366
    239         try:
    240 
    241                 GMETRIC_TARGET  = cfg.get( 'DEFAULT', 'GMETRIC_TARGET' )
    242 
    243         except ConfigParser.NoOptionError:
    244 
    245                 GMETRIC_TARGET  = None
    246 
    247                 if not GMOND_CONF:
    248 
    249                         debug_msg( 0, "FATAL ERROR: GMETRIC_TARGET and GMOND_CONF both not set! Set at least one!" )
    250                         sys.exit( 1 )
    251                 else:
    252 
    253                         debug_msg( 0, "ERROR: GMETRIC_TARGET not set: internal Gmetric handling aborted. Failing back to DEPRECATED use of gmond.conf/gmetric binary. This will slow down jobmond significantly!" )
    254 
    255367        return True
    256368
    257369def fqdn_parts (fqdn):
     370
    258371        """Return pair of host and domain for fully-qualified domain name arg."""
     372
    259373        parts = fqdn.split (".")
     374
    260375        return (parts[0], string.join(parts[1:], "."))
    261376
     
    290405
    291406                if GMOND_CONF:
    292 
    293                         try:
    294                                 gmond_file = GMOND_CONF
    295 
    296                         except NameError:
    297                                 gmond_file = '/etc/gmond.conf'
    298 
    299                         if not os.path.exists( gmond_file ):
    300                                 debug_msg( 0, 'FATAL ERROR: ' + gmond_file + ' does not exist' )
    301                                 sys.exit( 1 )
    302407
    303408                        incompatible = self.checkGmetricVersion()
     
    822927# Abstracted from PBS original.
    823928# Fixme:  Is it worth (or appropriate for PBS) sorting the result?
    824 def do_nodelist (nodes):
     929#
     930def do_nodelist( nodes ):
     931
    825932        """Translate node list as appropriate."""
     933
    826934        nodeslist               = [ ]
    827         my_domain = fqdn_parts(socket.getfqdn())[1]
     935        my_domain               = fqdn_parts( socket.getfqdn() )[1]
     936
    828937        for node in nodes:
     938
    829939                host            = node.split( '/' )[0] # not relevant for SGE
    830940                h, host_domain  = fqdn_parts(host)
     941
    831942                if host_domain == my_domain:
     943
    832944                        host    = h
     945
    833946                if nodeslist.count( host ) == 0:
     947
    834948                        for translate_pattern in BATCH_HOST_TRANSLATE:
     949
    835950                                if translate_pattern.find( '/' ) != -1:
     951
    836952                                        translate_orig  = \
    837953                                            translate_pattern.split( '/' )[1]
Note: See TracChangeset for help on using the changeset viewer.