Blame view

src/com/sitech/ismp/coll/CollCiscoRouterWithSNMP.java 10.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
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.cisco.PhysicalEntry;
import com.sitech.ismp.coll.net.cisco.PhysicalEntryWalker;
import com.sitech.ismp.coll.net.config.IpMacEntry;
import com.sitech.ismp.coll.net.config.IpMacEntryWalker;
import com.sitech.ismp.coll.net.table.IfTable;
import com.sitech.ismp.coll.net.table.IfTableRow;
import com.sitech.util.Formater;

public class CollCiscoRouterWithSNMP {
	private Logger logger = Logger.getLogger("COLL");
	private String PRE_UNITID = "10-17-10"; // Cisco Router
	private String HOST_IP = "";
	private int PORT = 161;
	private String COMMUNITY = "public";
	private String HOST_NAME = "";	
	private String VERSION = "";
	private String NEAT_HOST_NAME = "";
	private Snmp mSnmp = null;
	private CommunityTarget mCommunityTarget = null;

	public CollCiscoRouterWithSNMP(String ip, int port, String community,
			String host_name, String version) {
		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);
		this.VERSION = version;
	}

	/**
	 * 初始化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 = "unknown";
		try {
			temp = this.getKPIInfo(oid);
		} catch (RuntimeException e) {
			temp = "unknown";
		}
		String UNIT_ID = PRE_UNITID + "-10" + ":" + NEAT_HOST_NAME + "-total";
		return this.createKPI(UNIT_ID, "CM-00-02-002-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-00-02-001-03", 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("IOS"));
			String[] ss = temp.split(",");
			temp = ss[0].trim() + "," + ss[1];
		} catch (RuntimeException e) {
			return temp;
		}
		return temp;
	}

	/**
	 * 端口指标采集
	 */
	public Vector getPortKpi() {
		Vector kpiVector = new Vector();
		IfTable ifTable = new IfTable();
		ifTable.FillTable(HOST_IP, PORT, COMMUNITY);
		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
					.getIfoperstatus());
			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));

			String ifspeed = row.getIfspeed();
			// 端口设置速率 CM-00-02-002-03
			kpiVector.add(createKPI(UNIT_ID, "CM-00-02-002-03", ifspeed));
		}
		return kpiVector;
	}
	
     /**
      * CM-00-13-001-14	10-13	实体详细分类
        CM-00-13-001-15	10-13	实体索引
		CM-00-13-001-16	10-13	实体描述
		CM-00-13-001-17	10-13	类型oid
		CM-00-13-001-18	10-13	实体类别
		CM-00-13-001-19	10-13	实体名
	  */
	
	public Vector getPhysical(){
		Vector kpiVector = new Vector();
		PhysicalEntryWalker walker = new PhysicalEntryWalker();
		walker.walker(HOST_IP, PORT, COMMUNITY);
		Vector physicalVector = walker.getEntry();
		for (int i = 0; i < physicalVector.size(); i++) {
			PhysicalEntry row = (PhysicalEntry) physicalVector.get(i);
            String UNIT_ID = PRE_UNITID + "-19" + ":" + NEAT_HOST_NAME+"-"+Formater.neatenunitid(row.getRowIndex());
            kpiVector.add(createKPI(UNIT_ID, "CM-00-13-001-15", row.getRowIndex()));
			kpiVector.add(createKPI(UNIT_ID, "CM-00-13-001-16", row.getPhysicalDescr()));
			kpiVector.add(createKPI(UNIT_ID, "CM-00-13-001-17", row.getPhysicalVendorType()));
			kpiVector.add(createKPI(UNIT_ID, "CM-00-13-001-18", row.getPhysicalClass()));
			kpiVector.add(createKPI(UNIT_ID, "CM-00-13-001-19", row.getPhysicalName()));
		}
		return kpiVector;
	}
	
	/**
	 * 
	 * 
	 * CM-00-13-001-11 10-13 接口对应IP
	   CM-00-13-001-12 10-13 物理地址
	   CM-00-13-001-13 10-13 转换类型
	*/
	public Vector getIpNetToMedia(){
		Vector kpiVector = new Vector();
		IpMacEntryWalker walker = new IpMacEntryWalker();
		walker.walker(HOST_IP, PORT, COMMUNITY);
		Vector ipVector = walker.getEntry();
		for (int i = 0; i < ipVector.size(); i++) {
			IpMacEntry row = (IpMacEntry) ipVector.get(i);
            String UNIT_ID = PRE_UNITID + "-18" + ":" + NEAT_HOST_NAME+"-"+row.getIpNetToMediaifIndex();
            kpiVector.add(createKPI(UNIT_ID, "CM-00-13-001-11", row.getIpNetToMediaNetAddress()));
			kpiVector.add(createKPI(UNIT_ID, "CM-00-13-001-12", row.getIpNetToMediaPhyAddress()));
			kpiVector.add(createKPI(UNIT_ID, "CM-00-13-001-13", row.getIpNetToMediaType()));
		}
		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;
	}
	/**
	 * PM-00-02-001-07 设备内存利用率 
	 * PM-00-02-001-06 设备CPU利用率
	 */
	public Vector getSystemPerformance() {
		Vector kpiVector = new Vector();
		String UNIT_ID = PRE_UNITID + "-10" + ":" + NEAT_HOST_NAME + "-total";
		String cup_use_rate = this.getKPIInfo(".1.3.6.1.4.1.9.2.1.57.0");

		kpiVector.add(createKPI(UNIT_ID, "PM-00-02-001-06", cup_use_rate));
		SNMPTarget snmptarget = null;
		try {
			snmptarget = new SNMPTarget();
			snmptarget.init(HOST_IP, this.PORT, this.COMMUNITY);
			Vector memusedpool = snmptarget.walkKPIInfo(".1.3.6.1.4.1.9.9.48.1.1.1.5");
			Vector memfreepool = snmptarget.walkKPIInfo(".1.3.6.1.4.1.9.9.48.1.1.1.6");
			int usedmem = snmptarget.sumKPI(memusedpool);
			int freemem = snmptarget.sumKPI(memfreepool);
			int mem = usedmem + freemem;
			float memper = usedmem * 100F / mem;
			kpiVector.add(createKPI(UNIT_ID, "PM-00-02-001-07", String.valueOf((int) memper)));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			snmptarget.release();
		}
		return kpiVector;
	}
	
	public static void main(String[] args) {
		CollCiscoRouterWithSNMP cisco = new CollCiscoRouterWithSNMP("10.208.192.67", 161, "sx_boss41", "AAA", "2");
		cisco.init();
		System.out.println(cisco.getSV());
		cisco.release();
	}
}