背景
weblogic可以通过console查看数据源运行时统计信息,如当前活动数,最大容量,最小容量等,如果Server数量庞大或者需要做数据源统计监控功能,那么需要通过程序定时获取数据源运行时信息,有两个方案可以获取到数据源运行时信息。
- 通过JMX获取weblogic运行时MBean信息,从MBean中获取数据源运行时信息。
- 通过jython脚本获取数据源运行时信息,这也是本文要介绍的方法。
wlst 脚本
基本思路就是通过连接服务器获取数据源运行时信息,通过csv格式输出到文件里,csv文件可以通过excel打开做统计分析,也可以将csv文件上传到服务器通过第三方程序进行统计分析。
import sys
url=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环境,建议在服务器上执行。
- 先进入到
$DOMAIN_HOME/bin/
目录,执行setDomainEnv.sh
脚本,设置weblogic环境
cd /u01/Middleware/user_projects/domains/portal_domain/bin
. ./setDomainEnv.sh
注意第二行脚本以
.
开头,如果没有这个点,设置不生效。
- 将脚本上传至服务器,通过wlst工具执行脚本
cd /home/oracle/scripts
java weblogic.WLST datasource.py localhost:7001 weblogic password
脚本需要指定console地址,管理员账号,管理员密码,脚本执行成功后,会在当前目录生成一个datasource.csv
文件,可以直接用excel打开,也可以将文件通过curl
命令上传至远程服务器。
统计多台服务器
如果要统计多台服务器,可以再写一个调用脚本,多次调用datasource.py
即可。
cd /u01/Middleware/user_projects/domains/portal_domain/bin
. ./setDomainEnv.sh
cd /home/oracle/scripts
java weblogic.WLST datasource.py localhost:7001 weblogic password
java weblogic.WLST datasource.py localhost:7002 weblogic password
java weblogic.WLST datasource.py localhost:7003 weblogic password
java weblogic.WLST datasource.py localhost:7004 weblogic password
java weblogic.WLST datasource.py localhost:7005 weblogic password
# ...
写在最后
jython属于偏小众的语言,网上资料较少,但用法和python大部分一致,如果查不到jython的资料可以试试查python。