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

📄 ciscoswcat.java

📁 Network Administration Visualized 网络管理可视化源码
💻 JAVA
字号:
package no.ntnu.nav.getDeviceData.deviceplugins.CiscoSwCAT;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.regex.Matcher;import java.util.regex.Pattern;import no.ntnu.nav.ConfigParser.ConfigParser;import no.ntnu.nav.SimpleSnmp.SimpleSnmp;import no.ntnu.nav.SimpleSnmp.TimeoutException;import no.ntnu.nav.getDeviceData.Netbox;import no.ntnu.nav.getDeviceData.dataplugins.DataContainer;import no.ntnu.nav.getDeviceData.dataplugins.DataContainers;import no.ntnu.nav.getDeviceData.dataplugins.Module.ModuleContainer;import no.ntnu.nav.getDeviceData.dataplugins.Swport.SwModule;import no.ntnu.nav.getDeviceData.dataplugins.Swport.SwportContainer;import no.ntnu.nav.getDeviceData.deviceplugins.DeviceHandler;import no.ntnu.nav.logger.Log;/** * <p> * DeviceHandler for collecting the standard Cisco CAT switch port OIDs. * </p> * * <p> * This plugin handles the following OID keys: * </p> * * <ul> *	<li>From Cisco CAT</li> *	<ul> *	 <li>portIfIndex</li> *	 <li>portDuplex</li> *	 <li>portVlan</li> *	 <li>portVlansAllowed</li> *	 <li>portTrunk</li> *	 <li>portPortName</li> *	</ul> * </ul> * </p> * */public class CiscoSwCAT implements DeviceHandler{	private static String[] canHandleOids = {			"portDuplex", 			"portIfIndex", 			"portVlan", 			"portTrunk", 			"portVlansAllowed", 			"portPortName",	};	/**	 * This pattern should match things like Fa1/0/1 or Gi0/5.  Use group number	 * 2 to get the port number part.	 */	private static Pattern portPattern = Pattern.compile("(.*/(\\d+))$");	private SimpleSnmp sSnmp;	public int canHandleDevice(Netbox nb) {		int v = nb.isSupportedOids(canHandleOids) ? ALWAYS_HANDLE : NEVER_HANDLE;		Log.d("CAT_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("CAT_DEVHANDLER");		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;		}		this.sSnmp = sSnmp;		processCAT(nb, mc, sc);		// Commit data		if (mc.isCommited()) sc.setEqual(mc);		sc.commit();	}	/**	 * <p>Collects and processes data from a Cisco Switch running CatOS (or otherwise OID compatible box).</p>	 * 	 * <p>As opposed to the CiscoSwCAT plugin, this one requires the portIfIndex OID to be supported, or	 * no interfaces will be collected.</p>	 * 	 */	private void processCAT(Netbox nb, ModuleContainer mc, SwportContainer sc) throws TimeoutException	{		HashMap modPortIfindex = new HashMap();		List portIfIndexes = sSnmp.getAll(nb.getOid("portIfIndex"));		Map ifNames = sSnmp.getAllMap(nb.getOid("ifName"), true);		Map ifIndices = sSnmp.getAllMap(nb.getOid("ifIndex"));		if (portIfIndexes != null) {			// Iterate the portIfIndex response to enumerate modules/ports			for (Iterator it = portIfIndexes.iterator(); it.hasNext();) {				String[] s = (String[])it.next();				String moduleDotPort = s[0];				String ifindex = s[1];				/* Tests reveal that some switches report non-real ifindices in 				 * this table, so we verify the existence of the ifindex before				 * proceeding:				 */				if (!ifIndices.containsKey(ifindex)) {					Log.w("PROCESS_CAT", "Ignoring non-existant ifindex " + ifindex + " from portIfIndex map");					continue;				}								String[] s2 = moduleDotPort.split("\\.");				Integer module = Integer.valueOf(s2[0]);				// Set the initial port number from the portIfIndex OID				Integer port = Integer.valueOf(s2[1]);				if (ifNames != null && ifNames.containsKey(ifindex)) {					// Interpret the ifName string to get a port number					String ifName = (String) ifNames.get(ifindex);					Matcher matcher = portPattern.matcher(ifName);					if (matcher.matches()) {						port = Integer.valueOf(matcher.group(2));					}				}				modPortIfindex.put(moduleDotPort, ifindex);				Log.d("PROCESS_CAT", "ifIndex " + ifindex+ " maps to " + moduleDotPort);								SwModule swm = sc.swModuleFactory(module.intValue());				swm.swportFactory(ifindex).setPort(port); // Create module <-> ifindex mapping			}						List response = sSnmp.getAll(nb.getOid("portDuplex"));			if (response != null) {				for (Iterator it = response.iterator(); it.hasNext();) {					String[] s = (String[])it.next();					String modport = s[0];					String ifindex = (String)modPortIfindex.get(modport);					char duplex = (s[1].equals("1") ? 'h' : 'f');					sc.swportFactory(ifindex).setDuplex(duplex);				}			}			response = sSnmp.getAll(nb.getOid("portPortName"), true);			if (response != null) {				for (Iterator it = response.iterator(); it.hasNext();) {					String[] s = (String[])it.next();					String ifindex = (String) modPortIfindex.get(s[0]);					sc.swportFactory(ifindex).setPortname(s[1]);				}			}							response = sSnmp.getAll(nb.getOid("portVlan"));			if (response != null) {				for (Iterator it = response.iterator(); it.hasNext();) {					String[] s = (String[])it.next();					int vlan = 0;					try{						vlan = Integer.parseInt(s[1]);					} catch	 (NumberFormatException e) {						Log.w("PROCESS_CAT", "netboxid: " + nb.getNetboxid() + " ifindex: " + s[0] + " NumberFormatException on vlan: " + s[1]);					}					String ifindex = (String) modPortIfindex.get(s[0]);					sc.swportFactory(ifindex).setVlan(vlan);				}			}			response = sSnmp.getAll(nb.getOid("portTrunk"));			if (response != null) {				for (Iterator it = response.iterator(); it.hasNext();) {					String[] s = (String[])it.next();					boolean trunk = (s[1].equals("1") ? true : false);					String ifindex = (String) modPortIfindex.get(s[0]);					sc.swportFactory(ifindex).setTrunk(trunk);				}			}			response = sSnmp.getAll(nb.getOid("portVlansAllowed"));			if (response != null) {				for (Iterator it = response.iterator(); it.hasNext();) {					String[] s = (String[])it.next();					String ifindex = (String) modPortIfindex.get(s[0]);					sc.swportFactory(ifindex).setHexstring(s[1]);				}			}						}	}}

⌨️ 快捷键说明

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