📄 networkdiscovery.java
字号:
// Record already exists, check if update is necessary HashMap swrec = (HashMap)swrecMap.get(key); //swrecMap.remove(key); swrec.put("deleted", null); String link = (String)swrec.get("link"); if (link != null) link = link.toLowerCase(); if ("n".equals(link)) continue; // Ignore non-up links String idbak = (String)swrec.get("to_netboxid"); if (idbak == null || idbak != null && Integer.parseInt(idbak) != bmp.boksbak.intValue()) { // Update necessary updcnt++; // swport { String[] updateFields = { "to_netboxid", bmp.boksbak.toString() }; String[] condFields = { "swportid", (String)swrec.get("swportid") }; Database.update("swport", updateFields, condFields); } String vlan = "non-s"; swrec.put("to_netboxid", bmp.boksbak.toString()); swrec.put("change", "Updated ("+vlan+")"); } // Then we must check whether swportbak (swportbehind) needs to be updated boolean swportbakOK = false; if (bmp.toIfindex != null) { // OK, look up in swportMap to find the correct swportid Map swrecBak = (Map)swrecMap.get(bmp.hashKey()); if (swrecBak != null) { swportbakOK = true; String new_swportbak = (String)swrecBak.get("swportid"); String cur_swportbak = (String)swrec.get("to_swportid"); if (cur_swportbak == null || !cur_swportbak.equals(new_swportbak)) { String[] updateFields = { "to_swportid", new_swportbak }; String[] condFields = { "swportid", (String)swrec.get("swportid") }; Database.update("swport", updateFields, condFields); } } else { // Error! outl("<font color=\"red\">ERROR:</font> Could not find record in swport, boks("+bmp.boksbak+"): <b>" + boksNavn.get(bmp.boksbak) + "</b> Ifindex: <b>" + bmp.toIfindex + "</b> boksbak: <b>" + boksNavn.get(new Integer(boksid)) + "</b> ("+bmp.hashKey()+")<br>"); } } // Then we must check whether we have a trunk without allowedvlan if ("t".equals(swrec.get("trunk")) && (swrec.get("hexstring") == null || swrec.get("hexstring").equals("")) ) { // We have a trunk that is static or is missing hexstring, then we just grab and insert the hexstring from the other end Boks b = (Boks)bokser.get(bmp.boksbak); //Mp mpBak = b.getMpTo(Integer.parseInt(boksid), modul, port); String toIfindex = b.getIfindexTo(Integer.parseInt(boksid), ifindex); if (toIfindex != null) { // Port on the other end found, but does it exist in the table? String keyBak = bmp.boksbak+":"+toIfindex; if (swrecMap.containsKey(keyBak)) { // Exists, check whether it is a trunk port HashMap swrecBak = (HashMap)swrecMap.get(keyBak); if ("t".equals(swrecBak.get("trunk"))) { // Trunk, check if we need to update swportallowedvlan String allowedVlan = (String)swrec.get("hexstring"); String allowedVlanBak = (String)swrecBak.get("hexstring"); if (allowedVlan == null || allowedVlan.length() == 0) { if (allowedVlanBak == null || allowedVlanBak.length() == 0) { // Error! Now we are in big trouble, there's a static trunk on both ends... outl("<font color=\"red\">ERROR:</font> Link is trunk with no swportallowedvlan on either side! boks: " + boksNavn.get(new Integer(boksid)) + " Ifindex: " + ifindex+ " boksBak: " + boksNavn.get(bmp.boksbak) + " ToIfindex: " + swrecBak.get("ifindex") + "<br>"); } else { // Now we must insert a new record into swportallowedvlan String[] fields = { "swportid", (String)swrec.get("swportid"), "hexstring", allowedVlanBak }; if (DEBUG_OUT) outl("Inserting new record in swportallowedvlan, swportid: " + swrec.get("swportid") + " new hexstring: " + allowedVlanBak + "<br>"); boolean update = false; try { Database.insert("swportallowedvlan", fields); } catch (SQLException e) { // Woops, try to update instead update = true; } if (update) try { outl("<font color=\"red\">ERROR:</font> swportallowedvlan seems to already have an empty record! Trying to update instead...<br>"); Database.update("UPDATE swportallowedvlan SET hexstring='"+allowedVlanBak+"' WHERE swportid='"+swrec.get("swportid")+"'"); } catch (SQLException e) { outl("<font color=\"red\">ERROR:</font> Cannot update swportallowedvlan, SQLException: " + e.getMessage() + "<br>"); } } } else if (!allowedVlan.equals(allowedVlanBak)) { // Update necessary String[] updateFields = { "hexstring", allowedVlanBak }; String[] condFields = { "swportid", (String)swrec.get("swportid") }; Database.update("swportallowedvlan", updateFields, condFields); if (DEBUG_OUT) outl("Updated swportallowedvlan, swportid: " + condFields[0] + " old hexstring: " + allowedVlan + " new hexstring: " + allowedVlanBak + "<br>"); } } else { // Error, trunk<->non-trunk! outl("<font color=\"red\">ERROR:</font> Link is trunk / non-trunk: boks: " + boksNavn.get(new Integer(boksid)) + " Ifindex: " + ifindex + " boksBak: " + boksNavn.get(bmp.boksbak) + " ToIfindex: " + swrecBak.get("ifindex") + "<br>"); } } } } } else { // This is now an error which should not occur! :-) (Wow, an eror that shouldn't occur, what a grand concept!) outl("<font color=\"red\">ERROR:</font> Could not find record for other side of link! boks("+boksid+"): <b>" + boksNavn.get(new Integer(boksid)) + "</b> Ifindex: <b>" + ifindex + "</b> boksBak: <b>" + boksNavn.get(bmp.boksbak) + "</b><br>"); } } if (DEBUG_OUT) outl("boksMp listing done.<br>"); iter = swrecMap.entrySet().iterator(); while (iter.hasNext()) { Map.Entry me = (Map.Entry)iter.next(); String key = (String)me.getKey(); HashMap swrec = (HashMap)me.getValue(); String swportid = (String)swrec.get("swportid"); String boksbak = (String)swrec.get("to_netboxid"); String swportbak = (String)swrec.get("to_swportid"); if (swportbak != null && swportbak.length() > 0) { boolean reset = false; if (boksbak == null || boksbak.length() == 0) { reset = true; } else { Map swrecBak = (Map)swrecSwportidMap.get(swportbak); if (swrecBak == null || !boksbak.equals(swrecBak.get("netboxid"))) { reset = true; } } if (reset) { resetcnt++; // Set fields to null String[] updateFields = { "to_swportid", "null" }; String[] condFields = { "swportid", swportid }; Database.update("swport", updateFields, condFields); if (DEBUG_OUT) outl("Want to reset swportbak(2) for swportid: " + swportid + "<br>"); swportbak = null; } } if (swrec.containsKey("deleted")) continue; if (boksbak != null && boksbak.length() > 0) { // Set to null resetcnt++; String[] updateFields = { "to_netboxid", "null", "to_swportid", "null" }; String[] condFields = { "swportid", swportid }; Database.update("swport", updateFields, condFields); if (DEBUG_OUT) outl("Want to reset boksbak for swportid: " + swportid + "<br>"); } else if (swportbak != null && swportbak.length() > 0) { // Set fields to null resetcnt++; String[] updateFields = { "to_swportid", "null" }; String[] condFields = { "swportid", swportid }; Database.update("swport", updateFields, condFields); if (DEBUG_OUT) outl("Want to reset swportbak for swportid: " + swportid + "<br>"); } } outl("<table>"); outl(" <tr>"); outl(" <td><b>swpid</b></td>"); outl(" <td><b>boksid</b></td>"); outl(" <td><b>sysName</b></td>"); outl(" <td><b>typeId</b></td>"); outl(" <td><b>Speed</b></td>"); outl(" <td><b>Duplex</b></td>"); outl(" <td><b>Ifindex</b></td>"); outl(" <td><b>Portnavn</b></td>"); outl(" <td><b>Boksbak</b></td>"); outl(" <td><b>Change (vlan)</b></td>"); outl(" </tr>"); int attCnt=0; for (int i=0; i < swport.size(); i++) { HashMap swrec = (HashMap)swport.get(i); String boksid = (String)swrec.get("netboxid"); String ifindex = (String)swrec.get("ifindex"); String portnavn = (String)swrec.get("portname"); //boolean isStatic = swrec.get("static").equals("t"); String change = (String)swrec.get("change"); if (portnavn == null) portnavn = ""; String boksbak = ""; //Integer idbak = (Integer)boksMp.get(boksid+":"+modul+":"+port); BoksMpBak bmp = (BoksMpBak)boksMp.get(boksid+":"+ifindex); Integer idbak = (bmp != null) ? bmp.boksbak : null; if (idbak != null) boksbak = (String)boksNavn.get(idbak); if (boksbak == null) { outl("ERROR! boksbak is null for idbak: " + idbak + "<br>"); 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"; } if (!color.equals("purple") && !color.equals("red")) continue; if (portnavn.length() > 2 && portnavn.charAt(0) == 'n' && portnavn.charAt(2) == ':') continue; attCnt++; String color1 = "<font color="+color+">"; String color2 = "</font>"; outl("<tr>"); //outl("<td align=right>"+color1+ swrec.get("swportid") + color2+"</td>"); outl("<td align=right><a href=\"#" + swrec.get("swportid") + "\">" + swrec.get("swportid") + "</a></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>"+color1+ boksType.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("Found <b>" + attCnt + "</b> rows in need of attention.<br>"); outl("<h2>swport:</h2>"); outl("<table>"); outl(" <tr>"); outl(" <td><b>swpid</b></td>"); outl(" <td><b>boksid</b></td>"); outl(" <td><b>sysName</b></td>"); outl(" <td><b>Speed</b></td>"); outl(" <td><b>Duplex</b></td>"); outl(" <td><b>Ifindex</b></td>"); outl(" <td><b>Portnavn</b></td>"); outl(" <td><b>Boksbak</b></td>"); outl(" <td><b>Change (vlan)</b></td>"); outl(" </tr>"); for (int i=0; i < swport.size(); i++) { HashMap swrec = (HashMap)swport.get(i); String boksid = (String)swrec.get("netboxid"); String ifindex = (String)swrec.get("ifindex"); String portnavn = (String)swrec.get("portname"); //boolean isStatic = swrec.get("static").equals("t"); String change = (String)swrec.get("change"); if (portnavn == null) portnavn = ""; String boksbak = ""; BoksMpBak bmp = (BoksMpBak)boksMp.get(boksid+":"+ifindex); Integer idbak = (bmp != null) ? bmp.boksbak : null; if (idbak != null) boksbak = (String)boksNavn.get(idbak); if (boksbak == null) { outl("ERROR! boksbak is null for idbak: " + idbak + "<br>"); continue; } // Because of too high volume, EDGE equipment with empty portnames and boksbak (boxbehind) are not included in the list if (boksKat.get(new Integer(boksid)) == null) { System.err.println("ERROR, boksKat is null for boksid: " + boksid); continue; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -