Tuesday, August 3, 2010

Configure Email Notification in WebLogic Server

Introduction

If you are administering applications deployed on WebLogic Server or if you are operations personal responsible to manage a WebLogic Server environment you might have at least asked the following question once:

“Can I setup WebLogic Server to send automatic notifications?”

People always ask me whether WebLogic Server can send automatic notifications. My answer is - "Yes, WebLogic Server had always supported SNMP which can be configured to do standard SNMP Trap notifications to your enterprise monitoring system which can then be sent as an email or pager out to administrators". But the next question is - "I don't want to set up SNMP and I want WebLogic Server to directly send notifications. Can we do that?" Again the answer is - "Yes, starting from version 9.x WebLogic Server supports a new framework called WebLogic Diagnostic Framework - WLDF, which can be used for this purpose". Enterprise monitoring solutions like Oracle Enterprise Manager, HP Open View can monitor WebLogic Server from outside and can trigger notification among various other management and monitoring features. But you can also set up WebLogic Server to directly send many types of notifications. Let us see how to simply set up an email notification with WLS in this article.

Oracle WebLogic Server is Java EE certified application server which is the foundation of many Service Oriented Architecture (SOA) implementations. So many enterprises have their mission-critical applications deployed on WebLogic Server. WebLogic provides a scalable and highly available environment. If you are deploying your mission critical applications on Oracle WebLogic Server you can make use of WebLogic Diagnostic Framework (WLDF) to get diagnostic data from WebLogic Server instances and from applications deployed to them. This will help administrator and system operators to see more information from under the hoods.

This powerful feature will play a significant role in production environment where the application environment should alert operational personal when there is a performance or service level violation. This also eliminates the need to setup custom or third-party monitoring solutions for alerting and notification.

WebLogic Diagnostics Framework – Watch & Notification

One of the features of WLDF is Watch and Notification. You can configure server(s) with watch condition and respective notification(s) to be sent if the watch conditions are met. For example, if the number of request waiting on a JDBC Data Source to grab a connection exceeds 10 and if the free heap in the server is less than 5% then you can trigger a notification. The notification can be one or more of the following types:

  • JMX Notification - Application(s) can register notification listener with WLDF to receive notifications.
  • JMS Notification - WLDF can post a message to JMS destination to alert the situation which can be consumed by a message listener or MDBs to further processing.
  • SNMP Notification - SNMP traps can be sent to alert SNMP managers.
  • SMTP Notification - Email notifications through a Java Mail session.
  • Image Notification - Generates a server image which contains information from different subsystem during the watch.

Figure: 1 WebLogic Domain


You can configure Watch conditions on the MBean data, Log messages and WLDF Event data. The notification can be one or more of the above notifications. Since email notification is a common request, I shall illustrate how to setup WLS to send email notification using an open source SMTP server - The Apache Java Enterprise Mail Server (Apache James). To download Apache James, go to - http://james.apache.org/server/ [or] http://people.apache.org/dist/james/server/binaries/. Download and extract the zip file to a convenient location (Eg. /usr/james-2.3.1rcl/). Start the server using run.sh from the bin folder under the JAMES_HOME. Setup a test user in James as per these instructions - http://james.apache.org/server/2.3.1/adding_users.html. The default admin username and password for James are root/root.

Ensure that the James mail server and the administration server for the domain are started. If not, use DOMAIN_HOME/startWebLogic.cmd/sh for starting the administration server of your domain and use JAMES_HOME/bin/run.bat/sh to start James Server. You can use console or WLST to configure the following setting. Let us use the web-based administration console for configuration. Log in to WebLogic Administration Console at - http://localhost:7001/console using an administrative account.

Creating JavaMail Session

First step is to make sure that the underlying resources for the notification are setup. In this case we are going to use SMTP notification so let us first create the Java Mail Session to connect to James Mail Server.

  1. Click "Lock & Edit" to acquire a configuration lock
  2. Expand Services > Mail Sessions and click "New" to create a new Java Mail Session.
  3. Provide Name, JNDI Name and JavaMail Properties as follow:
  4. Name - James-MailSession
  5. JNDI Name - com.test.myjamesMailSession
  6. JavaMail Properties:

mail.transport.protocol=smtp

mail.smtp.host=localhost

mail.smtp.user=admin

mail.smpt.password=password

  1. Click next to target the mail session to a server (Eg. AdminServer or mgd_s1) and click finish.
  2. Activate the changes by clicking "Activate Changes".


Figure: 2 Configuring JavaMail Session


If you are using a different mail server use appropriate information.

Creating WLDF Module

Next step in configuring watch and notification is to create a diagnostic module and target it to the server. You can use console or WLST to create a diagnostic module. The following steps illustrate the steps to create a WLDF system module:

  1. Click "Lock & Edit" to acquire a configuration lock
  2. Click Diagnostics > Diagnostics Modules and click "New" to create a new diagnostics module. Provide a name and description.
  3. Name - Test_WLDF_Module
  4. Description - This is a WLDF module for testing email notifications

  1. Select the newly created module and select targets tab. Select the appropriate server for target as before
  2. Click "Save" and "Activate"



Figure: 3 Configuring Diagnostic Module

Creating Watch & Notification

Each WebLogic Server instance can be configured with only one diagnostic module but you can target the same diagnostic module to multiple servers or clusters. Once the WLDF system module is created and targeted to the appropriate server then watches and notifications can be configured. The following steps illustrate how to create a Watch and a Notification.

To create a watch:

  1. Click "Lock & Edit" to acquire a configuration lock
  2. Navigate to the diagnostic module created above (Test_WLDF_Module) and select the "Watches and Notifications" tab and "Watches" sub-tab (if not already selected)
  3. Click "New" to create a new Watch. Provide the name and select the type as "Collected Metrics" [Select "Collected Metrics" for inspecting Runtime MBean values, "Server Log" to watch log data and "Event Data" to watch instrumented data]
  4. Name - Heap_Watch
  5. Watch Type - Collected Metrics
  6. Enable Watch - Selected
  7. Click "Next"
  8. Click "Add Expressions"
  9. Ensure that "ServerRuntime" is selected and click "Next"
  10. If you are running WebLogic Server on Java HotSpot VM, make sure "Select an MBean Type from the following list" is selected and select "weblogic.management.runtime.JVMRuntimeMBean" and click "Next". If you are using JRockit VM, make sure "Select an MBean Type from the following list" is selected and select "weblogic.management.runtime. JRockitRuntimeMBean" and click "Next".
  11. Select the instance for the appropriate server from the list for "Instance" and click "Next"
  12. For "Message Attribute" select "HeapFreePercent", "<" for "Operator" and type "85" for "Value". Click "Finish" twice to create a Watch
  13. Activate the changes



Figure: 4-a Configuring Watch



Figure: 4-b Configuring Watch


The above watch will trigger the configured notification if the percentage of free heap is less than 90. You could make complex rule expression by combining multiple attributes from different MBean. For now we haven't configured any notification for this watch. We will create the notification and will associate it to the watch in the following steps.

To create a notification:

  1. Click "Lock & Edit" to acquire a configuration lock
  2. Navigate to the diagnostic module created above and select the "Watches and Notifications" tab and "Notification" sub-tab
  3. Click "New" to create a new Notification
  4. Select "SMTP (E-Mail) for Type and click "Next"
  5. Provide a name - James_Email_Notification. Make sure the notification is enabled and click "Next".
  6. Configure the following properties for the "Config Notification - SMTP Properties" page
  7. Mail Session Name - James-MailSession
  8. E-Mail Recipients - admin@localhost

  1. Click "Finish".
  2. Activate the changes


Figure: 5-a Configuring SMTP Notification


Figure: 5-b Configuring SMTP Notification


To associate the watch and the notification:

  1. Click "Lock & Edit" to acquire a configuration lock
  2. Navigate to the diagnostic module created above (Test_WLDF_Module) and select the "Watches and Notifications" tab and "Watches" sub-tab (if not already selected)
  3. Select the watch you created earlier - Heap_Watch
  4. Select the "Notifications" tab and move the "James_Email_Notification" from Available to Chosen
  5. Click "Save"
  6. Select the "Alarms" tab and select "Use an automatic reset alarm". Set the "Automatic reset period" to 6 and click "Save". This will ensure that the notifications are at least 90 seconds apart.
  7. 5. Click "Activate Changes"

Figure: 6-a Associating Watch and Notification

Figure: 6-b Configuring Alarm

As we configured the watch type to be Collected Metrics, the attributes involved in the watch rule will be harvested and tested for watch rule expression(s). The default sampling period for the harvester is 300,000 milliseconds (or 5 minutes). So the time between samples will be 5 minutes. To change the sampling period for metrics collection:

  1. Click "Lock & Edit" to acquire a configuration lock
  2. Navigate to the diagnostic module created above (Test_WLDF_Module) and select the "Collected Metrics" tab
  3. Ensure that "Enabled' check-box is selected
  4. Change the "Sampling Period" as "120000" so that it will collect samples once every two minute


Figure: 7 Configuring Sampling Period


To receive the notifications sent by WebLogic Server as emails, install and configure an email client like Mozilla Thunderbird. The following is an example of the email received from WebLogic Server:

Using WLST

The configurations that are illustrated using Administration Console here can also be done using WLST. The following script is an example WLST script to configure a Javamail session, WLDF Module, Watch and Notification:




#Connect to the admin sever
connect('weblogic','Welcome1','t3://localhost:7001')

#Start a change session
edit()
startEdit()
cd('/')

#Create and configure a Javamail session
cmo.createMailSession('James-MailSession')
cd('/MailSessions/James-MailSession')
cmo.setJNDIName('com.test.myjamesMailSession')
cmo.setProperties({mail.smtp.user=user;, mail.smpt.password=password;, mail.smtp.host=localhost;, mail.transport.protocol=smtp;})
set('Targets',jarray.array([ObjectName('com.bea:Name=AdminServer,Type=Server')], ObjectName))

#Create and configure a WLDF Module
cd('/')
cmo.createWLDFSystemResource('Test_WLDF_Module')
cd('/SystemResources/Test_WLDF_Module')
cmo.setDescription('This is a WLDF module for testing email notifications.')
set('Targets',jarray.array([ObjectName('com.bea:Name=AdminServer,Type=Server')], ObjectName))

#Create and configure a WLDF Watch
cd('/WLDFSystemResources/Test_WLDF_Module/WLDFResource/Test_WLDF_Module/WatchNotification/Test_WLDF_Module')
cmo.createWatch('Heap_Watch')
cd('/WLDFSystemResources/Test_WLDF_Module/WLDFResource/Test_WLDF_Module/WatchNotification/Test_WLDF_Module/Watches/Heap_Watch')
cmo.setRuleType('Harvester')
cmo.setEnabled(true)
#For HotSpot VM
#cmo.setRuleExpression('(${ServerRuntime//[weblogic.management.runtime.JVMRuntimeMBean]com.bea:Name=AdminServer,ServerRuntime=AdminServer,Type=JVMRuntime//HeapFreePercent} < 85)')
#For JRockit VM
cmo.setRuleExpression('(${ServerRuntime//[weblogic.management.runtime.JRockitRuntimeMBean]com.bea:Name=AdminServer,ServerRuntime=AdminServer,Type=JRockitRuntimeMBean//HeapFreePercent} < 85)')
cmo.setAlarmType(None)

#Create and configure a WLDF SMTP Notification
cd('/WLDFSystemResources/Test_WLDF_Module/WLDFResource/Test_WLDF_Module/WatchNotification/Test_WLDF_Module')
cmo.createSMTPNotification('James_Email_Notification')
cd('/WLDFSystemResources/Test_WLDF_Module/WLDFResource/Test_WLDF_Module/WatchNotification/Test_WLDF_Module/SMTPNotifications/James_Email_Notification')
cmo.setEnabled(true)
cmo.setMailSessionJNDIName('com.test.myjamesMailSession')
set('Recipients',jarray.array([String('admin@localhost')], String))
cmo.setSubject(None)
cmo.setBody(None)

#Associate the watch and notification
cd('/WLDFSystemResources/Test_WLDF_Module/WLDFResource/Test_WLDF_Module/WatchNotification/Test_WLDF_Module/Watches/Heap_Watch')
set('Notifications',jarray.array([ObjectName('com.bea:Name=James_Email_Notification,Type=weblogic.diagnostics.descriptor.WLDFSMTPNotificationBean,Parent=[testdomain]/WLDFSystemResources[Test_WLDF_Module],Path=WLDFResource[Test_WLDF_Module]/WatchNotification[Test_WLDF_Module]/SMTPNotifications[James_Email_Notification]')], ObjectName))
cmo.setAlarmType('AutomaticReset')
cmo.setAlarmResetPeriod(60000)

#Set the sampling period
cd('/WLDFSystemResources/Test_WLDF_Module/WLDFResource/Test_WLDF_Module/Harvester/Test_WLDF_Module')
cmo.setSamplePeriod(120000)
cmo.setEnabled(true)

#Activate the changes
save()
activate()

#Exit
exit()

Thursday, July 1, 2010

Using WLST Script To List Messages From A JMS Queue

WebLogic Server added support for Runtime Message Management for Destination hosted on WebLogic JMS Servers in WLS 9.0. It will an exciting feature if you are currently in WLS 8.1 or new to WLS. Now you can administratively view and browse all messages, and manipulate most messages in a running JMS server, by using the Administration Console or WLS public runtime APIs. Once I start getting excited about this feature to customer the very next logical question from administrators are can I script this? The answer is obviously, Yes. Java or WLST is your answer. If you are a developer and there is a requirement to provide a Java/JEE application for your administrators or business users to view or browse the messages then Java will be your option. For administrators who are non-Java, there are MBeans like JMSDestinationRuntimeMBean and JMSDurableSubscriberRuntimeMBean that you can use from WLST to script any of the message managment features. Needless to say if there are existing Java implementation for message management then you can simply import and use them within WLST as well. But WLST scripts are natural choice for administrators.

So I started out writing a simple WLST script that can list all the messages from a given JMS Queue. The script is written to list all the headers and properties along with the body. I have made enough comments in the script to explain the different steps. But the high-level steps are:
  1. Connect to the server
  2. Get to the JMSDestinationRuntimeMBean
  3. Get the cursor to find the number of messages
  4. Get the messages without body to find the cursor and the Message ID
  5. Get the message with body using the Message ID
  6. Print the Headers, Properties and Body (or) Pay Load of the Message

A key information to note here is if you have any Object Messages in the Queue then those classes should be added to the Java Classpath before you execute this WLST Script. I haven't implemented any exception handling logic in my script whereas handling exceptions are best practices and you might want to refer to my other entries regarding that information.


#Import necessary classes/interfaces
from weblogic.jms.extensions import JMSMessageInfo
from javax.jms import TextMessage
from javax.jms import ObjectMessage

#Define constants
url='t3://localhost:7001'
username='weblogic'
password='weblogic1'
jmsservername='test-jms-server'
jmsmodulename='test-jms-module'
jmsdestname='test-queue'

#Connect
connect(username,password,url)

#Switch to the server runtime tree
serverRuntime()

#Navigate to the JMS Destination Runtime MBean
cd('JMSRuntime/' + serverName + '.jms/JMSServers/' + jmsservername)
cd('Destinations/' + jmsmodulename + '!' + jmsdestname)

#Get the cursor (JMSMessageCursorRuntimeMBean) to browse the messages - No selector & No time out
cursor = cmo.getMessages('',0)

#Determine the number of messages in the destination
cursorsize = cmo.getCursorSize(cursor)
print '------------------------------------------'
print 'Total Number of Messages -> ', cursorsize
print '------------------------------------------'

#Get all the messages as an array of javax.management.openmbean.CompositeData
messages = cmo.getNext(cursor, cursorsize)

#Loop through the array of messages to print
for message in messages:

#Create WebLogic JMSMessageInfo to get Message ID
jmsmsginfo = JMSMessageInfo(message)
wlmsg = jmsmsginfo.getMessage()
wlmsgid = wlmsg.getJMSMessageID()

#Get Message with body
fullcursormsg = cmo.getMessage(cursor,wlmsgid)
fulljmsmsginfo = JMSMessageInfo(fullcursormsg)
handle = fulljmsmsginfo.getHandle()
compdata = cmo.getMessage(cursor, handle)
msgwithbody = JMSMessageInfo(compdata)

#Print Key Message Headers
print 'Message ID - ' + msgwithbody.getMessage().getJMSMessageID()
print 'Message Priority -' , msgwithbody.getMessage().getJMSPriority()
if msgwithbody.getMessage().getJMSRedelivered() == 0:
redeliv = 'false'
else:
redeliv = 'true'
print 'Message Redelivered - ' + redeliv
print 'Message TimeStamp -' , msgwithbody.getMessage().getJMSTimestamp()
print 'Message DeliveryMode -' , msgwithbody.getMessage().getJMSDeliveryMode()

#Print Message Properties
prop_enum = msgwithbody.getMessage().getPropertyNames()
print ' '
print 'Message Properties :'
print ' '
for prop in prop_enum:
print prop + ' - > ' + msgwithbody.getMessage().getStringProperty(prop)

#Print Message Body
fullwlmsg = fulljmsmsginfo.getMessage()
print ' '
print 'Message Body :'
print ' '
if isinstance(fullwlmsg, TextMessage):
print fullwlmsg.getText()
else:
if isinstance(fullwlmsg, ObjectMessage):
print fullwlmsg.getObject()
else:
print '***Not a Text or Object Message***'
print fullwlmsg.toString()
print ' '
print '--------------------------------------------------------------'
print ' '

#Close cursor as No Time Out specified - Best practice
cmo.closeCursor(cursor)

#Disconnect & Exit
disconnect()
exit()

Wednesday, June 16, 2010

Running OS Commands From WLST


If you spend a lot of time with WLST in the interactive mode, I am sure you have opened another command prompt/shell to check something at the OS file system level. If you wonder whether can execute an OS command from within WLST prompt, the answer is "Yes". This is very convenient similar to how you can execute OS commands from within "Vi" editor.

In fact this feature is not from WLST but from underlying Jython. There is "os" module in Jython that provides a unified interface to a number of operating system functions. There is "system" function in the "os" module that can take a OS command as a string input and returns either "0" or "1" depends on whether the command was successful or failed. So this feature can also be used with scripting mode where the return code can be verified for further processing in the script.

For eg. os.system(r'dir c:\bea') can list the contents of the bea folder under c drive. Notice the use of the preceding 'r' for escaping the entire string.

Thursday, April 22, 2010

Effective HTTP Session Monitoring in WebLogic Server With monitoring-attribute-name

Monitoring HTTP Session on WebLogic Server is one of the common task for administrators when monitoring web applications for performance or for activity or usage. Web application modules deployed to WebLogic Server maintains a "monitoring ID", which although looks like a session ID is not the same as the real session ID. You can use the monitoring id to identify the session uniquely. Monitoring IDs are not same as session IDs for security reason. You can turn on session debugging which prints session IDs to the log but these will not match what you see in the console/WLST. Monitoring ids are randomly generated by default. You can set a value to this monitoring id through a session attribute using "monitoring-attribute-name" element in session-descriptor of the weblogic.xml. This can simplify the task of monitoring sessions for administrators in production environment.

For e.g. if you set the "monitoring-attribute-name" to be "user-name" then it will use the string value of the session attribute named "user-name" as the value for Monitoring ID instead of the randomly generated value. When getMonitoringID(sessionid) method is called on the ServletSession, it will return session.getAttribute("user-name").toString().


weblogic.xml
-------------
...

600
user-name



true

...



So if you have an attribute within the HTTP Session which can identify the user/session uniquely then you can use that as the monitoring id. This is the recommended approach to track/monitor sessions instead of the session id which is an internal attribute. If you don't set the monitoring-attribute-name, WLS will randomly generate a monitoring id for each session. When developers set the monitoring-attribute-name, this will help administrator to associate sessions to the actual users.

Sample WLST script to get all the monitoring ids (for the respective session) as an array:


...
AppName = 'MedRecEar'
TargetServerName = 'MedRecSvr1'
WebCompName = 'MedRecWar'
serverRuntime()
cd('ApplicationRuntimes/' + AppName + '/ComponentRuntimes/' + TargetServerName + '_/' + WebCompName) MIDs = mon_ids = cmo.getServletSessionsMonitoringIds()
...


In the above example, the variable mon_ids will contain an array of monitoring ids as strings. Using these monitoring ids information about the individual session can be obtained from other operations like:
- getSessionLastAccessedTime
- getSessionMaxInactiveInterval

One can even invalidate the session using the following opertion:
- invalidateServletSession

This will give an option for administrators to cleanup or delete stale sessions if there are any. These are the only information that are exposed for sessions unless you want to use the deprecated ServletSessionRuntimeMBeans. This is an old MBean that has more methods/operations. Admin console doesn't expose the session id for security reason. If you need access to all the session ids for some reason then you have to use this deprecated ServletSessionRuntimeMBeans. You can get all the ServletSessionRuntimeMBeans as an array from a WebAppComponentRuntimeMBean using a deprecated getServletSessions() method. WLST script to access the above mentioned deprecated ServletSessionRuntimeMBeans:


...
AppName = 'MedRecEar'
TargetServerName = 'MedRecSvr1'
WebCompName = 'MedRecWar'
serverRuntime()
cd('ApplicationRuntimes/' + AppName + '/ComponentRuntimes/' + TargetServerName + '_/' + WebCompName + '/ServletSessions')
ls()
....


The above ls() command will list you all the sessions with session id as their name. Even though this post has lot of information on how to monitor sessions and make use of the monitoring-attribute-name, I shall write another to provide a complete example of how to use this.

Also see my other post on how to monitor HTTP sessions from the administration console and WLST.

Wednesday, April 21, 2010

User Lockout & WLST

WebLogic server provides an option to lockout users to protect accounts from password guessing attack. It is implemented with a realm-wide Lockout Manager. This feature can be used with custom authentication provider also. But if you implement your own authentication provider and wish to implement your own lockout manager that is possible too.

If your domain is configured to use the user lockout manager the following WLST script will help you to:
- check whether a user is locked using a WLST script
- find out the number of locked users in the realm


#Define constants
url='t3://localhost:7001'
username='weblogic'
password='weblogic'
checkuser='test-deployer'

#Connect
connect(username,password,url)

#Get Lockout Manager Runtime
serverRuntime()
dr = cmo.getServerSecurityRuntime().getDefaultRealmRuntime()
ulmr = dr.getUserLockoutManagerRuntime()

print '-------------------------------------------'
#Check whether a user is locked
if (ulmr.isLockedOut(checkuser) == 0):
islocked = 'NOT locked'
else:
islocked = 'locked'
print 'User ' + checkuser + ' is ' + islocked

#Print number of locked users
print 'No. of locked user -> ', Integer(ulmr.getUserLockoutTotalCount())

print '-------------------------------------------'
print ''

#Disconnect & Exit
disconnect()
exit()

Friday, February 26, 2010

WLDF Browser

WebLogic Diagnostic Framework (WLDF) is a comprehensive framework for diagnosing isuees related to servers, applications, services in WebLogic Server. Image capturing is a component of WLDF which can be used to get a snapshot of all the key information from a server. The WLDF Image gets generated as a zip file with an "img" file for information from each sub-system. For more information regarding WLDF Image capturing refer to the WLS documentation here.

Currently there are no official tools available to view the image or to compare 2 or more images. The attached tool WLDF Browser can help you to view the diagnostic image.

HTTP Session Monitoring in WebLogic Server

Monitoring HTTP sessions on WebLogic Server is a common task when you have web component (WAR) deployed as a part of your enterprise application (EAR). By default WebLogic Server provides a way to access the number of active HTTP sessions for each web application. But if you need more information about the individual sessions like their creation time, last accessed time, maximum inactive interval etc. then you have to enable the session monitoring option in the web component's Deployment Descriptor (DD). If the application is already packaged and deployed you can make use of the deployment plan to enable this attribute. WebLogic Administration Console provide options to modify some of the attribute for the components and application through the interface. Eventually a deployment plan will be generated if there are no deployment plan associated with the application or the existing plan will be modified automatically by Administration Console. This will ease administrators job of enabling session monitoring for a packaged application. You may have to update or redeploy the application with the plan for the monitoring to take effect. No restarting of any server is required for this change.

Administration Console will automatically populate the Session tab under the Monitoring tab with the individual session information for the specific application. By default the following column are displayed on the console page:

  • Context Root
  • Server
  • Creation Time
  • Time Last Accessed
  • Max Inactive Interval

You can also add the following columns to the table by customizing the view:

  • Monitoring ID
  • Application
  • Machine

The Monitoring ID provides a unique ID by default to identify the session. You can also customize the Monitoring ID by specifying a session attribute name which is unique for identifying the session for users. For example you can configure customer_id or user_id or order_id as Monitoring ID for tracking session. I shall talk about Monitoring IDs and finding session size in a different post. Internally WebLogic will concatenate the Monitoring ID with the creation time to make a unique name for each session. The following is an example screen shot of monitoring sessions after customizing the view:



WLST can also be able to retrieve these information from the ServletSessionRuntimeMBeans. The following is an example WLST script to connect to the Administration Server of a domain to retrieve session information for a specific application on a particular server.I had used the deprecated ServletSessionRuntimeMBeans in my WLST example as the session creation time is only available through that runtime mbean. You can also access all the information about the session except the creation time through the WebAppComponentRuntimeMBean. But you will have to use the Monitoring IDs (available through WebAppComponentRuntimeMBean) to get information about a specific session.


from java.util import Calendar

def defineVariables()
#Define all the variables used
username='system'
password='weblogic'
adminurl='t3://localhost:7001'
servername='AdminServer'
appname='CompanyStore'

def connectToAdminServer()
#Connect to Admin Server
connect(username,password,adminurl)

def extractAndPrintSessionInfo(servletSessionRuntime)
#Get session name
session_name=ssession.getName().split('!')[0]
#Creation time is concatinated to the session name. So split it
ct_millis=ssession.getName().split('!')[1]
cal = Calendar.getInstance()
cal.setTimeInMillis(Long(ct_millis))
ct = cal.getTime()
#Get last accessed time
tla_millis=ssession.getTimeLastAccessed()
cal.setTimeInMillis(Long(tla_millis))
tla = cal.getTime()
print ' '
print 'Session Name - ' + session_name
print 'Creation Time - ', ct
print 'Last Accessed Time - ', tla

#Main
defineVariables()
connectToAdminServer()

#Get Runtime for our server
domainRuntime()
cd('/ServerRuntimes/'+servername)

#Get all running applications
apps=cmo.getApplicationRuntimes()
for app in apps:

#We are intersted only on this application
if app.getName() == appname:

print 'Application Name - ', app.getName()

#Get all components in that application
comps=app.getComponentRuntimes()
for comp in comps:

#We are interested in only web components
if comp.getType() == 'WebAppComponentRuntime':
comp_name = comp.getName().split(servername+'_/')
if len(comp_name) == 1:
display_comp_name = '(default web app)'
else:
display_comp_name = comp_name
print 'Component Context Root - ', display_comp_name

#Get all active sessions
sessions=comp.getServletSessions()
print 'Total no. of sessions - ', len(sessions)

#Loop through all the available sessions
for ssession in sessions:
extractAndPrintSessionInfo(session)

#Disconnect and exit
disconnect()
exit()



The output from the above script will look something like this:


[test@mywlssvr-orcl ~]$ java weblogic.WLST sessions.py

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

Connecting to t3://localhost:7001 with userid system ...
Successfully connected to Admin Server 'AdminServer' that belongs to domain 'dizzyworld'.

Warning: An insecure protocol was used to connect to the
server. To ensure on-the-wire security, the SSL port or
Admin port should be used instead.

Location changed to domainRuntime tree. This is a read-only tree with DomainMBean as the root.
For more help, use help(domainRuntime)

Application Name - CompanyStore
Component Context Root - (default web app)
Total no. of sessions - 2

Session Name - dYvLLHgRGxQ8zS0ZYySLZKx1j2KK9Ws2G22nlJXvrQRFv64zsPTm
Creation Time - Fri Feb 26 01:13:21 UTC 2010
Last Accessed Time - Fri Feb 26 01:13:26 UTC 2010

Session Name - nGbTLHgFLnfNjwGT1cpHGQhJZGZynBYRsp2TjC8kC7KFRK1vCCf3
Creation Time - Fri Feb 26 01:13:41 UTC 2010
Last Accessed Time - Fri Feb 26 01:13:45 UTC 2010
Disconnected from weblogic server: AdminServer


Exiting WebLogic Scripting Tool.

Saturday, January 23, 2010

Node Manager as a UNIX Startup Process

Configuring WebLogic Server (WLS) as a startup process was a common administrative process in the past. But after Node Manager (NM) was introduced as an agent to start, stop, restart and monitor WebLogic Server it is now recommended that you start WebLogic Server using NM. But NM has to configured as a startup process on the respective OS so that it is always running to manage the respective WLS instances on that machine. NM can either be Java based NM or Script based NM. Script based NM uses OS services like SSH or RSH to execute the NM script. But Java based NM is a Java process that should be started. So setting up NM as a service will ensure that NM is always running. There is a lot of information in the WebLogic Server documentation about setting up NM. But there is very less or no information about setting NM as a service on a non-Windows environment. For Windows environment there are pre-packged scripts that you can use. These scripts exists under WL_HOME/server/bin folder (installNodeMgrSvc.cmd, uninstallNodeMgrSvc.cmd). But for UNIX based environment you should do this manually or with the help of OS specific utilities like chkconfig etc.

For *nix environment NM should be configured as startup process using rc scripts. Creating rc scripts for configuring NM as a service is even simpler than configuring WebLogic Server as a service. This is mainly because NM doesn't have to be shutdown gracefully. All you have to do is write a simple shell script that starts the NM and create the symbolic link to that script from the right run level folder. The following illustrates the steps to confiure NM as service under LINUX environment. It should be very similar on other *nix environments. You need root permission to peform the following steps.


1. Create a shell script (wlsnmd) under /etc/init.d/ folder:


#! /bin/bash
case "$1" in
start)
WL_HOME = /usr/middleware/11.1.0/wlserver_10.3
$WL_HOME/server/bin/startNodeManager.sh
;;

*)
echo "Usage: $0 {start}"
exit 1
esac

exit_status



2. Assuming that we are going to start NM at run level 5, create a sysmbolic link in /etc/rc.d/rc5.d/ folder. Since it a startup script we should start the symbolic link with "S" and followed by a number which decide the order in which services will be called. As we are fine to start NM after all the other application/services are start let us choose 99 which is the highest number.



ln -s /etc/init.d/wlsnmd S99weblogic



If you don't want to do these manually you can also use utilities like chkconfig (included in Red Hat distributions) for managing your startup scripts. For configuring WebLogic Server as a startup process there are pre-packaged scripts (installSvc.cmd, uninstallSvc.cmd). But you have to create rc scripts similarly for WLS as well in UNIX based environments. Here is an example rc script for configuring a WebLogic Server as a startup process. But here we also need to implement the kill/stop to gracefully shutdown the server.



#! /bin/bash

case "$1" in
start|reload)
DOMAIN_HOME = /usr/middleware/domains/med_analysis
$DOMAIN_HOME/bin/startWebLogic.sh
;;
stop)
$DOMAIN_HOME/bin/stopWebLogic.sh
;;
restart|force-reload)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac

exit_status