📄 snmpv1trapsend.java~1~
字号:
package zjkdata;
import com.adventnet.snmp.snmp2.SnmpAPI;
import com.adventnet.snmp.snmp2.*;
import java.sql.*;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.lang.*;
import java.net.*;
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};
if (Udpdate[1].trim().equals("4")) {
Udpdate[1] = "开关量";
}
if (Udpdate[1].trim().equals("5")) {
Udpdate[1] = "模拟量";
}
if (Udpdate[3].trim().equals("1")) {
Udpdate[3] = "未知告警类型";
}
if (Udpdate[3].trim().equals("2")) {
Udpdate[3] = "正常";
}
if (Udpdate[3].trim().equals("3")) {
Udpdate[3] = "越上限";
}
if (Udpdate[3].trim().equals("4")) {
Udpdate[3] = "越上上限";
}
if (Udpdate[3].trim().equals("5")) {
Udpdate[3] = "越下限";
}
if (Udpdate[3].trim().equals("6")) {
Udpdate[3] = "越下下限";
}
if (Udpdate[3].trim().equals("7")) {
Udpdate[3] = "开关跳变";
}
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
DBUrl = "jdbc:odbc:datalink";
conn = DriverManager.getConnection(DBUrl);
stmt = conn.createStatement();
rsSQLSelect = stmt.executeQuery(
"select * from zgtable where zgname like '" + Udpdate[27] + "%'");
if (rsSQLSelect.next()) {
Udpdate[27] = rsSQLSelect.getString("zgname");
}
rsSQLSelect = stmt.executeQuery(
"select * from devicetype where typename = '" + Udpdate[20].trim() +
"'");
if (rsSQLSelect.next()) {
Udpdate[20] = Udpdate[20] + "." + rsSQLSelect.getString("deviceid");
}
conn.close();
stmt.close();
}
catch (Exception eee) {
}
System.out.println(TrapIp + "___" + TrapPort);
for (int i = 0; i < 28; i++) {
try {
System.out.println(Udpdate[i]);
Udpdate[i] = URLEncoder.encode(Udpdate[i].trim(), "UTF-8");
}
catch (java.io.UnsupportedEncodingException ex) {
}
}
String varBind[] = {
TrapIp, "1234", "NanRui", "6", "1", "0",
".1.3.6.1.2.1.1234.1.1", "STRING", Udpdate[0],
".1.3.6.1.2.1.1234.1.2", "STRING", Udpdate[1],
".1.3.6.1.2.1.1234.1.3", "STRING", Udpdate[2],
".1.3.6.1.2.1.1234.1.4", "STRING", Udpdate[3],
".1.3.6.1.2.1.1234.1.5", "STRING", Udpdate[4],
".1.3.6.1.2.1.1234.1.6", "STRING", Udpdate[5],
".1.3.6.1.2.1.1234.1.7", "STRING", Udpdate[6],
".1.3.6.1.2.1.1234.1.8", "STRING", Udpdate[7],
".1.3.6.1.2.1.1234.1.9", "STRING", Udpdate[8],
".1.3.6.1.2.1.1234.1.10", "STRING", Udpdate[9],
".1.3.6.1.2.1.1234.1.11", "STRING", Udpdate[10],
".1.3.6.1.2.1.1234.1.12", "STRING", Udpdate[11],
".1.3.6.1.2.1.1234.1.13", "STRING", Udpdate[12],
".1.3.6.1.2.1.1234.1.14", "STRING", Udpdate[13],
".1.3.6.1.2.1.1234.1.15", "STRING", Udpdate[14],
".1.3.6.1.2.1.1234.1.16", "STRING", Udpdate[15],
".1.3.6.1.2.1.1234.1.17", "STRING", Udpdate[16],
".1.3.6.1.2.1.1234.1.18", "STRING", Udpdate[17],
".1.3.6.1.2.1.1234.1.19", "STRING", Udpdate[18],
".1.3.6.1.2.1.1234.1.20", "STRING", Udpdate[19],
".1.3.6.1.2.1.1234.1.21", "STRING", Udpdate[20],
".1.3.6.1.2.1.1234.1.22", "STRING", Udpdate[21],
".1.3.6.1.2.1.1234.1.23", "STRING", Udpdate[22],
".1.3.6.1.2.1.1234.1.24", "STRING", Udpdate[23],
".1.3.6.1.2.1.1234.1.25", "STRING", Udpdate[24],
".1.3.6.1.2.1.1234.1.26", "STRING", Udpdate[25],
".1.3.6.1.2.1.1234.1.27", "STRING", Udpdate[26],
".1.3.6.1.2.1.1234.1.28", "STRING", Udpdate[27],
};
api = new SnmpAPI();
api.start();
if (values[0].equals("Set")) {
api.setDebug(true);
}
SnmpSession session = new SnmpSession(api);
session.setPeername(varBind[0]);
if (values[1] != null)
session.setCommunity(values[1]);
try {
if (TrapPort != null)
session.setRemotePort(Integer.parseInt(TrapPort));
else
session.setRemotePort(162);
}
catch (NumberFormatException ex) {
System.err.println("Invalid Integer Arg. for port number");
return;
}
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])); // set specific code
if (varBind.length > 4)
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 + -