NetDeviceSystem.java 4.46 KB
package com.sitech.ismp.coll.net.config;

import java.io.IOException;
import java.net.UnknownHostException;

import org.apache.log4j.Logger;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;

public class NetDeviceSystem {
	
	private Logger logger = Logger.getLogger("COLL");
	private String HOST_IP = "";
	private int PORT = 161;
	private String COMMUNITY = "public";
	private Snmp mSnmp = null;
	private CommunityTarget mCommunityTarget = null;
	
	public NetDeviceSystem(String ip,int port,String community){
		this.HOST_IP = ip;
		this.PORT = port;
		this.COMMUNITY = community;
	}
	public void init() {
		try {
			String addr = "udp:" + this.HOST_IP + "/"
					+ String.valueOf(this.PORT);
			OctetString community = new OctetString(this.COMMUNITY);
			Address address = GenericAddress.parse(addr);
			mCommunityTarget = new CommunityTarget(address, community);
			org.snmp4j.TransportMapping vTransport = new org.snmp4j.transport.DefaultUdpTransportMapping();
			mSnmp = new org.snmp4j.Snmp(vTransport);
			vTransport.listen();
			mCommunityTarget.setCommunity(community);
			mCommunityTarget.setAddress(new UdpAddress(java.net.InetAddress
					.getByName(this.HOST_IP), this.PORT));
			mCommunityTarget.setRetries(2);
			mCommunityTarget.setTimeout(200);
			// mCommunityTarget.setVersion(org.snmp4j.mp.SnmpConstants.version2c);
		} catch (UnknownHostException e) {
			
			logger.error("UnknownHostError", e);
			this.release();
		} catch (IOException e) {
			
			logger.error("IOError", e);
			this.release();
		}
	}
	
	public NetDeviceConfig gentConfigInfo(){
		NetDeviceConfig config = new NetDeviceConfig();
		
		config.setPortNum(this.getPortNum());
		config.setSysDescr(this.getSysDescr());
		config.setSysUpTime(this.getSysUpTime());
		config.setSysName(this.getSysName());
		config.setSysLocation(this.getSysLocation());
		config.setSysService(this.getSysService());
		config.setSysContact(this.getSysContact());
		return config;
	}
	
	public String getPortNum() {
		// 1.3.6.1.2.1.2.1.0
		String oid = "1.3.6.1.2.1.2.1.0";
		String temp = "";
		try {
			temp = this.getKPIInfo(oid);
		} catch (RuntimeException e) {
			temp = "0";
		}
		return temp;
	}
	
	public String getSysDescr(){
		// 1.3.6.1.2.1.2.1.0
		String oid = ".1.3.6.1.2.1.1.1.0";
		String temp = "";
		try {
			temp = this.getKPIInfo(oid);
		} catch (RuntimeException e) {
			temp = "0";
		}
		return temp;
	}

	public String getSysUpTime(){
		
		String oid = ".1.3.6.1.2.1.1.3.0";
		String temp = "";
		try {
			temp = this.getKPIInfo(oid);
		} catch (RuntimeException e) {
			temp = "no system up time";
		}
		return temp;
	}
	
	public String getSysContact(){
		String oid = ".1.3.6.1.2.1.1.4.0";
		String temp = "";
		try {
			temp = this.getKPIInfo(oid);
		} catch (RuntimeException e) {
			temp = "no contact information";
		}
		return temp;
	}
	
	public String getSysName(){
		String oid = ".1.3.6.1.2.1.1.5.0";
		String temp = "";
		try {
			temp = this.getKPIInfo(oid);
		} catch (RuntimeException e) {
			temp = "no system name";
		}
		return temp;
	}
	
	public String getSysLocation(){
		String oid = ".1.3.6.1.2.1.1.6.0";
		String temp = "";
		try {
			temp = this.getKPIInfo(oid);
		} catch (RuntimeException e) {
			temp = "no sytem location";
		}
		return temp;
	}
	
	public String getSysService(){
		String oid = ".1.3.6.1.2.1.1.7.0";
		String temp = "";
		try {
			temp = this.getKPIInfo(oid);
		} catch (RuntimeException e) {
			temp = "no sytem location";
		}
		return temp;
	}
	
	
	
	/**
	 * 释放SNMP连接
	 * 
	 */
	public void release() {
		try {
			if (mSnmp != null) {
				mSnmp.close();
			}
		} catch (IOException e) {
			
			mSnmp = null;
		}
	}
	
	/**
	 * 得到单个oid对应值
	 * 
	 * @param oid
	 * @return
	 */
	public String getKPIInfo(String oid) {
		String result = "";
		this.init();
		PDU pdu = new PDU();
		VariableBinding var = new VariableBinding(new OID(oid),
				new org.snmp4j.smi.OctetString());
		pdu.add(var);
		pdu.setType(PDU.GET);
		try {
			ResponseEvent res = mSnmp.send(pdu, mCommunityTarget);
			PDU result_pdu = res.getResponse();
			if (result_pdu != null) {
				VariableBinding vb = result_pdu.get(0);
				result = vb.getVariable().toString();
			}
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		this.release();
		return result;
	}

	
}