networkdiscovery.java
来自「监控大型网络的软件。能够自动发现拓扑结构」· Java 代码 · 共 1,565 行 · 第 1/5 页
JAVA
1,565 行
if (((String)boksKat.get(new Integer(boksid))).equalsIgnoreCase("edge") && portnavn.length() == 0 && boksbak.length() == 0) continue; String color = "gray"; if (change != null && change.startsWith("Error")) { color = "red"; } else if (portnavn.length() == 0 && boksbak.length()>0) { color = "blue"; } else if (portnavn.length() > 0 && boksbak.length()==0) { if (portnavn.indexOf("-h") != -1 || portnavn.indexOf("-sw") != -1 || portnavn.indexOf("-gw") != -1) { color = "purple"; } } else if (portnavn.length() > 0 && boksbak.length()>0 && portnavn.endsWith(boksbak) ) { color = "green"; } else if (portnavn.length() > 0 && boksbak.length()>0 && !portnavn.endsWith(boksbak) ) { color = "red"; } String color1 = "<font color="+color+">"; String color2 = "</font>"; outl("<tr><a name=\"" + swrec.get("swportid") + "\">"); outl("<td align=right>"+color1+ swrec.get("swportid") + color2+"</td>"); outl("<td align=right>"+color1+ swrec.get("netboxid") + color2+"</td>"); outl("<td>"+color1+ boksNavn.get(new Integer((String)swrec.get("netboxid"))) + color2+"</td>"); outl("<td align=right>"+color1+ swrec.get("speed") + color2+"</td>"); outl("<td align=right>"+color1+ swrec.get("duplex") + color2+"</td>"); outl("<td align=right>"+color1+ swrec.get("ifindex") + color2+"</td>"); outl("<td>"+color1+ portnavn + color2+"</td>"); outl("<td>"+color1+ boksbak + color2+"</td>"); if (change != null) outl("<td><b>"+change+"</b></td>"); outl("</tr>"); } outl("</table>"); //outl("New rows: <b>" + newcnt + "</b> Updated rows: <b>" + updcnt + "</b> Removed rows: <b>"+remcnt+"</b><br>"); outl("New rows: <b>" + newcnt + "</b> Updated rows: <b>" + updcnt + "</b><br>"); outl("Sum rows: <b>" + swport.size() + "</b><br>"); outl("All done.<br>"); } private HashMap getHashFromResultSet(ResultSet rs, ResultSetMetaData md) throws SQLException { HashMap hm = new HashMap(); for (int i=md.getColumnCount(); i > 0; i--) { hm.put(md.getColumnName(i), rs.getString(i)); } return hm; } /** * avledVlan means "derive vlan" */ public void avledVlan() throws SQLException { boolean DB_UPDATE = true; boolean DB_COMMIT = true; boolean DEBUG_OUT = false; boolean TIME_OUT = true; long beginTime; //String debugParam = com.getp("debug"); if (debugParam != null && debugParam.equals("yes")) DEBUG_OUT = true; if (DEBUG_OUT) outl("Begin<br>"); // Show date { java.util.Date currentTime = new GregorianCalendar().getTime(); outl("Generated on: <b>" + currentTime + "</b><br>"); } // We start by setting boksbak (boxbehind) to null everywhere status='down', so we avoid loops { Database.update("UPDATE swport SET to_netboxid = NULL, to_swportid = NULL WHERE link!='y' AND to_netboxid IS NOT NULL"); } // Find mapping for firewalled VLANs Map fwVlanMap = new HashMap(); { beginTime = System.currentTimeMillis(); ResultSet rs = Database.query("select vlan,netaddr from vlan join prefix using(vlanid) where vlan not in (select vlan from swport where vlan is not null) and nettype='lan' and prefixid in (select prefixid from arp where end_time='infinity' and mac not in (select mac from netboxmac))", true); while (rs.next()) { String vlan = rs.getString("vlan"); String netaddr = rs.getString("netaddr"); ResultSet rs2 = Database.query("select cam.sysname,cam.netboxid,cam.ifindex,vlan from arp join cam using(mac) join swport on (moduleid in (select moduleid from module where module.netboxid=cam.netboxid) and swport.ifindex=cam.ifindex) where ip << '" + netaddr + "' and cam.end_time='infinity' and arp.end_time='infinity' and (trunk=false or trunk is null) and vlan > 1"); if (rs2.next()) { fwVlanMap.put(vlan, rs2.getString("vlan")); } } Database.free(rs); if (TIME_OUT) outl("Spent " + (System.currentTimeMillis()-beginTime) + " ms finding firewalled VLAN mappings (found " + fwVlanMap.size() + " mappings: " + fwVlanMap + ")<br>"); } // select cam.sysname,cam.netboxid,cam.ifindex,vlan from arp join cam using(mac) join swport on (moduleid in (select moduleid from module where module.netboxid=cam.netboxid) and swport.ifindex=cam.ifindex) where ip << '129.241.23.0/26' and cam.end_time='infinity' and arp.end_time='infinity'and vlan > 1; beginTime = System.currentTimeMillis(); Map dataStructs = new HashMap(); // This is really only necessary for debugging HashMap boksName = new HashMap(); ResultSet rs = Database.query("SELECT netboxid,sysname FROM netbox"); while (rs.next()) boksName.put(rs.getString("netboxid"), rs.getString("sysname")); Map vlanidVlan = new HashMap(); Map vlanidNettype = new HashMap(); rs = Database.query("SELECT vlanid,vlan,nettype FROM vlan"); while (rs.next()) { if (rs.getString("vlan") != null) vlanidVlan.put(rs.getString("vlanid"), rs.getString("vlan")); vlanidNettype.put(rs.getString("vlanid"), rs.getString("nettype")); } // Need to know what boxes are GW, all links to these are 'o', and those are not to be traversed HashSet boksGwSet = new HashSet(); rs = Database.query("SELECT netboxid FROM netbox WHERE catid IN ('GW', 'v6GW')"); while (rs.next()) boksGwSet.add(rs.getString("netboxid")); // Overview of which vlans are active on a swport connected to a gw Map swportGwVlanMap = new HashMap(); rs = Database.query("SELECT DISTINCT to_swportid,vlan,gwportid FROM gwport JOIN gwportprefix USING(gwportid) JOIN prefix USING(prefixid) JOIN vlan USING(vlanid) JOIN module USING(moduleid) WHERE to_swportid IS NOT NULL AND vlan IS NOT NULL"); while (rs.next()) swportGwVlanMap.put(rs.getString("to_swportid")+":"+rs.getString("vlan"), rs.getString("gwportid")); // Mapping from gwportid to the running vlanid and prefixid (needed for updating) Map gwportVlanidMap = new HashMap(); rs = Database.query("SELECT DISTINCT gwportid,vlanid,netboxid FROM gwport JOIN gwportprefix USING(gwportid) JOIN prefix USING(prefixid) JOIN vlan USING(vlanid) JOIN module USING(moduleid) WHERE to_swportid IS NOT NULL"); while (rs.next()) gwportVlanidMap.put(rs.getString("gwportid"), new String[] { rs.getString("vlanid"), rs.getString("netboxid") } ); // Overview of which links:vlan are blocked by spanning tree HashSet spanTreeBlocked = new HashSet(); rs = Database.query("SELECT swportid,vlan FROM swportblocked"); while (rs.next()) spanTreeBlocked.add(rs.getString("swportid")+":"+rs.getString("vlan")); // Overview of non-trunks going out from each box, per vlan HashMap nontrunkVlan = new HashMap(); rs = Database.query("SELECT swportid,netboxid,to_netboxid,to_swportid,COALESCE(vlan,1) AS vlan FROM swport JOIN module USING(moduleid) WHERE (trunk='f' OR trunk IS NULL) AND to_netboxid IS NOT NULL"); while (rs.next()) { HashMap nontrunkMap; String key = rs.getString("netboxid")+":"+rs.getString("vlan"); if ( (nontrunkMap = (HashMap)nontrunkVlan.get(key)) == null) { nontrunkMap = new HashMap(); nontrunkVlan.put(key, nontrunkMap); } HashMap hm = new HashMap(); hm.put("swportid", rs.getString("swportid")); hm.put("netboxid", rs.getString("netboxid")); hm.put("to_netboxid", rs.getString("to_netboxid")); String toid = rs.getString("to_swportid") != null ? rs.getString("to_swportid") : rs.getString("to_netboxid"); nontrunkMap.put(toid, hm); } // First, we need to retrieve an overview of which VLANs are allowed on each port HashMap allowedVlan = new HashMap(); rs = Database.query("SELECT netboxid,swportid,module,port,to_netboxid,hexstring FROM swport JOIN module USING(moduleid) JOIN swportallowedvlan USING (swportid) WHERE to_netboxid IS NOT NULL ORDER BY to_netboxid"); while (rs.next()) { HashMap boksAllowedMap; String boksid = rs.getString("netboxid"); if ( (boksAllowedMap = (HashMap)allowedVlan.get(boksid)) == null) { boksAllowedMap = new HashMap(); allowedVlan.put(boksid, boksAllowedMap); } HashMap hm = new HashMap(); hm.put("swportid", rs.getString("swportid")); hm.put("netboxid", rs.getString("netboxid")); hm.put("module", rs.getString("module")); hm.put("port", rs.getString("port")); hm.put("to_netboxid", rs.getString("to_netboxid")); hm.put("hexstring", rs.getString("hexstring")); String boksbak = rs.getString("to_netboxid"); if (boksAllowedMap.containsKey(boksbak)) outl("<font color=red>WARNING</font>: Multiple trunks between <b>"+boksName.get(boksid)+"</b> and <b>"+boksName.get(boksbak)+"</b><br>"); boksAllowedMap.put(boksbak, hm); } // We need to know which VLANs go out on non-trunk ports from a given box // We use a HashMap of HashSets HashMap activeVlan = new HashMap(); // VLAN is active on port, even if port is down, and we must include the VLAN of the IP address of the box itself rs = Database.query("SELECT DISTINCT swportid,netboxid,COALESCE(vlan,1) AS vlan FROM swport JOIN module USING(moduleid) WHERE (trunk='f' OR trunk IS NULL) AND to_netboxid IS NULL"); while (rs.next()) { Map m; String netboxid = rs.getString("netboxid"); if ((m = (Map)activeVlan.get(netboxid)) == null) activeVlan.put(netboxid, m = new HashMap()); Set s; if ((s = (Set)m.get(new Integer(rs.getInt("vlan")))) == null) m.put(new Integer(rs.getInt("vlan")), s = new HashSet()); s.add(rs.getString("swportid")); } // The VLAN of the netbox' IP should also be added to activeVlan rs = Database.query("SELECT netboxid,vlan FROM netbox JOIN prefix USING(prefixid) JOIN vlan USING(vlanid) WHERE vlan IS NOT NULL"); while (rs.next()) { Map m; String netboxid = rs.getString("netboxid"); if ((m = (Map)activeVlan.get(netboxid)) == null) activeVlan.put(netboxid, m = new HashMap()); Integer vl = new Integer(rs.getInt("vlan")); if (!m.containsKey(vl)) m.put(vl, new HashSet()); } // Mapping of which swport is connected to another swport HashMap swportidMap = new HashMap(); rs = Database.query("SELECT swportid,COALESCE(vlan,1) AS vlan,to_swportid FROM swport WHERE (trunk='f' OR trunk IS NULL) AND to_swportid IS NOT NULL"); while (rs.next()) { HashMap hm = new HashMap(); hm.put("vlan", rs.getString("vlan")); hm.put("to_swportid", rs.getString("to_swportid")); swportidMap.put(rs.getString("swportid"), hm); } // Mapping of which VLAN that runs between two boxes where we do not have to_swportid Map nbvlanMap = new HashMap(); dataStructs.put("nbvlanMap", nbvlanMap); rs = Database.query("SELECT netboxid,to_netboxid,COALESCE(vlan,1) AS vlan FROM module JOIN swport USING(moduleid) WHERE (trunk='f' OR trunk IS NULL) AND to_netboxid IS NOT NULL AND to_swportid IS NULL ORDER BY netboxid"); while (rs.next()) { String key = rs.getString("netboxid")+":"+rs.getString("to_netboxid"); if (nbvlanMap.containsKey(key)) { outl("<font color=red>WARNING</font>: Multiple links between <b>"+boksName.get(rs.getString("netboxid"))+"</b> and <b>"+boksName.get(rs.getString("to_netboxid"))+" without exact swport knowledge (swportid)</b><br>"); } else { nbvlanMap.put(key, rs.getString("vlan")); } } // Using cam/arp to check VLAN behind a netbox/ifindex (when we come from trunk) Map swportidVlanMap = new HashMap(); Set swportidVlanDupeSet = new HashSet(); dataStructs.put("swportidVlanMap", swportidVlanMap); rs = Database.query("SELECT swportid,vlanid,COUNT(*) AS count FROM module JOIN swport USING(moduleid) JOIN cam ON (module.netboxid = cam.netboxid AND swport.ifindex = cam.ifindex and cam.end_time = 'infinity') JOIN arp ON (cam.mac = arp.mac AND arp.end_time = 'infinity') JOIN prefix ON (arp.prefixid = prefix.prefixid) JOIN vlan USING(vlanid) WHERE (trunk='f' OR trunk IS NULL) GROUP BY swportid,vlanid ORDER BY swportid,count DESC"); while (rs.next()) { String key = rs.getString("swportid")+":"+rs.getString("vlanid"); if (swportidVlanDupeSet.add(key)) { swportidVlanMap.put(rs.getString("swportid"), rs.getString("vlanid")); } else { outl("<font color=red>WARNING</font>: Multiple VLANs detected behind non-trunk port (swportid="+rs.getString("swportid")+", vlanid="+rs.getString("vlanid")+")<br>"); } } if (TIME_OUT) outl("Spent " + (System.currentTimeMillis()-beginTime) + " ms fetching data from db<br>"); // Then we retrieve all VLANs and which switch the VLAN "starts at" outl("<pre>"); beginTime = System.currentTimeMillis(); rs = Database.query("SELECT DISTINCT module.netboxid,vlanid,vlan.vlan,sysname,gwportid,gwport.to_netboxid,gwport.to_swportid,trunk,hexstring FROM prefix JOIN vlan USING(vlanid) JOIN gwportprefix ON (prefix.prefixid = gwportprefix.prefixid AND (hsrp='t' OR gwip::text IN (SELECT MIN(gwip::text) FROM gwportprefix GROUP BY prefixid HAVING COUNT(DISTINCT hsrp) = 1))) JOIN gwport USING(gwportid) JOIN module USING(moduleid) JOIN netbox USING (netboxid) LEFT JOIN swport ON (gwport.to_swportid=swportid) LEFT JOIN swportallowedvlan USING (swportid) WHERE (gwport.to_netboxid IS NOT NULL OR catid='GSW') AND vlan.vlan IS NOT NULL ORDER BY vlan.vlan"); Set vlansWithRouter = new HashSet(); while (rs.next()) { vlansWithRouter.add(rs.getString("vlan")); } rs.beforeFirst(); ArrayList trunkVlan = new ArrayList(); Set doneVlan = new HashSet(); Set visitedNodeSet = new HashSet(); // The set of nodes we've visited; reset for each VLAN Set foundGwSet = new HashSet(); // ***** BEGIN DEPTH FIRST SEARCH ***** // while (rs.next()) { int vlan = rs.getInt("vlan"); if (fwVlanMap.containsKey(""+vlan) && !vlansWithRouter.contains(fwVlanMap.get(""+vlan))) { if (DEBUG_OUT) outl("Mapping vlan " + vlan + " to " + fwVlanMap.get(""+vlan)); vlan = Integer.parseInt((String)fwVlanMap.get(""+vlan)); } int vlanid = rs.getInt("vlanid"); String boksid = rs.getString("netboxid"); String nettype = (String)vlanidNettype.get(""+vlanid); doneVlan.add(""+vlan); visitedNodeSet.clear(); String netaddr = "NA"; String boksbak = rs.getString("to_netboxid"); if (boksbak == null || boksbak.length() == 0) boksbak = boksid; // Spesialtilfelle for GSW enheter String swportbak = rs.getString("to_swportid"); boolean cameFromTrunk = rs.getBoolean("trunk"); String hexstring = rs.getString("hexstring"); if (DEBUG_OUT) outl("\n<b>NEW VLAN: " + vlan + "</b> (netaddr: <b>"+netaddr+"</b>)<br>"); // Check whether there is a trunk or non-trunk down to the GW if (cameFromTrunk) { // Now we expect a hexstring to be in place if (hexstring == null) { if (DEBUG_OUT) outl("\n<b>AllowedVlan hexstring for trunk down to switch is missing, skipping...</b><br>"); continue; } // Verify that the VLAN is actually are allowed on the trunk if (!isAllowedVlan(hexstring, vlan)) { if (DEBUG_OUT) outl("\n<b>Vlan is not allowed on trunk down to switch, and there is no non-trunk, skipping...</b><br>"); continue; } } // List of gwports we have uplink to List foundGwports = new ArrayList(); if (vlanTraverseLink(vlan, vlanid, boksid, boksbak, cameFromTrunk, true, nontrunkVlan, allowedVlan, activeVlan, swportidMap, spanTreeBlocked, trunkVlan, dataStructs, foundGwports, visitedNodeSet, 0, DEBUG_OUT, boksGwSet, swportGwVlanMap, boksName)) { // The vlan is active on this unit, so we add it if (swportbak != null) { String[] tvlan = { swportbak, String.valueOf(vlanid), "o" }; trunkVlan.add(tvlan);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?