Tag: DataSource

Target and Untargeting of Multiple DataSources using WLST

Ravish Mody

Yesterday one of our subscribers Ravi Shankar Kumar had asked us using comment how to target and untarget multiple datasources in the same domian using a single generic WLST script, as they have total 5 clusters and want to target each cluster to different datasources and as per his requirement we created this simple WLST script which can do the same things in this single script.

As usual we have used a properties file which has all the required details in it like the datasources names, targets name and number of datasoures which has to be updated. This way you would not have to modify anything in your actual WLST script.

Target and Untargeting of Multiple DataSources using WLST

Step1). Create a Directory somewhere in your file system like : “C:WLSTDataSource

Step2). Write a Properties fileDsDetails.properties inside “C:WLSTDataSource” like following:

userName=weblogic
password=weblogic
admin.Url=t3://localhost:7001

####### DataSources names ######
ds.Name.1 = MyJDBC_Data_Source-1
ds.Name.2 = MyJDBC_Data_Source-2

##### DataSources Target names #####
target.1= Cluster
target.2= Cluster-0

##### Total Numbers of DataSources has to be modified  #####
total.Ds.Count=2

Step2). Now in the same directory write the following WLST Script “Tragate_UnTragate_DS.py” like following:

#############################################################################
#
# @author Copyright (c) 2010 - 2011 by Middleware Magic, All Rights Reserved.
#
#############################################################################

from java.io import FileInputStream

propInputStream = FileInputStream("DsDetails.properties")
configProps = Properties()
configProps.load(propInputStream)

userName = configProps.get("userName")
password = configProps.get("password")
adminUrl = configProps.get("admin.Url")
totalDsCount = configProps.get("total.Ds.Count")

connect(userName,password,adminUrl)
edit()
startEdit()
print ''
print '======================================================================'
print 'UnTargeting and Targeting of the DataSources has started.....'
print '======================================================================'

dsCount=1
while (dsCount <= int(totalDsCount)) :
	dsName = configProps.get("ds.Name."+ str(dsCount))
	tgName = configProps.get("target."+ str(dsCount))
	cd ('/JDBCSystemResources/'+ dsName)
	set('Targets',jarray.array([], ObjectName))
	print ''
	print 'DataSource = ', dsName ,', has been UnTargeted'
	set('Targets',jarray.array([ObjectName('com.bea:Name='+tgName+',Type=Cluster')], ObjectName))
	print 'Congrats!!! DataSource = ', dsName ,', now has been Targeted to "',tgName,'"'
	print ''
	dsCount = dsCount + 1

print '======================================================================'
print 'UnTrageting and Targeting of the DataSources has been completed !!!'
print '======================================================================'
print ''

activate()
exit()

Step3). Now Open a Command/Shell Prompt and then run the “setWLSEnv.sh” script to set the CLASSPATH and PATH environment variables. Run the “. ./setWLSEnv.sh” by adding two DOTs separated by a single space …..before the actual script like following : (use ‘cd’ command to move inside the <BEA_HOME>/wlserver_10.3/server/bin) then run the following command….
. ./setWLSEnv.sh

Note: Here The first DOT represents that set the Environment in the current Shell, AND the second ./ represents execute the script from the current directory.

Step4). Run the Above WLST Script like following:

java weblogic.WLST Tragate_UnTragate_DS.py

Following would be the Output


java weblogic.WLST Tragate_UnTragate_DS.py

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

Connecting to t3://localhost:7001 with userid weblogic ...
Successfully connected to Admin Server 'AdminServer' that belongs to domain 'Domain_7001'.

Warning: An insecure protocol was used to connect to the
server. To ensure on-the-wire security, the SSL port or
Admin port should be used instead.

Location changed to edit tree. This is a writable tree with
DomainMBean as the root. To make changes you will need to start
an edit session via startEdit().

For more help, use help(edit)

Starting an edit session ...
Started edit session, please be sure to save and activate your
changes once you are done.

======================================================================
UnTargeting and Targeting of the DataSources has started .....
======================================================================

DataSource =  MyJDBC_Data_Source-1 , has been UnTargeted
Congrats!!! DataSource =  MyJDBC_Data_Source-1 , now has been Targeted to " Cluster "

DataSource =  MyJDBC_Data_Source-2 , has been UnTargeted
Congrats!!! DataSource =  MyJDBC_Data_Source-2 , now has been Targeted to " Cluster-0 "

======================================================================
UnTargeting and Targeting of the DataSources has been completed !!!
======================================================================

Activating all your changes, this may take a while ...
The edit lock associated with this edit session is released
once the activation is completed.
Activation completed

Exiting WebLogic Scripting Tool.

Note:

Make sure that the Database is running while you are running the DataSource.

Regards,

Ravish Mody


Creating Multiple DataSource Using WLST with Properties File

Ravish Mody

Today, few hours back one of our subscriber  Kaushik had asked us that as per their requirement they need a WLST script which should get its details from a property file and create multiple datasource instead of only one, hence while watching the Quarter Final Cricket match between India Vs Australia I have created this script which would fulfil Kaushik’s and others subscribers requirements also. So make the best out of this WLST script and have fun with the cricket fever 😉

The best thing here is the administrator need to just edit the properties file with the datasource  details and the rest of the things will be taken care by the WLST Script.

Steps to Create Multiple DataSource Using WLST

Step1). Create a Directory somewhere in your file system like : “C:WLST_DS”

Step2). Write a Properties filedetails.properties inside “C:WLST_DS” like following:

domain.name=Domain_7001
admin.url=t3://localhost:7001
admin.userName=weblogic
admin.password=weblogic

total.DS=3

datasource.name.1=DS_1
datasource.database.name.1=demo
datasource.target.1=AdminServer
datasource.filename.1=DS_1.xml
datasource.jndiname.1=DS_1_JNDI
datasource.driver.class.1=com.pointbase.jdbc.jdbcUniversalDriver
datasource.url.1=jdbc:pointbase:server://localhost:9092/demo
datasource.username.1=PBPUBLIC
datasource.password.1=PBPUBLIC
datasource.test.query.1=SQL SELECT * FROM DUAL

datasource.name.2=DS_2
datasource.database.name.2=demo
datasource.target.2=AdminServer
datasource.filename.2=DS_2.xml
datasource.jndiname.2=DS_2_JNDI
datasource.driver.class.2=com.pointbase.jdbc.jdbcUniversalDriver
datasource.url.2=jdbc:pointbase:server://localhost:9092/demo
datasource.username.2=PBPUBLIC
datasource.password.2=PBPUBLIC
datasource.test.query.2=SQL SELECT * FROM DUAL

datasource.name.3=DS_3
datasource.database.name.3=demo
datasource.target.3=AdminServer
datasource.filename.3=DS_3.xml
datasource.jndiname.3=DS_3_JNDI
datasource.driver.class.3=com.pointbase.jdbc.jdbcUniversalDriver
datasource.url.3=jdbc:pointbase:server://localhost:9092/demo
datasource.username.3=PBPUBLIC
datasource.password.3=PBPUBLIC
datasource.test.query.3=SQL SELECT * FROM DUAL

Step2). Now in the same directory write the following WLST Script “createMultipleDataSource.py” like following:

#############################################################################
#
# @author Copyright (c) 2010 - 2011 by Middleware Magic, All Rights Reserved.
#
#############################################################################

from java.io import FileInputStream

propInputStream = FileInputStream("details.properties")
configProps = Properties()
configProps.load(propInputStream)

domainName=configProps.get("domain.name")
adminURL=configProps.get("admin.url")
adminUserName=configProps.get("admin.userName")
adminPassword=configProps.get("admin.password")

totalDataSource_to_Create=configProps.get("total.DS")

connect(adminUserName, adminPassword, adminURL)
edit()
startEdit()
print '========================================='
print 'Creating DataSource....'
print '========================================='
i=1
while (i <= int(totalDataSource_to_Create)) :

	try:
		cd('/')
		dsName=configProps.get("datasource.name."+ str(i))
		dsFileName=configProps.get("datasource.filename."+ str(i))
		dsDatabaseName=configProps.get("datasource.database.name."+ str(i))
		datasourceTarget=configProps.get("datasource.target."+ str(i))
		dsJNDIName=configProps.get("datasource.jndiname."+ str(i))
		dsDriverName=configProps.get("datasource.driver.class."+ str(i))
		dsURL=configProps.get("datasource.url."+ str(i))
		dsUserName=configProps.get("datasource.username."+ str(i))
		dsPassword=configProps.get("datasource.password."+ str(i))
		dsTestQuery=configProps.get("datasource.test.query."+ str(i))

		print ''
		print 'Creating DataSource: ',dsName,' ....'
		cmo.createJDBCSystemResource(dsName)
		cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName)
		cmo.setName(dsName)

		cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDataSourceParams/' + dsName )
		set('JNDINames',jarray.array([String('jdbc/' + dsName )], String))

		cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName )
		cmo.setUrl(dsURL)
		cmo.setDriverName( dsDriverName )
		cmo.setPassword(dsPassword)

		cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCConnectionPoolParams/' + dsName )
		cmo.setTestTableName(dsTestQuery)
		cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName + '/Properties/' + dsName )
		cmo.createProperty('user')

		cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName + '/Properties/' + dsName + '/Properties/user')
		cmo.setValue(dsUserName)

		cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName + '/Properties/' + dsName )
		cmo.createProperty('databaseName')

		cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName + '/Properties/' + dsName + '/Properties/databaseName')
		cmo.setValue(dsDatabaseName)

		cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDataSourceParams/' + dsName )
		cmo.setGlobalTransactionsProtocol('OnePhaseCommit')

		cd('/SystemResources/' + dsName )
		set('Targets',jarray.array([ObjectName('com.bea:Name=' + datasourceTarget + ',Type=Server')], ObjectName))

		print 'DataSource: ',dsName,', has been created Successfully !!!'
		print ''

	except:
		print '***** CANNOT CREATE DATASOURCE !!! Check If the DataSource With the Name : ' , dsName ,' Alreday exists or NOT...'
		print ''
	i = i + 1
print '========================================='
save()
activate()

Step3). Now Open a Command/Shell Prompt and then run the “setWLSEnv.sh” script to set the CLASSPATH and PATH environment variables. Run the “. ./setWLSEnv.sh” by adding two DOTs separated by a single space …..before the actual script like following : (use ‘cd’ command to move inside the <BEA_HOME>/wlserver_10.3/server/bin) then run the following command….
. ./setWLSEnv.sh


Note: Here The first DOT represents that set the Environment in the current Shell, AND the second ./ represents execute the script from the current directory.

Step4). Run the Above WLST Script like following:

java weblogic.WLST createMultipleDataSource.py

Make sure that the Database is running while you are creating the DataSource.

Following would be the Output

java weblogic.WLST createMultipleDataSource.py

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

Connecting to t3://localhost:7001 with userid weblogic ...
Successfully connected to Admin Server 'AdminServer' that belongs to domain 'Domain_7001'.

Warning: An insecure protocol was used to connect to the
server. To ensure on-the-wire security, the SSL port or
Admin port should be used instead.

Location changed to edit tree. This is a writable tree with
DomainMBean as the root. To make changes you will need to start
an edit session via startEdit().

For more help, use help(edit)
You already have an edit session in progress and hence WLST will
continue with your edit session.

Starting an edit session ...
Started edit session, please be sure to save and activate your
changes once you are done.
=========================================
Creating DataSource....
=========================================

Creating DataSource:  DS_1  ....
DataSource:  DS_1 , has been created Successfully !!!

Creating DataSource:  DS_2  ....
DataSource:  DS_2 , has been created Successfully !!!

Creating DataSource:  DS_3  ....
DataSource:  DS_3 , has been created Successfully !!!

=========================================
Saving all your changes ...
Saved all your changes successfully.
Activating all your changes, this may take a while ...
The edit lock associated with this edit session is released
once the activation is completed.
Activation completed

Regards,
Ravish Mody


Sending Email Alert for WebLogic DataSource Monitoring State & Connections Usages

Hi,

Jay SenSharma

Jay SenSharma

As almost in every production environment monitoring the resources is the most important thing. We have started a chain of posts related to getting Alerts whenever something goes wrong or the WebLogic Application Server starts behaving abnormal. In the current demo we are going to see how to get an E-Mail alert whenever the DataSource is not “Running”  or the “ActiveConnectionCurrentCount” crosses the limit specified by us in the  Properties file.

This script will monitor A perticular instanace of WebLogic Server and the dataSource which is targeted to that particular server.  This WLST script can be further enhanced to monitor more than 1 WebLogic Server Instance by putting the complete Script inside a for Loop. For Simplicity of the concept we are doing it only on a single Server.

Note: Below script can be executed as a Cron Job at a regular interval of lets say 10 minutes or 5 minutes based on the requirement.  This WLST Script is supported from WLS 9.x onwards .

Steps to Create an Email Alert for WebLogic DataSource Monitoring State & Connections Usages

Step1) Create a Directory somewhere in your file system like : “C:WLST”

Step2) Write a Properties file “details.properties” inside “C:WLST” like following:

#### Datasource.targetServer.url   Is the URL of the WebLogic Server in which the DataSource is Targeted.
datasource.targetServer.url=t3://10.10.10.10:9001
admin.username=weblogic
admin.password=weblogic

#### connectionPool.alert.limit    is the total number of Used Connections after which you want to get an alert
connectionPool.alert.limit=12
.

Step-3) Create a WLST Script somewhere in your file system with some name like “monitorDS.py” inside “C:WLST” contents will be something like following:

#############################################################################
#
# @author Copyright (c) 2010 - 2011 by Middleware Magic, All Rights Reserved.
#
#############################################################################
from java.io import FileInputStream
import java.lang
import os
import string

propInputStream = FileInputStream("details.properties")
configProps = Properties()
configProps.load(propInputStream)

targetServerURL = configProps.get("datasource.targetServer.url")
adminUser = configProps.get("admin.username")
adminPassword = configProps.get("admin.password")
connectionPoolAlertLimit = configProps.get("connectionPool.alert.limit")

#############  This method would send the Alert Email  #################
def sendMailString():
	os.system('/bin/mailx -s  "ALERT: Connection Pool Health is in WARNING !!! " abcd@company.com < commectionLimit_file')
	print ''

def checkConnectionUsage(activeConnectionsCurrentCount,dataSourceName,dataSourceState):
	dsStateSendMail="yes"
	dsLimitSendMail="yes"
	state=" DataSourceName: " + dataSourceName
	check = string.find(dataSourceState,"Running")
	print 'Zero 0 Means Running :  Check State ' , str(check)
	if check != 0:
		state = state + " Checking Connection Pool HealthState: " + dataSourceState
		print '!!!! ALERT !!!! Connection Pool Health is in Not OK Sending E-Mail Alert.'
	else:
		dsStateSendMail="no"

	if int(activeConnectionsCurrentCount) >= int (connectionPoolAlertLimit):
		state = state + " Connection Pool Is Crossing the Alert Limit ActiveConnectionsCurrentCount = " + str(activeConnectionsCurrentCount)
		print '!!!! ALERT !!!! Connection Pool Connection Pool Is Crossing The Alert Limit.'
		print ''
	else:
		dsLimitSendMail="no"

	checkData_A = string.find(dsStateSendMail,"yes")
	checkData_B = string.find(dsLimitSendMail,"yes")
	if  ((checkData_A==0) | (checkData_B==0)):
		cmd = "echo " + state +" > commectionLimit_file"
		os.system(cmd)
		sendMailString()
	else:
		print 'Every Thing in Pool Is OK For DataSource ', dataSourceName

#############  Main Execution @ Middleware Magic 2010 #################
connect(adminUser, adminPassword, targetServerURL)
serverRuntime()
dsMBeans = cmo.getJDBCServiceRuntime().getJDBCDataSourceRuntimeMBeans()
for ds in dsMBeans:
	print 'DS name is: '+ds.getName()
	print 'State is ' +ds.getState()
	dataSourceName=ds.getName()
	dataSourceState=ds.getState()
	# ActiveConnectionsCurrentCount: Means The number of connections currently in use by applications.
	activeConnectionsCurrentCount=ds.getActiveConnectionsCurrentCount()
	currCapacity=ds.getCurrCapacity()
	print 'DS ActiveConnectionsCurrentCount: ', activeConnectionsCurrentCount
	checkConnectionUsage(activeConnectionsCurrentCount,dataSourceName,dataSourceState)
	print '------------------------------------'

Step-4) Open a command prompt and then run the “setWLSEnv.cmd” or “setWLSEnv.sh” to set the CLASSPATH and PATH variables. Better you do echo %CLASSPATH% or echo $CLASSPATH to see whether the CLASSPATH is set properly or not. If you see an Empty Classpath even after running the “setWLSEnv.sh” then please refer to the Note mentioned at Step3) in the Following post:

http://middlewaremagic.com/weblogic/?page_id=1492

Step-5) Now run the WLST Script in the same command prompt like following:

<br />
java weblogic.WLST monitorDS_WORKING.py

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

Connecting to t3://10.10.10.10:9001 with userid weblogic ...
Successfully connected to Admin Server 'AdminServer' that belongs to domain 'base_domain'.

Warning: An insecure protocol was used to connect to the
server. To ensure on-the-wire security, the SSL port or
Admin port should be used instead.

Location changed to serverRuntime tree. This is a read-only tree with ServerRuntimeMBean as the root.
For more help, use help(serverRuntime)

DS name is: SQLAuthDS
State is Suspended
DS ActiveConnectionsCurrentCount:  0
Zero 0 Means Running :  Check State  -1
!!!! ALERT !!!! Connection Pool Health is in Not OK Sending E-Mail Alert.

-----
DS name is: PointBaseDataSource
State is Running
DS ActiveConnectionsCurrentCount:  12
Zero 0 Means Running :  Check State  0
!!!! ALERT !!!! Connection Pool Connection Pool Is Crossing The Alert Limit.

-----

NOTE: This script is using mailx (i.e. but Windows box does not have mailx utility) so please do check if your mailx is configured properly or else script would run properly but the mail would not be sent.

ConnectionPool LimitExceeded

ConnectionPool LimitExceeded

DataSource State

DataSource State

.

.

Thanks

Jay SenSharma


Copyright © 2010-2012 Middleware Magic. All rights reserved. |