wlst获取weblogic数据源运行时信息

背景weblogic可以通过console查看数据源运行时统计信息,如当前活动数,最大容量,最小容量等,如果Server数量庞大或者需要做数据源统计监控功能,那么需要通过程序定时获取数据源运行时信息,有两个方案可以获取到数据源运行时信息。 通过JMX获取weblogic运行时MBean信息,从MBean中获取数据源运行时信息。通过jython脚本获取数据源运行时信息,这也是本文要介绍的方法。 wlst 脚本基本思路就是通过连接服务器获取数据源运行时信息,通过csv格式输出到文件里,csv文件可以通过excel打开做统计分析,也可以将csv文件上传到服务器通过第三方程序进行统计分析。 import sysurl=sys.argv[1]username=sys.argv[2]password=sys.argv[3]connect(username,password,'t3://'+url)file=open("datasource.csv",'a')file.write("machine,server,Name,ActiveConnectionsAverageCount,ActiveConnectionsCurrentCount,ActiveConnectionsHighCount,ConnectionDelayTime,ConnectionsTotalCount,CurrCapacity,CurrCapacityHighCount,DeploymentState,FailedReserveRequestCount,FailuresToReconnectCount,HighestNumAvailable,HighestNumUnavailable,LeakedConnectionCount,NumAvailable,NumUnavailable,PrepStmtCacheAccessCount,PrepStmtCacheAddCount,PrepStmtCacheCurrentSize,PrepStmtCacheDeleteCount,PrepStmtCacheHitCount,PrepStmtCacheMissCount,Properties,ReserveRequestCount,State,WaitingForConnectionCurrentCount,WaitingForConnectionFailureTotal,WaitingForConnectionHighCount,WaitingForConnectionSuccessTotal,WaitingForConnectionTotal,WaitSecondsHighCount\n")allServers=domainRuntimeService.getServerRuntimes();if (len(allServers) > 0): for tempServer in allServers: jdbcServiceRT = tempServer.getJDBCServiceRuntime(); dataSources = jdbcServiceRT.getJDBCDataSourceRuntimeMBeans(); if (len(dataSources) > 0): for dataSource in dataSources: print "Server:" , tempServer print "Datasource:" , dataSource.getName() print "ActiveCount" , dataSource.getActiveConnectionsCurrentCount() file.write(url+",") file.write(str(tempServer.getName())+",") file.write(str(dataSource.getName())+",") file.write(str(dataSource.getActiveConnectionsAverageCount())+",") file.write(str(dataSource.getActiveConnectionsCurrentCount())+",") file.write(str(dataSource.getActiveConnectionsHighCount())+",") file.write(str(dataSource.getConnectionDelayTime())+",") file.write(str(dataSource.getConnectionsTotalCount())+",") file.write(str(dataSource.getCurrCapacity())+",") file.write(str(dataSource.getCurrCapacityHighCount())+",") file.write(str(dataSource.getDeploymentState())+",") file.write(str(dataSource.getFailedReserveRequestCount())+",") file.write(str(dataSource.getFailuresToReconnectCount())+",") file.write(str(dataSource.getHighestNumAvailable())+",") file.write(str(dataSource.getHighestNumUnavailable())+",") file.write(str(dataSource.getLeakedConnectionCount())+",") file.write(str(dataSource.getNumAvailable())+",") file.write(str(dataSource.getNumUnavailable())+",") file.write(str(dataSource.getPrepStmtCacheAccessCount())+",") file.write(str(dataSource.getPrepStmtCacheAddCount())+",") file.write(str(dataSource.getPrepStmtCacheCurrentSize())+",") file.write(str(dataSource.getPrepStmtCacheDeleteCount())+",") file.write(str(dataSource.getPrepStmtCacheHitCount())+",") file.write(str(dataSource.getPrepStmtCacheMissCount())+",") file.write(str(dataSource.getProperties())+",") file.write(str(dataSource.getReserveRequestCount())+",") file.write(str(dataSource.getState())+",") file.write(str(dataSource.getWaitingForConnectionCurrentCount())+",") file.write(str(dataSource.getWaitingForConnectionFailureTotal())+",") file.write(str(dataSource.getWaitingForConnectionHighCount())+",") file.write(str(dataSource.getWaitingForConnectionSuccessTotal())+",") file.write(str(dataSource.getWaitingForConnectionTotal())+",") file.write(str(dataSource.getWaitSecondsHighCount())) file.write("\n")运行脚本要执行wlst脚本,你需要有weblogic环境,建议在服务器上执行。 ...

October 1, 2019 · 1 min · jiezi

weblogic 脚本创建Managed Server (受管服务器)

说明weblogic安装目录下有一个创建Managed Server的脚本,脚本位于/u01/app/Oracle/Middleware/oracle_common/common/bin/config.sh下,但脚本会启动一个GUI界面程序,在Linux下需要安装图形界面程序,非常不方便。wlst(WebLogic Scripting Tools,WebLogic)是一个用来管理和配置weblogic的CLI命令行工具,可以运行Jython脚本,本文介绍如何通过该工具创建Managed Server。WLST介绍wlst位于/u01/app/Oracle/Middleware/wlserver_10.3/common/bin/wlst.sh目录下,其中/u01/app/Oracle/Middleware/wlserver_10.3目录为$WEBLOGIC_HOME,所以严谨的讲,是安装在$WEBLOGIC_HOME/common/bin/wlst.sh下。直接执行该脚本即可运行wlst工具。$ cd /u01/app/Oracle/Middleware/wlserver_10.3/common/bin/$ ./wlst.shInitializing WebLogic Scripting Tool (WLST) …Welcome to WebLogic Server Administration Scripting ShellType help() for help on available commandswls:/offline> connect(‘weblogic’,‘weblogic1’,’t3://localhost:7001’)Connecting to t3://localhost:7001 with userid weblogic …Successfully connected to Admin Server ‘AdminServer’ that belongs to domain ‘base_domain’.Warning: An insecure protocol was used to connect to theserver. To ensure on-the-wire security, the SSL port orAdmin port should be used instead.wls:/base_domain/serverConfig> cd(’/’)wls:/base_domain/serverConfig> ls()dr– AdminConsoledr– AppDeploymentsdr– BridgeDestinationsdr– Clustersdr– CoherenceClusterSystemResourcesdr– CoherenceServers….wlst有offline(离线)和online(在线)两种模式,通过connect命令可以从offline进入online,wlst按照liux目录形式对weblogic资源进行管理,甚至连操作的命令都和linux高度相似,比如cd是切换到指定资源路径下,ls()是列出该目录下所有资源。如要了解更多关于wlst的内容,可以查看官方文档。wlst脚本本脚本原作者为Tim Hall,本文这里稍作修改,以下为脚本代码 create_managed_server.py#!/usr/bin/python# Author : Tim Hall# Modified : Jianfeng.Zheng# Save Script as : create_managed_server.pyimport timeimport getoptimport sysimport re# Get location of the properties file.properties = ‘’try: opts, args = getopt.getopt(sys.argv[1:],“p:h::”,[“properies=”])except getopt.GetoptError: print ‘create_managed_server.py -p <path-to-properties-file>’ sys.exit(2)for opt, arg in opts: if opt == ‘-h’: print ‘create_managed_server.py -p <path-to-properties-file>’ sys.exit() elif opt in ("-p", “–properties”): properties = argprint ‘properties=’, properties# Load the properties from the properties file.from java.io import FileInputStream propInputStream = FileInputStream(properties)configProps = Properties()configProps.load(propInputStream)# Set all variables from values in properties file.adminUsername=configProps.get(“admin.username”)adminPassword=configProps.get(“admin.password”)adminURL=configProps.get(“admin.url”)msName=configProps.get(“ms.name”)msAddress=configProps.get(“ms.address”)msPort=configProps.get(“ms.port”)msMachine=configProps.get(“ms.machine”)# Display the variable values.print ‘adminUsername=’, adminUsernameprint ‘adminPassword=’, adminPasswordprint ‘adminURL=’, adminURLprint ‘msName=’, msNameprint ‘msAddress=’, msAddressprint ‘msPort=’, msPortprint ‘msMachine=’, msMachine# Connect to the AdminServer.connect(adminUsername, adminPassword, adminURL)edit()startEdit()# Create the managed Server.cd(’/’)cmo.createServer(msName)cd(’/Servers/’ + msName)cmo.setListenAddress(msAddress)cmo.setListenPort(int(msPort))# Associated with a node manager.cd(’/Servers/’ + msName)cmo.setMachine(getMBean(’/Machines/’ + msMachine))save()activate()disconnect()exit()properties文件脚本所需参数通过properties文件传入,以下是properties文件 api-api-managed-server.properties # AdminServer connection details.admin.username=weblogicadmin.password=weblogic1admin.url=t3://10.1.11.71:7001ms.name=api-serverms.address=0.0.0.0ms.port=7002ms.machine=api-server-machineadmin.username: weblogic管理员用户名admin.password: weblogic管理员密码admiin.url: weblogic控制台地址需要加上t3协议字段ms.name: managed server名称,可以自定义ms.address: managed server监听地址ms.port: managed server监听端口号ms.machine: managed server关联的计算机名称ms.machine需要提前创建好,可以在weblogic控制台页面http://localhost:7001/console/console.portal?_nfpb=true&_pageLabel=CoreMachineMachineTablePage创建运行将create_managed_server.py和api-api-managed-server.properties文件上传到服务器,这里上传到目录~/shell下$ cd /u01/app/Oracle/Middleware/user_projects/domains/base_domain/bin$ . ./setDomainEnv.sh$ java weblogic.WLST ~/shell/create_managed_server.py -p ~/shell/api-managed-server.properties ps:. ./setDomainEnv.sh第一个点(.)不能省略执行完毕后登录console查看结果。参考https://oracle-base.com/articles/web/wlst-create-managed-serverhttps://docs.oracle.com/cd/E13222_01/wls/docs90/config_scripting/using_WLST.html ...

February 18, 2019 · 2 min · jiezi