⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snmpv1trapsend.java

📁 这是一个udp接收程序
💻 JAVA
字号:
package zjkdata;

import java.net.*;
import java.sql.*;
import com.adventnet.snmp.snmp2.*;

public class SnmpV1TrapSend {
  private SnmpAPI api;
  private String DBUrl = null;
  private Connection conn = null;
  private Statement stmt = null;
  private ResultSet rsSQLSelect = null;

  public void TrapSend(String Udpdate[], String TrapIp, String TrapPort) {
    String values[] = {
        "None", null, null};

    try {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      DBUrl = "jdbc:odbc:datalink";
      conn = DriverManager.getConnection(DBUrl);
      stmt = conn.createStatement();
      int alarmID = Integer.parseInt(Udpdate[5]);
      rsSQLSelect = stmt.executeQuery(
          " select * from alarmmsg where  AlarmGroup = '" + Udpdate[4] +
          "' and AlarmID='" + alarmID + "'");

      if (rsSQLSelect.next()) {
        //中文告警信息
        //Udpdate[12] = rsSQLSelect.getString("AlarmZnInfo");

        if (Udpdate[12] == null) {
          Udpdate[12] = " ";
        }
      }
      conn.close();
      stmt.close();
    }
    catch (Exception eee) {
    }
    //System.out.println(Udpdate[12]);
    //System.out.println(TrapIp + "___" + TrapPort);

    for (int i = 0; i < 13; i++) {
      try {
        Udpdate[i] = URLEncoder.encode(Udpdate[i].trim(), "utf-8");
      }
      catch (java.io.UnsupportedEncodingException ex) {
      }
    }
    String varBind[] = {
        TrapIp, "1234", "192.168.0.11", "6", "4", "987897",
        ".1.3.6.1.2.1.1234.4.1", "STRING", Udpdate[0],
        ".1.3.6.1.2.1.1234.4.2", "STRING", Udpdate[1],
        ".1.3.6.1.2.1.1234.4.3", "STRING", Udpdate[2],
        ".1.3.6.1.2.1.1234.4.4", "STRING", Udpdate[3],
        ".1.3.6.1.2.1.1234.4.5", "STRING", Udpdate[4],
        ".1.3.6.1.2.1.1234.4.6", "STRING", Udpdate[5],
        ".1.3.6.1.2.1.1234.4.7", "STRING", Udpdate[6],
        ".1.3.6.1.2.1.1234.4.8", "STRING", Udpdate[7],
        ".1.3.6.1.2.1.1234.4.9", "STRING", Udpdate[8],
        ".1.3.6.1.2.1.1234.4.10", "STRING", Udpdate[9],
        ".1.3.6.1.2.1.1234.4.11", "STRING", Udpdate[10],
        ".1.3.6.1.2.1.1234.4.12", "STRING", Udpdate[11],
        ".1.3.6.1.2.1.1234.4.13", "STRING", Udpdate[12],
    };
    api = new SnmpAPI();
    api.start();
    if (values[0].equals("Set")) {
      api.setDebug(true);
    }
    UDPProtocolOptions options = new UDPProtocolOptions();
    options.setRemoteHost(varBind[0]);
    options.setRemotePort(162);
    try {
      if (TrapPort != null) {
        options.setRemotePort(Integer.parseInt(TrapPort));
      }
      else {
        options.setRemotePort(162);
      }
    }
    catch (NumberFormatException ex) {
      System.err.println("Trap端口设置出错!");
      return;
    }
    SnmpSession session = new SnmpSession(api);
    session.setProtocolOptions(options);
    if (values[1] != null) {
      session.setCommunity(values[1]);
    }
    SnmpPDU pdu = new SnmpPDU();
    pdu.setCommand(api.TRP_REQ_MSG);
    try {
      pdu.setEnterprise(new SnmpOID(varBind[1])); // set agent address
      pdu.setAgentAddress(InetAddress.getByName(varBind[2])); // set generic trap type
      pdu.setTrapType(Integer.parseInt(varBind[3]));
      if (varBind.length > 4) { // set specific code
        pdu.setSpecificType(Integer.parseInt(varBind[4])); // set time-stamp
      }
      if (varBind.length > 5) {
        pdu.setUpTime(Integer.parseInt(varBind[5]));
      }
    }
    catch (Exception ex) {
      System.err.println("error in one or more required fields: " + ex);
    }
    for (int i = 6; i < varBind.length; ) {
      if (varBind.length < i + 3) {
        //System.out.println("usage_error");
      }
      SnmpOID oid = new SnmpOID(varBind[i++]);
      if (oid.toValue() == null) {
        System.err.println("Invalid OID argument: " + varBind[i]);
        return;
      }
      else {
        addVarBind(pdu, oid, varBind[i++], varBind[i++]);
      }
    } // end of add variable bindings
    try {
      session.open(); // open session
      session.send(pdu); // Send PDU
    }
    catch (SnmpException e) {
      System.err.println("Sending PDU" + e.getMessage());
    }
    api.close();
  }
  static void addVarBind(SnmpPDU pdu, SnmpOID oid, String type, String value) {
    byte dataType;
    if (type.equals("INTEGER")) {
      dataType = SnmpAPI.INTEGER;
    }
    else if (type.equals("STRING")) {
      dataType = SnmpAPI.STRING;
    }
    else if (type.equals("GAUGE")) {
      dataType = SnmpAPI.GAUGE;
    }
    else if (type.equals("TIMETICKS")) {
      dataType = SnmpAPI.TIMETICKS;
    }
    else if (type.equals("OPAQUE")) {
      dataType = SnmpAPI.OPAQUE;
    }
    else if (type.equals("IPADDRESS")) {
      dataType = SnmpAPI.IPADDRESS;
    }
    else if (type.equals("COUNTER")) {
      dataType = SnmpAPI.COUNTER;
    }
    else if (type.equals("OID")) {
      dataType = SnmpAPI.OBJID;
    }
    else { // unknown type
      System.err.println("Invalid variable type: " + type);
      return;
    }

    SnmpVar var = null;
    try {
      // create variable
      var = SnmpVar.createVariable(value, dataType);
    }
    catch (SnmpException e) {
      System.err.println("Cannot create variable: " + oid + " with value: " +
                         value);
      return;
    }
    // create varbind
    SnmpVarBind varbind = new SnmpVarBind(oid, var);
    // add variable binding
    pdu.addVariableBinding(varbind);
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -