`
avery_leo
  • 浏览: 382358 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

snmp4j获取数据实例(二)之SnmpTrap示例

    博客分类:
  • JAVA
阅读更多

snmp4j的jar包可以在它的官方网站http://www.snmp4j.org/上下载。

 

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; public class SnmpUtil { private Snmp snmp = null; private Address targetAddress = null; public void initComm() throws IOException { // 设置Agent方的IP和端口 targetAddress = GenericAddress.parse("udp:192.168.7.203/161"); TransportMapping transport = new DefaultUdpTransportMapping(); snmp = new Snmp(transport); transport.listen(); } public void sendPDU() throws IOException { // 设置 target CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString("public")); target.setAddress(targetAddress); // 通信不成功时的重试次数 target.setRetries(2); // 超时时间 target.setTimeout(1500); target.setVersion(SnmpConstants.version1); // 创建 PDU PDU pdu = new PDU(); pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 5, 0 }))); // MIB的访问方式 pdu.setType(PDU.GET); // 向Agent发送PDU,并接收Response ResponseEvent respEvnt = snmp.send(pdu, target); // 解析Response if (respEvnt != null && respEvnt.getResponse() != null) { 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 { SnmpUtil util = new SnmpUtil(); util.initComm(); util.sendPDU(); } catch (IOException e) { e.printStackTrace(); } } }

 

 

  上面的这段代码直接参考snmp4j API说明文档中提供的例子,是一个最简单的snmp4j的应用。只要你的机器里安装了snmp通讯组件,上面的代码应该可以运行成功。

在上一个例子中,我们只做了读取的工作,接下来,我们进行一下设置操作,通过Snmp修改读取的机器名。
    public的默认权限是只读,要想进行写操作,我们必须进行手动的设置。具体的做法是:进入管理工具→服务,找到Snmp Service→属性→安全。在这个选项卡中我们可以看到public的权限是只读,你可以修改public的权限,也可以重新创建一个community。从安全角度来讲当然应该新建一个,在这里为了测试方便,我就直接给public添加写入权限了。
    接下来就可以编写代码了,我把上面的例子重构一下,代码如下:

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; public class SnmpUtil { private Snmp snmp = null; private Address targetAddress = null; public void initComm() throws IOException { // 设置Agent方的IP和端口 targetAddress = GenericAddress.parse("udp:192.168.7.203/161"); TransportMapping transport = new DefaultUdpTransportMapping(); snmp = new Snmp(transport); transport.listen(); } public ResponseEvent sendPDU(PDU pdu) throws IOException { // 设置 target CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString("public")); target.setAddress(targetAddress); // 通信不成功时的重试次数 target.setRetries(2); // 超时时间 target.setTimeout(1500); target.setVersion(SnmpConstants.version1); // 向Agent发送PDU,并返回Response return snmp.send(pdu, target); } public void setPDU() throws IOException { // set PDU PDU pdu = new PDU(); pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 5, 0 }), new OctetString("SNMPTEST"))); pdu.setType(PDU.SET); sendPDU(pdu); } 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 })));//pcName pdu.setType(PDU.GET); readResponse(sendPDU(pdu)); } public void readResponse(ResponseEvent respEvnt) { // 解析Response if (respEvnt != null && respEvnt.getResponse() != null) { 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 { SnmpUtil util = new SnmpUtil(); util.initComm(); util.setPDU(); util.getPDU(); } catch (IOException e) { e.printStackTrace(); } } }

 

 

   接收trap

snmp4j提供了一个抽象类CommandResponder类用于接收trap,这个类里面有一个必须实现的方法processPdu(),当接收到trap,会自动进入这个方法,因此我们可以将对trap的处理写在这里。

下面修改上例子中的initComm()方法:

 

private TransportMapping transport = null; public void initComm() throws IOException { // 设置Agent方的IP和端口 targetAddress = GenericAddress.parse("udp:192.168.7.203/161"); // 设置接收trap的IP和端口 transport = new DefaultUdpTransportMapping(new UdpAddress( "192.168.7.12/162")); snmp = new Snmp(transport); CommandResponder trapRec = new CommandResponder() { public synchronized void processPdu(CommandResponderEvent e) { // 接收trap PDU command = e.getPDU(); if (command != null) { System.out.println(command.toString()); } } }; snmp.addCommandResponder(trapRec); transport.listen(); }

 

 

 

其中targetAddress指Agent端也就是trap发送,transport指trap接收方,这里就是本机,假设IP是192.168.7.203,但注意不能写成127.0.0.1。

 

因为我们无法得知trap什么时候会发送,所以需要有一个线程等待trap的到来,在这个例子中我们使用wait()来等待trap的到来,具体应用中就要根据实际情况来做了。

 

public synchronized void listen() { System.out.println("Waiting for traps.."); try { this.wait();//Wait for traps to come in } catch (InterruptedException ex) { System.out.println("Interrupted while waiting for traps: " + ex); System.exit(-1); } } public static void main(String[] args) { try { SnmpUtil util = new SnmpUtil(); util.initComm(); util.listen(); } catch (IOException e) { e.printStackTrace(); } }

 

将上面的代码添加到以上的例子中,就可以接收trap了。

 

但是还有一个问题,如何让192.168.7.203发送trap呢?这个也可以使用snmp4j来做。其实发送trap和发送set、get PDU是类似的,同样是发送PDU,只不过类型不一样。我们把前面的例子复制到192.168.7.203,在里面添加一段代码:

 

public void setTrap() throws IOException { // 构造Trap PDU PDU pdu = new PDU(); pdu.add(new VariableBinding(new OID(".1.3.6.1.2.3377.10.1.1.1.1"), new OctetString("SnmpTrap"))); pdu.setType(PDU.TRAP); sendPDU(pdu); System.out.println("Trap sent successfully."); }

 

 

 

这里PDU的OID和Value可以自己构造,无需使用特定的值。

然后修改地址
targetAddress = GenericAddress.parse("udp:192.168.7.203/162");
transport = new DefaultUdpTransportMapping();

 

另外需要修改target的version,即改为target.setVersion(SnmpConstants.version2c)

接下来修改main()函数,调用setTrap()。

然后回到本机运行刚才的例子,当控制台显示“Waiting for traps..”时,运行Agent端的例子。此时如果192.168.7.12打出我们刚刚设置的PDU的信息,就说明Trap的收发成功了。

分享到:
评论
1 楼 david.org 2010-05-06  
hi, 楼主, 我取了一组IfOutOctets数据, 但怎么算都和Cacti不同呀, 我想算出某网卡流出速度.
不知道你有没有做过.
dongtalk@gmail.com

相关推荐

    TCP-IP详解卷一:协议

    第25章 SNMP:简单网络管理协议 270 25.1 引言 270 25.2 协议 270 25.3 管理信息结构 272 25.4 对象标识符 274 25.5 管理信息库介绍 274 25.6 实例标识 276 25.6.1 简单变量 276 25.6.2 表格 276 25.6.3 字典式排序 ...

    TCP_IP详解卷1

    第25章 SNMP:简单网络管理协议 270 25.1 引言 270 25.2 协议 270 25.3 管理信息结构 272 25.4 对象标识符 274 25.5 管理信息库介绍 274 25.6 实例标识 276 25.6.1 简单变量 276 25.6.2 表格 276 25.6.3 字典式排序 ...

    TCPIP详解卷[1].part04

    第25章 SNMP:简单网络管理协议 270 25.1 引言 270 25.2 协议 270 25.3 管理信息结构 272 25.4 对象标识符 274 25.5 管理信息库介绍 274 25.6 实例标识 276 25.6.1 简单变量 276 25.6.2 表格 276 25.6.3 字典式排序 ...

    TCPIP详解卷[1].part09

    第25章 SNMP:简单网络管理协议 270 25.1 引言 270 25.2 协议 270 25.3 管理信息结构 272 25.4 对象标识符 274 25.5 管理信息库介绍 274 25.6 实例标识 276 25.6.1 简单变量 276 25.6.2 表格 276 25.6.3 字典式排序 ...

    TCPIP详解卷[1].part03

    第25章 SNMP:简单网络管理协议 270 25.1 引言 270 25.2 协议 270 25.3 管理信息结构 272 25.4 对象标识符 274 25.5 管理信息库介绍 274 25.6 实例标识 276 25.6.1 简单变量 276 25.6.2 表格 276 25.6.3 字典式排序 ...

    TCPIP详解卷[1].part05

    第25章 SNMP:简单网络管理协议 270 25.1 引言 270 25.2 协议 270 25.3 管理信息结构 272 25.4 对象标识符 274 25.5 管理信息库介绍 274 25.6 实例标识 276 25.6.1 简单变量 276 25.6.2 表格 276 25.6.3 字典式排序 ...

    TCPIP详解卷[1].part06

    第25章 SNMP:简单网络管理协议 270 25.1 引言 270 25.2 协议 270 25.3 管理信息结构 272 25.4 对象标识符 274 25.5 管理信息库介绍 274 25.6 实例标识 276 25.6.1 简单变量 276 25.6.2 表格 276 25.6.3 字典式排序 ...

    TCP/IP详解part_2

    第25章 SNMP:简单网络管理协议 270 25.1 引言 270 25.2 协议 270 25.3 管理信息结构 272 25.4 对象标识符 274 25.5 管理信息库介绍 274 25.6 实例标识 276 25.6.1 简单变量 276 25.6.2 表格 276 25.6.3 字典式排序 ...

    TCPIP详解卷[1].part08

    第25章 SNMP:简单网络管理协议 270 25.1 引言 270 25.2 协议 270 25.3 管理信息结构 272 25.4 对象标识符 274 25.5 管理信息库介绍 274 25.6 实例标识 276 25.6.1 简单变量 276 25.6.2 表格 276 25.6.3 字典式排序 ...

    TCPIP详解卷[1].part11

    第25章 SNMP:简单网络管理协议 270 25.1 引言 270 25.2 协议 270 25.3 管理信息结构 272 25.4 对象标识符 274 25.5 管理信息库介绍 274 25.6 实例标识 276 25.6.1 简单变量 276 25.6.2 表格 276 25.6.3 字典式排序 ...

    TCPIP详解卷[1].part12

    第25章 SNMP:简单网络管理协议 270 25.1 引言 270 25.2 协议 270 25.3 管理信息结构 272 25.4 对象标识符 274 25.5 管理信息库介绍 274 25.6 实例标识 276 25.6.1 简单变量 276 25.6.2 表格 276 25.6.3 字典式排序 ...

    TCPIP详解卷[1].part07

    第25章 SNMP:简单网络管理协议 270 25.1 引言 270 25.2 协议 270 25.3 管理信息结构 272 25.4 对象标识符 274 25.5 管理信息库介绍 274 25.6 实例标识 276 25.6.1 简单变量 276 25.6.2 表格 276 25.6.3 字典式排序 ...

    TCPIP详解卷[1].part10

    第25章 SNMP:简单网络管理协议 270 25.1 引言 270 25.2 协议 270 25.3 管理信息结构 272 25.4 对象标识符 274 25.5 管理信息库介绍 274 25.6 实例标识 276 25.6.1 简单变量 276 25.6.2 表格 276 25.6.3 字典式排序 ...

    TCP-IP详解卷一:协议

    第25章 SNMP:简单网络管理协议 25.1 引言 25.2 协议 25.3 管理信息结构 25.4 对象标识符 25.5 管理信息库介绍 25.6 实例标识 25.6.1 简单变量 25.6.2 表格 25.6.3 字典式排序 25.7 一些简单的例子 25.7....

    TCPIP详解--共三卷

    第25章 SNMP:简单网络管理协议 270 25.1 引言 270 25.2 协议 270 25.3 管理信息结构 272 25.4 对象标识符 274 25.5 管理信息库介绍 274 25.6 实例标识 276 25.6.1 简单变量 276 25.6.2 表格 276 25.6.3 字典式排序 ...

    TCP/IP详解卷 pdf格式

    第25章 SNMP:简单网络管理协议 270 25.1 引言 270 25.2 协议 270 25.3 管理信息结构 272 25.4 对象标识符 274 25.5 管理信息库介绍 274 25.6 实例标识 276 25.6.1 简单变量 276 25.6.2 表格 276 25.6.3 字典式排序 ...

    TCP/IP详解 卷1:协议--源代码

    第25章 SNMP:简单网络管理协议 270 25.1 引言 270 25.2 协议 270 25.3 管理信息结构 272 25.4 对象标识符 274 25.5 管理信息库介绍 274 25.6 实例标识 276 25.6.1 简单变量 276 25.6.2 表格 276 ...

    TCP_IP协议详解卷一

    第25章 SNMP:简单网络管理协议 270 25.1 引言 270 25.2 协议 270 25.3 管理信息结构 272 25.4 对象标识符 274 25.5 管理信息库介绍 274 25.6 实例标识 276 25.6.1 简单变量 276 25.6.2 表格 276 25.6.3 字典式排序 ...

Global site tag (gtag.js) - Google Analytics