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

📄 queryboks.java

📁 监控大型网络的软件。能够自动发现拓扑结构
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
										continue;									}									Log.d("MAC_ENTRY", "Ifindex: " + ifindex + " on VLAN: " + dbVlan + " ("+vlan+") is now in blocking mode (swportid="+swportid+")");									try {										String[] ins = {											"swportid", swportid,											"vlan", dbVlan										};										Database.insert("swportblocked", ins);										if (DB_COMMIT) Database.commit(); else Database.rollback();										blockedCnt++;									} catch (SQLException e) {										Log.d("MAC_ENTRY", "SQLException: " + e.getMessage());										e.printStackTrace(System.err);									}								} else {									Log.d("MAC_ENTRY", "Missing swportid for ifindex " + ifindex + " on boks: " + boksIdName.get(netboxid));								}							} else {								blockedCnt++;							}						}						// Nå har vi tatt bort alle porter som fortsatt er blokkert, og resten er da ikke blokkert, så de må slettes						for (Iterator iter = blockedIfind.entrySet().iterator(); iter.hasNext();) {							Map.Entry me = (Map.Entry)iter.next();							String swportid = (String)me.getKey();							String ifindex = (String)me.getValue();							String dbVlan = (vlan.length() == 0 ? (String)vlanMap.get(netboxid+":"+ifindex) : vlan);							String sql = "DELETE FROM swportblocked WHERE swportid='"+swportid+"'";							if (dbVlan == null) {								Log.d("MAC_ENTRY", "swportid: " + swportid + " (vlan not found, ifindex: " + ifindex+") is no longer in blocking mode.");							} else {								sql += " AND vlan='"+dbVlan+"'";							}							Log.d("MAC_ENTRY", "swportid: " + swportid + " on VLAN: " + dbVlan + " ("+vlan+") is no longer in blocking mode.");							try {								Database.update(sql);								if (DB_COMMIT) Database.commit(); else Database.rollback();							} catch (SQLException e) {								Log.d("MAC_ENTRY", "SQLException: " + e.getMessage());								e.printStackTrace(System.err);							}						}					}					if (macVlan == null || macVlan.size() == 0) continue;					if (!csAtVlan) vlan = "[all]";					Log.d("MAC_ENTRY", "Querying vlan: " + vlan + ", MACs: " + macVlan.size() + " Mappings: " + portIndexMap.size() + " Blocked: " + blockedCnt + " / " + mpBlocked.size() );											activeVlanCnt++;					boolean b = false;					for (Iterator vlanIt = macVlan.iterator(); vlanIt.hasNext();) {						String[] s = (String[])vlanIt.next();						String mac = decimalToHexMac(s[0]);						if (mac.length() != 12) {							Log.d("PROCESS_MAC", "Wrong length: " + s[0] + " vs " + mac);						}						//Log.d("MAC_ENTRY", "Found mac: " + mac + " portIndex: " + s[1] + "("+ boksIdName.get(macBoksId.get(mac)) +")");													// Sjekk om MAC adressen vi har funnet er dem samme som den for enheten vi spør						// Dette skjer på C35* enhetene.						if (netboxid.equals(macBoksId.get(mac))) continue;													// Finn ifIndex						String ifindex = (String)portIndexMap.get(s[1]);						if (ifindex == null) {							if (!"0".equals(s[1])) {								Log.d("MAC_ENTRY", "MAC: " + mac + " (" + s[0] + ") ("+ boksIdName.get(macBoksId.get(mac)) +") found at index: " + s[1] + ", but no ifIndex mapping exists.");							}							continue;						}													// Nå har vi funnet minst en MAC fra denne enheten, og da sier vi at den er oppe og aktiv,						safeCloseBoksidAdd(netboxid);													// Prosesser Mac (CAM)						processMac(netboxid, ifindex, mac);													// Sjekk om vi skal ta med denne mac						if (!macBoksId.containsKey(mac)) continue;						String to_netboxid = (String)macBoksId.get(mac);													String to_cat = (String)boksidKat.get(to_netboxid);						if (to_cat == null || isNetel(to_cat)) {							foundBoksBak.add(ifindex);						}						PortBoks pm = new PortBoks(ifindex, to_netboxid, "MAC");						if (dupCheck.contains(netboxid+":"+pm)) continue;						dupCheck.add(netboxid+":"+pm);						l.add(pm);												}				}			} catch (TimeoutException te) {				// Vi gjør ingenting her, ikke svar på dette vlan				continue;			}		}		// Nå kan vi sjekke om CAM-køen skal settes inn i cam-tabellen eller ikke		runCamQueue(netboxid, foundBoksBak);		sSnmp.setDefaultTimeoutLimit();		Log.d("MAC_ENTRY", "MACs found on " + activeVlanCnt + " / " + vlanSet.size() + " VLANs, units on " + unitVlanCnt + ".");		return l;	}		/*	private ArrayList processCisco1900(String boksid, String ip, String cs_ro, String boksType, HashMap ifindexMp) throws TimeoutException	{		ArrayList l = new ArrayList();		String baseOid = ".1.3.6.1.2.1.17.4.3.1.2";		// Get the list of macs		// Vi får ut alle MAC-adressene tre ganger, under <baseOid>.1, .2 og .3. Den første kobler desimal-mac til hex-mac		// Den andre kobler desimal-mac til port, og det er denne som brukes her. Desimal-mac'en blir konvertert til hex		// istedenfor å hente ut i hex-format fra enheten. Den tredje angir status, dette benyttes ikke her.		//ArrayList macList = getOIDs(ip, cs_ro, baseOid);		sSnmp.setParams(ip, cs_ro, baseOid);		ArrayList macList = sSnmp.getAll();		HashSet foundBoksBak = new HashSet();		for (int i=0; i<macList.size(); i++) {			String[] macs = (String[])macList.get(i);			String mac = decimalToHexMac(macs[0]);			String ifind = macs[1];			if (!ifind.equals("0")) {				String[] mp = (String[])ifindexMp.get(ifind);				if (mp == null) {					outla("processCisco1900: Error, " + boksIdName.get(boksid) + "("+boksid+"), could not find mp for ifindex: " + ifind);					continue;				}				String modul = mp[0];				String port = mp[1];				// Nå har vi funnet minst en MAC fra denne enheten, og da sier vi at den er oppe og aktiv,				safeCloseBoksidAdd(boksid);				// Prosesser Mac-adressen (CAM)				processMac(boksid, modul, port, mac);				if (macBoksId.containsKey(mac)) {					String boksidBak = (String)macBoksId.get(mac);					String boksBakKat = (String)boksidKat.get(boksidBak);					if (boksBakKat == null || isNetel(boksBakKat)) {						foundBoksBak.add(modul+":"+port);					}					PortBoks pm = new PortBoks(modul, port, boksidBak, "MAC");					l.add(pm);				}			}		}		// Nå kan vi sjekke om CAM-køen skal settes inn i cam-tabellen eller ikke		runCamQueue(boksid, foundBoksBak);		return l;	}	private ArrayList processCisco1Q(String boksid, String ip, String cs_ro, String boksType) throws TimeoutException	{		ArrayList l = new ArrayList();		//String baseOid = ".1.3.6.1.4.1.9.5.14.4.3.1.3.1";		String baseOid = "1.3.6.1.4.1.9.5.14.4.3.1.4.1";		// Get the list of macs		//ArrayList macList = getOIDs(ip, cs_ro, baseOid);		sSnmp.setParams(ip, cs_ro, baseOid);		ArrayList macList = sSnmp.getAll();		// Modul er alltid 1 på denne typen enhet		String modul = "1";		HashSet foundBoksBak = new HashSet();		for (int i=0; i<macList.size(); i++) {			String[] s = (String[])macList.get(i);			// Kun enheter av type 1 er lokal (type 2 = remote)			if (Integer.parseInt(s[1]) != 1) continue;			String port = s[0].substring(0, s[0].indexOf("."));			String deciMac = s[0].substring(s[0].indexOf(".")+1, s[0].length());			String mac = decimalToHexMac(deciMac);			// Nå har vi funnet minst en MAC fra denne enheten, og da sier vi at den er oppe og aktiv,			safeCloseBoksidAdd(boksid);			// Prosesser Mac (CAM)			processMac(boksid, modul, port, mac);			if (macBoksId.containsKey(mac)) {				String boksidBak = (String)macBoksId.get(mac);				String boksBakKat = (String)boksidKat.get(boksidBak);				if (boksBakKat == null || isNetel(boksBakKat)) {					foundBoksBak.add(modul+":"+port);				}				PortBoks pm = new PortBoks(modul, port, boksidBak, "MAC");				l.add(pm);			}		}		// Nå kan vi sjekke om CAM-køen skal settes inn i cam-tabellen eller ikke		runCamQueue(boksid, foundBoksBak);		return l;	}	private ArrayList processCisco2Q(String boksid, String ip, String cs_ro, String typegruppe, String boksType, HashMap ifindexMp) throws TimeoutException	{		ArrayList l = new ArrayList();		String ciscoIndexMapBaseOid = ".1.3.6.1.2.1.17.1.4.1.2";		String ciscoMacBaseOid = ".1.3.6.1.2.1.17.4.3.1.2";		String spanningTreeOid = ".1.3.6.1.2.1.17.2.15.1.3";		// HashSet for å sjekke for duplikater		HashSet dupCheck = new HashSet();		HashSet foundBoksBak = new HashSet();		// Hent macadresser for hvert vlan og knytt disse til riktig mp (port)		int activeVlanCnt=0;		int unitVlanCnt=0;		Set vlanSet = (Set)vlanBoksid.get(boksid);		if (vlanSet == null) return l;		// Så vi ikke venter så lenge dersom vi ikke får svar fra et vlan		sSnmp.setTimeoutLimit(1);		for (Iterator it = vlanSet.iterator(); it.hasNext();) {			String vlan = String.valueOf(it.next());			out("  Fetch vlan: " + vlan + "...");			// Hent porter som er i blocking (spanning-tree) mode			//ArrayList spanningTree = getOIDs(ip, cs_ro+"@"+vlan, spanningTreeOid);			sSnmp.setParams(ip, cs_ro+"@"+vlan, spanningTreeOid);			ArrayList spanningTree;			try {				spanningTree = sSnmp.getAll();			} catch (TimeoutException te) {				// Vi gjør ingenting her, ikke svar på dette vlan				outl("timeout");				continue;			}			outl("ok");			ArrayList mpBlocked = new ArrayList();			for (int j=0; j < spanningTree.size(); j++) {				String[] s = (String[])spanningTree.get(j);				if (s[1].equals("2")) mpBlocked.add(s[0]);			}			// Hent macadresser på dette vlan			//ArrayList macVlan = getOIDs(ip, cs_ro+"@"+vlan, ciscoMacBaseOid);			sSnmp.setParams(ip, cs_ro+"@"+vlan, ciscoMacBaseOid);			ArrayList macVlan = sSnmp.getAll();			if (mpBlocked.size() == 0) {				// Nå vet vi at ingen porter er blokkert på denne enheten på dette vlan				HashMap blockedIfind = (HashMap)spanTreeBlocked.get(boksid+":"+vlan);				if (blockedIfind != null) {					// Slett eksisterende innslag i databasen					try {						outl("    All ports on " + sysnameMap.get(boksid) + " are now non-blocking");						Database.update("DELETE FROM swportblocked WHERE EXISTS (SELECT swportid FROM swport JOIN module USING(moduleid) WHERE netboxid="+boksid+" AND swportblocked.swportid=swportid) AND vlan='"+vlan+"'");						if (DB_COMMIT) Database.commit(); else Database.rollback();					} catch (SQLException e) {						outld("*ERROR* While deleting from swportblocked ("+boksid+","+vlan+"): SQLException: " + e.getMessage());					}				}				if (macVlan.size() == 0) continue;			}			// Lag mapping mellom ifIndex og cisco intern portindex			//ArrayList indexList = getOIDs(ip, cs_ro+"@"+vlan, ciscoIndexMapBaseOid);			sSnmp.setParams(ip, cs_ro+"@"+vlan, ciscoIndexMapBaseOid);			ArrayList indexList = sSnmp.getAll();			HashMap hIndexMap = new HashMap();			for (int j=0; j < indexList.size(); j++) {				String[] s = (String[])indexList.get(j);				hIndexMap.put(s[0], s[1]);			}			int blockedCnt=0;			if (mpBlocked.size() > 0) {				HashMap blockedIfind = (HashMap)spanTreeBlocked.get(boksid+":"+vlan);				if (blockedIfind == null) blockedIfind = new HashMap(); // Ingen porter er blokkert på dette vlan				for (int j=0; j < mpBlocked.size(); j++) {					String s = (String)mpBlocked.get(j);					String ifind = (String)hIndexMap.get(s);					if (ifind == null) continue;					// OK, nå kan vi sjekke om denne eksisterer fra før					String swportid = (String)blockedIfind.remove(ifind);					if (swportid == null) {						// Eksisterer ikke fra før, må settes inn, hvis den eksisterer i swport						// Finn porten						String[] mp = (String[])ifindexMp.get(ifind);						if (mp == null) {							errl("    Error, mp not found for ifIndex: " + ifind + " on boks: " + boksIdName.get(boksid));							continue;						}						swportid = (String)swportSwportidMap.get(boksid+":"+mp[0]+":"+mp[1]);						if (swportid != null) {							outl("    Ifindex: " + ifind + " on VLAN: " + vlan + " is now in blocking mode.");							//String query = "INSERT INTO swportblocked (swportid,vlan) VALUES ((SELECT swportid FROM swport WHERE boksid='"+boksid+"' AND ifindex='"+ifind+"'),'"+vlan+"')";							String query = "INSERT INTO swportblocked (swportid,vlan) VALUES ('"+swportid+"','"+vlan+"')";							if (DB_UPDATE) {								try {									Database.update(query);									if (DB_COMMIT) Database.commit(); else Database.rollback();									blockedCnt++;								} catch (SQLException e) {									outld("*ERROR* SQLException: " + e.getMessage());								}							}						} else {							outla("    Error, swportid is null for mp=["+mp[0]+","+mp[1]+"] on boks: " + boksIdName.get(boksid));						}					} else {						blockedCnt++;					}				}				// Nå har vi tatt bort alle porter som fortsatt er blokkert, og resten er da ikke blokkert, så det må slettes				Iterator iter = blockedIfind.values().iterator();				while (iter.hasNext()) {					String swportid = (String)iter.next();					outl("    swportid: " + swportid + " on VLAN: " + vlan + " is no longer in blocking mode.");					String query = "DELETE FROM swportblocked WHERE swportid='"+swportid+"' AND vlan='"+vlan+"'";					if (DB_UPDATE) {						try {							Database.update(query);							if (DB_COMMIT) Database.commit(); else Database.rollback();						} catch (SQLException e) {							outld("*ERROR* SQLException: " + e.getMessage());						}					}				}			}			if (macVlan.size() == 0) continue;			outl("  Querying vlan: " + vlan + ", MACs: " + macVlan.size() + " Mappings: " + indexList.size() + " Blocked: " + blockedCnt + " / " + mpBlocked.size() );			activeVlanCnt++;			boolean b = false;			for (int j=0; j < macVlan.size(); j++) {				String[] s = (String[])macVlan.get(j);				String mac = decimalToHexMac(s[0]);				// Sjekk om MAC adressen vi har funnet er dem samme som den for enheten vi spør				// Dette skjer på C35* enhetene.				if (boksid.equals(macBoksId.get(mac))) continue;				// Finn ifIndex				String ifInd = (String)hIndexMap.get(s[1]);				if (ifInd == null) {					outl("  WARNING! MAC: " + mac + " ("+ boksIdName.get(macBoksId.get(mac)) +") found at index: " + s[1] + ", but no ifIndex mapping exists.");					continue;				}				// Finn mp				String[] mp = (String[])ifindexMp.get(ifInd);				if (mp == null) {					outl("  WARNING! MAC: " + mac + " ("+ boksIdName.get(macBoksId.get(mac)) +") found at index: " + s[1] + ", ifIndex: " + ifInd + ", but no mp mapping exists.");					continue;				}				String modul = mp[0];				String port = mp[1];				// Nå har vi funnet minst en MAC fra denne enheten, og da sier vi at den er oppe og aktiv,				safeCloseBoksidAdd(boksid);				// Prosesser Mac (CAM)				processMac(boksid, modul, port, mac);				// Sjekk om vi skal ta med denne mac				if (!macBoksId.containsKey(mac)) continue;				String boksidBak = (String)macBoksId.get(mac);				String boksBakKat = (String)boksidKat.get(boksidBak);				if (boksBakKat == null || isNetel(boksBakKat)) {					foundBoksBak.add(modul+":"+port);				}				//outl("  Unit: " + unit + " Port: " + port + " Mac: " + mac);				// Legg til i listen over macer				if (modul == null || port == null || boksidBak == null) {					errl("--**-- errError modul|port|boksidBak null, boksid: " + boksid + " ifInd: " + ifInd + " modul: " + modul + " port: " + port + " boksidBak: "+ boksidBak);					continue;				}				PortBoks pm = new PortBoks(modul, port, boksidBak, "MAC");				if (dupCheck.contains(boksid+":"+pm)) continue;				dupCheck.add(boksid+":"+pm);				l.add(pm);				if (!b) { unitVlanCnt++; b=true; }			}		}		// Nå kan vi sjekke om CAM-køen skal settes inn i cam-tabellen eller ikke		runCamQueue(boksid, foundBoksBak);		sSnmp.setDefaultTimeoutLimit();		outl("  MACs found on " + activeVlanCnt + " / " + vlanSet.size() + " VLANs, units on " + unitVlanCnt + ".");		return l;	}	private HashMap fetchIfindexMpMap(String ip, String cs_ro, String typegruppe) throws TimeoutException	{		List l;		//String ciscoIndexMapBaseOid = ".1.3.6.1.2.1.17.1.4.1.2";		// Hent kobling mellom ifIndex<->mp		HashMap ifindexH = new HashMap();				if (l != null) {			outl("  Found " + l.size() + " ifindex<->mp mappings.");			for (int i=0; i < l.size(); i++) {				String[] s = (String[])l.get(i);				StringTokenizer st = new StringTokenizer(s[0], ".");				String[] mp = { st.nextToken(), st.nextToken() };				ifindexH.put(s[1], mp);			}					} else {			l = sSnmp.getAll(nb.getOid("ifDescr"), true);

⌨️ 快捷键说明

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