Adding WAS JVM Arguments

Adding JVM arguments in a safe way: update existing, adding new.

############################################################################
# Safely adding JVM arguments.
# New arguments are provided in the command line, for example:
#
# wsadmin -language=jython -f was_jvm.py \  
# JVM_ARGS="-Xgcpolicy:gencon -Xcompressedrefs" \ 
# JVM_RM_ARGS="-Xhealthcenter"
#
# if provided argument exists, it will be updated.
# if it does not exit, it will be added
############################################################################
 
DEBUG = 1
 
# default parameters
pars = { 
	'JVM_ARGS'     : '',
	'JVM_RM_ARGS'  : ''
 
}
 
 
#########################################################
# update defaults from command line
#########################################################
for arg in sys.argv:
	arr = arg.split("=", 1)
	if len(arr) > 1:
		pars[arr[0].upper()] = arr[1]
 
 
if DEBUG :
	for key in pars.keys():
		print key + " = '" + pars[key] + "'"
 
 
#########################################################
# Parse JVM Args
#########################################################
jvm_pars = {}
jvm_rm_pars = {}
 
for jvm_par in pars['JVM_ARGS'].split() :
	pair = jvm_par.split(":", 1)
	jvm_pars[pair[0]] = pair
 
for jvm_par in pars['JVM_RM_ARGS'].split() :
	pair = jvm_par.split(":", 1)
	jvm_rm_pars[pair[0]] = pair
 
if DEBUG:
	for key in jvm_pars.keys() :
		pair = jvm_pars[key]
		if len(pair) > 1 :
			print "Key='" + pair[0] + "', value='" + pair[1] + "'"
		else :
			print "Key='" + pair[0] + "'"
 
	for key in jvm_rm_pars.keys() :
		pair = jvm_rm_pars[key]
		if len(pair) > 1 :
			print "RM Key='" + pair[0] + "', value='" + pair[1] + "'"
		else :
			print "RM Key='" + pair[0] + "'"
# end if
 
#########################################################
# Java Virtual Machine configurations                   #
#########################################################
servers = AdminTask.listServers('[-serverType APPLICATION_SERVER ]')
if (servers != ['']):
	servers = servers [0:len(servers)].split(lineSeparator)
	for server in servers:
		serverName = AdminConfig.showAttribute(server, "name")
		print "\nSetting configuration for Application Server " + serverName
		jvm = AdminConfig.list('JavaVirtualMachine', server)
 
		#########################################################
		# Set recommended JVM args                              #
		#########################################################
		jvm_args = {}
		try:
			_excp_ = 0
			result = AdminConfig.showAttribute(jvm, 'genericJvmArguments')
			print "\nCurrent JVM Settings: " + result + "\n"
			newJvmSettings = []
			params = result.split()
			for param in params :
				pair = param.split(':', 1)
				key = pair[0]
				jvm_args[key] = 1
				if jvm_rm_pars.get(key) == None :
					if jvm_pars.get(key) == None :
						newJvmSettings.append(param)
					else :
						pair = jvm_pars[key]
						newParam = key
						if len(pair) > 1 :
							newParam += ":" + pair[1]
						# end if
						newJvmSettings.append(newParam)
					# end if
				# end if
			# end for
			for key in jvm_pars.keys() :
				if jvm_args.get(key) == None :
					# new argument has not been set yet
					# in it is not in the list of args to be removed
					if jvm_rm_pars.get(key) == None :
						pair = jvm_pars[key]
						newParam = key
						if len(pair) > 1 :
							newParam += ":" + pair[1]
						# end if
						newJvmSettings.append(newParam)
					# end if	
				# end if
			# end for
			newJvmSettingsString = " ".join(newJvmSettings)
			print "New JVM Settings: " + newJvmSettingsString + "\n"
 
			result = AdminConfig.modify(jvm, [['genericJvmArguments', newJvmSettingsString]])
			print "Set JVM args successfully"
		except:
			_type_, _value_, _tbck_ = sys.exc_info()
			result = `_value_`
			_excp_ = 1
		#endTry 
		if (_excp_ ):
			print "Error setting JVM args"
			print "Error = "+result
		#endIf
	#endFor
#endIf
 
# save configuration
AdminConfig.save()

 

Leave a Reply

Your email address will not be published. Required fields are marked *