Friday, October 22, 2010

Monitor your Weblogic Servers

As we have running SOA Suite, Service Bus or any other application on top of weblogic. You are intereseted in the behavior of the server. I have created a script that reads for all managed servers the following information:
  • Memory usage
  • Datasource connections
  • HTTP Session
  • Threads

The script is straight forward and appends each time a line in a result file. This can be used for furthe analyzing.

The property file:
# 
# Properties file
#
username=weblogic
password=welcome1
adminurl=t3://node1.vijfhuizen.com:7001
domain=MyDomain

#
# Show Sessions
#
showsession=true
appname=HelloWorldApplication
webname=/helloworld

# Show threads
showthreads=true

# Show memory
showmemory=true

# Show datasources
showdatasources=true

#
# Result file
#
appendresultsto=/tmp/wlmon.csv
The WLST script:
import traceback
import getopt

from java.util import Calendar

import getopt
import sys

# simulate command line invocation

propFile= file('wlsmon.properties', 'r')
propDict= dict()
for propLine in propFile:
  propDef= propLine.strip()
  if len(propDef) == 0:
        continue
  if propDef[0] in ( '!', '#' ):
        continue
  punctuation= [ propDef.find(c) for c in ':= ' ] + [ len(propDef) ]
  found= min( [ pos for pos in punctuation if pos != -1 ] )
  name= propDef[:found].rstrip()
  value= propDef[found:].lstrip(":= ").rstrip()
  propDict[name]= value

propFile.close()

# Connection properties
username = propDict['username']
password = propDict['password']
adminurl = propDict['adminurl']
domain   = propDict['domain']
appname  = propDict['appname']
webname  = propDict['webname']

# Switches
showsession     = propDict['showsession']
showthreads     = propDict['showthreads']
showmemory      = propDict['showmemory']
showdatasources = propDict['showdatasources']

# Open file in append mode
appendresultsto = propDict['appendresultsto']
f = open(appendresultsto,'a')

# Connect to Adminserver
connect(username, password, adminurl)

#Get number of servers from the domain config
domainConfig()
managedServers=cmo.getServers()

heap_string       = ''
session_string    = ''
datasource_string = ''
thread_string     = ''

#Get Runtime for our server
domainRuntime()
for managedServer in managedServers:
  servername=managedServer.getName()

  path = '/ServerRuntimes/' + servername
  cd(path)

  if cmo.getState() == 'RUNNING':

    #Get all running applications
    path='/ServerRuntimes/' + servername + '/JVMRuntime/' + servername
    cd(path)

    if showmemory == 'true':
      hpfree = str(cmo.getHeapFreeCurrent())
      hpfreepercent = str(cmo.getHeapFreePercent())
      hpsizecurrent = str(cmo.getHeapSizeCurrent())
      hpsizemax = str(cmo.getHeapSizeMax())
      uptime = str(cmo.getUptime())
 
      heap_string = uptime + ';' + hpsizemax + ';' + hpsizecurrent + ';' + hpfree + ';' + hpfreepercent
    
    path = '/ServerRuntimes/' + servername
    cd(path)

    if showsession == 'true':

      apps=cmo.getApplicationRuntimes()
      for app in apps:
      
        #We are intersted only on this application
        if app.getName() == appname:
    
          #Get all components in that application
          comps=app.getComponentRuntimes()
          for comp in comps:
              
            if comp.getName() == servername + '_/' + webname:
              #We are interested in only web components
              if comp.getType() == 'WebAppComponentRuntime':
                #Get all active sessions
                sessionids=comp.getServletSessionsMonitoringIds()
                session_string = str(len(sessionids))

    path='/ServerRuntimes/' + servername + '/JDBCServiceRuntime/' + servername
    cd(path)

    if showdatasources == 'true':
      dsMBeans = cmo.getJDBCDataSourceRuntimeMBeans()
      for ds in dsMBeans:        
        name                             = ds.getName()
        state                            = ds.getState()
        currcapacity                     = str(ds.getCurrCapacity())
        activeconnectionscurrentcount    = str(ds.getActiveConnectionsCurrentCount())
        activeconnectionshighcount       = str(ds.getActiveConnectionsHighCount())
        datasource_string = datasource_string + ';' + name + ';' + state + ';' + currcapacity + ';' + activeconnectionscurrentcount + ';' + activeconnectionshighcount 

    path='/ServerRuntimes/' + servername
    cd(path)

    if showthreads == 'true':
      opensocks = cmo.getOpenSocketsCurrentCount();

      path = '/ServerRuntimes/' + servername + '/ThreadPoolRuntime/ThreadPoolRuntime/'
      cd(path)

      executethreadtotal    = str(cmo.getExecuteThreadTotalCount())
      standbythreads        = str(cmo.getStandbyThreadCount())
      idlethreads           = str(cmo.getExecuteThreadIdleCount())
      pending               = str(cmo.getPendingUserRequestCount())
      hoggingthreads        = str(cmo.getHoggingThreadCount())
      serverthroughput      = str(cmo.getThroughput())

      thread_string = str(opensocks) + ';' + executethreadtotal + ';' + standbythreads + ';' + idlethreads + ';' + pending + ';' + hoggingthreads + ';' + serverthroughput

    cal = Calendar.getInstance()
    dt = cal.getTime()
    f.write(str(dt) + ';' + servername + ';' + heap_string + ';' + session_string + datasource_string + ';' + thread_string + '\n')

#Disconnect and exit
f.close()
disconnect()
exit()

Post a Comment