adventnet trap

研究了adventnet 的API接口应用,走了很多弯路,为了给大家做指导,特此记录。

首先,adventnet分为高版本和低版本的API。以trap为例:高版本Using High-Level API,是在低版本的基础上在封装了一层,对于底版本我必须知道agent的upd端口号和地址,而高版本不需要知道,它会自动分发到agent端。

[color=red][b]Using Low-Level API[/b][/color]
public class Sendv2cTrap  
{

public static void main(String args[])
{
if( args.length == 0)
{
System.out.println("Usage : java SnmpSendTrap hostname");
System.exit(0);
}

// 需要发给哪个主机的IP地址
String remoteHost = args[0];

// Start SNMP API
SnmpAPI api = new SnmpAPI();
api.setDebug(true);
SnmpSession session = new SnmpSession(api);

// set remote Host
UDPProtocolOptions option = new UDPProtocolOptions(hostname);
SnmpPDU pdu = new SnmpPDU();
pdu.setProtocolOptions(option);

// 需要发给哪个主机的端口号
option.setRemotePort(8001);

//open the session
try
{
session.open();
}
catch (SnmpException e )
{
System.err.println("Error opening socket: "+e);
}

// Build SNMPv2c Trap PDU
pdu.setCommand(SnmpAPI.TRP2_REQ_MSG );

//参数一
SnmpVar var = null;
SnmpOID oid = new SnmpOID(".1.3.6.1.2.1.1.3.0");
try
{
//create SnmpVar instance
var = SnmpVar.createVariable(systemuptime, SnmpAPI.TIMETICKS);
}
catch(SnmpException e)
{
System.err.println("Cannot create variable: " + oid + " with value: " + systemuptime);
return;
}

//create varbind
SnmpVarBind varbind = new SnmpVarBind(oid,var);

// add variable binding
pdu.addVariableBinding(varbind);

//参数二
oid = new SnmpOID(".1.3.6.1.6.3.1.1.4.1.0");
try
{
//create SnmpVar instance
var = SnmpVar.createVariable(trapoidvalue,SnmpAPI.OBJID);
}
catch(SnmpException e)
{
System.err.println("Cannot create variable: " + oid + " with value: " + trapoidvalue);
return;
}

//create varbind
SnmpVarBind varbind = new SnmpVarBind(oid,var);

// add variable binding
pdu.addVariableBinding(varbind);

// send PDU
try
{
session.send(pdu);
}
catch (SnmpException e)
{
System.err.println("Sending PDU"+e.getMessage());
}

System.exit(0);


}



以上是低版本,一般来说,是我知道对方的IP地址在做trap,这种不适合我的需要,我目前是直接分发出去。所以用高版本。

[color=red][b]Using High-Level API[/b][/color]

public class Sendv2Trap {

private static final String OID = "1.3.6.1.2.33779.1";

public static void main(String args[]) {

// instantiate SNMP target bean
SnmpTarget target = new SnmpTarget();
//本机主机地址
target.settargetHost("hostname");

//端口号
target.setTargetPort(162);

//Set the SNMP version.
target.setSnmpVersion(target.VERSION2C);

//值为public
target.setCommunity(community);

//测试值
String values[] = {"testing"};

try
{
//创建snmpoid
SnmpOID snmpOID = new SnmpOID(OID);
//通过oid绑定参数和变量
SnmpVar snmpVar = SnmpVar.createVariable("test Trap", SnmpAPI.STRING);
//发送trap
this.eventAPI.snmpSendNotification(1000, snmpOID, new SnmpVar[] { snmpVar }); }
catch (Exception e)
{
System.err.println("Error Sending Trap: "+e.getMessage());
}
System.exit(0);
}



最后就是接收端接收了。
public class SnmpTrapd implements TrapListener {
public static void main(String args[]) {
// instantiate SNMP Trap Receiver bean
SnmpTrapReceiver trapreceiver = new SnmpTrapReceiver();
// set the port in which the trap is received
trapreceiver.setPort(162);
// register the listener for trap events
trapreceiver.addTrapListener(new SnmpTrapd());
System.out.println("Waiting to receive traps .......");

}

@Override
public void receivedTrap(TrapEvent trap) {
System.out.println("Got a trap from: " + trap.getRemoteHost());
// print PDU details
System.out.println(((SnmpTrapReceiver) trap.getSource()).getMibOperations().toString(trap.getTrapPDU()));
if (trap.getTrapPDU().getCommand() == SnmpAPI.TRP_REQ_MSG) {
com.adventnet.snmp.mibs.MibTrap trapDefn = trap.getTrapDefinition();
if (trapDefn != null) // print name and description
System.out.println("Trap Name: " + trapDefn.getName() + "\nDescr: " + trapDefn.getDescription());
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值