在window系统中简单的配置之后,使用SNMP4J的测试的信息如下:
以下部分的SNMP中MIB中OID可能错误,希望大家不惜指出,谢谢!
package com.easyway.snmp; import java.io.IOException; import java.util.Vector; import org.snmp4j.CommunityTarget; import org.snmp4j.PDU; import org.snmp4j.Snmp; import org.snmp4j.TransportMapping; import org.snmp4j.event.ResponseEvent; import org.snmp4j.mp.SnmpConstants; import org.snmp4j.smi.Address; import org.snmp4j.smi.GenericAddress; import org.snmp4j.smi.OID; import org.snmp4j.smi.OctetString; import org.snmp4j.smi.VariableBinding; import org.snmp4j.transport.DefaultUdpTransportMapping; /** * * snmp简单应用 * @author longgangbai * */ public class SnmpUtils { private Snmp snmp = null; private Address targetAddress = null; /** * 初始化SNMP的各种信息 * @throws IOException */ public void initComm() throws IOException { //创建设备代理的地址信息 targetAddress = GenericAddress.parse("udp:192.168.0.175/161"); //创建默认的Udp传输影响对象 TransportMapping transport = new DefaultUdpTransportMapping(); //创建snmp对象 snmp = new Snmp(transport); //开始snmp监听性 transport.listen(); } /** * 响应snmp的信息的方法 * @param pdu * @return * @throws IOException */ public ResponseEvent sendPDU(PDU pdu) throws IOException { //创建团体对象(一般在客户端设置的) CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString("public")); //设置snmp监听地址的团体 target.setAddress(targetAddress); //设置请求的次数 target.setRetries(2); //设置请求的超时时间的 target.setTimeout(1500); //设置snmp的版本信息 target.setVersion(SnmpConstants.version1); //设置设备Agent的代理的响应的对象团体 return snmp.send(pdu, target); } /** * 设置要监控的PDU对象 * @throws IOException */ public void setPDU() throws IOException { //设置设备Agent代理的电脑名字的PDU PDU pdu = new PDU(); pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 5, 0 }), new OctetString("SNMP-PCNAME"))); pdu.setType(PDU.SET); sendPDU(pdu); //设置设备Agent代理电脑的工作组的PDU PDU pdu0 = new PDU(); pdu0.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 4, 1, 77, 1, 4,1,0 }), new OctetString("SNMP-WORKGROUP"))); pdu0.setType(PDU.SET); sendPDU(pdu0); //设置设备Agent代理电脑的系统信息 PDU pdu1 = new PDU(); pdu1.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 1, 0 }), new OctetString("SNMP-SYSTEM-INFO"))); pdu1.setType(PDU.SET); sendPDU(pdu1); //设置设备Agent代理电脑的启动时间 PDU pdu2 = new PDU(); pdu2.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 3 }), new OctetString("SNMP-SYSTEM-STARTUP"))); pdu2.setType(PDU.SET); sendPDU(pdu2); //设置设备代理电脑的联系人 PDU pdu3 = new PDU(); pdu3.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 4,0 }), new OctetString("SNMP-SYSTEM-LINKMAN"))); pdu3.setType(PDU.SET); sendPDU(pdu3); //设置设备代理电脑序列号 //Getting Server Serial Number: //snmputil get 127.0.0.1 public .1.3.6.1.4.1.23.2.28.1.2.0 PDU pdu4 = new PDU(); pdu4.add(new VariableBinding(new OID(new int[] { 1,3,6,1,4,1,23,2,28,1,2,0 }), new OctetString("SNMP-SYSTEM-SERIAL-NUMBER"))); pdu4.setType(PDU.SET); sendPDU(pdu4); //设置设备代理DOS内存使用率 PDU dosmermory = new PDU(); dosmermory.add(new VariableBinding(new OID(new int[] { 1,3,6,1,2,1,25,2,3,1,6,1 }), new OctetString("SNMP-SYSTEM-DOS-MERMORY"))); dosmermory.setType(PDU.SET); sendPDU(dosmermory); //设置设备代理电脑ALLOC-MEMORY-POOL PDU memroypoll = new PDU(); memroypoll.add(new VariableBinding(new OID(new int[] { 1,3,6,1,2,1,25,2,3,1,6,2 }), new OctetString("SNMP-SYSTEM-ALLOC-MEMORY-POOL"))); memroypoll.setType(PDU.SET); sendPDU(memroypoll); //设置设备代理缓存Buffer PDU cachebuffer = new PDU(); cachebuffer.add(new VariableBinding(new OID(new int[] { 1,3,6,1,2,1,25,2,3,1,6,3 }), new OctetString("SNMP-SYSTEM-CACHE-BUFFER"))); cachebuffer.setType(PDU.SET); sendPDU(cachebuffer); //设置设备代理电脑Cache Moveable Memory PDU cachemoveablemoemory = new PDU(); cachemoveablemoemory.add(new VariableBinding(new OID(new int[] { 1,3,6,1,2,1,25,2,3,1,6,4 }), new OctetString("SNMP-SYSTEM-SERIAL-NUMBER"))); cachemoveablemoemory.setType(PDU.SET); sendPDU(cachemoveablemoemory); //设置设备代理电脑Cache No Moveable Memory PDU cachenomoveablemoemory = new PDU(); cachenomoveablemoemory.add(new VariableBinding(new OID(new int[] { 1,3,6,1,2,1,25,2,3,1,6,5 }), new OctetString("SNMP-SYSTEM-SERIAL-NUMBER"))); cachenomoveablemoemory.setType(PDU.SET); sendPDU(cachenomoveablemoemory); //设置设备代理电脑Code and Data Memory: PDU memory = new PDU(); memory.add(new VariableBinding(new OID(new int[] { 1,3,6,1,2,1,25,2,3,1,6,6 }), new OctetString("SNMP-SYSTEM-CODE-DATA-MEMORY"))); memory.setType(PDU.SET); sendPDU(memory); //设置设备代理内存使用
PDU usedmemory = new PDU(); usedmemory.add(new VariableBinding(new OID(new int[] { 1,3,6,1,2,1,25,5,1,1,2,1 }), new OctetString("SNMP-SYSTEM-USSED-MEMORY"))); usedmemory.setType(PDU.SET); sendPDU(usedmemory); //设置设备代理电脑CPU使用 PDU cpu = new PDU(); cpu.add(new VariableBinding(new OID(new int[] { 1,3,6,1,2,1,25,5,1,1,1,1}), new OctetString("SNMP-SYSTEM-USED-CPU"))); cpu.setType(PDU.SET); sendPDU(cpu); //设置设备代理电脑系统服务 PDU sysSrv = new PDU(); sysSrv.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,1,7 }), new OctetString("SNMP-SYSTEM-SERVICES"))); sysSrv.setType(PDU.SET); sendPDU(sysSrv); //设置设备代理电脑RAM空闲空间 PDU ramFree = new PDU(); ramFree.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,25,2,2,0 }), new OctetString("SNMP-SYSTEM-RAM-FREE"))); ramFree.setType(PDU.SET); sendPDU(ramFree); //设置代理虚拟内存 PDU vmmemory = new PDU(); vmmemory.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,25,2,1,3 }), new OctetString("SNMP-VM-MEMORY"))); vmmemory.setType(PDU.SET); sendPDU(vmmemory); //设置代理电脑的系统进程数量 PDU processNum = new PDU(); processNum.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,25,1,60 }), new OctetString("SNMP-SYTEM-PROCESSNUM"))); processNum.setType(PDU.SET); sendPDU(processNum);
PDU systemDate = new PDU(); systemDate.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,25,1,2 }), new OctetString("SNMP-SYSTEM-DATE"))); systemDate.setType(PDU.SET); sendPDU(systemDate); } /** * 获取要监控的PDU * @throws IOException */ public void getPDU() throws IOException { // get PDU PDU pdu = new PDU(); pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 5, 0 }))); pdu.setType(PDU.GET); readResponse(sendPDU(pdu)); // get PDU PDU pdu0 = new PDU(); pdu0.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 4, 1, 77, 1, 4,1,0 }))); pdu0.setType(PDU.GET); readResponse(sendPDU(pdu0)); PDU pdu1 = new PDU(); pdu1.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 1, 0 }))); pdu1.setType(PDU.GET); readResponse(sendPDU(pdu1)); PDU pdu2 = new PDU(); pdu2.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 3 }))); pdu2.setType(PDU.GETNEXT); readResponse(sendPDU(pdu2)); PDU pdu3 = new PDU(); pdu3.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 4,0 }))); pdu3.setType(PDU.GET); readResponse(sendPDU(pdu3)); PDU pdu4 = new PDU(); pdu4.add(new VariableBinding(new OID(new int[] { 1,3,6,1,4,1,23,2,28,1,2,0 }))); pdu4.setType(PDU.GETNEXT); readResponse(sendPDU(pdu4)); //设置设备代理DOS内存使用 PDU dosmermory = new PDU(); dosmermory.add(new VariableBinding(new OID(new int[] { 1,3,6,1,2,1,25,2,3,1,6,1}))); dosmermory.setType(PDU.GETNEXT); readResponse(sendPDU(dosmermory)); //设置设备代理电脑ALLOC-MEMORY-POOL PDU memroypoll = new PDU(); memroypoll.add(new VariableBinding(new OID(new int[] { 1,3,6,1,2,1,25,2,3,1,6,2 }))); memroypoll.setType(PDU.GETNEXT); readResponse(sendPDU(memroypoll)); //设置设备代理缓存Buffer PDU cachebuffer = new PDU(); cachebuffer.add(new VariableBinding(new OID(new int[] { 1,3,6,1,2,1,25,2,3,1,6,3 }))); cachebuffer.setType(PDU.GET); readResponse(sendPDU(cachebuffer)); //设置设备代理电脑Cache Moveable Memory PDU cachemoveablemoemory = new PDU(); cachemoveablemoemory.add(new VariableBinding(new OID(new int[] { 1,3,6,1,2,1,25,2,3,1,6,4 }))); cachemoveablemoemory.setType(PDU.GET); readResponse(sendPDU(cachemoveablemoemory)); //设置设备代理电脑Cache No Moveable Memory PDU cachenomoveablemoemory = new PDU(); cachenomoveablemoemory.add(new VariableBinding(new OID(new int[] { 1,3,6,1,2,1,25,2,3,1,6,5 }))); cachenomoveablemoemory.setType(PDU.GET); readResponse(sendPDU(cachenomoveablemoemory)); //设置设备代理电脑Code and Data Memory: PDU memory = new PDU(); memory.add(new VariableBinding(new OID(new int[] { 1,3,6,1,2,1,25,2,3,1,6,6 }))); memory.setType(PDU.GET); readResponse(sendPDU(memory)); //设置设备代理内存使用 PDU usedmemory = new PDU(); usedmemory.add(new VariableBinding(new OID(new int[] { 1,3,6,1,2,1,25,5,1,1,2,1 }))); usedmemory.setType(PDU.GETNEXT); readResponse(sendPDU(usedmemory)); //设置设备代理电脑CPU使用 PDU cpu = new PDU(); cpu.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,25,5,1,1,1,1}))); cpu.setType(PDU.GETNEXT); readResponse(sendPDU(cpu)); //设置设备代理电脑系统服务 PDU sysSrv = new PDU(); sysSrv.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,1,7 }))); sysSrv.setType(PDU.GET); readResponse(sendPDU(sysSrv)); //设置设备代理电脑RAM空闲空间 PDU ramFree = new PDU(); ramFree.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,25,2,2,0 }))); ramFree.setType(PDU.GETNEXT); readResponse(sendPDU(ramFree)); //设置代理电脑的虚拟内存 PDU vmmemory = new PDU(); vmmemory.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,25,2,1,3 }))); vmmemory.setType(PDU.GETNEXT); readResponse(sendPDU(vmmemory)); //设置代理电脑的进程数量 PDU processNum = new PDU(); processNum.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,25,1,60 }))); processNum.setType(PDU.GETNEXT); readResponse(sendPDU(processNum));
PDU systemDate = new PDU(); systemDate.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,25,1,2 }))); systemDate.setType(PDU.GETNEXT); readResponse(sendPDU(systemDate)); } /** * 获取设备响应信息 * @param respEvnt */ public void readResponse(ResponseEvent respEvnt) { if (respEvnt != null && respEvnt.getResponse() != null) { @SuppressWarnings("unchecked") Vector<VariableBinding> recVBs = respEvnt.getResponse() .getVariableBindings(); for (int i = 0; i < recVBs.size(); i++) { VariableBinding recVB = recVBs.elementAt(i); System.out.println(recVB.getOid() + " : " + recVB.getVariable()); } } } public static void main(String[] args) { try { SnmpUtils util = new SnmpUtils(); // util.initComm(); util.setPDU(); util.getPDU(); } catch (IOException e) { e.printStackTrace(); } } }
测试显示结果如下: