weblogic创建domain 域 以及创建connectionpool 连接资源池

# 创建域
domainTemplate = 'C:/python/appserver/Oracle/Middleware/wlserver_10.3/common/templates/domains/wls.jar'
port = 7001
user = 'weblogic'
password = 'weblogic0'
domainName = 'python_domain'
jdkHome = 'C:/python/appserver/Oracle/Middleware/jdk160_29'
# Weblogic Domain Template 
# Use Basic Domain Template
readTemplate(domainTemplate)

# Config AdminServer Listen Address and Port
cd('Servers/AdminServer')
set('ListenAddress','')
set('ListenPort', port)

# Config username and password of Console User
cd('/')
# 'Security/base_domain/User/weblogic' The 'weblogic' is username
cd('Security/base_domain/User/'+ user)
cmo.setPassword(password)

# If the domain already exists, overwrite the domain
setOption('OverwriteDomain', 'true')
# Config home directory for the JVM to be used when starting the weblogic server
setOption('JavaHome',jdkHome )
# Config the Domain folder path
writeDomain('C:/python/appserver/Oracle/Middleware/user_projects/domains/'+ domainName)

# Close Template
closeTemplate()

# Exit script
exit()


#创建连接池
# Modify these values as necessary
url = '127.0.0.1:7001'
username = 'weblogic'
password = 'weblogic0'
dsName = 'mysql_ds'
dsJNDIName = 'jdbc/mysql'
targetName = 'AdminServer'
initialCapacity = 1
maxCapacity = 10
capacityIncrement = 1
driverName = 'com.mysql.jdbc.Driver'
driverURL = 'jdbc:mysql://localhost:3306/crawler'
driverUsername = 'root'
driverPassword = 'root'

# Connect to administration server
connect(username, password, url)

# Check if data source already exists
try:
    cd('/JDBCSystemResources/' + dsName)
    print 'The JDBC Data Source ' + dsName + ' already exists.'
    exit()
except WLSTException:
    pass

print 'Creating new JDBC Data Source named ' + dsName + '.'

edit()
startEdit()
cd('/')

# Save reference to target server
targetServer = getMBean('/Servers/' + targetName)

# Create data source
jdbcSystemResource = create(dsName, 'JDBCSystemResource')
jdbcResource = jdbcSystemResource.getJDBCResource()
jdbcResource.setName(dsName)

# Set JNDI name
jdbcResourceParameters = jdbcResource.getJDBCDataSourceParams()
jdbcResourceParameters.setJNDINames([dsJNDIName])
jdbcResourceParameters.setGlobalTransactionsProtocol('TwoPhaseCommit')

# Create connection pool
connectionPool = jdbcResource.getJDBCConnectionPoolParams()
connectionPool.setInitialCapacity(initialCapacity)
connectionPool.setMaxCapacity(maxCapacity)
connectionPool.setCapacityIncrement(capacityIncrement)

# Create driver settings
driver = jdbcResource.getJDBCDriverParams()
driver.setDriverName(driverName)
driver.setUrl(driverURL)
driver.setPassword(driverPassword)
driverProperties = driver.getProperties()
userProperty = driverProperties.createProperty('user')
userProperty.setValue(driverUsername)

# Set data source target
jdbcSystemResource.addTarget(targetServer)

# Activate changes
save()
activate(block='true')
print 'Data Source created successfully.'
exit()


#监控weblogic

# WLST WebLogic Server Monitoring Script
# Author: Madan M Noru
# This script can be used to monitor JVM, EXECUTE QUEUE, JDBC and JMS Resources 
# of all the WebLogic Instances running in a domain.

# NOTE: If you are unable to run this script, it could be an indentation problem. Please post your E-mail Id, I can send you the script.

# import pymysql
while True:
    import time
    print(time.strftime("%Y-%m-%d %X"))
    Now = time.localtime(time.time())
    timestr=time.strftime("%Y-%m-%d %X")
    print(timestr)
    print('=========================')
    username = 'weblogic'
    password = 'weblogic0'
    url = 't3://127.0.0.1:7001'
    urldict = {}
    connect(username, password, url)
    serverlist = adminHome.getMBeansByType('Server')
    print(serverlist)

    for svr in serverlist:
        # urldict['' + svr.getName() + ''] = str(svr.getListenPort())
        urldict[svr.getName()] = 't3://' + svr.getListenAddress() + ':' + str(svr.getListenPort())
    disconnect()
    # curTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    fjvm = open("jvm.txt","w")
    fjvm.truncate()
    fjvm.close()
    fpools = open("pools.txt","w")
    fpools.truncate()
    fpools.close()
    for svr, url in urldict.items():
        try:
            connect('weblogic', 'weblogic0', 't3://127.0.0.1:7001')
            jvmrtlist = home.getMBeansByType('JVMRuntime')
            print(jvmrtlist)
            print(' ')

            print('The Runtime Stats of Server: ' + svr)
            print(' ')
            print('JVM')
            print(' ')
            print('FreeJVM          usedJVM           totalJVM           persentJVM')
            print(' ')
            for jvmRT in jvmrtlist:
                freejvm = jvmRT.getAttribute("HeapFreeCurrent")
                usedjvm = jvmRT.getAttribute('HeapSizeCurrent')
                totaljvm = jvmRT.getAttribute("HeapSizeMax")
                persentjvm = jvmRT.getAttribute("HeapFreePercent")
                print(freejvm, ' ', usedjvm, ' ', totaljvm,' ',persentjvm)
                print(' ')
                print('------------------------------------------------')
                # 获取系统时间(只取分:秒)
                f = open("jvm.txt", "a")
                f.write(str(timestr)+','+str('base_domain')+','+str(svr)+','+str(freejvm)+','+str(usedjvm)+','+str(totaljvm)+','+str(persentjvm))
                f.write('\n')
                print('===============1')
                f.close()
                # time.sleep(5)

            # 注释掉部分为执行队列的监控
            #               eqrtlist=home.getMBeansByType('ExecuteQueueRuntime')
            #               print ' '
            #               print ' '
            #               print 'EXECUTE QUEUES'
            #               print ' '
            #               print 'ExecuteQueueName TotalCount CurrIdleCount PendRequestCurrCount ServicedRequestTotalCount'
            #               print ' '
            #               for eqRT in eqrtlist:
            #                       eqname = eqRT.getAttribute("Name")
            #                       eqtthreads = eqRT.getAttribute("ExecuteThreadTotalCount")
            #                       eqithreads = eqRT.getAttribute("ExecuteThreadCurrentIdleCount")
            #                       eqqc = eqRT.getAttribute("PendingRequestCurrentCount")
            #                       eqthrougp = eqRT.getAttribute("ServicedRequestTotalCount")
            #                       print eqname,' ',eqtthreads,' ',eqithreads,' ',eqqc,' ',eqthrougp
            #                       print ' '
            #
            poolrtlist = home.getMBeansByType('JDBCConnectionPoolRuntime')
            print(' ')
            print(' ')
            print('JDBC CONNECTION POOLS')

            print('Name Maxcapacity ActiveCurrent ActiveHighCount WaitSecondsHighCount WaitingCurrentCount State')
            print(' ')
            for poolRT in poolrtlist:
                pname = poolRT.getName()
                ptype = 'normal'
                pmaxcapacity = poolRT.getAttribute("MaxCapacity")
                paccc = poolRT.getAttribute("ActiveConnectionsCurrentCount")
                paverage=poolRT.getAttribute("ActiveConnectionsAverageCount")
                # pachc = poolRT.getAttribute("ActiveConnectionsHighCount")
                # pwshc = poolRT.getAttribute("WaitSecondsHighCount")
                # pwfccc = poolRT.getAttribute("WaitingForConnectionCurrentCount")
                pstate = poolRT.getAttribute("State")
                print(pname, ' ', pmaxcapacity, ' ', paccc,  '  ', pstate)
                print(' ')
                print('-------------------------------------------------------------')
                f = open("pools.txt", "a")
                f.write(str(timestr)+','+str(pname)+','+str(svr)+','+str(ptype)+','+str(pstate)+','+str(paccc)+','+str(pmaxcapacity))
                f.write('\n')
                f.close()
            domainRuntime()
            # cd('ServerRuntimes/'+svr+'/WorkManagerRuntimes/weblogic.kernel.Default')
            cd('ServerRuntimes/' + svr + '/ThreadPoolRuntime/ThreadPoolRuntime')
            # print(ls())

            print('-----------------------------------------------------------------')

            jmsrtlist = home.getMBeansByType('JMSDestinationRuntime')
            print(' ')
            print(' ')
            print('JMS DESTINATIONS')
            print(' ')
            print('Name ByteCurr Pending Received High MsgCurr Pending High Received ConsumersTotal')
            print(' ')
            for jmsRT in jmsrtlist:
                jmsname = jmsRT.getAttribute("Name")
                jmsbcc = jmsRT.getAttribute("BytesCurrentCount")
                jmsbpc = jmsRT.getAttribute("BytesPendingCount")
                jmsbrc = jmsRT.getAttribute("BytesReceivedCount")
                jmsbhc = jmsRT.getAttribute("BytesHighCount")
                jmsmcc = jmsRT.getAttribute("MessagesCurrentCount")
                jmsmpc = jmsRT.getAttribute("MessagesPendingCount")
                jmsmhc = jmsRT.getAttribute("MessagesHighCount")
                jmsmrc = jmsRT.getAttribute("MessagesReceivedCount")
                jmsctc = jmsRT.getAttribute("ConsumersTotalCount")
                print(jmsname, ' ', jmsbcc, ' ', jmsbpc, ' ', jmsbrc, ' ', jmsbhc, ' ', jmsmcc, ' ', jmsmpc, ' ', jmsmhc, ' ', jmsmrc, ' ',
                      jmsctc)
                print(' ')
                print('-------------------------------------------------------------------')
                # 获取系统时间(只取分:秒)
                disconnect()
        except:
            print("Skipping " + svr)
            continue
    java.lang.Thread.sleep(5000)

 

posted @ 2018-06-12 10:20  huanglei2010  阅读(790)  评论(0编辑  收藏  举报