CollHuaWeiSwitchWithSNMP.java 10.4 KB
package com.sitech.ismp.coll;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Vector;

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;

import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
import com.sitech.ismp.coll.net.ifOperStatusHelper;
import com.sitech.ismp.coll.net.ifTypeHelper;
import com.sitech.ismp.coll.net.table.IfTable;
import com.sitech.ismp.coll.net.table.IfTableRow;
import com.sitech.util.Formater;

public class CollHuaWeiSwitchWithSNMP {
	private Logger logger = Logger.getLogger("COLL");

	private String PRE_UNITID = "10-16-11"; // HuaWei Switch

	private String HOST_IP = "";

	private int PORT = 161;

	private String COMMUNITY = "public";

	private String HOST_NAME = "";

	private String NEAT_HOST_NAME = "";

	private Snmp mSnmp = null;

	private CommunityTarget mCommunityTarget = null;

	public CollHuaWeiSwitchWithSNMP(String ip, int port, String community, String host_name) {
		this.HOST_IP = ip;
		this.PORT = port;
		this.COMMUNITY = community;
		this.HOST_NAME = host_name;
		this.NEAT_HOST_NAME = com.sitech.util.Formater.neatenunitid(host_name);
	}

	/**
	 * 初始化SNMP target
	 * 
	 */
	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();
		}
	}// end init

	/**
	 * 释放SNMP连接
	 * 
	 */
	public void release() {
		try {
			if (mSnmp != null) {
				mSnmp.close();
			}
		} catch (IOException e) {
			mSnmp = null;
		}
	}// end release

	/**
	 * 采集主机名称
	 * 
	 * @return
	 */
	public TblATO_KPIDETAIL getHostName() {
		String UNIT_ID = PRE_UNITID + "-10" + ":" + NEAT_HOST_NAME + "-total";
		return this.createKPI(UNIT_ID, "CM-00-01-001-01", this.HOST_NAME);
	}

	/**
	 * 采集IpAddr
	 * 
	 * @return
	 */
	public TblATO_KPIDETAIL getIpAddr() {
		String UNIT_ID = PRE_UNITID + "-10" + ":" + NEAT_HOST_NAME + "-total";
		return this.createKPI(UNIT_ID, "CM-00-02-001-05", this.HOST_IP);
	}

	/**
	 * 采集主机状态
	 * 
	 * @return
	 */
	public TblATO_KPIDETAIL getDeviceState() {
		String oid = ".1.3.6.1.2.1.1.2.0";
		String temp = this.getKPIInfo(oid);
		String status = "";
		if (temp == null || temp.equals("")) {
			status = "DOWN";
		} else {
			status = "UP";
		}

		// FM-01-01-01-01
		String UNIT_ID = PRE_UNITID + "-10" + ":" + NEAT_HOST_NAME + "-total";
		return this.createKPI(UNIT_ID, "FM-00-02-001-01", status);
	}

	/**
	 * 采集端口数量
	 * 
	 * @return
	 */
	public TblATO_KPIDETAIL 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";
		}
		String UNIT_ID = PRE_UNITID + "-10" + ":" + NEAT_HOST_NAME + "-total";
		return this.createKPI(UNIT_ID, "CM-01-02-01-06", temp);
	}

	/**
	 * 采集主机软件版本
	 * 
	 * @return
	 */
	public TblATO_KPIDETAIL getSoftVersion() {
		String temp = this.getSV();
		String UNIT_ID = PRE_UNITID + "-10" + ":" + NEAT_HOST_NAME + "-total";
		return this.createKPI(UNIT_ID, "CM-01-02-01-03", temp);
	}
	
	public Vector<TblATO_KPIDETAIL> getCPUKpi() {
		Vector<TblATO_KPIDETAIL> kpiVector = new Vector<TblATO_KPIDETAIL>();
		String temp = this.getCPU();
		String UNIT_ID = PRE_UNITID + "-10" + ":" + NEAT_HOST_NAME + "-total";
		TblATO_KPIDETAIL tk = this.createKPI(UNIT_ID, "PM-00-01-001-05", temp);
		kpiVector.add(tk);
		return kpiVector;
	}
	
	/**
	 * 采集内存大小(中兴交换机)
	 */
	public Vector<TblATO_KPIDETAIL> getMEMKpi() {
		Vector<TblATO_KPIDETAIL> kpiVector = new Vector<TblATO_KPIDETAIL>();
		String temp = this.getMEM();
		String UNIT_ID = PRE_UNITID + "-10" + ":" + NEAT_HOST_NAME + "-total";
		TblATO_KPIDETAIL tk = this.createKPI(UNIT_ID, "CM-00-03-002-01", 
				(Formater.string2int(temp) / 1024.0 / 1024.0) + "");
		kpiVector.add(tk);
		return kpiVector;
	}
	
	/**
	 * 采集内存使用率(中兴交换机)
	 */
	public TblATO_KPIDETAIL getMemUsed() {
		String temp = this.getMU();
		String tot = this.getMEM();
		String UNIT_ID = PRE_UNITID + "-10" + ":" + NEAT_HOST_NAME + "-total";
		return this.createKPI(UNIT_ID, "FM-00-02-002-04", 
				Formater.formatDecimalKpivalue((Float.parseFloat(temp) * 100 / Float.parseFloat(tot)) + ""));
	}
	
	/**
	 * 采集CPU利用率(中兴交换机)
	 */
	private String getCPU() {
		String oid = "1.3.6.1.4.1.1872.2.5.1.2.2.3.0";
		String temp = "0";
		try {
			temp = this.getKPIInfo(oid);
		} catch (RuntimeException e) {
			return temp;
		}
		return temp;
	}
	
	/**
	 * 采集内存大小(中兴交换机)
	 */
	private String getMEM() {
		String oid = "1.3.6.1.4.1.1872.2.5.1.2.8.1.0";
		String temp = "0";
		try {
			temp = this.getKPIInfo(oid);
		} catch (RuntimeException e) {
			return temp;
		}
		return temp;
	}
	
	/**
	 * 采集内存使用率(中兴交换机)
	 */
	private String getMU() {
		String oid = "1.3.6.1.4.1.1872.2.5.1.2.8.2.0";
		String temp = "0";
		try {
			temp = this.getKPIInfo(oid);
		} catch (RuntimeException e) {
			return temp;
		}
		return temp;
	}

	/**
	 * 采集主机软件版本
	 * 
	 * @return
	 */
	private String getSV() {
		String oid = "1.3.6.1.2.1.1.1.0";
		String temp = "--";
		try {
			temp = this.getKPIInfo(oid);
			temp = temp.substring(temp.indexOf("Version"), 
					temp.indexOf("Version") + "Version".length() + 5);
		} catch (RuntimeException e) {
			return temp;
		}
		return temp;
	}

	/**
	 * 端口指标采集
	 * 
	 * @return
	 */
	public Vector<TblATO_KPIDETAIL> getPortCMKpi() {
		Vector<TblATO_KPIDETAIL> kpiVector = new Vector<TblATO_KPIDETAIL>();
		IfTable ifTable = new IfTable();
		ifTable.FillTable(HOST_IP, PORT, COMMUNITY, 500);
		Vector<?> ifTableRows = ifTable.getRrows();
		for (int i = 0; i < ifTableRows.size(); i++) {
			IfTableRow row = (IfTableRow) ifTableRows.get(i);
			String ifIndex = row.getIfindex();
			String r_ifDesc = "--";
			String ifDesc = row.getIfdesc();
			if (ifDesc != null && (!(ifDesc.equals("")))
					&& (!(ifDesc.equalsIgnoreCase("null")))) {
				r_ifDesc = ifDesc;
			}
			String ifType = ifTypeHelper.getTypeDesc(row.getIftype());
			//String ifOperStatus = ifOperStatusHelper.getOperStatusDesc(row.getIfadminstatus());
			String r_ifPhyAddr = "--";
			String ifPhyAddr = row.getIfphyaddr();
			if (ifPhyAddr != null && (!(ifPhyAddr.equals("")))) {
				r_ifPhyAddr = ifPhyAddr;
			}

			String UNIT_ID = PRE_UNITID + "-11" + ":" + NEAT_HOST_NAME + "-"
					+ ifIndex + "-interface";
			// 端口索引号 CM-01-02-02-01
			kpiVector.add(createKPI(UNIT_ID, "CM-01-02-02-01", ifIndex));
			// 端口描述 CM-01-02-02-05
			kpiVector.add(createKPI(UNIT_ID, "CM-01-02-02-05", r_ifDesc));
			// 端口类型 CM-01-02-02-02
			kpiVector.add(createKPI(UNIT_ID, "CM-01-02-02-02", ifType));
			// 端口Mac地址 CM-01-02-02-04
			kpiVector.add(createKPI(UNIT_ID, "CM-01-02-02-04", r_ifPhyAddr));
			// 端口状态 FM-01-02-01-02
			//kpiVector.add(createKPI(UNIT_ID, "FM-01-02-01-02", ifOperStatus));
		}
		return kpiVector;
	}
	
	public Vector<TblATO_KPIDETAIL> getPortFMKpi() {
		Vector<TblATO_KPIDETAIL> kpiVector = new Vector<TblATO_KPIDETAIL>();
		IfTable ifTable = new IfTable();
		ifTable.FillTable(HOST_IP, PORT, COMMUNITY, 500);
		Vector<?> ifTableRows = ifTable.getRrows();
		for (int i = 0; i < ifTableRows.size(); i++) {
			IfTableRow row = (IfTableRow) ifTableRows.get(i);
			String ifIndex = row.getIfindex();
			/*String r_ifDesc = "--";
			String ifDesc = row.getIfdesc();
			if (ifDesc != null && (!(ifDesc.equals("")))
					&& (!(ifDesc.equalsIgnoreCase("null")))) {
				r_ifDesc = ifDesc;
			}
			String ifType = ifTypeHelper.getTypeDesc(row.getIftype());
			String r_ifPhyAddr = "--";
			String ifPhyAddr = row.getIfphyaddr();
			if (ifPhyAddr != null && (!(ifPhyAddr.equals("")))) {
				r_ifPhyAddr = ifPhyAddr;
			}*/
			String ifOperStatus = ifOperStatusHelper.getOperStatusDesc(row.getIfadminstatus());
			String UNIT_ID = PRE_UNITID + "-11" + ":" + NEAT_HOST_NAME + "-" + ifIndex + "-interface";
			// 端口索引号 CM-01-02-02-01
			//kpiVector.add(createKPI(UNIT_ID, "CM-01-02-02-01", ifIndex));
			// 端口描述 CM-01-02-02-05
			//kpiVector.add(createKPI(UNIT_ID, "CM-01-02-02-05", r_ifDesc));
			// 端口类型 CM-01-02-02-02
			//kpiVector.add(createKPI(UNIT_ID, "CM-01-02-02-02", ifType));
			// 端口Mac地址 CM-01-02-02-04
			//kpiVector.add(createKPI(UNIT_ID, "CM-01-02-02-04", r_ifPhyAddr));
			// 端口状态 FM-01-02-01-02
			kpiVector.add(createKPI(UNIT_ID, "FM-01-02-01-02", ifOperStatus));
		}
		return kpiVector;
	}

	/**
	 * 生成TblATO_KPIDETAIL
	 * 
	 * @param KBP_ID
	 * @param KPI_ID
	 * @param KPI_VALUE
	 * @return
	 */
	private TblATO_KPIDETAIL createKPI(String KBP_ID, String KPI_ID,
			String KPI_VALUE) {
		TblATO_KPIDETAIL tblato_kpidetail = new TblATO_KPIDETAIL();
		tblato_kpidetail.UNIT_ID = KBP_ID;
		tblato_kpidetail.KPI_ID = KPI_ID;
		tblato_kpidetail.KPI_VALUE = KPI_VALUE;
		tblato_kpidetail.CLL_TIME = new Date();
		return tblato_kpidetail;
	}

	/**
	 * 得到单个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;
	}
}