- 论坛徽章:
- 0
|
SnmpHibernate is a MIB/Object Mapping framework inspired by Hibernate Project(O/R Mapping framework). It will release u from coding tedious SNMP Client code. Below is some samples,
The project location:
SnmpHibernate on Sourceforge
The 1.0.0beta release is based on
SNMP4J project
.
I hope u could contribute to the project. Contact me by ery.lee@gmail.com if any problems and suggestions.
1 Write the SystemInfo POJO first,
import java.io.Serializable;import net.sourceforge.snmphibernate.api.SmiType;import net.sourceforge.snmphibernate.api.annotation.MibObjectType;import net.sourceforge.snmphibernate.api.annotation.MibObjectType.Access;public class SystemInfo implements Serializable { @MibObjectType(oid = "1.3.6.1.2.1.1.1.0", smiType = SmiType.DISPLAY_STRING, access = Access.READ) public String sysDesc; @MibObjectType(oid = "1.3.6.1.2.1.1.2.0", smiType = SmiType.OID, access = Access.READ) public String sysObjectID; @MibObjectType(oid = "1.3.6.1.2.1.1.3.0", smiType = SmiType.TIMETICKS, access = Access.READ) public long sysUpTime; @MibObjectType(oid = "1.3.6.1.2.1.1.4.0", smiType = SmiType.DISPLAY_STRING, access = Access.WRITE) public String sysContact; @MibObjectType(oid = "1.3.6.1.2.1.1.5.0", smiType = SmiType.DISPLAY_STRING, access = Access.WRITE) public String sysName; @MibObjectType(oid = "1.3.6.1.2.1.1.6.0", smiType = SmiType.DISPLAY_STRING, access = Access.WRITE) public String sysLocation;}
2. Get/Set by the session API,
ISnmpClientFacade facade = new Snmp4JClientFacade(); ISnmpSessionFactory sessionFactory = facade.getSnmpSessionFactory(); ISnmpTargetFactory targetFactory = facade.getSnmpTargetFactory(); ISnmpSession session = sessionFactory.newSnmpSession(targetFactory.newSnmpTarget( "127.0.0.1", 161));SystemInfo sysMIB = (SystemInfo)session.get(SystemInfo.class);
1. Write the IfEntry POJO first,
import java.io.Serializable;import net.sourceforge.snmphibernate.api.SmiType;import net.sourceforge.snmphibernate.api.annotation.MibIndex;import net.sourceforge.snmphibernate.api.annotation.MibObjectType;import net.sourceforge.snmphibernate.api.annotation.MibTable;import net.sourceforge.snmphibernate.api.annotation.MibObjectType.Access;@MibTablepublic class IfEntry implements Serializable { @MibIndex(no = 0, length = 1) @MibObjectType(oid = "1.3.6.1.2.1.2.2.1.1", smiType = SmiType.INTEGER32, access = Access.READ) public int ifIndex; @MibObjectType(oid = "1.3.6.1.2.1.2.2.1.2", smiType = SmiType.DISPLAY_STRING, access = Access.READ) public String ifDescr; @MibObjectType(oid = "1.3.6.1.2.1.2.2.1.3", smiType = SmiType.INTEGER32, access = Access.READ) public int ifType; @MibObjectType(oid = "1.3.6.1.2.1.2.2.1.4", smiType = SmiType.INTEGER32, access = Access.READ) public int ifMtu; @MibObjectType(oid = "1.3.6.1.2.1.2.2.1.5", smiType = SmiType.GAUGE32, access = Access.READ) public long ifSpeed; @MibObjectType(oid = "1.3.6.1.2.1.2.2.1.6", smiType = SmiType.OCTET_STRING, access = Access.READ) public byte[] ifPhysAddress; @MibObjectType(oid = "1.3.6.1.2.1.2.2.1.7", smiType = SmiType.INTEGER32, access = Access.READ) public int ifAdminStatus; @MibObjectType(oid = "1.3.6.1.2.1.2.2.1.8", smiType = SmiType.INTEGER32, access = Access.READ) public int ifOperStatus; @MibObjectType(oid = "1.3.6.1.2.1.2.2.1.9", smiType = SmiType.TIMETICKS, access = Access.READ) public long ifLastChange; @MibObjectType(oid = "1.3.6.1.2.1.2.2.1.10", smiType = SmiType.COUNTER32, access = Access.READ) public long ifInOctets; @MibObjectType(oid = "1.3.6.1.2.1.2.2.1.11", smiType = SmiType.COUNTER32, access = Access.READ) public long ifInUcastPkts; @MibObjectType(oid = "1.3.6.1.2.1.2.2.1.12", smiType = SmiType.COUNTER32, access = Access.READ) public long ifInNUcastPkts; @MibObjectType(oid = "1.3.6.1.2.1.2.2.1.13", smiType = SmiType.COUNTER32, access = Access.READ) public long ifInDiscards; @MibObjectType(oid = "1.3.6.1.2.1.2.2.1.14", smiType = SmiType.COUNTER32, access = Access.READ) public long ifInErrors; @MibObjectType(oid = "1.3.6.1.2.1.2.2.1.15", smiType = SmiType.COUNTER32, access = Access.READ) public long ifInUnknownProtos; @MibObjectType(oid = "1.3.6.1.2.1.2.2.1.16", smiType = SmiType.COUNTER32, access = Access.READ) public long ifOutOctets; @MibObjectType(oid = "1.3.6.1.2.1.2.2.1.17", smiType = SmiType.COUNTER32, access = Access.READ) public long ifOutUcastPkts; @MibObjectType(oid = "1.3.6.1.2.1.2.2.1.18", smiType = SmiType.COUNTER32, access = Access.READ) public long ifOutNUcastPkts; @MibObjectType(oid = "1.3.6.1.2.1.2.2.1.19", smiType = SmiType.COUNTER32, access = Access.READ) public long ifOutDiscards; @MibObjectType(oid = "1.3.6.1.2.1.2.2.1.20", smiType = SmiType.COUNTER32, access = Access.READ) public long ifOutErrors; public IfEntry() {} public IfEntry(int index) { this.ifIndex = index; } public boolean isLoopback() { return ifType == 24; }}
2. Get the whole table,
ISnmpClientFacade facade = new Snmp4JClientFacade();
ISnmpSessionFactory sessionFactory = facade.getSnmpSessionFactory();
ISnmpTargetFactory targetFactory = facade.getSnmpTargetFactory();
ISnmpSession session = sessionFactory.newSnmpSession(targetFactory.newSnmpTarget(
"127.0.0.1", 161));
List list = session.getTable(IfEntry.class);
- There are other features listed below,
1. You can get part of properties a Mib entry or scalar group.2. Create an entry of a MIB table.3. Delete an entry of a MIB table.4. Lanched as an OSGi bundle. If you are not familiar with OSGI, I suggest u remove the "Activator" from the src of release jars to pass compile.
Enjoy it:)
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/14066/showart_76388.html |
|