欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

adventnet trap

程序员文章站 2022-07-12 19:33:30
...
研究了adventnet  的API接口应用,走了很多弯路,为了给大家做指导,特此记录。

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

Using Low-Level API
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,这种不适合我的需要,我目前是直接分发出去。所以用高版本。

Using High-Level API

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());
        }
    }
}