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

If you enjoyed this post, please considerleaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.