天天看点

SNMP4j 发送 trap v2,v3

关于snmp的资料网上还是挺少的,我也是弄了很久才把snmp trap v3实现好。

snmptrapd.conf文件配置如下所示:

SNMP4j 发送 trap v2,v3

查看输出命令:snmptrapd -f -C -c /etc/snmp/snmptrapd.conf -Le udp:162

trap v2命令:snmptrap -v 2c -c public localhost:162 “” 1.3.6.1.4.1.48183 1.3.6.1.4.1.48183.1 s “smartmon”

trap v3命令:snmptrap -e 0x8000000001020304 -v 3 -u myuser -a MD5 -A mypassword -x AES -X mypassword1 -l authPriv localhost:162 “” 1.3.6.1.4.1.48183 1.3.6.1.4.1.48183.1 s “smartmon”

trap 接受内容如下图所示:

SNMP4j 发送 trap v2,v3

SNMP4J trap v2:

package smartmon.falcon.remote.snmp;

import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.IpAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

import java.util.Date;

public class SnmpV2TrapTest {
  public static void main(String[] args) {
    sendSnmpV2Trap();
  }

  public static void sendSnmpV2Trap() {
    String community = "public";
    String trapOid = ".1.3.6.1.4.1.48183";
    String trapOid1 = ".1.3.6.1.4.1.48183.1";
    String trapOid2 = ".1.3.6.1.4.1.48183.2";
    String trapOid3 = ".1.3.6.1.4.1.48183.3";

    String ipAddress = "172.24.8.133";
    int port = 162;
    try {
      //Create Transport Mapping
      TransportMapping<UdpAddress> transport = new DefaultUdpTransportMapping();
      transport.listen();

      //Create Target
      CommunityTarget comtarget = new CommunityTarget();
      comtarget.setCommunity(new OctetString(community));
      comtarget.setVersion(SnmpConstants.version2c);
      comtarget.setAddress(new UdpAddress(ipAddress + "/" + port));
      comtarget.setRetries(2);
      comtarget.setTimeout(5000);

      //Create PDU for V2
      PDU pdu = new PDU();

      // need to specify the system up time
      pdu.add(new VariableBinding(SnmpConstants.sysUpTime, new OctetString(new Date().toString())));
      pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID(trapOid)));
      pdu.add(new VariableBinding(new OID(trapOid1), new OctetString("smartmon")));
      pdu.add(new VariableBinding(new OID(trapOid2), new OctetString("host133")));
      pdu.add(new VariableBinding(new OID(trapOid3), new OctetString("192.168.135.9")));
      pdu.add(new VariableBinding(SnmpConstants.snmpTrapAddress, new IpAddress(ipAddress)));

      // variable binding for Enterprise Specific objects, Severity (should be defined in MIB file)
      pdu.add(new VariableBinding(new OID(trapOid), new OctetString("Major")));
      pdu.setType(PDU.NOTIFICATION);

      //Send the PDU
      Snmp snmp = new Snmp(transport);
      System.out.println("Sending V2 Trap to " + ipAddress + " on Port " + port);
      snmp.send(pdu, comtarget);
      snmp.close();
    } catch (Exception e) {
      System.err.println("Error in Sending V2 Trap to " + ipAddress + " on Port " + port);
      System.err.println("Exception Message = " + e.getMessage());
    }
  }
}
           

SNMP4J trap v3:

package smartmon.falcon.remote.today;

import org.snmp4j.PDU;
import org.snmp4j.ScopedPDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.UserTarget;
import org.snmp4j.mp.MPv3;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.AuthMD5;
import org.snmp4j.security.PrivAES128;
import org.snmp4j.security.SecurityLevel;
import org.snmp4j.security.SecurityModels;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
import org.snmp4j.security.UsmUser;
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;

import java.io.IOException;
import java.util.Date;

/**
 * 本类用于发送v3 Trap信息
 */
public class SnmpV3TrapTest {

  private Snmp snmp = null;

  private Address targetAddress = null;

  public SnmpSendV3Trap(String target, int port) throws IOException {
    //配置EngineID
    OctetString localEngineID = new OctetString(MPv3.createLocalEngineID());
    // 设置目的地的IP和端口
    targetAddress = GenericAddress.parse(String.format("udp:%s/%s", target, port));
    TransportMapping transport = new DefaultUdpTransportMapping();
    //配置USM,传入 engine id
    USM usm = new USM(SecurityProtocols.getInstance(), localEngineID, 0);
    //添加USM到Security Models中
    SecurityModels.getInstance().addSecurityModel(usm);
    //构建SNMP对象, 并且添加v3模式到其中
    snmp = new Snmp(transport);
    //添加Usm user, 这里的配置的user, 目标接收端也正确认证才可以接受到这个trap
    //这里创建User的是全部使用null是表示使用NOAUTH_NOPRIV的模式发送trap
    //具体的Auth模式可以参考收集snmp数据,是一样配置的。
    snmp.getUSM().addUser(new OctetString("myuser"),
      OctetString.fromString("8000000001020304", 16), new UsmUser(new OctetString("myuser"),
        AuthMD5.ID,
        new OctetString("mypassword"),
        PrivAES128.ID,
        new OctetString("mypassword1")));
    //set the same engine to localEngineId
    snmp.setLocalEngine(OctetString.fromString("8000000001020304", 16).getValue(),0,0);
    snmp.getMessageDispatcher().addMessageProcessingModel(new MPv3(usm));
    transport.listen();

  }

  /**
   * 向管理进程发送Trap报文
   */
  public void sendPDU() throws IOException {

    // 设置 target, v3 trap 一定要使用UserTarget
    UserTarget target = new UserTarget();
    target.setAddress(targetAddress);
    target.setVersion(SnmpConstants.version3);
    //设置用户认证信息
    target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
    target.setSecurityName(new OctetString("myuser"));

    // 创建 PDU, 注意这里要使用ScopedPDU
    ScopedPDU pdu = new ScopedPDU();
    pdu.add(new VariableBinding(SnmpConstants.sysUpTime, new OctetString(new Date().toString())));
    pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID("1.3.6.1.4.1.48183")));
    pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.48183.0"),
      new OctetString("SnmpTrapsdfsa")));
    pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.48183.1"),
      new OctetString("v3adf")));
    pdu.setType(PDU.TRAP);
    // 发送trap
    snmp.send(pdu, target);
  }

  public static void main(String[] args) {
    try {
      SnmpSendV3Trap trapSender = new SnmpSendV3Trap("172.24.8.133", 162);
      trapSender.sendPDU();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

           

下面是一些关于学习snmp的链接:

1.snmp4j官网:https://agentpp.com/

2.net-snmp官网:http://www.net-snmp.org/

遇到不解可以上stackoverflow或者snmp4j官网论坛提问

https://forum.snmp.app/c/snmp4j

https://stackoverflow.com/