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

📄 networkdiscovery.java

📁 监控大型网络的软件。能够自动发现拓扑结构
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
				}				// If any gwports use a different vlanid we must change it to the current one				for (Iterator it = foundGwports.iterator(); it.hasNext();) {					String gwportid = (String)it.next();					String[] vlanPrefix = (String[])gwportVlanidMap.get(gwportid);					String oldVlanid = vlanPrefix[0];					String gwNetboxid = vlanPrefix[1];					foundGwSet.add(gwNetboxid+":"+vlanid);					if (vlanid != Integer.parseInt(oldVlanid)) {						// Swap in prefix						Database.update("UPDATE prefix SET vlanid="+vlanid+" WHERE prefixid IN (SELECT prefixid FROM gwportprefix WHERE gwportid="+gwportid+")");					}				}			}		}		outl("</pre>");		if (TIME_OUT) outl("Spent " + (System.currentTimeMillis()-beginTime) + " ms traversing VLANs from router<br>");		// XXX: The below commented out code is an attempt at support for private (i.e. non-routed) VLANs.		//      Unfortunately it does not work at all and has an ugly tendency to mess up people's NAV installs,		//      thus its current commented out state. May be cleaned up at some future date.		/*		outl("\n<b>VLANs with no router to start from:</b><br>");		outl("<pre>");		updateVlanDb(trunkVlan, vlanidVlan, allowedVlan, boksName, false);		beginTime = System.currentTimeMillis();		// All VLANs we cannot find a starting point for, where we actually need to start everywhere to be sure we find everything		// SELECT DISTINCT ON (vlan,boksid) boksid,modul,port,boksbak,vlan,trunk FROM swport NATURAL JOIN swportvlan WHERE vlan NOT IN (SELECT DISTINCT vlan FROM (prefiks JOIN gwport USING (prefiksid)) JOIN boks USING (boksid) WHERE boksbak IS NOT NULL AND vlan IS NOT NULL) AND boksbak IS NOT NULL ORDER BY vlan,boksid		if (DEBUG_OUT) outl("\n<b><h3>VLANs with no router to start from:</h3></b><br>");		{			// Delete mismatching ports			// DELETE FROM swport WHERE swportid IN (SELECT swportid FROM swport JOIN swportvlan USING(swportid) JOIN vlan USING(vlanid) WHERE vlan.vlan != swport.vlan AND direction IN ('x','u'))			String[] vlansDone = util.stringArray(doneVlan);			String vlansDoneS = "";			if (vlansDone.length > 0) {				Arrays.sort(vlansDone);				vlansDoneS = "AND vlan NOT IN (" + util.join(vlansDone, ",") + ")";			}			//String sql = "SELECT swportid,vlan,vlanid,sysname,to_netboxid,to_swportid,trunk FROM swport JOIN module USING(moduleid) JOIN netbox ON (to_netboxid=netbox.netboxid) LEFT JOIN swportvlan USING(swportid) WHERE vlan NOT IN (" + util.join(vlansDone, ",") + ") AND to_netboxid IS NOT NULL AND vlan IS NOT NULL AND (direction IS NULL OR direction IN ('x','u')) ORDER BY vlan, vlanid";			String sql = "SELECT DISTINCT netboxid,vlan,vlanid,sysname FROM swport JOIN module USING(moduleid) JOIN netbox USING (netboxid) LEFT JOIN swportvlan USING(swportid) WHERE vlan IS NOT NULL " + vlansDoneS + " AND link='y' AND trunk!=TRUE AND (direction IS NULL OR direction IN ('x','u')) ORDER BY vlan DESC, vlanid";			rs = Database.query(sql);			outl("SQL: " + sql);			int prevvlan=-1;			int vlanid = -1;			HashSet visitNode = null;			while (rs.next()) {				int vlan = rs.getInt("vlan");				if (vlan != prevvlan) {					visitNode = new HashSet();					prevvlan = vlan;					vlanid = rs.getInt("vlanid");					if (vlanid == 0) {						String[] ins = new String[] {							"vlanid", "",							"vlan", ""+vlan,							"nettype", "lan"						};						vlanid = Integer.parseInt(Database.insert("vlan", ins, null));						vlanidVlan.put(""+vlanid, ""+vlan);						outl("\n<b>CREATE NEW VLAN: " + vlan + "</b>, vlanid="+vlanid+", starting from <b>"+rs.getString("sysname")+"</b><br>");					} else {						outl("\n<b>NEW VLAN: " + vlan + "</b>, vlanid="+vlanid+", starting from <b>"+rs.getString("sysname")+"</b><br>");					}				}				if (visitNode.contains(rs.getString("netboxid"))) continue;				vlanTraverseLink(vlan, vlanid, null, rs.getString("netboxid"), false, false, nontrunkVlan, allowedVlan, activeVlan, swportidMap, spanTreeBlocked, trunkVlan, dataStructs, new ArrayList(), visitNode, 0, DEBUG_OUT, boksGwSet, swportGwVlanMap, boksName);			}		}		outl("</pre>");		if (TIME_OUT) outl("Spent " + (System.currentTimeMillis()-beginTime) + " ms traversing VLANs with no router<br>");		*/				Map activeOnTrunk = updateVlanDb(trunkVlan, vlanidVlan, allowedVlan, boksName, true);		// Then we report mismatches between swportallowedvlan and what is actually active		outl("<h2>Allowed, but non-active VLANs:</h2>");		outl("<h4>(<i><b>Note</b>: VLANs 1 and 1000-1005 are for interswitch control traffic and are always allowed</i>)</h4>");		int allowedcnt=0, totcnt=0;		Iterator iter = allowedVlan.values().iterator();		while (iter.hasNext()) {			HashMap boksAllowedMap = (HashMap)iter.next();			Iterator iter2 = boksAllowedMap.values().iterator();			while (iter2.hasNext()) {				HashMap hm = (HashMap)iter2.next();				String swportid = (String)hm.get("swportid");				String hexstring = (String)hm.get("hexstring");				HashSet activeVlanOnTrunk = (HashSet)activeOnTrunk.get(swportid);				if (activeVlanOnTrunk == null) {					//outl("ERROR, swrecTrunk is missing for swportid: " + swportid + "<br>");					continue;				}				totcnt++;				String boksid = (String)hm.get("netboxid");				String modul = (String)hm.get("module");				String port = (String)hm.get("port");				String boksbak = (String)hm.get("to_netboxid");				boolean printMsg = false;				int startRange=0;				boolean markLast=false;				int MIN_VLAN = 2;				int MAX_VLAN = 999;				for (int i=MIN_VLAN; i <= MAX_VLAN+1; i++) {					if (isAllowedVlan(hexstring, i) && !activeVlanOnTrunk.contains(String.valueOf(i)) && i != MAX_VLAN+1 ) {						if (!markLast) {							startRange=i;							markLast = true;						}					} else {						if (markLast) {							String range = (startRange==i-1) ? String.valueOf(i-1) : startRange+"-"+(i-1);							if (!printMsg) {								allowedcnt++;								outl("Working with trunk From("+boksid+"): <b>"+boksName.get(boksid)+"</b>, Modul: <b>"+modul+"</b> Port: <b>"+port+"</b> To("+boksbak+"): <b>"+boksName.get(boksbak)+"</b><br>");								// Print aktive VLANs								out("&nbsp;&nbsp;Active VLANs: <b>");								Iterator vlanIter = activeVlanOnTrunk.iterator();								int[] vlanA = new int[activeVlanOnTrunk.size()];								int vlanAi=0;								while (vlanIter.hasNext()) vlanA[vlanAi++] = Integer.parseInt((String)vlanIter.next());								Arrays.sort(vlanA);								boolean first=true;								for (vlanAi=0; vlanAi < vlanA.length; vlanAi++) {									if (!first) out(", "); else first=false;									out(String.valueOf(vlanA[vlanAi]));								}								outl("</b><br>");								//outl("&nbsp;&nbsp;The following VLANs are allowed on the trunk, but does not seem to be active:<br>");								printMsg = true;								out("&nbsp;&nbsp;Excessive VLANs: <b>"+range+"</b>");							} else {								out(", <b>"+range+"</b>");							}							markLast=false;						}						//startRange=i+1;					}				}				if (printMsg) outl("<br><br>");			}		}		outl("A total of <b>"+allowedcnt+"</b> / <b>"+totcnt+"</b> trunks have allowed VLANs that are not active.<br>");		// Send event to eventengine		Map varMap = new HashMap();		varMap.put("command", "updateFromDB");		EventQ.createAndPostEvent("getDeviceData", "eventEngine", 0, 0, 0, "notification", Event.STATE_NONE, 0, 0, varMap);		outl("All done.<br>");	}	public Map updateVlanDb(List trunkVlan, Map vlanidVlan, Map allowedVlan, Map boksName, boolean deleteNoDirection) throws SQLException {		HashMap swportvlan = new HashMap();		HashMap swportvlanNontrunk = new HashMap();		HashMap swportvlanDupe = new HashMap();		String sql = "SELECT swportvlanid,swportid,vlanid,direction,trunk FROM swportvlan JOIN swport USING (swportid)";		ResultSet rs = Database.query(sql);		HashSet reportMultipleVlan = new HashSet();		while (rs.next()) {			String swportid = rs.getString("swportid");			String key = swportid+":"+rs.getString("vlanid");			swportvlanDupe.put(key, rs.getString("direction") );			swportvlan.put(key, rs.getString("swportvlanid") );			if (!rs.getBoolean("trunk")) {				if (swportvlanNontrunk.containsKey(swportid)) {					if (!reportMultipleVlan.contains(swportid)) {						outl("<font color=\"red\">ERROR!</font> Multiple vlans for non-trunk port, swportid: " + key + "<br>");						reportMultipleVlan.add(swportid);					}					continue;				}				swportvlanNontrunk.put(swportid, rs.getString("swportvlanid") );			}		}		outl("<br><b>Report:</b> (found "+trunkVlan.size()+" records)<br>");		HashMap activeOnTrunk = new HashMap(); // This is used to verify swportallowedvlan against what is actually active		int newcnt=0,updcnt=0,dupcnt=0,remcnt=0,renamecnt=0;		for (int i=0; i < trunkVlan.size(); i++) {			String[] s = (String[])trunkVlan.get(i);			String swportid = s[0];			String vlanid = s[1];			String vlan = (String)vlanidVlan.get(vlanid);			String direction = s[2];			String key = swportid+":"+vlanid;			if (swportvlanDupe.containsKey(key)) {				// The element already exists in the database, so we do not insert it				// Check if we should update				String dbRetning = (String)swportvlanDupe.get(key);				if (!dbRetning.equals(direction)) {					// Update necessary					String[] updateFields = {						"direction", direction					};					String[] condFields = {						"swportid", swportid,						"vlanid", vlanid					};					Database.update("swportvlan", updateFields, condFields);					outl("[UPD] swportid: " + swportid + " vlan: <b>"+ vlan +"</b> Direction: <b>" + direction + "</b> (old: "+dbRetning+")<br>");					updcnt++;				} else {					dupcnt++;				}				// We are not to delete this record now				swportvlan.remove(key);			} else {				// This could be a non-trunk port where we've rewritten/translated the VLAN value				if (swportvlanNontrunk.containsKey(swportid)) {					// Yep, let's just update					updcnt++;					outl("[UPD] swportid: " + swportid + " vlan: <b>"+ vlan +"</b> Direction: <b>" + direction + "</b> (renamed)<br>");					String swportvlanid = (String)swportvlanNontrunk.get(swportid);					String[] updateFields = {						"vlanid", vlanid,						"direction", direction					};					String[] condFields = {						"swportvlanid",						swportvlanid					};					Database.update("swportvlan", updateFields, condFields);				} else {					// swportvlan does not already contain this entry, so we must insert it. 					newcnt++;					swportvlanDupe.put(key, direction);					outl("[NEW] swportid: " + swportid + " vlan: <b>"+ vlan +"</b> Retning: <b>" + direction + "</b><br>");					// Insert into swportvlan					String[] fields = {						"swportid", swportid,						"vlanid", vlanid,						"direction", direction,					};					Database.insert("swportvlan", fields);				}			}			// Then add to activeOnTrunk			//HashMap swrecTrunk;			HashSet activeVlanOnTrunk;			if ( (activeVlanOnTrunk = (HashSet)activeOnTrunk.get(swportid)) == null) {				activeVlanOnTrunk = new HashSet();				activeOnTrunk.put(swportid, activeVlanOnTrunk);			}			if (vlan == null) {				System.err.println("WARNING: vlan is null for vlanid: " + vlanid);				vlan = "-1";			}			activeVlanOnTrunk.add(vlan);		}		// Now we can iterate swportvlan and delete those entries that no longer exist.		Iterator iter = swportvlan.entrySet().iterator();		while (iter.hasNext()) {			remcnt++;			Map.Entry me = (Map.Entry)iter.next();			String key = (String)me.getKey();			String swportvlanid = (String)me.getValue();							StringTokenizer st = new StringTokenizer(key, ":");			String swportid = st.nextToken();			String vlanid = st.nextToken();			String vlan = (String)vlanidVlan.get(vlanid);			if (!deleteNoDirection) {				String direction = (String) swportvlanDupe.get(key);				if ("x".equals(direction) || "u".equals(direction)) continue;			}						outl("[REM] swportid: " + swportid + " vlan: <b>"+ vlan +"</b> ("+swportvlanid+")<br>");			Database.update("DELETE FROM swportvlan WHERE swportvlanid = '"+swportvlanid+"'");		}		// Then we delete all vlans without either prefices or swports		int delPrefix = Database.update("DELETE FROM prefix WHERE prefixid NOT IN (SELECT prefixid FROM gwportprefix) AND vlanid NOT IN (SELECT vlanid FROM vlan JOIN swportvlan USING(vlanid) UNION SELECT vlanid FROM vlan WHERE nettype='scope')");		int delVlan = Database.update("DELETE FROM vlan WHERE vlanid NOT IN (SELECT vlanid FROM prefix UNION SELECT vlanid FROM swportvlan UNION SELECT vlanid FROM vlan WHERE nettype='scope')");		outl("New count: <b>"+newcnt+"</b>, Update count: <b>"+updcnt+"</b> Dup count: <b>"+dupcnt+"</b>, Rem count: <b>"+remcnt+"</b> delPrefix: <b>"+delPrefix+"</b>, delVlan: <b>"+delVlan+"</b>, Rename vlan count: <b>"+renamecnt+"</b><br>");		return activeOnTrunk;	}	private boolean vlanTraverseLink(int vlan,																	 int vlanid,																	 String fromid,																	 String boksid,																	 boolean cameFromTrunk,																	 boolean setDirection,																	 HashMap nontrunkVlan,																	 HashMap allowedVlan,																	 HashMap activeVlan,

⌨️ 快捷键说明

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