首页 > 代码库 > 使用python脚本监控weblogic

使用python脚本监控weblogic

1.python的脚本如下:

  1 ###############################################################################  2 #created on 2013-07-09  3 #author : zhaolijun  4 #used to get weblogic  server runtime infomation  5 #wls_ver:weblogic 10.3.5.0  6 ###############################################################################  7   8 ###############################################################################  9 # parameters define 10 ############################################################################### 11 username=weblogic 12 password=isp902isp 13 url=t3://10.200.36.210:17101 14 LOOPS=3 15 IntervalTime=30000 16 FILEPATH="e:/logs/" 17 newline = "\n" 18 ############################################################################### 19 # define functions 20 ############################################################################### 21 def WriteToFile(ServerName, SubModule, LogString, LSTARTTIME, FILENAME): 22  23     if SubModule == "ServerCoreInfo": 24         HeadLineInfo = "DateTime,ServerName,ExecuteThreadIdleCount,StandbyThreadCount,ExecuteThreadTotalCount,busythread,HoggingThreadCount" 25     elif SubModule == "DataSourceInfo": 26         HeadLineInfo = "DateTime,ServerName,DataSourceName,ActiveConnectionsCurrentCount,CurrCapacity,WaitingForConnectionCurrentCount,WaitingForConnectionTotal" 27              28     if not os.path.exists(FILENAME):  29         print  "path not exist, create log file by self." 30         f = open(FILENAME, "a+") 31         f.write(HeadLineInfo + newline) 32         f.write(LSTARTTIME + "," + ServerName + "," + LogString + newline) 33         f.close() 34         f = None 35     else: 36         f = open(FILENAME, "a+")     37         f.write(LSTARTTIME + "," + ServerName + ","  + LogString + newline) 38         f.close() 39         f = None 40      41 def getCurrentTime(): 42     s=SimpleDateFormat("yyyyMMdd HHmmss") 43     currentTime=s.format(Date()) 44     return currentTime 45 def GetJdbcRuntimeInfo(): 46     domainRuntime() 47     servers = domainRuntimeService.getServerRuntimes(); 48     print  ******************DATASOURCE CONNECTION POOL RUNTIME INFORMATION******* 49     for server in servers:    50         print SERVER:  + server.getName(); 51         ServerName=server.getName() 52         jdbcRuntime = server.getJDBCServiceRuntime(); 53         datasources = jdbcRuntime.getJDBCDataSourceRuntimeMBeans(); 54         for datasource in datasources: 55             ds_name=datasource.getName() 56             print(-Data Source:  + datasource.getName() + , Active Connections:  + repr(datasource.getActiveConnectionsCurrentCount()) + , CurrCapacity:  + repr(datasource.getCurrCapacity())+ , WaitingForConnectionCurrentCount: +repr(datasource.getWaitingForConnectionCurrentCount())+ , WaitingForConnectionTotal: +str(datasource.getWaitingForConnectionTotal())); 57             FILENAME=FILEPATH + ServerName +"_"+ ds_name + "_"+ "DataSourceInfo" +".csv" 58             LSTARTTIME=getCurrentTime() 59             dsLogString=ds_name +,+ str(datasource.getActiveConnectionsCurrentCount())+,+repr(datasource.getCurrCapacity())+,+str(datasource.getWaitingForConnectionCurrentCount())+,+str(datasource.getWaitingForConnectionTotal()) 60             WriteToFile(ServerName, "DataSourceInfo", dsLogString, LSTARTTIME, FILENAME) 61              62 def GetThreadRuntimeInfo(): 63     domainRuntime() 64     servers=domainRuntimeService.getServerRuntimes(); 65     print  ******************SERVER QUEUE THREAD RUNTIME INFOMATION*************** 66     for server in servers: 67         print SERVER:  + server.getName() 68         ServerName=server.getName() 69         threadRuntime=server.getThreadPoolRuntime() 70         hoggingThreadCount = str(threadRuntime.getHoggingThreadCount()) 71         idleThreadCount = str(threadRuntime.getExecuteThreadIdleCount()) 72         standbycount = str(threadRuntime.getStandbyThreadCount()) 73         threadTotalCount = str(threadRuntime.getExecuteThreadTotalCount()) 74         busythread=str(threadRuntime.getExecuteThreadTotalCount()-threadRuntime.getStandbyThreadCount()-threadRuntime.getExecuteThreadIdleCount()-1) 75         print (-Thread : + idleThreadCount: + idleThreadCount+ ,standbycount:+standbycount+ , threadTotalCount: +threadTotalCount+ , hoggingThreadCount:+hoggingThreadCount+ ,busythread:+busythread) 76         FILENAME=FILEPATH + ServerName +"_"+ "ServerCoreInfo" +".csv" 77         LSTARTTIME=getCurrentTime() 78         serLogString=idleThreadCount+,+standbycount+,+threadTotalCount+,+busythread+,+hoggingThreadCount 79         WriteToFile(ServerName, "ServerCoreInfo", serLogString, LSTARTTIME, FILENAME) 80 ############################################################################### 81 ############ main  82 ###############################################################################                83 if __name__ == __main__:  84     from wlstModule import *#@UnusedWildImport 85 #import sys, re, os 86 #import java 87 from java.util import Date 88 from java.text import SimpleDateFormat 89 print starting the script .... 90 connect(username,password, url); 91 try: 92     for i in range(LOOPS) : 93          94         GetThreadRuntimeInfo() 95         GetJdbcRuntimeInfo() 96         java.lang.Thread.sleep(IntervalTime) 97          98 except Exception, e: 99     print e 100     dumpStack()101     raise 102 disconnect() 
ColletRuntime.py

2.将脚本放到weblogic的安装目录D:\weblogic\bea\wlserver_10.3\common\bin下
3.修改collectionRuntime.py中的weblogic的用户名,密码,IP,端口,日志路径修改成正确的数据。

4.打开CMD窗口,切换到D:\weblogic\bea\wlserver_10.3\common\bin下,然后执行命令wlst.cmd CollectRuntime.py

5.在日志路径下会看到自动生成的CSV文件。能看到HoggingThread和busyThread的数据。

监控gc执行情况的方法如下:

1. 首先需要安装jdk,在jdk/bin目录下,例如:C:\Program Files (x86)\Java\jdk1.6.0_10\bin

2. 打开CMD窗口,切换到jdk/bin目录下,使用命令jstat -gcutil 4556 5s 100 ,其中的4556为服务器的进程号。通过jconsole查询得出。

3. 在输出的结果中,可以看到full gc的输出结果。

至此,监控过程全部完成。

使用python脚本监控weblogic