CollCiscoWithSNMP.java 15.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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
/*
 * Created on 2006-8-7
 *
 * 
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.sitech.ismp.coll;

import java.util.Date;
import java.util.HashMap;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
import com.sitech.util.DES3;
import com.sitech.util.Formater;

/**
 * 26个指标 LAN(半双工)端口利用率 PM-00-02-001-01 WAN(全双工)端口流入利用率 PM-00-02-001-02
 * WAN(全双工)端口流出利用率 PM-00-02-001-03 端口误码率 FM-00-02-002-01 端口丢包率 FM-00-02-002-02
 * 端口IP地址 CM-00-02-002-05 端口数量 CM-00-02-002-06 端口类型 CM-00-02-002-02 端口设置速率
 * CM-00-02-002-03 端口物理地址 CM-00-02-002-04 端口标识 CM-00-02-002-01 设备内存利用率
 * FM-00-02-002-04 设备CPU利用率 FM-00-02-002-03 域名解析服务 FM-00-02-001-04 服务状态
 * NTP时钟同步服务状 FM-00-02-001-05 IP地址重复 FM-00-02-001-06 设备内存利用率 PM-00-02-001-07
 * 网元类型 CM-00-02-001-01 网元厂商 CM-00-02-001-02 设备软件版本 CM-00-02-001-03 网元名
 * CM-00-02-001-04 网元IP地址 CM-00-02-001-05 网元状态 FM-00-02-001-01 设备端口状态
 * FM-00-02-001-02 网络链路状态 FM-00-02-001-03
 */
public class CollCiscoWithSNMP {

	static Logger logger = Logger.getLogger("LOGER");
	private final String DEVICENAMEOID = ".1.3.6.1.2.1.1.5.0";
	String UNIT_ID = "10-13-10-11";

	SNMPTarget snmptarget1 = new SNMPTarget();
	String hostname = "";
	String community = null;
	String ip = null;
	int port = 0;
	private Vector result = new Vector();

	public Vector coll(HashMap hs) {
		getParams(hs);
		coll(hs, 2);
		// 返回操作
		return snmptarget1.KPISet;
	}

	/* 取得默认参数 */
	private boolean getParams(java.util.HashMap params) {
		try {
			port = Integer.parseInt((String) params.get("PORT"));
			ip = (String) params.get("IP_ADDR");
			hostname = (String) params.get("DRIVERID");
			community = DES3.decrypt((String) params.get("COMMUNICATION"));
			snmptarget1.init(ip, port, community);
			// snmptarget1.release();
		} catch (Exception e) {
		}
		return true;
	}

	/**
	 * LAN(半双工)端口利用率 PM-00-02-001-01 WAN(全双工)端口流入利用率 PM-00-02-001-02
	 * WAN(全双工)端口流出利用率 PM-00-02-001-03 端口误码率 FM-00-02-002-01 端口丢包率
	 * FM-00-02-002-02
	 */
	private void getNetDeviceperformance(String driverID, SNMPTarget snmptarget) {
		String KBP_ID = this.UNIT_ID + "-11" + ":" + driverID + "-"
				+ snmptarget.ObjectIP + "-interfaces-";

		Vector vindex = snmptarget.walkKPIInfo(".1.3.6.1.2.1.2.2.1.1");
		Vector fixedspeed = snmptarget.walkKPIInfo(".1.3.6.1.2.1.2.2.1.5"); // 端口设置速率
		Vector indiscard = snmptarget.walkKPIInfo(".1.3.6.1.2.1.2.2.1.13"); // 入丢包数
		Vector inerror = snmptarget.walkKPIInfo(".1.3.6.1.2.1.2.2.1.14"); // 入错误包数
		Vector inoctetsb = snmptarget.walkKPIInfo(".1.3.6.1.2.1.2.2.1.10"); // 入总字节数数/8
		Vector outoctetsb = snmptarget.walkKPIInfo(".1.3.6.1.2.1.2.2.1.16"); // 出总字节数数/8
		try {
			Thread.sleep(2000);// 等待2秒
		} catch (Exception e) {
		}
		Vector inoctetse = snmptarget.walkKPIInfo(".1.3.6.1.2.1.2.2.1.10"); // 入总字节数数/8
		Vector outoctetse = snmptarget.walkKPIInfo(".1.3.6.1.2.1.2.2.1.16"); // 出总字节数数/8

		Vector inpackage = snmptarget.walkKPIInfo(".1.3.6.1.2.1.2.2.1.11"); // 入总包数
		Vector outpackage = snmptarget.walkKPIInfo(".1.3.6.1.2.1.2.2.1.17"); // 出总包数
		Vector outdiscard = snmptarget.walkKPIInfo(".1.3.6.1.2.1.2.2.1.19"); // 出丢包数
		Vector outerror = snmptarget.walkKPIInfo(".1.3.6.1.2.1.2.2.1.20"); // 出错误包数

		/*
		 * Vector discardper = new Vector(); Vector errorper = new Vector();
		 * Vector inoutper = new Vector(); Vector inper = new Vector(); Vector
		 * outper = new Vector();
		 */
		for (int i = 0; i < outerror.size(); i++) {
			String index = (String) vindex.elementAt(i);
			float speed = Float.parseFloat((String) fixedspeed.elementAt(i)) / 64; // 一个octet8字节,1字节8bit
			float inoctets = Float.parseFloat((String) inoctetse.elementAt(i))
					- Float.parseFloat((String) inoctetsb.elementAt(i));// /speed;
			float outoctets = Float
					.parseFloat((String) outoctetse.elementAt(i))
					- Float.parseFloat((String) outoctetsb.elementAt(i)); // /speed;

			float totalpackage = Float.parseFloat((String) inpackage
					.elementAt(i))
					+ Float.parseFloat((String) outpackage.elementAt(i));
			float totalerrorpackage = Float.parseFloat((String) inerror
					.elementAt(i))
					+ Float.parseFloat((String) outerror.elementAt(i));
			float totaldiscardpackage = Float.parseFloat((String) indiscard
					.elementAt(i))
					+ Float.parseFloat((String) outdiscard.elementAt(i));
			if (totalpackage < 1)
				totalpackage = 1F;

			/*
			 * errorper.add(String.valueOf((totalerrorpackage/totalpackage)*5000F));
			 * //表示十万分之X的误码率
			 * discardper.add(String.valueOf((totaldiscardpackage/totalpackage)*5000F));
			 * //表示十万分之X的丢包率 inper.add(String.valueOf(inoctets/speed*50F));
			 * //千分比,等待了2秒 outper.add(String.valueOf(outoctets/speed*50F));
			 * //千分比,等待了2秒
			 * inoutper.add(String.valueOf((inoctets+outoctets)/speed*50F));
			 */
			String cerrorper = Formater.formatDecimalByScale(String
					.valueOf((totalerrorpackage / totalpackage) * 5000F), 4); // 表示十万分之X的误码率
			String cdiscardper = Formater.formatDecimalByScale(String
					.valueOf((totaldiscardpackage / totalpackage) * 5000F), 4); // 表示十万分之X的丢包率
			String cinper = Formater.formatDecimalByScale(String
					.valueOf(inoctets / speed * 50F), 4); // 千分比,等待了2秒
			String coutper = Formater.formatDecimalByScale(String
					.valueOf(outoctets / speed * 50F), 4); // 千分比,等待了2秒
			String cinoutper = Formater.formatDecimalByScale(String
					.valueOf((inoctets + outoctets) / speed * 50F), 4);

			snmptarget.addKPI(KBP_ID + index, "FM-00-02-002-01", cerrorper); // 端口误码率
			snmptarget.addKPI(KBP_ID + index, "FM-00-02-002-02", cdiscardper); // 端口丢包率
			snmptarget.addKPI(KBP_ID + index, "PM-00-02-001-01", cinoutper); // LAN(半双工)端口利用率
			snmptarget.addKPI(KBP_ID + index, "PM-00-02-001-02", cinper); // WAN(全双工)端口流入利用率率
			snmptarget.addKPI(KBP_ID + index, "PM-00-02-001-03", coutper); // WAN(全双工)端口流出利用率率
		}
	}

	/**
	 * CM-00-02-002-05 端口IP地址 CM-00-02-002-06 端口数量 CM-00-02-002-02 端口类型
	 * CM-00-02-002-03 端口设置速率 CM-00-02-002-04 端口物理地址 CM-00-02-002-01 端口标识
	 */
	private void getNetDeviceConfigure(String driverID, SNMPTarget snmptarget) {
		try {
			String KBP_ID = this.UNIT_ID + "-11" + ":" + driverID + "-"
					+ snmptarget.ObjectIP + "-interfaces-";
			Vector vindex = snmptarget.walkKPIInfo(".1.3.6.1.2.1.2.2.1.1");
			Vector vdescr = snmptarget.walkKPIInfo(".1.3.6.1.2.1.2.2.1.2");
			Vector viftype = snmptarget.walkKPIInfo(".1.3.6.1.2.1.2.2.1.3");
			Vector vspeed = snmptarget.walkKPIInfo(".1.3.6.1.2.1.2.2.1.5");
			Vector vphys = snmptarget.walkKPIInfo(".1.3.6.1.2.1.2.2.1.6");
			Vector vindex2 = snmptarget.walkKPIInfo(".1.3.6.1.2.1.4.20.1.2");// index
			Vector vaddr = snmptarget.walkKPIInfo(".1.3.6.1.2.1.4.20.1.1");
			Vector vnetmask = snmptarget.walkKPIInfo(".1.3.6.1.2.1.4.20.1.3");

			for (int i = 0; i < vindex.size(); i++) {
				String index = (String) vindex.elementAt(i);
				String descr = (String) vdescr.elementAt(i);
				String iftype = (String) viftype.elementAt(i);
				String speed = (String) vspeed.elementAt(i);
				String phys = (String) vphys.elementAt(i);
				if (!descr.startsWith("Null") && !descr.startsWith("unrouted")) {
					System.out.println(descr);
					snmptarget.addKPI(KBP_ID + index, "CM-00-02-002-01", descr); // 端口标识
				}

				snmptarget.addKPI(KBP_ID + index, "CM-00-02-002-02", iftype);// 端口类型
				snmptarget.addKPI(KBP_ID + index, "CM-00-02-002-03", speed);// 端口设置速率
				snmptarget.addKPI(KBP_ID + index, "CM-00-02-002-04", phys); // 端口物理地址
			}

			for (int i = 0; i < vindex2.size(); i++) {
				String index2 = (String) vindex2.elementAt(i);
				String addr = (String) vaddr.elementAt(i);
				String netmask = (String) vnetmask.elementAt(i);
				snmptarget.addKPI(KBP_ID + index2, "CM-00-02-002-05", addr); // 端口IP地址
				snmptarget.addKPI(KBP_ID + index2, "CM-00-02-002-06", netmask);// 端口数量
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * FM-00-02-002-04 设备内存利用率 FM-00-02-002-03 设备CPU利用率
	 */
	private void getSystemPerformance(String driverID, SNMPTarget snmptarget) {
		String kpiid = UNIT_ID + "-10" + ":" + driverID + "-"
				+ snmptarget.ObjectIP;
		snmptarget.addKPI(kpiid, "FM-00-02-002-03", snmptarget
				.getKPIInfo(".1.3.6.1.4.1.9.2.1.57.0")); // 设备CPU利用率
		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;
		snmptarget.addKPI(kpiid, "FM-00-02-002-04", String
				.valueOf((int) memper)); // 设备内存利用率
	}

	/**
	 * 4 FM-00-02-001-04 域名解析服务 可以正常解析得到重要设备的网络地址 5分钟 字符型 major (可选) 5
	 * FM-00-02-001-05 NTP服务状态 NTP时钟同步服务状态 5分钟 字符型 major (可选) 6 FM-00-02-001-06
	 * IP地址重复 在网络内,存在两个或两个以上相同的IP地址(不包括集群系统中的心跳私有地址) 15分钟 字符型 major 7
	 * PM-00-02-001-07 设备内存利用率
	 */
	private void getCMStandKPI(String driverID, SNMPTarget snmptarget) {
		String kpiid = UNIT_ID + "-10" + ":" + driverID + "-"
				+ snmptarget.ObjectIP;
		snmptarget.addKPI(kpiid, "FM-00-02-001-04", "noneffective");
		snmptarget.addKPI(kpiid, "FM-00-02-001-05", "noneffective");
		snmptarget.addKPI(kpiid, "FM-00-02-001-06", "noneffective");
		snmptarget.addKPI(kpiid, "PM-00-02-001-07", "noneffective");
	}

	/**
	 * CM-00-02-001-01 网元类型 CM-00-02-001-02 网元厂商 CM-00-02-001-03 设备软件版本
	 * CM-00-02-001-04 网元名 CM-00-02-001-05 网元IP地址
	 */
	private void getNetDevicAllConfiguration(String driverID,
			SNMPTarget snmptarget) {
		String kpiid = UNIT_ID + "-10" + ":" + driverID + "-"
				+ snmptarget.ObjectIP;

		snmptarget.addKPI(kpiid, "CM-00-02-001-01", getDeviceType(snmptarget
				.getKPIInfo(".1.3.6.1.2.1.1.1.0"))); // 网元类型
		snmptarget.addKPI(kpiid, "CM-00-02-001-02", getVendor(snmptarget
				.getKPIInfo(".1.3.6.1.2.1.1.1.0"))); // 网元厂商

		snmptarget.addKPI(kpiid, "CM-00-02-001-03", getSV(snmptarget)); // 设备软件版本

		String devicename = snmptarget.getKPIInfo(DEVICENAMEOID);
		snmptarget.addKPI(kpiid, "CM-00-02-001-04", devicename); // 网元名
		snmptarget.addKPI(kpiid, "CM-00-02-001-05", snmptarget.ObjectIP); // 网元IP地址
		// snmptarget.addKPI(kpiid, "CM-00-02-001-06", snmptarget
		// .getKPIInfo(".1.3.6.1.2.1.2.1.0"));
		// snmptarget.addKPI(kpiid, "CM-00-02-001-07", snmptarget
		// .getKPIInfo(".1.3.6.1.2.1.1.1.0"));
		// snmptarget.addKPI(kpiid, "PM-10-02-001-08", snmptarget
		// .getKPIInfo(".1.3.6.1.2.1.1.3.0"));
	}

	private String getVendor(String desc) {
		if ((desc == null) || desc.equals(""))
			return "UNKNOW";
		desc = desc.toUpperCase();
		if (desc.indexOf("AIX") > -1)
			return "IBM";
		if (desc.indexOf("HP-UX") > -1)
			return "HP";
		if (desc.indexOf("SUN") > -1)
			return "SUN";
		if (desc.indexOf("CISCO") > -1)
			return "CISCO";
		if (desc.indexOf("WINDOWS") > -1)
			return "MICROSOFT";
		if (desc.indexOf("HUAWEI") > -1)
			return "HUAWEI";
		return "UNKNOW";
	}

	private String getDeviceType(String desc) {
		if ((desc == null) || desc.equals(""))
			return "UNKNOW";
		desc = desc.toUpperCase();
		if (desc.indexOf("AIX") > -1)
			return "AIX";
		if (desc.indexOf("HP-UX") > -1)
			return "HP_UX";
		if (desc.indexOf("SUN") > -1)
			return "SUN";
		if (desc.indexOf("CISCO") > -1)
			return "SWITCH/ROUTER";
		if (desc.indexOf("WINDOWS") > -1)
			return "WINDOWS";
		return "UNKNOW";
	}

	public Vector getState(String driverID, String ObjectIP, int port,
			String communication) {
		SNMPTarget snmptarget = new SNMPTarget();
		snmptarget.init(ObjectIP, port, communication);

		if (snmptarget.getSNMPState()) {
			getSystemState(driverID, snmptarget);
			getInterfacesState(driverID, snmptarget);
		}
		snmptarget.release();
		return snmptarget.getKPISet();
	}

	/**
	 * 网元状态 FM-00-02-001-01
	 * 
	 */
	private void getSystemState(String driverID, SNMPTarget snmptarget) {
		String kpiid = UNIT_ID + "-10" + ":" + driverID + "-"
				+ snmptarget.ObjectIP;
		if (snmptarget.getSNMPState()) {
			snmptarget.addKPI(kpiid, "FM-00-02-001-01", "UP");
		} else {
			snmptarget.addKPI(kpiid, "FM-00-02-001-01", "DOWN");
		}
	}

	/**
	 * FM-00-02-001-02 设备端口状态 FM-00-02-001-03 网络链路状态
	 */
	private void getInterfacesState(String driverID, SNMPTarget snmptarget) {
		String KBP_ID = this.UNIT_ID + "-11" + ":" + driverID + "-"
				+ snmptarget.ObjectIP + "-interfaces";

		snmptarget.addKPI(KBP_ID, "FM-00-02-001-02", snmptarget
				.walkKPIInfo(".1.3.6.1.2.1.2.2.1.8"));
		snmptarget.addKPI(KBP_ID, "FM-00-02-001-03", snmptarget
				.walkKPIInfo(".1.3.6.1.2.1.2.2.1.7"));
	}

	private void coll(HashMap params, int flag) {
		logger.info("params is " + hostname + "|" + ip + "|" + port + "|"
				+ community);
		try {
			// this.getCMStandKPI(hostname, snmptarget1);
			this.getInterfacesState(hostname, snmptarget1);
			this.getNetDevicAllConfiguration(hostname, snmptarget1);
			this.getNetDeviceConfigure(hostname, snmptarget1);
			this.getNetDeviceperformance(hostname, snmptarget1);
			this.getState(hostname, ip, port, community);
			System.out.println("-------" + snmptarget1.ObjectIP);

			this.getSystemPerformance(hostname, snmptarget1);
			this.getSystemState(hostname, snmptarget1);
		} catch (Exception e) {
			e.printStackTrace();
			String unitID = "10-12-11-10-10:" + hostname + "-total";
			// FM-00-04-003-03 主机状态 true 连接正常 false 连接失败可能宕机
			setResult("false", unitID, "FM-00-04-003-03");
			logger.error(e);
		} finally {
			snmptarget1.release();
		}
	}

	private void setResult(String value, String unitID, String kpiID) {
		try {
			TblATO_KPIDETAIL record;
			record = new TblATO_KPIDETAIL();
			record.setCLL_TIME(new java.util.Date()); // 采集所消耗的时间
			record.setKPI_ID(kpiID); // kpi编号
			record.setKPI_VALUE(value); // 具体采集得到的值信息
			record.setUNIT_ID(unitID); // unit
			record.setCLL_TIME(new Date()); // 日期
			result.add(record);
		} catch (Exception e) {
			logger.error(e);
		}
	}

	/**
	 * 采集主机软件版本
	 * 
	 * @return
	 */
	private String getSV(SNMPTarget snmptarget) {
		String oid = ".1.3.6.1.2.1.1.1.0";
		String temp = "--";
		try {
			temp = snmptarget.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;
	}

	private void callCollBase() {
		CollBase coll = new CollBase();
		coll.addKPISet(result);
		coll.saveKPI2File();
		// coll.displayKPI();
	}

	public static void main(String[] args) {
		CollCiscoWithSNMP s = new CollCiscoWithSNMP();
		HashMap params = new HashMap();
		params.put("IP_ADDR", "172.18.1.1");
		params.put("COMMUNICATION",
				"40c7df20f4ff35a2b1f9cecdec34174daf342d5094eff0a1");
		params.put("DRIVERID", "sitech");
		params.put("PORT", "161");
		s.snmptarget1.displayKPI(s.coll(params));
	}
}