⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 _3com.java

📁 Network Administration Visualized 网络管理可视化源码
💻 JAVA
字号:
package no.ntnu.nav.getDeviceData.deviceplugins._3Com;import java.util.*;import java.util.regex.*;import no.ntnu.nav.logger.*;import no.ntnu.nav.SimpleSnmp.*;import no.ntnu.nav.ConfigParser.*;import no.ntnu.nav.netboxinfo.*;import no.ntnu.nav.getDeviceData.Netbox;import no.ntnu.nav.getDeviceData.deviceplugins.*;import no.ntnu.nav.getDeviceData.dataplugins.*;import no.ntnu.nav.getDeviceData.dataplugins.Netbox.*;import no.ntnu.nav.getDeviceData.dataplugins.Module.*;import no.ntnu.nav.getDeviceData.dataplugins.Swport.*;/** * <p> * DeviceHandler for collecting switch port data from 3Com switches. * </p> * * <p> * This plugin handles the following OID keys: * </p> * * <p> * <ui> *  <li>3cIfDescr</li> *  <li>3cPS40PortState</li> *  <li>3cIfMauType</li> *  <li>3cSerial</li> *  <li>3cHwVer</li> *  <li>3cSwVer</li> * </ul> * </p> * */public class _3Com implements DeviceHandler{	public static final int HANDLE_PRI_3COM = -28;	private static String[] canHandleOids = {		"3cIfDescr",		"3cPS40PortState",		"3cIfMauType",		"3cSerial",		"3cHwVer",		"3cSwVer"	};	private SimpleSnmp sSnmp;	public int canHandleDevice(Netbox nb) {		int v = nb.isSupportedOids(canHandleOids) ? HANDLE_PRI_3COM : NEVER_HANDLE;		Log.d("3COM_CANHANDLE", "CHECK_CAN_HANDLE", "Can handle device: " + v);		return v;	}	public void handleDevice(Netbox nb, SimpleSnmp sSnmp, ConfigParser cp, DataContainers containers) throws TimeoutException	{		Log.setDefaultSubsystem("3COM_DEVHANDLER");				NetboxContainer nc;		{			DataContainer dc = containers.getContainer("NetboxContainer");			if (dc == null) {				Log.w("NO_CONTAINER", "No NetboxContainer found, plugin may not be loaded");				return;			}			if (!(dc instanceof NetboxContainer)) {				Log.w("NO_CONTAINER", "Container is not a NetboxContainer! " + dc);				return;			}			nc = (NetboxContainer)dc;		}		ModuleContainer mc;		{			DataContainer dc = containers.getContainer("ModuleContainer");			if (dc == null) {				Log.w("NO_CONTAINER", "No ModuleContainer found, plugin may not be loaded");				return;			}			if (!(dc instanceof ModuleContainer)) {				Log.w("NO_CONTAINER", "Container is not a ModuleContainer! " + dc);				return;			}			mc = (ModuleContainer)dc;		}		SwportContainer sc;		{			DataContainer dc = containers.getContainer("SwportContainer");			if (dc == null) {				Log.w("NO_CONTAINER", "No SwportContainer found, plugin may not be loaded");				return;			}			if (!(dc instanceof SwportContainer)) {				Log.w("NO_CONTAINER", "Container is not an SwportContainer! " + dc);				return;			}			sc = (SwportContainer)dc;		}		String netboxid = nb.getNetboxidS();		String ip = nb.getIp();		String cs_ro = nb.getCommunityRo();		String type = nb.getType();		String sysName = nb.getSysname();		String cat = nb.getCat();		this.sSnmp = sSnmp;		process3Com(nb, netboxid, ip, cs_ro, type, nc, mc, sc);		// Commit data		if (mc.isCommited()) sc.setEqual(mc);		sc.commit();	}	/*	 * 3COM	 *	 */	private void process3Com(Netbox nb, String netboxid, String ip, String cs_ro, String type, NetboxContainer nc, ModuleContainer mc, SwportContainer sc) throws TimeoutException {		type = type.toLowerCase();		/*		PS40:		=====		Kan finne oppe/nede med .1.3.6.1.2.1.26.1.1.1.6.<unit>.<port>.1 = 3|4		3 = oppe		4 = nede		Synes man her skal lage en ifindex av typen		 unit1port15 har ifindex 115		speed og duplex: for ps40 har vi jo bare halv duplex, speed 10, forutsatt		 at porten er oppe.		SWxx00		======	IfIndex:		Tolkes fra ifDescr:		.1.3.6.1.2.1.2.2.1.2.ifindex = tekststring av litt diverse typer. Må		plukke ut de som inneholder ordene 'unit' og 'port'. Tungvint, men har		ikke noe bedre :(		SW9300: SuperStack II Switch 9300, manuf: 3Com, Gigabit-Ethernet Port 1		SW3300: RMON:10/100 Port 13 on Unit 4		SW1100: RMON:V2 Port 1 on Unit 2	Speed & Duplex		kan finnes fra ifMauType:		.1.3.6.1.2.1.26.2.1.1.3.ifindex.1 = oid		SW9300: N/A		SW3300: 26.4.16 / enterprises.43.18.8.2.3		SW1100: 26.4.10		26.4.3 : FOIRL         10 HD		26.4.10: 10BaseTHD     10 HD		26.4.11: 10BaseTFD     10 FD		26.4.15: 100BaseTXHD  100 HD		26.4.16: 100BaseTXFD  100 FD		26.4.17: 100BaseFXHD  100 HD		26.4.18: 100BaseFXFD  100 FD		enterprises.43.18.8.2.3: 1000 FD TP		enterprises.43.18.8.2.7: 1000 FD Fiber SX	Speed:		.1.3.6.1.2.1.2.2.1.5		SW9300: Gauge32: 1000000000	Up/down:		.1.3.6.1.2.1.2.2.1.8		SW9300:		1 = up		2 = down# typegroup 3ss    $mib{'3ss'}{mac} = '.1.3.6.1.4.1.43.10.27.1.1.1.2';    $mib{'3ss'}{model} = '.1.3.6.1.4.1.43.10.27.1.1.1.19';    $mib{'3ss'}{descr} = '.1.3.6.1.4.1.43.10.27.1.1.1.5';    $mib{'3ss'}{serial} = '.1.3.6.1.4.1.43.10.27.1.1.1.13';    $mib{'3ss'}{hw} = '.1.3.6.1.4.1.43.10.27.1.1.1.11';    $mib{'3ss'}{sw} = '.1.3.6.1.4.1.43.10.27.1.1.1.12';		*/		// (type.equals("off8") || type.equals("ps40")) {		{			// OID: 1.3.6.1.2.1.26.1.1.1.6.<modul>.<port>.1 = 3|4			// 3 = oppe			// 4 = nede			// IfIndex = <modul><port>			String speed = "10";			String duplex = "half";			String media = "10BaseT";			// Hent listen			List portList = sSnmp.getAll(nb.getOid("3cPS40PortState"));			if (portList != null) {				for (Iterator it = portList.iterator(); it.hasNext();) {					String[] s = (String[])it.next();					String portState = s[1];					s = s[0].split("\\.");					String moduleS = s[0];					int module;					Integer port;					try {						port = new Integer(s[1]);						if (port.intValue() > 32) continue;					} catch (NumberFormatException exp) {						Log.w("PROCESS_3COM", "NumberFormatException when converting port " + s[1] + " to number, netbox: " + netboxid + ", " + exp);						continue;					}					try {						module = Integer.parseInt(moduleS);						if (module > 16) continue;					} catch (NumberFormatException exp) {						Log.d("PROCESS_3COM", "NumberFormatException when converting module " + moduleS + " to number, netbox: " + netboxid + ", " + exp);						continue;					}					String ifindex = module + (port.intValue()<10?"0":"") + port;					char link = 'n';					try {						int n = Integer.parseInt(portState);						if (n == 1 || n == 3) link = 'y';											} catch (NumberFormatException e) {						Log.w("PROCESS_3COM", "NumberFormatException on link: " + portState + ", netboxid: " + netboxid + " ifindex: " + ifindex);						continue;					}					SwModule m = sc.swModuleFactory(module);					Swport swp = m.swportFactory(ifindex);					swp.setPort(port);					swp.setLink(link);					swp.setSpeed(speed);					swp.setDuplex(duplex.charAt(0));					swp.setMedia(media);					swp.setTrunk(false);					swp.setVlan(1);					Log.d("PROCESS_3COM", "Added port, netbox: "+ netboxid +", " + swp);				}							}		}		List l;		Set moduleSet = new HashSet();		l = sSnmp.getAll(nb.getOid("3cSerial"), true);		if (l != null) {			for (Iterator it = l.iterator(); it.hasNext();) {				String[] s = (String[])it.next();				String module = s[0];				moduleSet.add(module);			}		}		String moduleWithIP = "1";		if (moduleSet.size() > 1) {			// Check which module has the IP			String ipAdEntIfIndex = nb.getOid("ipAdEntIfIndex");			if (ipAdEntIfIndex != null) {				ipAdEntIfIndex += "."+nb.getIp();				List ipList = sSnmp.getAll(ipAdEntIfIndex, false, false);				if (ipList != null && !ipList.isEmpty()) {					String[] s = (String[])ipList.get(0);					String ifindex = s[1];					int module = Integer.parseInt(""+ifindex.charAt(0));					moduleWithIP = ""+module;					NetboxInfo.put(nb.getNetboxidS(), null, "ModuleWithIP", moduleWithIP);									}			}		}		if (nb.getNumInStack() == 2 && moduleSet.size() == 1) {			moduleWithIP = NetboxInfo.getSingleton(nb.getNetboxidS(), null, "ModuleWithIP");			String module = (String)moduleSet.iterator().next();			if (moduleWithIP != null && !module.equals(moduleWithIP)) {				// Since there are only two modules we are surely talking to the one with the IP				mc.moduleTranslate(module, moduleWithIP);				sc.moduleTranslate(module, moduleWithIP);			}		} 		// Fetch ifDescr		List ifDescrList = sSnmp.getAll(nb.getOid("3cIfDescr"), true);		if (ifDescrList != null) {			for (Iterator it = ifDescrList.iterator(); it.hasNext();) {				String[] s = (String[])it.next();				String ifindex = s[0];				String ifdescr = s[1];				// Use regex for extracting unit and port				String port = null;				{					String k = extractPortFromDescr(ifdescr);					if (k != null) port = k;				}				int module = 1;				{					int k = extractModuleFromDescr(ifdescr);					if (k >= 0) module = k;				}				SwModule swm = sc.swModuleFactory(module);				Swport swp = swm.swportFactory(ifindex);				if (port != null) swp.setPort(new Integer(port));				swp.setTrunk(false);				swp.setVlan(1);				// Special case for 3Com 9300 which only has FD gigabit ports				if (type.equals("sw9300")) {					swp.setDuplex('f');					swp.setMedia("1000BaseSX");				}			}		}		// Module data		l = sSnmp.getAll(nb.getOid("3cSerial"), true);		if (l != null) {			for (Iterator it = l.iterator(); it.hasNext();) {				String[] s = (String[])it.next();				String module = s[0];				String serial = s[1];				if (moduleWithIP.equals(module) && nc.netboxDataFactory(nb).getSerial() == null) {					nc.netboxDataFactory(nb).setSerial(serial);					nc.commit();				}				if (!(nb.getNumInStack() == 2 && l.size() == 1)) {					sc.swModuleFactory(Integer.parseInt(module)).setSerial(serial);				}				Log.d("PROCESS_3COM", "Module: " + module + " Serial: " + serial);			}		}		l = sSnmp.getAll(nb.getOid("3cHwVer"), true);		if (l != null) {			for (Iterator it = l.iterator(); it.hasNext();) {				String[] s = (String[])it.next();				sc.swModuleFactory(Integer.parseInt(s[0])).setHwVer(s[1]);				Log.d("PROCESS_3COM", "Set HwVer for module " + s[0] + " to " + s[1]);			}		}		l = sSnmp.getAll(nb.getOid("3cSwVer"), true);		if (l != null) {			for (Iterator it = l.iterator(); it.hasNext();) {				String[] s = (String[])it.next();				sc.swModuleFactory(Integer.parseInt(s[0])).setSwVer(s[1]);			}		}		l = sSnmp.getAll(nb.getOid("3cModel"), true);		Map descrMap = sSnmp.getAllMap(nb.getOid("3cDescr"), true);		if (l != null) {			for (Iterator it = l.iterator(); it.hasNext();) {				String[] s = (String[])it.next();				String module = s[0];				String model = s[1];				Module swm = sc.swModuleFactory(Integer.parseInt(module));				swm.setModel(model);				if (descrMap != null && descrMap.containsKey(module)) swm.setDescr(String.valueOf(descrMap.get(module)));			}		}		// Fetch mauType		List mauTypeList = sSnmp.getAll(nb.getOid("3cIfMauType"));		if (mauTypeList != null) {			String _10BaseT = "10BaseT";			if (type.equals("sw3300")) _10BaseT = "100BaseTX"; // 3300 har bare 100Mbit-porter						HashMap oidMap = new HashMap();			String pre = "1.3.6.1.2.1.26.4.";			oidMap.put(pre+"3", new String[] { "10", "half", "FOIRL" } );			oidMap.put(pre+"10", new String[] { "10", "half", _10BaseT } );			oidMap.put(pre+"11", new String[] { "10", "full", _10BaseT } );			oidMap.put(pre+"15", new String[] { "100", "half", "100BaseTX" } );			oidMap.put(pre+"16", new String[] { "100", "full", "100BaseTX" } );			oidMap.put(pre+"17", new String[] { "100", "half", "100BaseFX" } );			oidMap.put(pre+"18", new String[] { "100", "full", "100BaseFX" } );			oidMap.put(pre+"26", new String[] { "100", "full", "1000BaseSX" } );			oidMap.put(pre+"30", new String[] { "100", "full", "1000BaseFD" } );			pre = "1.3.6.1.4.1.43.18.8.2.";			oidMap.put(pre+"2", new String[] { "1000", "full", "1000BaseSX" } );			oidMap.put(pre+"3", new String[] { "1000", "full", "1000BaseSX" } );			oidMap.put(pre+"7", new String[] { "1000", "full", "1000BaseT" } );			oidMap.put(pre+"9", new String[] { "1000", "full", "1000BaseT" } );						for (Iterator it = mauTypeList.iterator(); it.hasNext();) {				String[] s = (String[])it.next();				String ifindex = s[0].split("\\.")[0];				String mauTypeOid = s[1];				if ("0.0".equals(mauTypeOid)) continue;				if (oidMap.containsKey(mauTypeOid)) {					s = (String[])oidMap.get(mauTypeOid);					Swport swp = sc.swportFactory(ifindex);					swp.setDuplex(s[1].charAt(0));					swp.setMedia(s[2]);					swp.setTrunk(false);					swp.setVlan(1);				} else {					Log.w("PROCESS_3COM", "Unknown mauTypeOid: " + mauTypeOid + ", netboxid: " + netboxid + " ifindex: " + ifindex);				}			}		}	}	private int extractModuleFromDescr(String ifdescr) {		Matcher m = Pattern.compile("Unit +(\\d+)\\b").matcher(ifdescr);		if (m.find()) return Integer.parseInt(m.group(1));		return -1;	}	private String extractPortFromDescr(String ifdescr) {		Matcher m = Pattern.compile("Port +(\\d+)\\b").matcher(ifdescr);		if (m.find()) return m.group(1);		return null;	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -