Index: trunk/jobmond/jobmond.py
===================================================================
--- trunk/jobmond/jobmond.py	(revision 353)
+++ trunk/jobmond/jobmond.py	(revision 354)
@@ -23,5 +23,5 @@
 
 import sys, getopt, ConfigParser
-
+import time, os, socket, string, re
 import xml, xml.sax
 from xml.sax import saxutils, make_parser
@@ -39,17 +39,17 @@
 	print
 
-
 def processArgs( args ):
 
-	SHORT_L = 'c:'
-	LONG_L = 'config='
+	SHORT_L		= 'hc:'
+	LONG_L		= [ 'help', 'config=' ]
 
 	global PIDFILE
-	PIDFILE = None
-	config_filename = '/etc/jobmond.conf'
+	PIDFILE		= None
+
+	config_filename	= '/etc/jobmond.conf'
 
 	try:
 
-		opts, args = getopt.getopt( args, SHORT_L, LONG_L )
+		opts, args	= getopt.getopt( args, SHORT_L, LONG_L )
 
 	except getopt.GetoptError, detail:
@@ -57,5 +57,5 @@
 		print detail
 		usage()
-		sys.exit(1)
+		sys.exit( 1 )
 
 	for opt, value in opts:
@@ -63,14 +63,14 @@
 		if opt in [ '--config', '-c' ]:
 		
-			config_filename = value
+			config_filename	= value
 
 		if opt in [ '--pidfile', '-p' ]:
 
-			PIDFILE = value
+			PIDFILE		= value
 		
 		if opt in [ '--help', '-h' ]:
  
 			usage()
-			sys.exit(1)
+			sys.exit( 0 )
 
 	return loadConfig( config_filename )
@@ -110,17 +110,19 @@
                 return my_list
 
-	cfg = ConfigParser.ConfigParser()
+	cfg		= ConfigParser.ConfigParser()
 
 	cfg.read( filename )
 
-	global DEBUG_LEVEL, DAEMONIZE, BATCH_SERVER, BATCH_POLL_INTERVAL, GMOND_CONF, DETECT_TIME_DIFFS, BATCH_HOST_TRANSLATE, BATCH_API, QUEUE, GMETRIC_TARGET
-
-	DEBUG_LEVEL = cfg.getint( 'DEFAULT', 'DEBUG_LEVEL' )
-
-	DAEMONIZE = cfg.getboolean( 'DEFAULT', 'DAEMONIZE' )
+	global DEBUG_LEVEL, DAEMONIZE, BATCH_SERVER, BATCH_POLL_INTERVAL
+	global GMOND_CONF, DETECT_TIME_DIFFS, BATCH_HOST_TRANSLATE
+	global BATCH_API, QUEUE, GMETRIC_TARGET
+
+	DEBUG_LEVEL	= cfg.getint( 'DEFAULT', 'DEBUG_LEVEL' )
+
+	DAEMONIZE	= cfg.getboolean( 'DEFAULT', 'DAEMONIZE' )
 
 	try:
 
-		BATCH_SERVER = cfg.get( 'DEFAULT', 'BATCH_SERVER' )
+		BATCH_SERVER		= cfg.get( 'DEFAULT', 'BATCH_SERVER' )
 
 	except ConfigParser.NoOptionError:
@@ -129,10 +131,10 @@
 		#
 
-		BATCH_SERVER = cfg.get( 'DEFAULT', 'TORQUE_SERVER' )
-		api_guess = 'pbs'
+		BATCH_SERVER		= cfg.get( 'DEFAULT', 'TORQUE_SERVER' )
+		api_guess		= 'pbs'
 	
 	try:
 	
-		BATCH_POLL_INTERVAL = cfg.getint( 'DEFAULT', 'BATCH_POLL_INTERVAL' )
+		BATCH_POLL_INTERVAL	= cfg.getint( 'DEFAULT', 'BATCH_POLL_INTERVAL' )
 
 	except ConfigParser.NoOptionError:
@@ -141,27 +143,28 @@
 		#
 
-		BATCH_POLL_INTERVAL = cfg.getint( 'DEFAULT', 'TORQUE_POLL_INTERVAL' )
-		api_guess = 'pbs'
+		BATCH_POLL_INTERVAL	= cfg.getint( 'DEFAULT', 'TORQUE_POLL_INTERVAL' )
+		api_guess		= 'pbs'
 	
 	try:
 
-		GMOND_CONF = cfg.get( 'DEFAULT', 'GMOND_CONF' )
+		GMOND_CONF		= cfg.get( 'DEFAULT', 'GMOND_CONF' )
 
 	except ConfigParser.NoOptionError:
 
-		GMOND_CONF = None
-
-	DETECT_TIME_DIFFS = cfg.getboolean( 'DEFAULT', 'DETECT_TIME_DIFFS' )
-
-	BATCH_HOST_TRANSLATE = getlist( cfg.get( 'DEFAULT', 'BATCH_HOST_TRANSLATE' ) )
+		GMOND_CONF		= None
+
+	DETECT_TIME_DIFFS	= cfg.getboolean( 'DEFAULT', 'DETECT_TIME_DIFFS' )
+
+	BATCH_HOST_TRANSLATE	= getlist( cfg.get( 'DEFAULT', 'BATCH_HOST_TRANSLATE' ) )
 
 	try:
 
-		BATCH_API = cfg.get( 'DEFAULT', 'BATCH_API' )
+		BATCH_API	= cfg.get( 'DEFAULT', 'BATCH_API' )
 
 	except ConfigParser.NoOptionError, detail:
 
 		if BATCH_SERVER and api_guess:
-			BATCH_API = api_guess
+
+			BATCH_API	= api_guess
 		else:
 			debug_msg( 0, "fatal error: BATCH_API not set and can't make guess" )
@@ -170,17 +173,17 @@
 	try:
 
-		QUEUE = getlist( cfg.get( 'DEFAULT', 'QUEUE' ) )
+		QUEUE		= getlist( cfg.get( 'DEFAULT', 'QUEUE' ) )
 
 	except ConfigParser.NoOptionError, detail:
 
-		QUEUE = None
+		QUEUE		= None
 
 	try:
 
-		GMETRIC_TARGET = cfg.get( 'DEFAULT', 'GMETRIC_TARGET' )
+		GMETRIC_TARGET	= cfg.get( 'DEFAULT', 'GMETRIC_TARGET' )
 
 	except ConfigParser.NoOptionError:
 
-		GMETRIC_TARGET = None
+		GMETRIC_TARGET	= None
 
 		if not GMOND_CONF:
@@ -193,7 +196,4 @@
 
 	return True
-
-
-import time, os, socket, string, re
 
 METRIC_MAX_VAL_LEN = 900
@@ -220,4 +220,5 @@
 
 		if GMOND_CONF:
+
 			try:
 				gmond_file = GMOND_CONF
@@ -513,31 +514,39 @@
 
 	def __init__( self ):
+
 		"""Setup appropriate variables"""
 
-		self.jobs = { }
-		self.timeoffset = 0
-		self.dp = DataProcessor()
+		self.jobs	= { }
+		self.timeoffset	= 0
+		self.dp		= DataProcessor()
+
 		self.initPbsQuery()
 
 	def initPbsQuery( self ):
 
-		self.pq = None
+		self.pq		= None
+
 		if( BATCH_SERVER ):
-			self.pq = PBSQuery( BATCH_SERVER )
+
+			self.pq		= PBSQuery( BATCH_SERVER )
 		else:
-			self.pq = PBSQuery()
+			self.pq		= PBSQuery()
 
 	def getAttr( self, attrs, name ):
+
 		"""Return certain attribute from dictionary, if exists"""
 
 		if attrs.has_key( name ):
-			return attrs[name]
+
+			return attrs[ name ]
 		else:
 			return ''
 
 	def jobDataChanged( self, jobs, job_id, attrs ):
+
 		"""Check if job with attrs and job_id in jobs has changed"""
 
 		if jobs.has_key( job_id ):
+
 			oldData = jobs[ job_id ]	
 		else:
@@ -558,33 +567,31 @@
 
 	def getJobData( self ):
+
 		"""Gather all data on current jobs in Torque"""
 
-		#self.initPbsQuery()
-	
-		#print self.pq.getnodes()
-	
-		joblist = {}
-
-		while len(joblist) == 0:
+		joblist		= {}
+
+		while len( joblist ) == 0:
+
 			try:
 				joblist = self.pq.getjobs()
+
 			except PBSError, detail:
-				debug_msg( 10, "Caught PBS unavaible, skipping until next polling interval: " + str( detail ) )
+
+				debug_msg( 10, "Caught PBS unavailable, skipping until next polling interval: " + str( detail ) )
 				return None
 
-		self.cur_time = time.time()
-
-		jobs_processed = [ ]
-
-		#self.printJobs( joblist )
+		self.cur_time	= time.time()
+
+		jobs_processed	= [ ]
 
 		for name, attrs in joblist.items():
 
-			job_id = name.split( '.' )[0]
+			job_id			= name.split( '.' )[0]
 
 			jobs_processed.append( job_id )
 
-			name = self.getAttr( attrs, 'Job_Name' )
-			queue = self.getAttr( attrs, 'queue' )
+			name			= self.getAttr( attrs, 'Job_Name' )
+			queue			= self.getAttr( attrs, 'queue' )
 
 			if QUEUE:
@@ -594,15 +601,15 @@
 					continue
 
-			owner = self.getAttr( attrs, 'Job_Owner' ).split( '@' )[0]
-			requested_time = self.getAttr( attrs, 'Resource_List.walltime' )
-			requested_memory = self.getAttr( attrs, 'Resource_List.mem' )
-
-			mynoderequest = self.getAttr( attrs, 'Resource_List.nodes' )
-
-			ppn = ''
+			owner			= self.getAttr( attrs, 'Job_Owner' ).split( '@' )[0]
+			requested_time		= self.getAttr( attrs, 'Resource_List.walltime' )
+			requested_memory	= self.getAttr( attrs, 'Resource_List.mem' )
+
+			mynoderequest		= self.getAttr( attrs, 'Resource_List.nodes' )
+
+			ppn			= ''
 
 			if mynoderequest.find( ':' ) != -1 and mynoderequest.find( 'ppn' ) != -1:
 
-				mynoderequest_fields = mynoderequest.split( ':' )
+				mynoderequest_fields	= mynoderequest.split( ':' )
 
 				for mynoderequest_field in mynoderequest_fields:
@@ -610,18 +617,20 @@
 					if mynoderequest_field.find( 'ppn' ) != -1:
 
-						ppn = mynoderequest_field.split( 'ppn=' )[1]
-
-			status = self.getAttr( attrs, 'job_state' )
-
-			queued_timestamp = self.getAttr( attrs, 'ctime' )
+						ppn	= mynoderequest_field.split( 'ppn=' )[1]
+
+			status			= self.getAttr( attrs, 'job_state' )
+
+			queued_timestamp	= self.getAttr( attrs, 'ctime' )
 
 			if status == 'R':
-				start_timestamp = self.getAttr( attrs, 'mtime' )
-				nodes = self.getAttr( attrs, 'exec_host' ).split( '+' )
-
-				nodeslist = [ ]
+
+				start_timestamp		= self.getAttr( attrs, 'mtime' )
+				nodes			= self.getAttr( attrs, 'exec_host' ).split( '+' )
+
+				nodeslist		= [ ]
 
 				for node in nodes:
-					host = node.split( '/' )[0]
+
+					host		= node.split( '/' )[0]
 
 					if nodeslist.count( host ) == 0:
@@ -631,8 +640,8 @@
 							if translate_pattern.find( '/' ) != -1:
 
-								translate_orig = translate_pattern.split( '/' )[1]
-								translate_new = translate_pattern.split( '/' )[2]
-
-								host = re.sub( translate_orig, translate_new, host )
+								translate_orig	= translate_pattern.split( '/' )[1]
+								translate_new	= translate_pattern.split( '/' )[2]
+
+								host		= re.sub( translate_orig, translate_new, host )
 				
 						if not host in nodeslist:
@@ -646,16 +655,17 @@
 					# than our local time.
 				
-					if int(start_timestamp) > int( int(self.cur_time) + int(self.timeoffset) ):
-
-						self.timeoffset = int( int(start_timestamp) - int(self.cur_time) )
+					if int( start_timestamp ) > int( int( self.cur_time ) + int( self.timeoffset ) ):
+
+						self.timeoffset	= int( int(start_timestamp) - int(self.cur_time) )
 
 			elif status == 'Q':
-				start_timestamp = ''
-				count_mynodes = 0
-				numeric_node = 1
+
+				start_timestamp		= ''
+				count_mynodes		= 0
+				numeric_node		= 1
 
 				for node in mynoderequest.split( '+' ):
 
-					nodepart = node.split( ':' )[0]
+					nodepart	= node.split( ':' )[0]
 
 					for letter in nodepart:
@@ -663,12 +673,15 @@
 						if letter not in string.digits:
 
-							numeric_node = 0
+							numeric_node	= 0
 
 					if not numeric_node:
-						count_mynodes = count_mynodes + 1
+
+						count_mynodes	= count_mynodes + 1
 					else:
 						try:
-							count_mynodes = count_mynodes + int( nodepart )
+							count_mynodes	= count_mynodes + int( nodepart )
+
 						except ValueError, detail:
+
 							debug_msg( 10, str( detail ) )
 							debug_msg( 10, "Encountered weird node in Resources_List?!" )
@@ -677,28 +690,28 @@
 							debug_msg( 10, 'attrs = ' + str( attrs ) )
 						
-				nodeslist = str( count_mynodes )
+				nodeslist	= str( count_mynodes )
 			else:
-				start_timestamp = ''
-				nodeslist = ''
-
-			myAttrs = { }
-			myAttrs['name'] = str( name )
-			myAttrs['queue'] = str( queue )
-			myAttrs['owner'] = str( owner )
-			myAttrs['requested_time'] = str( requested_time )
-			myAttrs['requested_memory'] = str( requested_memory )
-			myAttrs['ppn'] = str( ppn )
-			myAttrs['status'] = str( status )
-			myAttrs['start_timestamp'] = str( start_timestamp )
-			myAttrs['queued_timestamp'] = str( queued_timestamp )
-			myAttrs['reported'] = str( int( int( self.cur_time ) + int( self.timeoffset ) ) )
-			myAttrs['nodes'] = nodeslist
-			myAttrs['domain'] = string.join( socket.getfqdn().split( '.' )[1:], '.' )
-			myAttrs['poll_interval'] = str( BATCH_POLL_INTERVAL )
+				start_timestamp	= ''
+				nodeslist	= ''
+
+			myAttrs				= { }
+
+			myAttrs[ 'name' ]			= str( name )
+			myAttrs[ 'queue' ]		= str( queue )
+			myAttrs[ 'owner' ]		= str( owner )
+			myAttrs[ 'requested_time' ]	= str( requested_time )
+			myAttrs[ 'requested_memory' ]	= str( requested_memory )
+			myAttrs[ 'ppn' ]		= str( ppn )
+			myAttrs[ 'status' ]		= str( status )
+			myAttrs[ 'start_timestamp' ]	= str( start_timestamp )
+			myAttrs[ 'queued_timestamp' ]	= str( queued_timestamp )
+			myAttrs[ 'reported' ]		= str( int( int( self.cur_time ) + int( self.timeoffset ) ) )
+			myAttrs[ 'nodes' ]		= nodeslist
+			myAttrs[ 'domain' ]		= string.join( socket.getfqdn().split( '.' )[1:], '.' )
+			myAttrs[ 'poll_interval' ]	= str( BATCH_POLL_INTERVAL )
 
 			if self.jobDataChanged( self.jobs, job_id, myAttrs ) and myAttrs['status'] in [ 'R', 'Q' ]:
-				self.jobs[ job_id ] = myAttrs
-
-				#debug_msg( 10, printTime() + ' job %s state changed' %(job_id) )
+
+				self.jobs[ job_id ]	= myAttrs
 
 		for id, attrs in self.jobs.items():
@@ -711,4 +724,5 @@
 
 	def submitJobData( self ):
+
 		"""Submit job info list"""
 
@@ -719,20 +733,20 @@
 		for jobid, jobattrs in self.jobs.items():
 
-			gmetric_val = self.compileGmetricVal( jobid, jobattrs )
-
-			metric_increment = 0
+			gmetric_val		= self.compileGmetricVal( jobid, jobattrs )
+			metric_increment	= 0
 
 			for val in gmetric_val:
+
 				self.dp.multicastGmetric( 'MONARCH-JOB-' + jobid + '-' + str(metric_increment), val )
-				metric_increment = metric_increment + 1
+
+				metric_increment	= metric_increment + 1
 
 	def compileGmetricVal( self, jobid, jobattrs ):
+
 		"""Create a val string for gmetric of jobinfo"""
 
-		gval_lists = [ ]
-
-		mystr = None
-
-		val_list = { }
+		gval_lists	= [ ]
+		mystr		= None
+		val_list	= { }
 
 		for val_name, val_value in jobattrs.items():
@@ -748,4 +762,5 @@
 
 					if node_str:
+
 						node_str = node_str + ';' + node
 					else:
@@ -754,12 +769,16 @@
 					if (val_list_names_len + len(val_name) ) + (val_list_vals_len + len(node_str) ) > METRIC_MAX_VAL_LEN:
 
-						val_list[ val_name ] = node_str
+						val_list[ val_name ]	= node_str
+
 						gval_lists.append( val_list )
-						val_list = { }
-						node_str = None
-
-				val_list[ val_name ] = node_str
+
+						val_list		= { }
+						node_str		= None
+
+				val_list[ val_name ]	= node_str
+
 				gval_lists.append( val_list )
-				val_list = { }
+
+				val_list		= { }
 
 			elif val_value != '':
@@ -768,16 +787,18 @@
 
 					gval_lists.append( val_list )
-					val_list = { }
-
-				val_list[ val_name ] = val_value
-
-		if len(val_list) > 0:
+
+					val_list		= { }
+
+				val_list[ val_name ]	= val_value
+
+		if len( val_list ) > 0:
+
 			gval_lists.append( val_list )
 
-		str_list = [ ]
+		str_list	= [ ]
 
 		for val_list in gval_lists:
 
-			my_val_str = None
+			my_val_str	= None
 
 			for val_name, val_value in val_list.items():
@@ -794,4 +815,5 @@
 
 def printTime( ):
+
 	"""Print current time/date in human readable format for log/debug"""
 
@@ -799,4 +821,5 @@
 
 def debug_msg( level, msg ):
+
 	"""Print msg if at or above current debug level"""
 
@@ -808,11 +831,14 @@
 	# Write pidfile if PIDFILE exists
 	if PIDFILE:
-		pid = os.getpid()
-
-		pidfile = open(PIDFILE, 'w')
-		pidfile.write(str(pid))
+
+		pid	= os.getpid()
+
+		pidfile	= open(PIDFILE, 'w')
+
+		pidfile.write( str( pid ) )
 		pidfile.close()
 
 def main():
+
 	"""Application start"""
 
@@ -820,4 +846,5 @@
 
 	if not processArgs( sys.argv[1:] ):
+
 		sys.exit( 1 )
 
@@ -840,7 +867,9 @@
 	else:
 		debug_msg( 0, "fatal error: unknown BATCH_API '" + BATCH_API + "' is not supported" )
+
 		sys.exit( 1 )
 
 	if DAEMONIZE:
+
 		gather.daemon()
 	else:
