Eben Hewitt writes about Java, Service-Oriented Architecture, and general enterprise software development practices.

Using MBeans in WebLogic Server 10gR3 to Create an Alert on State Change

These instructions are for WebLogic 10gR3, which lets you do something depending on the value of an MBean attribute. For example, if you want to get notified by email that the server has changed from 'Running' to 'Overloaded' State, you can do that. You need to do a few things:

Set up the Notifier. This is what will be used to send you the notice. You can use SNMP traps, email, or drop a message on a JMS Queue.

Create a Diagnostic Module. This acts as a container for performing all the related diagnostic work you are going to set up.

Create a Collection of Metrics, in which you indicate what attributes of what MBeans you want to capture.

Create a Watch. This is basically a rule that is formed by wrapping an XPath-like comparison expression. It includes a set of simple mathematical operators with your attributes.

Create A Diagnostic ModuleUnder Domain Structure, go to Diagnostics > Diagnostic Modules New > Name & Description (Make this a general Name to define multiple Watches and notifications.) In this example, mine is called DomainMonitorModule, and the description is: This module monitors the DTD domain to collect JDBC and Server Health stats.

Save and Activate Changes

Now you have an empty module ready to hold your metrics collection and watches.

Collected MetricsIn the Summary of Diagnostic Modules screen, click the name of your new Module. Click the Collected Metrics tab. Make sure the check box is enabled, and set your sampling period. Set Sampling Period (300000 = 5 minutes) This is the default. Click New and choose choose ServerRuntime for MBean Server Location from the drop down. Click Next. You'll be taken to the MBean selection page.

Select MBean Type On this screen, you choose the MBean you want to collect metrics for. This can be a custom MBean that you have written. There are MBeans for monitoring server status, GC, classloading, and many more.

Here we'll choose weblogic.management.runtime.ServerRuntimeMBean, which is the same bean that gives the image of the System Status on the lower-left corner of the WL Console.

On the Additional Metric Collection Properties page, select the attributes on this MBean that you want to collect data for. Here I'll choose State by moving it to the Chosen box. Other values include Shutting Down, Open Sockets Current Count, and others.

Leave the Attribute Expressions box empty and click Next.

On the Select Instances screen, choose the instance from within your Cluster that you want to monitor. You have to do this for each instance in your cluster.

Click Finish. You should see your ServerRuntimeMBean in the Collected Metrics in this Module section. Click Save, and Activate Changes.

Now it's time to create a Watch and Notification for the metrics you're collecting from the server.

Watches and NotificationsClick the Watches and Notifications tab. Click Lock and Edit so you can make changes. You can change the Severity in the drop down to Warning so that the messages you receive are more meaningful.

Click the Notifications tab and click the New button in the Notifications area.

Give the Notification a Name. I'll call mine DOMAIN-DEFAULT-NOTIFY because it can be used by multiple modules so I don't want to be too specific. Choose for the Type: SMTP E-Mail. You can also choose SNMP Trap, JMS, and others. Make sure Enable Notification box is checked.

Click Next, and you will need to configure the SMTP properties for your emailer.

If you do not have a Mail Session associated with the server, you'll need to set that up now. Here's now:

Create a Mail SessionA mail session allows WebLogic to send SMTP messages. Click Create a New Mail Session. If you are doing this from scratch, you can start in the Domain Structure menu at Services > Mail Sessions and the rest of the steps are the same. First, give it a name. I'll choose AlertMailer

Then give it a JNDI name. I'll choose mail/alertMailer.

Then specify your JavaMail properties. These values are separated by a semi-colon. You can check the help on this page to see the complete set of values. These are what I'll use:

Now Save and Activate Changes. You should see EmailNotifier under your list of Notifications.

Now that you have a Nofication set up, it's time to make a Watch.

WatchesYou now have a way to send a notice through the Notification, and you have some metrics you are collecting from the MBeans running in the server. Now you need to create a watch to put these together with an expression you want to evaluate the runtime MBean values against, and trigger a notification if necessary. To create a new Watch, click the Watches tab. Under the Watches area, click New. Give your watch a name. I'll pick a very descriptive name that includes the MBean I'm checking and the server it's running on, because we'll need to repeat this step once for each MBean on each server we're watching. I'll choose this name: SVR-BEA1-S1-SERVER-STATE.

Click Next

Now you have to add a Watch Rule. Click Add Expressions. Choose ServerRuntime from the drop down, click next.

From the drop down, choose the same MBean type you are collecting metrics for. In this case it's weblogic.management.runtime.ServerRuntimeMBean. Click Next.

Choose the Server instance from the cluster that you want. Here we'll choose BEA1-S1. Click Next.

Set the Expression. Choose the attribute of this MBean you want to participate in the expression. Here we'll choose State for the Message Attribute.

Then choose Operator != and Value of RUNNING (it is case-sensitive and others, like the JDBC one, is 'Running').

This returns us to the Configure Watch Rule Expression screen and writes this rule into the text box: