目录:
- 环境参数
- 代码示例
- 同步WALK
- 异步WALK
[一]、环境参数
- jdk 1.6.0_18
- snmp4j 1.11.3
[二]、代码示例
1.同步WALK(v2c)
代码:SnmpWalk.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
package com.micmiu.snmp4j.demo1x; import java.io.IOException; 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.Integer32; import org.snmp4j.smi.Null; import org.snmp4j.smi.OID; import org.snmp4j.smi.OctetString; import org.snmp4j.smi.VariableBinding; import org.snmp4j.transport.DefaultUdpTransportMapping; /** * 演示:WALK的方式获取值 * * blog http://www.micmiu.com * * @author Michael */ public class SnmpWalk { public static final int DEFAULT_VERSION = SnmpConstants.version2c; public static final String DEFAULT_PROTOCOL = "udp"; public static final int DEFAULT_PORT = 161; public static final long DEFAULT_TIMEOUT = 3 * 1000L; public static final int DEFAULT_RETRY = 3; /** * 创建对象communityTarget * * @param targetAddress * @param community * @param version * @param timeOut * @param retry * @return CommunityTarget */ public static CommunityTarget createDefault(String ip, String community) { Address address = GenericAddress.parse(DEFAULT_PROTOCOL + ":" + ip + "/" + DEFAULT_PORT); CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString(community)); target.setAddress(address); target.setVersion(DEFAULT_VERSION); target.setTimeout(DEFAULT_TIMEOUT); // milliseconds target.setRetries(DEFAULT_RETRY); return target; } /** * @param ip * @param community * @param oid */ public static void snmpWalk(String ip, String community, String targetOid) { CommunityTarget target = SnmpUtil.createDefault(ip, community); TransportMapping transport = null; Snmp snmp = null; try { transport = new DefaultUdpTransportMapping(); snmp = new Snmp(transport); transport.listen(); PDU pdu = new PDU(); OID targetOID = new OID(targetOid); pdu.add(new VariableBinding(targetOID)); boolean finished = false; System.out.println("----> demo start <----"); while (!finished) { VariableBinding vb = null; ResponseEvent respEvent = snmp.getNext(pdu, target); PDU response = respEvent.getResponse(); if (null == response) { System.out.println("responsePDU == null"); finished = true; break; } else { vb = response.get(0); } // check finish finished = checkWalkFinished(targetOID, pdu, vb); if (!finished) { System.out.println("==== walk each vlaue :"); System.out.println(vb.getOid() + " = " + vb.getVariable()); // Set up the variable binding for the next entry. pdu.setRequestID(new Integer32(0)); pdu.set(0, vb); } else { System.out.println("SNMP walk OID has finished."); snmp.close(); } } System.out.println("----> demo end <----"); } catch (Exception e) { e.printStackTrace(); System.out.println("SNMP walk Exception: " + e); } finally { if (snmp != null) { try { snmp.close(); } catch (IOException ex1) { snmp = null; } } } } /** * 1)responsePDU == null<br> * 2)responsePDU.getErrorStatus() != 0<br> * 3)responsePDU.get(0).getOid() == null<br> * 4)responsePDU.get(0).getOid().size() < targetOID.size()<br> * 5)targetOID.leftMostCompare(targetOID.size(),responsePDU.get(0).getOid()) * !=0<br> * 6)Null.isExceptionSyntax(responsePDU.get(0).getVariable().getSyntax())<br> * 7)responsePDU.get(0).getOid().compareTo(targetOID) <= 0<br> * * @param resquestPDU * @param targetOID * @param responsePDU * @param vb * @return */ private static boolean checkWalkFinished(OID targetOID, PDU pdu, VariableBinding vb) { boolean finished = false; if (pdu.getErrorStatus() != 0) { System.out.println("[true] responsePDU.getErrorStatus() != 0 "); System.out.println(pdu.getErrorStatusText()); finished = true; } else if (vb.getOid() == null) { System.out.println("[true] vb.getOid() == null"); finished = true; } else if (vb.getOid().size() < targetOID.size()) { System.out.println("[true] vb.getOid().size() < targetOID.size()"); finished = true; } else if (targetOID.leftMostCompare(targetOID.size(), vb.getOid()) != 0) { System.out.println("[true] targetOID.leftMostCompare() != 0"); finished = true; } else if (Null.isExceptionSyntax(vb.getVariable().getSyntax())) { System.out .println("[true] Null.isExceptionSyntax(vb.getVariable().getSyntax())"); finished = true; } else if (vb.getOid().compareTo(targetOID) <= 0) { System.out.println("[true] Variable received is not " + "lexicographic successor of requested " + "one:"); System.out.println(vb.toString() + " <= " + targetOID); finished = true; } return finished; } /** * * @param args */ public static void main(String[] args) { String ip = "192.168.8.254"; String community = "public"; // 1.3.6.1.2.1.2.2.1.2 String targetOid = ".1.3.6.1.2.1.1"; SnmpWalk.snmpWalk(ip, community, targetOid); } } |
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
----> demo start <---- ==== walk each vlaue : 1.3.6.1.2.1.1.1.0 = H3C ICG2000 H3C Comware Platform Software Comware Software Version 5.20, Release 2104P02 Copyright(c) 2004-2010 Hangzhou H3C Technologies Co., Ltd. ==== walk each vlaue : 1.3.6.1.2.1.1.2.0 = 1.3.6.1.4.1.25506.1.287 ==== walk each vlaue : 1.3.6.1.2.1.1.3.0 = 39 days, 21:47:28.41 ==== walk each vlaue : 1.3.6.1.2.1.1.4.0 = R&D Hangzhou, Hangzhou H3C Technologies Co., Ltd. ==== walk each vlaue : 1.3.6.1.2.1.1.5.0 = HX_ICG2000 ==== walk each vlaue : 1.3.6.1.2.1.1.6.0 = Hangzhou, China ==== walk each vlaue : 1.3.6.1.2.1.1.7.0 = 78 [true] targetOID.leftMostCompare() != 0 SNMP walk OID has finished. ----> demo end <---- |
2.异步WALK(v2c)
代码:SnmpWalkAsyn.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
package com.micmiu.snmp4j.demo1x; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import org.snmp4j.CommunityTarget; import org.snmp4j.PDU; import org.snmp4j.Snmp; import org.snmp4j.event.ResponseEvent; import org.snmp4j.event.ResponseListener; import org.snmp4j.mp.SnmpConstants; import org.snmp4j.smi.Address; import org.snmp4j.smi.GenericAddress; import org.snmp4j.smi.Integer32; import org.snmp4j.smi.Null; import org.snmp4j.smi.OID; import org.snmp4j.smi.OctetString; import org.snmp4j.smi.VariableBinding; import org.snmp4j.transport.DefaultUdpTransportMapping; /** * 演示:异步GET OID值 * * blog http://www.micmiu.com * * @author Michael * */ public class SnmpWalkAsyn { public static final int DEFAULT_VERSION = SnmpConstants.version2c; public static final String DEFAULT_PROTOCOL = "udp"; public static final int DEFAULT_PORT = 161; public static final long DEFAULT_TIMEOUT = 3 * 1000L; public static final int DEFAULT_RETRY = 3; /** * 创建对象communityTarget * * @param targetAddress * @param community * @param version * @param timeOut * @param retry * @return CommunityTarget */ public static CommunityTarget createDefault(String ip, String community) { Address address = GenericAddress.parse(DEFAULT_PROTOCOL + ":" + ip + "/" + DEFAULT_PORT); CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString(community)); target.setAddress(address); target.setVersion(DEFAULT_VERSION); target.setTimeout(DEFAULT_TIMEOUT); // milliseconds target.setRetries(DEFAULT_RETRY); return target; } /** * 异步采集信息 * * @param ip * @param community * @param oid */ public static void snmpAsynWalk(String ip, String community, String oid) { final CommunityTarget target = createDefault(ip, community); Snmp snmp = null; try { System.out.println("----> demo start <----"); DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping(); snmp = new Snmp(transport); snmp.listen(); final PDU pdu = new PDU(); final OID targetOID = new OID(oid); final CountDownLatch latch = new CountDownLatch(1); pdu.add(new VariableBinding(targetOID)); ResponseListener listener = new ResponseListener() { public void onResponse(ResponseEvent event) { ((Snmp) event.getSource()).cancel(event.getRequest(), this); try { PDU response = event.getResponse(); // PDU request = event.getRequest(); // System.out.println("[request]:" + request); if (response == null) { System.out.println("[ERROR]: response is null"); } else if (response.getErrorStatus() != 0) { System.out.println("[ERROR]: response status" + response.getErrorStatus() + " Text:" + response.getErrorStatusText()); } else { System.out .println("Received Walk response value :"); VariableBinding vb = response.get(0); boolean finished = checkWalkFinished(targetOID, pdu, vb); if (!finished) { System.out.println(vb.getOid() + " = " + vb.getVariable()); pdu.setRequestID(new Integer32(0)); pdu.set(0, vb); ((Snmp) event.getSource()).getNext(pdu, target, null, this); } else { System.out .println("SNMP Asyn walk OID value success !"); latch.countDown(); } } } catch (Exception e) { e.printStackTrace(); latch.countDown(); } } }; snmp.getNext(pdu, target, null, listener); System.out.println("pdu 已发送,等到异步处理结果..."); boolean wait = latch.await(30, TimeUnit.SECONDS); System.out.println("latch.await =:" + wait); snmp.close(); System.out.println("----> demo end <----"); } catch (Exception e) { e.printStackTrace(); System.out.println("SNMP Asyn Walk Exception:" + e); } } private static boolean checkWalkFinished(OID walkOID, PDU pdu, VariableBinding vb) { boolean finished = false; if (pdu.getErrorStatus() != 0) { System.out.println("[true] pdu.getErrorStatus() != 0 "); System.out.println(pdu.getErrorStatusText()); finished = true; } else if (vb.getOid() == null) { System.out.println("[true] vb.getOid() == null"); finished = true; } else if (vb.getOid().size() < walkOID.size()) { System.out.println("[true] vb.getOid().size() < targetOID.size()"); finished = true; } else if (walkOID.leftMostCompare(walkOID.size(), vb.getOid()) != 0) { System.out.println("[true] targetOID.leftMostCompare() != 0"); finished = true; } else if (Null.isExceptionSyntax(vb.getVariable().getSyntax())) { System.out .println("[true] Null.isExceptionSyntax(vb.getVariable().getSyntax())"); finished = true; } else if (vb.getOid().compareTo(walkOID) <= 0) { System.out.println("[true] vb.getOid().compareTo(walkOID) <= 0 "); finished = true; } return finished; } /** * * @param args */ public static void main(String[] args) { String ip = "192.168.8.254"; String community = "public"; List<String> oidList = new ArrayList<String>(); oidList.add(".1.3.6.1.2.1.1.1.0"); oidList.add(".1.3.6.1.2.1.1.3.0"); oidList.add(".1.3.6.1.2.1.1.5.0"); // 异步采集数据 SnmpWalkAsyn.snmpAsynWalk(ip, community, "1.3.6.1.2.1.1"); } } |
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
----> demo start <---- pdu 已发送,等到异步处理结果... Received Walk response value : 1.3.6.1.2.1.1.1.0 = H3C ICG2000 H3C Comware Platform Software Comware Software Version 5.20, Release 2104P02 Copyright(c) 2004-2010 Hangzhou H3C Technologies Co., Ltd. Received Walk response value : 1.3.6.1.2.1.1.2.0 = 1.3.6.1.4.1.25506.1.287 Received Walk response value : 1.3.6.1.2.1.1.3.0 = 39 days, 21:52:48.80 Received Walk response value : 1.3.6.1.2.1.1.4.0 = R&D Hangzhou, Hangzhou H3C Technologies Co., Ltd. Received Walk response value : 1.3.6.1.2.1.1.5.0 = HX_ICG2000 Received Walk response value : 1.3.6.1.2.1.1.6.0 = Hangzhou, China Received Walk response value : 1.3.6.1.2.1.1.7.0 = 78 Received Walk response value : [true] targetOID.leftMostCompare() != 0 SNMP Asyn walk OID value success ! latch.await =:true ----> demo end <---- |
—
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
本文链接地址: http://www.micmiu.com/enterprise-app/snmp/snmp4j-walk/
您好,我测试了一下发现遇到跳跃的OID就会中断WALK?
卤煮好,可以像MIB walk工具一样,直接通过oid,扫出oid所对应的对象名称么?比如,通过各端口的oid,得到端口的名称。。。
我自己找到方法啦,通过不同的oid去遍历就行
当然可以
楼主,请问磁盘io吞吐量的oid是多少啊(linux)
不好意思没有采集过,这个估计私有mib才可能支持吧
你好!通过你这个程序可以获取每个根节点下面的oid,但是每个oid对应的具体的指标名我们如何知道呢?
这个oid 对应的涵义公有部分是标准有文档可以查阅,如果是私有mib 需要查厂商文档