Friday, August 29, 2014

Get your own JMX metrics into Enterprise Manager - Part 2

Once you have an application with a custom registered MBean, we are able to both create incidents and monitor it from Enterprise Manager. Did you also know that there is an excellent WebLogic MBean browser as well?

Let's start by finding the MBean that we created in Part 1


Start logging into Enterprise Manager and go directly to the WebLogic server where you deployed your MBean application and start the MBean Browser


Navigate down and you should find the MBean that we just created with the name "Duke". You can edit the content of our name directly from the MBean browser.


With that we just made sure that the MBean is visible. Now let's start to monitor it. For Enterprise Manager to be able to handle our new MBean metric, we need to create a metric extension.


We did deploy our EAR file to a WebLogic server, but it is the same approach on JBoss, Tomcat, GlassFish and other servers.


Create a new Metric Extension


Next, we browse the MBean tree. (Hint: To be able to browse you need to have entered preferred credentials for the target)


Next, you are able to set properties on your new metric by pressing the EDIT button



Next, we are going to test our new metric on the target:




Then press Next, and Finish. We are now done creating our new metric. Now let us deploy it to the target (Agent). First we have to Save our new metric extension as a Deployable Draft


Afterwards we are able to target it to a server:


And the last step is to publish our new metric extension


That is it!

Now we are going to get input every 15 minutes on our new metric and we can browse to the target WebLogic server and have a look!



Let's try to change the value. It should give us a new incident in Enterprise Manager. Go to the MBean browser and change the name to e.g Tux


After some minutes, there should be a new incident created


This concludes the example. It is now up to you to create notifications rules etc. for your own JMX metrics.

Get your own JMX metrics into Enterprise Manager - Part 1

How can you monitor your own Java applications in Enterprise Manager?
In most cases you would use the built in functionality around performance, incidents etc. but what if you need to get some diagnostic data or want to track changes or behaviour of your application?
A standard way to control and monitor Java applications is using JMX and MBeans. In this example I'll first go through creating a Java EE Application with a MBean interface, then register and monitor the parameter from Oracle Enterprise Manager.

In my little example below I have used the standard WebLogic documentation to create my MBean application: http://docs.oracle.com/cd/E14571_01/web.1111/e13729/instmbeans.htm



You can find several MBean example applications around on the internet. You may choose one of those or follow my simple recipe below.

For this programming part I used Oracle Enterprise Pack for Eclipse (OEPE) that can be downloaded from the Oracle Technology Network site: http://www.oracle.com/technetwork/developer-tools/eclipse/downloads/index.html

Start out by creating a new Java project (just use default settings).



Then a new java packet under the src directory. Mine is called com.oracle.demo.



We start with creating the MBean Interface:

  1. package com.oracle.demo;
  2.  
  3. public interface MyMBean {
  4. public String getMyName();
  5. public void setMyName(String name);
  6. }

Next the MyBeanImpl class that implements the MBean interface
  1. package com.oracle.demo;
  2.  
  3. import javax.management.StandardMBean;
  4.  
  5. public class MyMBeanImpl extends StandardMBean
  6. implements MyMBean {
  7.  
  8. String myName = "Duke";
  9. public MyMBeanImpl() throws
  10. javax.management.NotCompliantMBeanException {
  11. super(MyMBean.class);
  12. }
  13. @Override
  14. public String getMyName() {
  15. return myName;
  16. }
  17.  
  18. @Override
  19. public void setMyName(String name) {
  20. this.myName = name;
  21. }
  22. }

After that we need to create the registration class that will register our MyBeanImpl class in WebLogic.

  1. package com.oracle.demo;
  2.  
  3. import javax.management.MBeanServer;
  4. import javax.management.ObjectName;
  5.  
  6. import javax.naming.InitialContext;
  7.  
  8. import javax.naming.NamingException;
  9.  
  10. import weblogic.application.ApplicationLifecycleListener;
  11.  
  12. public class RegisterMyMBean extends ApplicationLifecycleListener {
  13.  
  14. // register the mbean when application starts.
  15. public void postStart(weblogic.application.ApplicationLifecycleEvent p1) {
  16. try {
  17. ObjectName mymbeanobj =
  18. new ObjectName("mymbean:Name=MyMBean,Type=MyMBean");
  19. InitialContext ctx = new InitialContext();
  20.  
  21. MBeanServer server = (MBeanServer)ctx.lookup("java:comp/jmx/runtime");
  22.  
  23. MyMBean mbean = new MyMBeanImpl();
  24.  
  25. server.registerMBean(mbean, mymbeanobj);
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }
  29.  
  30. }
  31.  
  32. // unregister the mbean when the application stops.
  33. public void preStop(weblogic.application.ApplicationLifecycleEvent p1) {
  34. InitialContext ctx;
  35. try {
  36. ObjectName mymbeanobj =
  37. new ObjectName("mymbean:Name=MyMBean,Type=MyMBean");
  38. ctx = new InitialContext();
  39. MBeanServer server = (MBeanServer)ctx.lookup("java:comp/jmx/runtime");
  40. server.unregisterMBean(mymbeanobj);
  41.  
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. }

Once done, you should have a similar directory structure:



Don't mind any compiler warnings or IDE warnings at this time. They will disappear with the next step, which is to create the .ear file.

Create a new Enterprise Application Project, New->Other->Java EE->Enterprise Application Project


Give the application a name and enter the run time information needed by Eclipse to deploy your application:



Click Next, and then add you Java project to the Java EE Application


Afterwards your project window should look something like:


We are almost there. But you need to add a couple of more things. First of all, lets change the weblogic-application.xml file to actually register the MBean at startup.

  1. <wls:weblogic-application xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-application" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/javaee_5.xsd http://xmlns.oracle.com/weblogic/weblogic-application http://xmlns.oracle.com/weblogic/weblogic-application/1.3/weblogic-application.xsd">
  2. <!--weblogic-version:10.3.6-->
  3. <wls:application-param>
  4. <wls:param-name>webapp.encoding.default<wls:param-name>
  5. <wls:param-value>UTF-8</wls:param-value>
  6. </wls:application-param>
  7. <wls:listener>
  8. <wls:listener-class>com.oracle.demo.RegisterMyMBean</wls:listener-class>
  9. </wls:listener>
  10. </wls:weblogic-application>
  11.  

The final little piece to the demo application is a web project. Normally this is where you add your code or application. Add a new Dynamic Web Project.


call the project hello, and remember to add the project to our EAR.



Then press Finish.


Now we have three projects in the explorer. The last thing to do is to add a JSP and add our web application to the application.xml file.

Right click on hello->WebContent and select JSP File. Name the file hello.jsp




  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. hello all!
  11. </body>
  12. </html>

Now we need to add the hello web application to the EAR application.xml deployment descriptor. If you did not "hook" the generate application.xml file during the EAR creation, you should just add the file under MyCustomMBeanApp->EarContent->META-INF
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:application="http://java.sun.com/xml/ns/javaee/application_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd" id="Application_ID" version="5">
  3. <display-name>MBeanProject</display-name>
  4. <module>
  5. <web>
  6. <web-uri>hello.war</web-uri>
  7. <context-root>hello</context-root>
  8. </web>
  9. </module>
  10. </application>

The project is now ready to deploy to a WebLogic server. Deploy directly from Eclipse or go through an .ear file.


Deploy the application to a WebLogic server managed by Enterprise Manager and make sure that you are able to see the hello world page.


Now log into Enterprise Manager and refresh the domain.

Tuesday, August 26, 2014

Using EMCLI with Fusion Middleware

If you are planning to automate deployment procedures, CRUD operations or do multiple steps like provision whole private cloud environments with both middleware and database there is a solution for you.
One feature of Oracle Enterprise Manager that is not always utilized is EMCLI, short for Enterprise Manager Command Line Interface. Like WLST it can be used in several modes: standard command-line, Interactive and Script. The script mode is based on Jython. Does it sound familiar? Yes, you will be able to re-use your P(J)ython skills from WLST scripting.

To start you off, you need Java and the EMCLI Client kit. There are two versions of the client. One "light weight" standard without the scripting functionality, and one advanced with all the modes. They are hopefully already available to you from the Enterprise Manager OMS server. Read more about the modes, pre-reqs etc. in the userinterface, or just download the kits directly. Open a browser and use the URL's:

Standard kit: https://<your_em_host:port>/em/public_lib_download/emcli/kit/emclikit.jar
Script kit: https://<your_em_host:port>/em/public_lib_download/emcli/kit/emcliadvancedkit.jar


You probably want to have the advanced kit with scripting functionality.

Once downloaded, you install by:

Java -jar emcliadvancedkit.jar


Next start creating Jython scripts. This is an example you could use as a starting point. It will take username as an input.

from emcli import *

#Set the OMS URL to connect to
set_client_property('EMCLI_OMS_URL','https://<your_em_install>:7801/em')

#Accept all the certificates
set_client_property('EMCLI_TRUSTALL','true')

inp_username=sys.argv[0]

#Login to the OMS
login (username=inp_username)

print version()

targets_array = get_targets(targets='weblogic_domain').out()['data']
   for target in targets_array:


Invoke it by running

 emcli @print_domains.py sysman 

where you may exchange sysman with another user.

The result should be something like:

C:\emcli>emcli @print_domains.py sysman 
Enter password ******** 

Oracle Enterprise Manager 12c EM CLI Version 12.1.0.4.0 

/OCS_SOA_soa_domain/soa_domain 
/OCS_osb_domain/osb_domain 
/WLS_12c_base_domain/base_domain 
/EMGC_GCDomain/GCDomain 
Logout successful 

C:\emcli>

You could also use EMCLI to refresh several domains at the same time. For that you need to create an input file (E=enable, D=disable, R=removes deleted targets):
/OCS_osb_domain/osb_domain,E
   /OCS_SOA_soa_domain/soa_domain,E
   /demo_wl_server/wl_server,E
Save the file as domain_refresh.csv, and the jython script refresh_domains.py looks like::


from emcli import *

#Set the OMS URL to connect to
set_client_property('EMCLI_OMS_URL','https://<your_em_install>:7801/em')

#Accept all the certificates
set_client_property('EMCLI_TRUSTALL','true')

inp_username=sys.argv[0]

#Login to the OMS
login (username=inp_username)

print version()

refresh_wls(input_file="domain_refresh_file:c:\emcli\domain_refresh.csv")



Invoke it by running

 emcli @refresh_domains.py sysman 


Adding targets
This just an example of the things you can do with EMCLI. A full list of verbs etc. is located here.