Preface
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).
Integration
Download
here, the last version.
Unzip the downloaded file and retrieve the following files for put it in the
classpath of your project:
- /lib/snmpAdaptor4j-jmx-1.1.jar
- /lib/joesnmp-0.3.4.jar
- /lib/log4j-1.2.16.jar
Alternatively, if you use
Maven, you can add the following dependency in the POM file of your project:
<dependency>
<groupId>net.sf.snmpadaptor4j</groupId>
<artifactId>snmpAdaptor4j-jmx</artifactId>
<version>1.1</version>
</dependency>
Building of the mapping files
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).
Building of the configuration file
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.
Registration and start of adaptor
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();
NOTE: The object returned by GetSystemInfo() is a MBean. It is possible
to register it in JMX.
Test
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.