|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjavax.management.StandardMBean
net.sf.snmpadaptor4j.SnmpAdaptor
public class SnmpAdaptor
snmpAdaptor4j is an adaptor for Java Management eXtensions (JMX) providing a simple access to MBeans via the SNMP protocol. Thus, this adapter you allow to connect most monitoring tools (like Nagios and Cacti) to your Java applications.
For each MBean, an XML mapping file allows to establish the relationships between attributes and the MIB of the SNMP adapter. No additional code is necessary to integrate the MBeans in the SNMP protocol.
This adapter can work in a multi-applications environment (such as application servers).
For each MBean of your project, create the mapping file in the same package as the MBean. For example:
<?xml version="1.0" encoding="utf-8"?>
<snmpAdaptor4j-mapping
xmlns="http://www.sf.net/snmpAdaptor4j/mapping/1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sf.net/snmpAdaptor4j/mapping/1.1
http://snmpAdaptor4j.sourceforge.net/xsd/snmpAdaptor4j-mapping-1.1.xsd">
<attributes>
<attribute name="attributA" type="integer32" node="1" writable="true"/>
<attribute name="attributB" type="counter64" node="2" writable="false"/>
<attribute name="attributC" type="gauge32" node="3" writable="false"/>
</attributes>
</snmpAdaptor4j-mapping>
Here, we map only MBean attributes by indicating the SNMP data type and the node constituting the OID (Object IDentifier). The mapping of MBean instances, is generally put in the configuration file (see next chapter).
Create the configuration file (one by application) like this:
<?xml version="1.0" encoding="utf-8"?>
<snmpAdaptor4j-config
xmlns="http://www.sf.net/snmpAdaptor4j/config/1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sf.net/snmpAdaptor4j/config/1.1
http://snmpAdaptor4j.sourceforge.net/xsd/snmpAdaptor4j-config-1.1.xsd">
<daemon address="127.0.0.1" port="161" version="2">
<readCommunity>public</readCommunity>
<writeCommunity>private</writeCommunity>
</daemon>
<roots default="1.3.6.1.4.1.99.12.8.1"/>
<mbeans>
<mbean name="fr.mydomain.myAppli:type=MyClass,name=objA" oid="1.1"/>
<mbean name="fr.mydomain.myAppli:type=MyClass,name=objB" oid="1.2"/>
</mbeans>
</snmpAdaptor4j-config>
Here, we define the OID for each instance of MBeans. The OID of an attribute, is built as follows:
${root}.${mbean oid}.${attribute node}.0
For example, the attributB OID of first MBean will be 1.3.6.1.4.1.99.12.8.1.1.1.2.0.
Insert the following code to install the adapter inside JMX:
URL url = getClass().getResource("/snmp.xml");
SnmpAdaptor adaptor = new SnmpAdaptor(url, true);
ObjectName name = new ObjectName("net.sf.snmpadaptor4j:adaptor=SnmpAdaptor");
ManagementFactory.getPlatformMBeanServer().registerMBean(adaptor, name);
snmp.xml is the configuration file previously built.
Update the information about your system:
adaptor.getSystemInfo().setSysName("MyApp");
adaptor.getSystemInfo().setSysDescr("This is a java application");
adaptor.getSystemInfo().setSysLocation("Web server at Montpellier (France)");
adaptor.getSystemInfo().setSysContact("root@mydomain.fr");
Start the adaptor:
snmpAdaptor.start();
Install the Net-SNMP commands and type:
snmpwalk -v2c -Os -c public 127.0.0.1 .1
You should see the attribute values of your MBean.
| Field Summary | |
|---|---|
protected Map<ClassLoader,SnmpAppContext> |
appContextMap
Application context map. |
| Constructor Summary | |
|---|---|
|
SnmpAdaptor(boolean classLoaderScope)
Constructor with a minimum of parameters. |
|
SnmpAdaptor(SnmpAppContext appContext,
boolean classLoaderScope)
Constructor with an application context. |
|
SnmpAdaptor(SnmpConfiguration configuration,
SnmpAppContext appContext,
SystemInfo systemInfo,
boolean classLoaderScope)
Constructor with all parameters. |
protected |
SnmpAdaptor(String defaultRootOid,
SnmpConfiguration configuration,
boolean classLoaderScope,
SystemSnmpMib systemMib,
JvmSnmpMib jvmMib,
JmxListener jmxListener,
JmxNotificationManager jmxNotificationManager,
SnmpDaemon daemon)
Hidden constructor (used for tests only). |
|
SnmpAdaptor(URL url,
boolean classLoaderScope)
Constructor with an URL to SNMP configuration file (XML). |
|
SnmpAdaptor(URL url,
SystemInfo systemInfo,
boolean classLoaderScope)
Constructor with an URL to SNMP configuration file (XML) and a SystemInfo. |
| Method Summary | |
|---|---|
void |
addAppContext(ClassLoader classLoader,
SnmpAppContext appContext)
Adds a new application context. |
protected static String |
formatAttribute(String value)
Formats an attribute value. |
String |
getDefaultRootOid()
Returns the default root OID containing the attributes of the application. |
String |
getListenerAddress()
Returns the listening IP address of SNMP daemon (127.0.0.1 by default). |
Integer |
getListenerPort()
Returns the UDP port of SNMP daemon (161 by default). |
String |
getListenerReadCommunity()
Returns the read community of SNMP daemon ("public" by default). |
Integer |
getListenerSnmpVersion()
Returns the protocol version of SNMP daemon (SNMP v2 by default). |
String |
getListenerWriteCommunity()
Returns the write community of SNMP daemon ("private" by default). |
List<SnmpManagerConfiguration> |
getManagerList()
Returns the parameter list to connect to each manager where to send all notifications (SNMP traps). |
Map<ObjectName,String> |
getMBeanOidMap()
Returns the map of MBean OIDs. |
Map<String,String> |
getRootOidMap()
Returns the map of root OIDs where the attributes of the application will stay. |
SystemInfo |
getSystemInfo()
Returns the informations on the system. |
boolean |
isStarted()
Returns TRUE if the SNMP daemon is started. |
void |
preDeregister()
|
ObjectName |
preRegister(MBeanServer server,
ObjectName name)
|
void |
removeAppContext(ClassLoader classLoader)
Removes an application context. |
void |
setListenerAddress(String listenerAddress)
Sets the listening IP address of SNMP daemon. |
void |
setListenerPort(Integer listenerPort)
Sets the UDP port of SNMP daemon. |
void |
setListenerReadCommunity(String listenerReadCommunity)
Sets the read community of SNMP daemon. |
void |
setListenerSnmpVersion(Integer listenerSnmpVersion)
Sets the protocol version of SNMP daemon. |
void |
setListenerWriteCommunity(String listenerWriteCommunity)
Sets the write community of SNMP daemon. |
void |
start()
Starts the SNMP daemon. |
void |
stop()
Stops the SNMP daemon. |
String |
toString()
|
| Methods inherited from class javax.management.StandardMBean |
|---|
cacheMBeanInfo, getAttribute, getAttributes, getCachedMBeanInfo, getClassName, getConstructors, getDescription, getDescription, getDescription, getDescription, getDescription, getDescription, getDescription, getImpact, getImplementation, getImplementationClass, getMBeanInfo, getMBeanInterface, getParameterName, getParameterName, invoke, postDeregister, postRegister, setAttribute, setAttributes, setImplementation |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
protected final Map<ClassLoader,SnmpAppContext> appContextMap
| Constructor Detail |
|---|
public SnmpAdaptor(boolean classLoaderScope)
throws Exception
Set TRUE to classLoaderScope for web applications: this avoids to publish MBeans of other applications of application server.
classLoaderScope - TRUE for handle only MBeans created by the same ClassLoader that the SNMP adapter. FALSE for handle
all MBeans of the JVM.
Exception - Exception if an error has occurred.
public SnmpAdaptor(SnmpAppContext appContext,
boolean classLoaderScope)
throws Exception
Set TRUE to classLoaderScope for web applications: this avoids to publish MBeans of other applications of application server.
appContext - Application context.classLoaderScope - TRUE for handle only MBeans created by the same ClassLoader that the SNMP adapter. FALSE for handle
all MBeans of the JVM.
Exception - Exception if an error has occurred.
public SnmpAdaptor(URL url,
boolean classLoaderScope)
throws Exception
URL to SNMP configuration file (XML).
Set TRUE to classLoaderScope for web applications: this avoids to publish MBeans of other applications of application server.
url - URL of SNMP configuration file (XML).classLoaderScope - TRUE for handle only MBeans created by the same ClassLoader that the SNMP adapter. FALSE for handle
all MBeans of the JVM.
Exception - Exception if an error occurred.
public SnmpAdaptor(URL url,
SystemInfo systemInfo,
boolean classLoaderScope)
throws Exception
URL to SNMP configuration file (XML) and a SystemInfo.
Set TRUE to classLoaderScope for web applications: this avoids to publish MBeans of other applications of application server.
url - URL of SNMP configuration file (XML).systemInfo - Informations on the system.classLoaderScope - TRUE for handle only MBeans created by the same ClassLoader that the SNMP adapter. FALSE for handle
all MBeans of the JVM.
Exception - Exception if an error occurred.
public SnmpAdaptor(SnmpConfiguration configuration,
SnmpAppContext appContext,
SystemInfo systemInfo,
boolean classLoaderScope)
throws Exception
Set TRUE to classLoaderScope for web applications: this avoids to publish MBeans of other applications of application server.
configuration - SNMP configuration settings.appContext - Application context.systemInfo - Informations on the system.classLoaderScope - TRUE for handle only MBeans created by the same ClassLoader that the SNMP adapter. FALSE for handle
all MBeans of the JVM.
Exception - Exception if an error has occurred.
protected SnmpAdaptor(String defaultRootOid,
SnmpConfiguration configuration,
boolean classLoaderScope,
SystemSnmpMib systemMib,
JvmSnmpMib jvmMib,
JmxListener jmxListener,
JmxNotificationManager jmxNotificationManager,
SnmpDaemon daemon)
throws Exception
Set TRUE to classLoaderScope for web applications: this avoids to publish MBeans of other applications of application server.
defaultRootOid - Default root OID containing the attributes of the application.configuration - SNMP configuration settings.classLoaderScope - TRUE for handle only MBeans created by the same ClassLoader that the SNMP adapter. FALSE for handle
all MBeans of the JVM.systemMib - Management Information Base (MIB) for system attributes.jvmMib - Management Information Base (MIB) for attributes of JVM.jmxListener - Object designed to respond to each registration or deregistration of MBeans.jmxNotificationManager - Manager of JMX notifications.daemon - SNMP daemon.
Exception - Exception if an error has occurred.| Method Detail |
|---|
public void addAppContext(ClassLoader classLoader,
SnmpAppContext appContext)
SnmpAdaptorMBeanSnmpAdaptor4j may be used by an application server. In this case you will inform the SNMP adapter to each deployment of a new application.
addAppContext in interface SnmpAdaptorMBeanclassLoader - Class loader of application.appContext - Application context.public void removeAppContext(ClassLoader classLoader)
SnmpAdaptorMBean
removeAppContext in interface SnmpAdaptorMBeanclassLoader - Class loader of application.public final String getListenerAddress()
SnmpDaemonConfiguration
getListenerAddress in interface SnmpDaemonConfigurationgetListenerAddress in interface SnmpAdaptorMBeanpublic final void setListenerAddress(String listenerAddress)
SnmpAdaptorMBean
setListenerAddress in interface SnmpAdaptorMBeanlistenerAddress - Listening IP address.public final Integer getListenerPort()
SnmpDaemonConfiguration
getListenerPort in interface SnmpDaemonConfigurationgetListenerPort in interface SnmpAdaptorMBeanpublic final void setListenerPort(Integer listenerPort)
SnmpAdaptorMBean
setListenerPort in interface SnmpAdaptorMBeanlistenerPort - UDP port.public final Integer getListenerSnmpVersion()
SnmpDaemonConfiguration
getListenerSnmpVersion in interface SnmpDaemonConfigurationgetListenerSnmpVersion in interface SnmpAdaptorMBeanpublic final void setListenerSnmpVersion(Integer listenerSnmpVersion)
SnmpAdaptorMBean
setListenerSnmpVersion in interface SnmpAdaptorMBeanlistenerSnmpVersion - SNMP protocol version.public final String getListenerReadCommunity()
SnmpDaemonConfiguration
getListenerReadCommunity in interface SnmpDaemonConfigurationgetListenerReadCommunity in interface SnmpAdaptorMBeanpublic final void setListenerReadCommunity(String listenerReadCommunity)
SnmpAdaptorMBean
setListenerReadCommunity in interface SnmpAdaptorMBeanlistenerReadCommunity - Read community of SNMP daemon.public final String getListenerWriteCommunity()
SnmpDaemonConfiguration
getListenerWriteCommunity in interface SnmpDaemonConfigurationgetListenerWriteCommunity in interface SnmpAdaptorMBeanpublic final void setListenerWriteCommunity(String listenerWriteCommunity)
SnmpAdaptorMBean
setListenerWriteCommunity in interface SnmpAdaptorMBeanlistenerWriteCommunity - Write community of SNMP daemon.public final List<SnmpManagerConfiguration> getManagerList()
SnmpConfiguration
getManagerList in interface SnmpAdaptorMBeangetManagerList in interface SnmpConfigurationpublic final String getDefaultRootOid()
SnmpAppContext
getDefaultRootOid in interface SnmpAppContextpublic final Map<String,String> getRootOidMap()
SnmpAppContext
getRootOidMap in interface SnmpAppContextNULL but can be empty).public final Map<ObjectName,String> getMBeanOidMap()
SnmpAppContext
getMBeanOidMap in interface SnmpAppContextNULL but can be empty).
public void start()
throws Exception
SnmpAdaptorMBean
start in interface SnmpAdaptorMBeanException - Exception if an error has occurred.
public void stop()
throws Exception
SnmpAdaptorMBean
stop in interface SnmpAdaptorMBeanException - Exception if an error has occurred.public final boolean isStarted()
SnmpAdaptorMBeanTRUE if the SNMP daemon is started.
isStarted in interface SnmpAdaptorMBeanTRUE if the SNMP daemon is started.public final SystemInfo getSystemInfo()
The returned object is a MBean. It is possible to register it in JMX.
protected static final String formatAttribute(String value)
value - Value to format.
public ObjectName preRegister(MBeanServer server,
ObjectName name)
throws Exception
preRegister in interface MBeanRegistrationpreRegister in class StandardMBeanException
public void preDeregister()
throws Exception
preDeregister in interface MBeanRegistrationpreDeregister in class StandardMBeanExceptionpublic String toString()
toString in class Object
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||