snmpget请求获取服务器参数(snmpv3) 并连接MySQL数据库

完整的项目代码:

1、连接MySQL数据库 
 

package mysnmp;
 
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.PreparedStatement;  
import java.sql.SQLException;  
 /**
  * 连接MySQL数据库 
  * @author daopinz
  *
  */
public class ConnectBase {  
    public static final String url = "jdbc:mysql://数据库地址:端口号/库名";  
    public static final String name = "com.mysql.jdbc.Driver";  
    public static final String user = "用户名";  
    public static final String password = "用户密码";  
  
    public Connection conn = null;  
    public PreparedStatement pst = null;  
  
    public ConnectBase(String sql) {  
        try {  
            Class.forName(name);//指定连接类型  
            conn = DriverManager.getConnection(url, user, password);//获取连接  
            pst = conn.prepareStatement(sql);//准备执行语句  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
  
    public void close() {  
        try {  
            this.conn.close();  
            this.pst.close();  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
    }  
}  

2、 本类用于向管理进程发送GET信息

package mysnmp;
 
import java.io.IOException;
import java.util.Vector;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.ScopedPDU;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.TransportMapping;
import org.snmp4j.UserTarget;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.MPv3;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.AuthMD5;
import org.snmp4j.security.AuthSHA;
import org.snmp4j.security.PrivAES128;
import org.snmp4j.security.PrivAES192;
import org.snmp4j.security.PrivAES256;
import org.snmp4j.security.PrivDES;
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.TcpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultTcpTransportMapping;
import org.snmp4j.transport.DefaultUdpTransportMapping;
 
/**
 * 本类用于向管理进程发送GET信息
 *
 * @author
 *
 */
@SuppressWarnings("rawtypes")
public class SnmpUtilSendGet {
	private Address targetAddress = null;
	private OID authProtocol;
	private OID privProtocol;
	private OctetString privPassphrase;
	private OctetString authPassphrase;
	private OctetString securityName = new OctetString();
	private int version;
	private String host;
	private Target target;
	private OctetString community = new OctetString("public");
	private TransportMapping transport;
 
	/**
	 * 初始化Snmp信息
	 * 
	 * @param host
	 * @param user
	 * @param authProtocol
	 * @param authPasshrase
	 * @param privProtocol
	 * @param privPassphrase
	 * @param version
	 */
	public SnmpUtilSendGet(String host, String user, String authProtocol, String authPasshrase
			                          , String privProtocol,String privPassphrase, int version) {
		this.authPassphrase = new OctetString(authPasshrase);
		this.securityName = new OctetString(user);
		this.privPassphrase = new OctetString(privPassphrase);
		this.version = version;
		this.host = host;
		if (authProtocol.equals("MD5")) {
			this.authProtocol = AuthMD5.ID;
		} else if (authProtocol.equals("SHA")) {
			this.authProtocol = AuthSHA.ID;
		}
		if (privProtocol.equals("DES")) {
			this.privProtocol = PrivDES.ID;
		} else if ((privProtocol.equals("AES128")) || (privProtocol.equals("AES"))) {
			this.privProtocol = PrivAES128.ID;
		} else if (privProtocol.equals("AES192")) {
			this.privProtocol = PrivAES192.ID;
		} else if (privProtocol.equals("AES256")) {
			this.privProtocol = PrivAES256.ID;
		}
	}
 
	/**
	 * 创建snmp
	 * 
	 * @throws IOException
	 */
	public Snmp createSnmpSession() throws IOException {
		// 设置管理进程的IP和端口
		targetAddress = GenericAddress.parse(host);
		if (targetAddress instanceof TcpAddress) {
			transport = new DefaultTcpTransportMapping();
		} else {
			transport = new DefaultUdpTransportMapping();
		}
		Snmp snmp = new Snmp(transport);
		if (version == SnmpConstants.version3) {
			USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(
					                             MPv3.createLocalEngineID()), 0);
			SecurityModels.getInstance().addSecurityModel(usm);
			// Add the configured user to the USM
			addUsmUser(snmp);
		}
		return snmp;
	}
 
	/**
	 * 加入user信息(snmp3)
	 * 
	 * @param snmp
	 */
	private void addUsmUser(Snmp snmp) {
		snmp.getUSM().addUser(securityName,
				new UsmUser(securityName, authProtocol, authPassphrase
						                , privProtocol, privPassphrase));
	}
 
	/**
	 * 创建PUD
	 * 
	 * @param target
	 * @return
	 */
	public PDU createPDU(Target target) {
		PDU request;
		if (target.getVersion() == SnmpConstants.version3) {
			request = new ScopedPDU();
		} else {
			request = new PDU();
		}
		return request;
	}
 
	/**
	 * 创建Target
	 * 
	 * @return
	 */
	private Target createTarget() {
		if (version == SnmpConstants.version3) {
			UserTarget target = new UserTarget();
			if (authPassphrase != null) {
				if (privPassphrase != null) {
					target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
				} else {
					target.setSecurityLevel(SecurityLevel.AUTH_NOPRIV);
				}
			} else {
				target.setSecurityLevel(SecurityLevel.NOAUTH_NOPRIV);
			}
			target.setSecurityName(securityName);
			return target;
		} else {
			CommunityTarget target = new CommunityTarget();
			target.setCommunity(community);
			return target;
		}
	}
 
	/**
	 * 向管理进程发送Get报文
	 *
	 * @throws IOException
	 */
	@SuppressWarnings({ "unchecked" })
	public String sendPDU(String oids) throws IOException {
		String v=null;
		Snmp snmp = createSnmpSession(); 
		target = createTarget();// 设置 target
		target.setAddress(targetAddress);
		target.setRetries(2);// 通信不成功时的重试次数
		target.setTimeout(1500);// 超时时间
		target.setVersion(version);// snmp版本
		transport.listen();
		// 创建 PDU
		ScopedPDU pdu = new createPDU();
		OID oid = new OID(oids);
		pdu.add(new VariableBinding(oid));
		pdu.setType(PDU.GET);
		ResponseEvent respEvnt = snmp.send(pdu, target);
		
		if (respEvnt != null && respEvnt.getResponse() != null) {
			Vector recVBs = (Vector) respEvnt.getResponse().getVariableBindings();
			//for (int i = 0; i < recVBs.size(); i++) {
				VariableBinding recVB = recVBs.elementAt(0);
				//System.out.println(recVB.getVariable().toString());
				//System.out.println("------------------------------------------");
				v=recVB.getVariable().toString();
				snmp.close();
			}
		return v;
 
	//}
 
		
	}
 
}

3、获取的结果存入到JSON数组中

package mysnmp;
 
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.snmp4j.mp.SnmpConstants;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
public class DataBaseDemo {
 
	static String sql = null;
	static ConnectBase db = null;
	static ResultSet rs = null;
	static String label = null;
	static String oid_command = null;
	static String b=null;
	static String jo = null;
	
	public String GetMessage(String Address) throws SQLException, IOException {
 SnmpUtilSendGet util = new SnmpUtilSendGet("udp:" + Address + "/161", "managerv3", "MD5", "authpassword", "DES",
				"pripassword", SnmpConstants.version3);
 
		JSONObject Json = new JSONObject();
		JSONArray JsonArray = new JSONArray();
		
		sql = "select * from 表名 where model='列名' AND method='列名'";// SQL语句
		db = new ConnectBase(sql);
		rs = db.pst.executeQuery();// 执行语句,得到结果集
		while (rs.next()) {
			label = rs.getString("label");
			oid_command = rs.getString("oid_command");
			b = util.sendPDU(oid_command);
			Json.put(label, b);// JSONObject对象中添加键值对
		}
		
		JsonArray.add(Json);// 将JSONObject对象添加到Json数组中
		for (int i1 = 0; i1 < JsonArray.size(); i1++) {
			jo = JsonArray.toString();
			// System.out.println(JsonArray);
			System.out.println(jo);
		}
         
		rs.close();
		db.close();
		return jo;
	}
}

4、测试代码

package mysnmp;
 
import java.io.IOException;
import java.sql.SQLException;
/**
 * @author daopinz
 *
 */
public class Test {
 
	public static void main(String[] args) throws SQLException, IOException {
		String Address="xxx.xxx.xxx.xxx";//服务器IP地址
		DataBaseDemo DB=new DataBaseDemo();
		DB.GetMessage(Address);
 
	}
 
}
 

在请求snmp之前,要首先在对应的服务器上设置对应的参数,如securityname,MD5、加密方式。。。,

 

看你测试的服务器,是否支持snmpv3,此文档是针对snmpv3的操作,v1和v2c相对简单一点。

你可能感兴趣的:(Java,SNMP)