Changeset 699
- Timestamp:
- 03/21/13 16:13:36 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/0.4/jobmond/jobmond.py
r694 r699 26 26 27 27 import sys, getopt, ConfigParser, time, os, socket, string, re 28 import xdrlib, socket, syslog, xml, xml.sax 28 import xdrlib, socket, syslog, xml, xml.sax, shlex 29 29 from xml.sax.handler import feature_namespaces 30 30 from collections import deque 31 from glob import glob 31 32 32 33 VERSION='0.4+SVN' … … 94 95 return loadConfig( config_filename ) 95 96 96 # Fixme: This doesn't DTRT with commented-out bits of the file. E.g.97 # it picked up a commented-out `mcast_join' and tried to use a98 # multicast channel when it shouldn't have done.99 97 class GangliaConfigParser: 100 98 101 def __init__( self, config_file ): 102 103 self.config_file = config_file 104 105 if not os.path.exists( self.config_file ): 106 107 debug_msg( 0, "FATAL ERROR: gmond config '" + self.config_file + "' not found!" ) 108 sys.exit( 1 ) 99 def __init__( self, filename ): 100 101 self.conf_lijst = [ ] 102 self.conf_dict = { } 103 self.filename = filename 104 self.file_pointer = file( filename, 'r' ) 105 self.lexx = shlex.shlex( self.file_pointer ) 106 self.lexx.whitespace_split = True 107 108 self.parse() 109 110 def __del__( self ): 111 112 """ 113 Cleanup: close file descriptor 114 """ 115 116 self.file_pointer.close() 117 del self.lexx 118 del self.conf_lijst 109 119 110 120 def removeQuotes( self, value ): 111 121 112 clean_value 113 clean_value 114 clean_value 115 clean_value 122 clean_value = value 123 clean_value = clean_value.replace( "'", "" ) 124 clean_value = clean_value.replace( '"', '' ) 125 clean_value = clean_value.strip() 116 126 117 127 return clean_value 118 128 119 def getVal( self, section, valname ): 120 121 cfg_fp = open( self.config_file ) 122 section_start = False 123 section_found = False 124 value = None 125 126 for line in cfg_fp.readlines(): 127 128 if line.find( section ) != -1: 129 130 section_found = True 131 132 if line.find( '{' ) != -1 and section_found: 133 134 section_start = True 135 136 if line.find( '}' ) != -1 and section_found: 137 138 section_start = False 139 section_found = False 140 141 if line.find( valname ) != -1 and section_start: 142 143 value = string.join( line.split( '=' )[1:], '' ).strip() 144 145 cfg_fp.close() 146 147 return value 129 def removeBraces( self, value ): 130 131 clean_value = value 132 clean_value = clean_value.replace( "(", "" ) 133 clean_value = clean_value.replace( ')', '' ) 134 clean_value = clean_value.strip() 135 136 return clean_value 137 138 def parse( self ): 139 140 """ 141 Parse self.filename using shlex scanning. 142 - Removes /* comments */ 143 - Traverses (recursively) through all include () statements 144 - Stores complete valid config tokens in self.conf_list 145 146 i.e.: 147 ['globals', 148 '{', 149 'daemonize', 150 '=', 151 'yes', 152 'setuid', 153 '=', 154 'yes', 155 'user', 156 '=', 157 'ganglia', 158 'debug_level', 159 '=', 160 '0', 161 <etc> ] 162 """ 163 164 t = 'bogus' 165 c = False 166 i = False 167 168 while t != self.lexx.eof: 169 #print 'get token' 170 t = self.lexx.get_token() 171 172 if len( t ) >= 2: 173 174 if len( t ) >= 4: 175 176 if t[:2] == '/*' and t[-2:] == '*/': 177 178 #print 'comment line' 179 #print 'skipping: %s' %t 180 continue 181 182 if t == '/*' or t[:2] == '/*': 183 c = True 184 #print 'comment start' 185 #print 'skipping: %s' %t 186 continue 187 188 if t == '*/' or t[-2:] == '*/': 189 c = False 190 #print 'skipping: %s' %t 191 #print 'comment end' 192 continue 193 194 if c: 195 #print 'skipping: %s' %t 196 continue 197 198 if t == 'include': 199 i = True 200 #print 'include start' 201 #print 'skipping: %s' %t 202 continue 203 204 if i: 205 206 #print 'include start: %s' %t 207 208 t2 = self.removeQuotes( t ) 209 t2 = self.removeBraces( t ) 210 211 for in_file in glob( self.removeQuotes(t2) ): 212 213 #print 'including file: %s' %in_file 214 parse_infile = GangliaConfigParser( in_file ) 215 216 self.conf_lijst = self.conf_lijst + parse_infile.getConfLijst() 217 218 del parse_infile 219 220 i = False 221 #print 'include end' 222 #print 'skipping: %s' %t 223 continue 224 225 #print 'keep: %s' %t 226 self.conf_lijst.append( self.removeQuotes(t) ) 227 228 def getConfLijst( self ): 229 230 return self.conf_lijst 231 232 def confListToDict( self, parent_list=None ): 233 234 """ 235 Recursively traverses a conf_list and creates dictionary from it 236 """ 237 238 new_dict = { } 239 count = 0 240 skip = 0 241 242 if not parent_list: 243 parent_list = self.conf_lijst 244 245 #print 'entering confListToDict(): (parent) list size %s' %len(parent_list) 246 247 for n, c in enumerate( parent_list ): 248 249 count = count + 1 250 251 #print 'CL: n %d c %s' %(n, c) 252 253 if skip > 0: 254 255 #print '- skipped' 256 skip = skip - 1 257 continue 258 259 if (n+1) <= (len( parent_list )-1): 260 261 if parent_list[(n+1)] == '{': 262 263 if not new_dict.has_key( c ): 264 new_dict[ c ] = [ ] 265 266 (temp_new_dict, skip) = self.confListToDict( parent_list[(n+2):] ) 267 new_dict[ c ].append( temp_new_dict ) 268 269 if parent_list[(n+1)] == '=' and (n+2) <= (len( parent_list )-1): 270 271 if not new_dict.has_key( c ): 272 new_dict[ c ] = [ ] 273 274 new_dict[ c ].append( parent_list[ (n+2) ] ) 275 276 skip = 2 277 278 if parent_list[n] == '}': 279 280 #print 'leaving confListToDict(): new dict = %s' %new_dict 281 return (new_dict, count) 282 283 def makeConfDict( self ): 284 285 """ 286 Walks through self.conf_list and creates a dictionary based upon config values 287 288 i.e.: 289 'tcp_accept_channel': [{'acl': [{'access': [{'action': ['"allow"'], 290 'ip': ['"127.0.0.1"'], 291 'mask': ['32']}]}], 292 'port': ['8649']}], 293 'udp_recv_channel': [{'port': ['8649']}], 294 'udp_send_channel': [{'host': ['145.101.32.3'], 295 'port': ['8649']}, 296 {'host': ['145.101.32.207'], 297 'port': ['8649']}]} 298 """ 299 300 new_dict = { } 301 skip = 0 302 303 #print 'entering makeConfDict()' 304 305 for n, c in enumerate( self.conf_lijst ): 306 307 #print 'M: n %d c %s' %(n, c) 308 309 if skip > 0: 310 311 #print '- skipped' 312 skip = skip - 1 313 continue 314 315 if (n+1) <= (len( self.conf_lijst )-1): 316 317 if self.conf_lijst[(n+1)] == '{': 318 319 if not new_dict.has_key( c ): 320 new_dict[ c ] = [ ] 321 322 ( temp_new_dict, skip ) = self.confListToDict( self.conf_lijst[(n+2):] ) 323 new_dict[ c ].append( temp_new_dict ) 324 325 if self.conf_lijst[(n+1)] == '=' and (n+2) <= (len( self.conf_lijst )-1): 326 327 if not new_dict.has_key( c ): 328 new_dict[ c ] = [ ] 329 330 new_dict[ c ].append( self.conf_lijst[ (n+2) ] ) 331 332 skip = 2 333 334 self.conf_dict = new_dict 335 #print 'leaving makeConfDict(): conf dict size %d' %len( self.conf_dict ) 336 337 def checkConfDict( self ): 338 339 if len( self.conf_lijst ) == 0: 340 341 raise Exception("Something went wrong generating conf list for %s" %self.file_name ) 342 343 if len( self.conf_dict ) == 0: 344 345 self.makeConfDict() 346 347 def getConfDict( self ): 348 349 self.checkConfDict() 350 return self.conf_dict 351 352 def getUdpSendChannels( self ): 353 354 self.checkConfDict() 355 return self.conf_dict[ 'udp_send_channel' ] 356 357 def getSectionLastOption( self, section, option ): 358 359 """ 360 Get last option set in a config section that could be set multiple times in multiple (include) files. 361 362 i.e.: getSectionLastOption( 'globals', 'send_metadata_interval' ) 363 """ 364 365 self.checkConfDict() 366 value = None 367 368 if not self.conf_dict.has_key( section ): 369 370 return None 371 372 # Could be set multiple times in multiple (include) files: get last one set 373 for c in self.conf_dict[ section ]: 374 375 if c.has_key( option ): 376 377 cluster_name = c[ option ][0] 378 379 return cluster_name 380 381 def getClusterName( self ): 382 383 return self.getSectionLastOption( 'cluster', 'name' ) 384 385 def getVal( self, section, option ): 386 387 return self.getSectionLastOption( section, option ) 148 388 149 389 def getInt( self, section, valname ): … … 152 392 153 393 if not value: 154 return False 155 156 value = self.removeQuotes( value ) 394 return None 157 395 158 396 return int( value ) … … 163 401 164 402 if not value: 165 return False 166 167 value = self.removeQuotes( value ) 403 return None 168 404 169 405 return str( value )
Note: See TracChangeset
for help on using the changeset viewer.