VMware Cloud Community
DeviAnantharama
Contributor
Contributor

**Writing Plugins for JMX in Hyperic**

Hi all,

I'm new to Hyperic. I've downloaded the free version of Hyperic. I've written MBeans that simply manages the Tomcat log. I want to write a plugin for including that in Hyperic. Please help me. If possible someone please send me a simple example where you have written MBeans and their corresponding plugin as well.

Thanks in advance,
Devi
-------------------------------------------------------------------------------------------------------------
**My LoggerMBean**
package jmxbook.ch4;

public interface LoggerMBean {

public int getLogLevel();
public void setLogLevel(int level);
public String retrieveLog(int linesback);
public void writeLog(String message, int type);
}
---------------------------------------------------------------------------------------------------------------
**My LoggerMBean implementation class**
package jmxbook.ch4;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class Logger implements LoggerMBean {

public static final int ALL = 3;
public static final int ERRORS = 2;
public static final int NONE = 0;

private PrintWriter out = null;
private int logLevel = Logger.ALL;

public Logger(){
try{
out = new PrintWriter(new BufferedWriter(new FileWriter("D:\\jakarta-tomcat-5.0.28\\logs\\localhost_log.2006-08-10.txt",true)),true);
}catch(Exception e){
e.printStackTrace();
}
}

public int getLogLevel() {
return logLevel;
}

public void setLogLevel(int level) {
logLevel = level;
}

public String retrieveLog(int linesback) {
BufferedReader in;
String str = null;
String buffer = null;
int i=0;
try {
in = new BufferedReader(new FileReader("D:\\jakarta-tomcat-5.0.28\\logs\\localhost_log.2006-08-10.txt"));
while (((str = in.readLine()) != null)&&(i<linesback)) {
i++;
buffer += str;
}
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return buffer;
}

public void writeLog(String message, int type) {
System.out.println("Message:-->"+message);
try{
if(type<=logLevel){
System.out.println("successful");
out.println(message);
}else
System.out.println("unsuccessful");
}catch(Exception e){
e.printStackTrace();
}
}

}
----------------------------------------------------------------------------------------------------------------
**My LoggerAgent class**
package jmxbook.ch4;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;

import com.sun.jdmk.comm.HtmlAdaptorServer;

public class LoggerAgent {

/**
* @param args
*/
public static void main(String[] args) {
Logger logger = new Logger();
MBeanServer mbs = MBeanServerFactory.createMBeanServer("logger");
HtmlAdaptorServer adaptor = new HtmlAdaptorServer();
adaptor.setPort(9094);
ObjectName loggerName,adaptorName;
try {
loggerName = new ObjectName("logger:name=logger");
adaptorName = new ObjectName("logger:type=adaptor,port=9094");
mbs.registerMBean(logger,loggerName);
mbs.registerMBean(adaptor,adaptorName);
adaptor.start();
} catch (MalformedObjectNameException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NullPointerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstanceAlreadyExistsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MBeanRegistrationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NotCompliantMBeanException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



}

}

Message was edited by: Devi Anantharaman
0 Kudos
8 Replies
dougm_hyperic
VMware Employee
VMware Employee

There are some docs here:
http://support.hyperic.com/confluence/display/DOCSHQ27/JMX+Plugin
And you'll find a few related threads in the forums too.
Your LoggerMBean doesn't have any monitoring attributes, but below is what a plugin would look like around it. The control feature is only available with the enterprise edition, which is what you'd need to invoke your retrieveLog and writeLog operations.

<plugin>
<!-- attach a new service type to the Sun JVM 1.5 server type -->
<service name="Tomcat Logger"
server="Sun JVM" version="1.5">

<!-- define ObjectName template used for auto-discovery and monitoring -->
<property name="OBJECT_NAME"
value="logger:name=logger"/>

<!-- format the auto-discovered service names -->
<property name="AUTOINVENTORY_NAME"
value="%platform.name% Logger"/>

<!-- enable auto-discovery -->
<plugin type="autoinventory"/>

<!-- plugin for monitoring -->
<plugin type="measurement"
class="org.hyperic.hq.product.jmx.MxMeasurementPlugin"/>

<!-- plugin for control -->
<plugin type="measurement"
class="org.hyperic.hq.product.jmx.MxControlPlugin"/>

<!-- LoggerMBean operations -->
<actions include="setLogLevel,retrieveLog,writeLog"/>

<!-- monitor template applied to all metrics below -->
<filter name="template"
value="${OBJECT_NAME}:${alias}"/>

<!-- Availablity metric for red/green lights -->
<metric name="Availability"
indicator="true"/>

<!-- LoggerMBean attributes -->
<metric name="Log Level"
indicator="true"/>
</service>
</plugin>
0 Kudos
BC_hyperic
Enthusiast
Enthusiast

Hi,

I have asked about the same question in the HQ Development Forum, the thread is “First JMX plugin.” Devi, you should take a look at that thread.

Could someone clear up a difference in the two suggested plugins. The loggerMBean plugin has

<!-- plugin for monitoring -->
<plugin type="measurement"
class="org.hyperic.hq.product.jmx.MxMeasurementPlugin"/>

<!-- plugin for control -->
<plugin type="measurement"
class="org.hyperic.hq.product.jmx.MxControlPlugin"/>


The IRR plugin has

<!-- plugin for monitoring -->
<plugin type="measurement"
class="org.hyperic.hq.product.jmx.MxMeasurementPlugin"/>

There was no mention of org.hyperic.hq.product.jmx.MxControlPlugin. In a later message when I was getting an exception, Doug said to add

<plugin type="control"
class="org.hyperic.hq.product.jmx.MxControlPlugin"/>

as a work around for the older 2.7.0 version.

This is similar to the loggerMBean plugin but not the same plugin type="control" vs plugin type="measurement" for class="org.hyperic.hq.product.jmx.MxControlPlugin"/>

Is this intentional? Can someone explain what the two similar statements are doing?

Thanks
Bob
0 Kudos
dougm_hyperic
VMware Employee
VMware Employee

Typo, it should be:
<plugin type="control"
class="org.hyperic.hq.product.jmx.MxControlPlugin"/>
0 Kudos
DeviAnantharama
Contributor
Contributor

dougm,

lemme try putting this info together ..thanx for the help :).

Devi
0 Kudos
DeviAnantharama
Contributor
Contributor

Doug,

I packaged my LoggerMBean class files and the xml file that you provided in a jar,logger-plugin.jar

The package structure of the jar is as shown:
logger-plugin/jmxbook/ch4/*.class
logger-plugin/etc/hq-plugin.xml
i.e; class files with the package structure and the xml file inside etc folder comes under logger-plugin

I copied the jar to the location D:\Program Files\agent-2.7.0\pdk\plugins and D:\Program Files\server-2.7.0\hq-engine\server\default\deploy\hq.ear\hq-plugins corresponding to agent and server. Then, I started the server and then the agent. The server started properly, but the agent console showed the following error.

Agent startup error: Unable to initialize plugin manager: failed to configure plugin jar=D:\Program Files\agent-2.7.0\.\pdk\plugins\logger-plugin.jar

Can you send me a simple example, where you have the MBeans, the jar and xml along with details about putting the jar at the right location and observing the output thru' Hyperic.

Many Thanks,
Dolby

Message was edited by: Devi Anantharaman
0 Kudos
dougm_hyperic
VMware Employee
VMware Employee

Your MBean(s) are not deployed to the server using an HQ plugin, so
there is no need to package them in a plugin jar. You should have
your MBean deployed and tested using a JMX console of your choice
before attempting to implement an HQ plugin to manage the MBean.
Once your MBeans are deployed, you would use the logger-plugin.xml as-
is without .jar packaging.


On Aug 14, 2006, at 3:03 AM, Devi Anantharaman wrote:

> Doug,
>
> I packaged my LoggerMBean class files and the xml file that you
> provided in a jar,logger-plugin.jar
>
> The package structure of the jar is as shown:
> logger-plugin/jmxbook/ch4/*.class
> logger-plugin/etc/logger-plugin.xml
> i.e; class files with the package structure and the xml file inside
> etc folder comes under logger-plugin
>
> I copied the jar to the location D:\Program Files\agent-2.7.0\pdk
> \plugins
and D:\Program Files\server-2.7.0\hq-engine\server
> \default\deploy\hq.ear\hq-plugins
corresponding to agent and
> server. Then, I started the server and then the agent. The server
> started properly, but the agent console showed the following error.
>
> Agent startup error: Unable to initialize plugin manager: failed
> to configure plugin jar=D:\Program Files\agent-2.7.0\.\pdk\plugins
> \logger-plugin.jar

>
> Can you send me a simple example, where you have the MBeans, the
> jar and xml along with details about putting the jar at the right
> location and observing the output thru' Hyperic.
>
> Many Thanks,
> Dolby


0 Kudos
mabc101
Contributor
Contributor

Is the control functionality available in the free version of Hyperic HQ ? From the documentation it looks like it is not but I just wanted to confirm since the thread started with the mention of free version.
0 Kudos
JohnMarkOrg
Hot Shot
Hot Shot

Hi,

At this time, the free version does not have control functionality, although that may change for future releases.

-John Mark
Community Development
0 Kudos