天天看點

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/