WLST JDBC Monitoring


WLST script - jdbc monitoring

we are going to write a WLST script, which will connect to the running AdminServer and then will monitor the JDBC Datasources targeted on all managed servers. We are NOT going to perform any lookup on the AdminServer, but only on to the managed servers.

If there are any jdbc-datasources targeted specifically to the AdminServer, then, they will be ignored.

Downloads:

The following code can be downloaded from the following link:

1. The first step is to create a properties file and name it as domain.properties. We are going to add the values to this properties file and the WLST script is going to read the values from properties file and will take action.

#*****************************************************
# Security Info
#*****************************************************
ADMIN_USERNAME=weblogic
ADMIN_PASSWORD=welcome1
ADMIN_URL=t3://192.168.1.9:7001
DOMAIN_NAME=wl_server
#*****************************************************

2. In the next step we are going to write a utility to read the properties file in a given format

#! /usr/bin/python
#****************************************************************************
# File: utils.py
#****************************************************************************
from java.lang import System
#****************************************************************************
# def notNullOrEmpty()
#****************************************************************************
def notNullOrEmpty(val):
    return val != None and len(val) != 0
#****************************************************************************
# def substitute()
#****************************************************************************
def substitute(orig, subs):
    startIndex = orig.find('${')
    #print 'startIndex =', startIndex
    if startIndex == -1:
        return orig
        endIndex = orig.find('}', startIndex)
        #print 'endIndex =', endIndex
        if endIndex == -1:
            return orig
            substitutionKey = orig[startIndex + 2:endIndex]
            value = System.getProperty(substitutionKey)
            if subs.has_key(substitutionKey):
                value = subs[substitutionKey]
                if value != None:
                    return orig[0:startIndex] + value + substitute(orig[endIndex + 1:], subs)
                    if len(orig) == endIndex + 1:
                        print 'WARNING: No substitution found for key:', orig
                        return orig
                    else:
                        return orig[0:endIndex + 1] + substitute(orig[endIndex + 1], subs)
#****************************************************************************
# def loadOrderedProps
#****************************************************************************
def loadOrderedProps(filename):
    def split(line):
        index = line.find('=')
        if index == -1:
            return None
        else:
            return (line[:index], line[index + 1:])
    propLines = open(filename, 'r').readlines()
    propLines = [line.strip() for line in propLines]
    propLines = [line for line in propLines if len(line) > 0 and not line.startswith('#')]
    propLines = [split(line) for line in propLines]
    propLines = [line for line in propLines if line]
    return propLines
#****************************************************************************
# def loadProperties()
#****************************************************************************
def loadProperties(propFile):
    props = {}
    originalProps = loadOrderedProps(propFile)
    for key, value in originalProps:
        #print ' KEY: [' + key + ']'
        value = substitute(value, props)
        #print ' VALUE: [' + value + ']'
        props[key] = value
    return props
#****************************************************************************
# def getValue
#****************************************************************************
def getValue(propKey, defaultKey, properties):
    if properties.get(propKey) != None:
        val = properties[propKey]
    else:
        if properties.get(defaultKey) != None:
            val = properties[defaultKey]
        else:
            val = ''
    return val
#****************************************************************************
# def getKeyValue
#****************************************************************************
def getKeyValue(propkey, properties):
    if properties.get(propkey) != None:
        val = properties[propkey]
    else:
        val = ''
    return val
Code Formatted by ToGoTutor

3. This is the step where we are going to write the WLST online script to create a user and assign that user to a group

#! /usr/bin/python
#################################################
#
# Name : Create User WLST Script
# Author : Togotutor
# Copyright ©2012 www.togotutor.com
#
################################################
"""
Connection To Admin Server
"""
def __connectToAdmin(properties):
    adminusername = getKeyValue('ADMIN_USERNAME', properties)
    adminpassword = getKeyValue('ADMIN_PASSWORD', properties)
    domainname = getKeyValue('DOMAIN_NAME', properties)
    adminurl = getKeyValue('ADMIN_URL', properties)
    print '[INFO]: Connecting to the domain' + ' ' + domainname
    try:
        connect(adminusername, adminpassword, adminurl)
    except WLSTException:
        print '[ERROR]: Trying to connect to the domain:' + domainname
        exit()

def __monitorJDBCConnections():
    ## Getting the serverRuntime information
    getListOfServers = domainRuntimeService.getServerRuntimes();
    ## Checking for number of servers within the domain
    if (len(getListOfServers) > 0):
        print('[INFO] : Total Number of servers in this domain : --> ' + str(len(getListOfServers)))
        for srvrs in getListOfServers:
            ## Getting the Name of all Servers within a Domain
            serverName = srvrs.getName()
            print('[INFO] : Datasource to be monitored for the following servers : --> ' + str(serverName))
            ## If the serverName is AdminServer and DONOT monitor JDBC resources
            if serverName == 'AdminServer':
                print('[INFO] : AdminServer Datasource will not be monitored')
            else:
                ## If the serverName is NOT AdminServer then monitor JDBC resources.
                getDsRuntime = srvrs.getJDBCServiceRuntime();
                getjdbcdsMbean = getDsRuntime.getJDBCDataSourceRuntimeMBeans();
                if (len(getjdbcdsMbean) > 0):
                    print('==============================================================================================================')
                    for jdbcds in getjdbcdsMbean:
                        print('<> [Name : %s]' %(jdbcds.getName()))
                        print('      |----- <> [State : %s]' %(jdbcds.getState()))
                        print('      |            |----- <> [ActiveConnectionsAverageCount : %s]' %(jdbcds.getActiveConnectionsAverageCount()))
                        print('      |            |----- <> [ActiveConnectionsCurrentCount : %s]' %(jdbcds.getActiveConnectionsCurrentCount()))
                        print('      |            |----- <> [ActiveConnectionsHighCount    : %s]' %(jdbcds.getActiveConnectionsHighCount()))
                        print('      |            |----- <> [ConnectionDelayTime           : %s]' %(jdbcds.getConnectionDelayTime()))
                        print('      |            |----- <> [CurrCapacity                  : %s]' %(jdbcds.getCurrCapacity()))
                        print('      |            |----- <> [CurrCapacityHighCount         : %s]' %(jdbcds.getCurrCapacityHighCount()))
                        print('      |            |----- <> [FailedReserveRequestCount     : %s]' %(jdbcds.getFailedReserveRequestCount()))
                        print('      |            |----- <> [FailuresToReconnectCount      : %s]' %(jdbcds.getFailuresToReconnectCount()))
                        print('      |            |----- <> [HighestNumAvailable           : %s]' %(jdbcds.getHighestNumAvailable()))
                        print('      |            |----- <> [HighestNumUnavailable         : %s]' %(jdbcds.getHighestNumUnavailable()))
                        print('      |            |----- <> [LeakedConnectionCount         : %s]' %(jdbcds.getLeakedConnectionCount()))
                        print('      |            |----- <> [NumAvailable                  : %s]' %(jdbcds.getNumAvailable()))
                        print('      |            |----- <> [NumUnavailable                : %s]' %(jdbcds.getNumUnavailable()))
                        print('      |            |----- <> [PrepStmtCacheAccessCount      : %s]' %(jdbcds.getPrepStmtCacheAccessCount()))
                        print('      |            |----- <> [PrepStmtCacheAddCount         : %s]' %(jdbcds.getPrepStmtCacheAddCount()))
                        print('      |            |----- <> [PrepStmtCacheCurrentSize      : %s]' %(jdbcds.getPrepStmtCacheCurrentSize()))
                        print('      |            |----- <> [PrepStmtCacheDeleteCount      : %s]' %(jdbcds.getPrepStmtCacheDeleteCount()))
                        print('      |            |----- <> [PrepStmtCacheHitCount         : %s]' %(jdbcds.getPrepStmtCacheHitCount()))
                        print('      |            |----- <> [NumUnavailable                : %s]' %(jdbcds.getNumUnavailable()))
                        print('      |            |----- <> [PrepStmtCacheAccessCount      : %s]' %(jdbcds.getPrepStmtCacheAccessCount()))
                        print('      |            |----- <> [PrepStmtCacheMissCount        : %s]' %(jdbcds.getPrepStmtCacheMissCount()))
                        print('      |            |----- <> [WaitSecondsHighCount          : %s]' %(jdbcds.getWaitSecondsHighCount()))
                        print('      |            |----- <> [WaitingForConnCurrentCount    : %s]' %(jdbcds.getWaitingForConnectionCurrentCount()))
                        print('      |            |----- <> [WaitingForConnFailureTotal    : %s]' %(jdbcds.getWaitingForConnectionFailureTotal()))
                        print('      |            |----- <> [WaitingForConnHighCount       : %s]' %(jdbcds.getWaitingForConnectionHighCount()))
                        print('      |            |----- <> [WaitingForConnSuccessTotal    : %s]' %(jdbcds.getWaitingForConnectionSuccessTotal()))
                        print('      |            |----- <> [WaitingForConnectionTotal     : %s]' %(jdbcds.getWaitingForConnectionTotal()))
                    print('==============================================================================================================')
"""                     
DisConnect To Admin Server
"""
def __disconnectToAdmin():
    disconnect()


#****************************************************************************
# MAIN
#****************************************************************************
#
# Calling all the Methods here
print(' -------------------------- ')
print('[INFO] Printing the output')
print('')
execfile('utils.py')
propFilename = 'domain.properties'
properties = {}
properties = loadProperties(propFilename)
__connectToAdmin(properties)
__monitorJDBCConnections()
__disconnectToAdmin()

Output:

The output of the WLST script will be as follows:

wlst-script-jdbc-monitor
WLST monitor JDBC Datsource Script : Completed