📄 networkdiscovery.java
字号:
/* * $Id: networkDiscovery.java 3912 2007-02-21 14:32:46Z mortenv $ * * Copyright 2004-2005 Norwegian University of Science and Technology * * This file is part of Network Administration Visualized (NAV) * * NAV is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * NAV is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with NAV; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * * Physical and vlan topology discovery * Authors: Kristian Eide <kreide@online.no> *//* * Source code comments were translated from Norwegian to English by Morten * Brekkevold, on a best effort basis. I do not know the code (yet), so I * can not guarantee that the translated comments make any sense. Some * pointers: * * - The Norwegian term "boks" means "box" or "netbox" * - The Norwegian term "avled" means "derive" * - The abbreviation "MP" refers to the combination "module+port" * - The Norwegian term "bak" means "behind" * * So I guess the term "BoksMpBak" refers to finding out which module+port a * box is behind, i.e. is connected to. * * - Morten B. 2007-02-21 */ import java.io.File;import java.io.IOException;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.GregorianCalendar;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import java.util.StringTokenizer;import no.ntnu.nav.Path;import no.ntnu.nav.ConfigParser.ConfigParser;import no.ntnu.nav.Database.Database;import no.ntnu.nav.event.Event;import no.ntnu.nav.event.EventQ;class networkDiscovery{ public static final String dbConfigFile = (Path.sysconfdir + "/db.conf").replace('/', File.separatorChar);; public static final String scriptName = "networkDiscovery"; private static String debugParam; public networkDiscovery() { } public static void main(String[] args) throws IOException { networkDiscovery nu = new networkDiscovery(); if (args.length < 1) { nu.outl("Arguments: [configFile] <options>\n"); nu.outl("Where options include:\n"); nu.outl(" topology\tDiscover the network topology using data collected via SNMP."); nu.outl(" vlan\tDiscover which VLANs are running on each network link"); nu.outl(" debug\tTurn on debugging output."); return; } int beginOptions = 0; String configFile = args[0]; ConfigParser cp, dbCp; try { dbCp = new ConfigParser(dbConfigFile); } catch (IOException e) { nu.outl("Error, could not read database config file: " + dbConfigFile); return; } if (!Database.openConnection(dbCp.get("dbhost"), dbCp.get("dbport"), dbCp.get("db_nav"), dbCp.get("script_"+scriptName), dbCp.get("userpw_"+dbCp.get("script_"+scriptName)))) { nu.outl("Error, could not connect to database!"); return; } Set argSet = new HashSet(); for (int i=beginOptions; i < args.length; i++) argSet.add(args[i]); if (argSet.contains("debug")) debugParam = "yes"; try { String title; if (argSet.contains("topology")) title = "Network discovery report"; else if (argSet.contains("vlan")) title = "Vlan discovery report"; else title = "Argument is not valid"; nu.outl("<html>"); nu.outl("<head><title>"+title+"</title></head>"); nu.outl("<body>"); if (argSet.contains("topology")) nu.avledTopologi(); else if (argSet.contains("vlan")) nu.avledVlan(); else { nu.outl("Argument is not valid, start without arguments for help."); } nu.outl("</body>"); nu.outl("</html>"); } catch (SQLException e) { nu.errl("SQLException: " + e.getMessage()); e.printStackTrace(System.err); } } /** * avledTopologi means "derive topology", presumably the physical topology. */ public void avledTopologi() throws SQLException { boolean DEBUG_OUT = false; //String debugParam = com.getp("debug"); if (debugParam != null && debugParam.equals("yes")) DEBUG_OUT = true; Boks.DEBUG_OUT = DEBUG_OUT; if (DEBUG_OUT) outl("Begin<br>"); // Show date { java.util.Date currentTime = new GregorianCalendar().getTime(); outl("Generated on: <b>" + currentTime + "</b><br>"); } Map boksNavn = new HashMap(); Map boksType = new HashMap(); Map boksKat = new HashMap(); ResultSet rs = Database.query("SELECT netboxid,sysName,typename,catid FROM netbox LEFT JOIN type USING(typeid)"); while (rs.next()) { String sysname = rs.getString("sysName"); // Must be included as sysname can be null !! boksNavn.put(new Integer(rs.getInt("netboxid")), (sysname==null?"<null>":sysname) ); boksType.put(new Integer(rs.getInt("netboxid")), rs.getString("typename")); boksKat.put(new Integer(rs.getInt("netboxid")), rs.getString("catid")); } Boks.boksNavn = boksNavn; Boks.boksType = boksType; Set gwUplink = new HashSet(); rs = Database.query("SELECT DISTINCT ON (to_netboxid) to_netboxid FROM gwport WHERE to_netboxid IS NOT NULL"); while (rs.next()) { gwUplink.add(rs.getString("to_netboxid")); } rs = Database.query("SELECT swp_netbox.netboxid,catid,swp_netbox.ifindex,swp_netbox.to_netboxid,swport.ifindex AS to_ifindex,module.netboxid AS gwnetboxid FROM swp_netbox JOIN netbox USING(netboxid) LEFT JOIN gwportprefix ON (netbox.prefixid = gwportprefix.prefixid AND (hsrp='t' OR gwip::text IN (SELECT MIN(gwip::text) FROM gwportprefix GROUP BY prefixid HAVING COUNT(DISTINCT hsrp) = 1))) LEFT JOIN gwport USING(gwportid) LEFT JOIN module USING (moduleid) LEFT JOIN swport ON (swp_netbox.to_swportid=swport.swportid) WHERE gwportid IS NOT NULL OR catid='GSW' ORDER BY netboxid,swp_netbox.ifindex"); Map bokser = new HashMap(); List boksList = new ArrayList(); List l = null; Set boksidSet = new HashSet(); Set boksbakidSet = new HashSet(); int previd = 0; while (rs.next()) { int boksid = rs.getInt("netboxid"); if (boksid != previd) { // New box l = new ArrayList(); boolean isSW = (rs.getString("catid").equals("SW") || rs.getString("catid").equals("GW") || rs.getString("catid").equals("GSW")); Boks b = new Boks(boksid, rs.getInt("gwnetboxid"), l, bokser, isSW, !gwUplink.contains(String.valueOf(boksid)) ); boksList.add(b); previd = boksid; } String[] s = { rs.getString("ifindex"), rs.getString("to_netboxid"), rs.getString("to_ifindex") }; l.add(s); boksidSet.add(new Integer(boksid)); boksbakidSet.add(new Integer(rs.getInt("to_netboxid"))); } int maxBehindMp=0; for (int i=0; i < boksList.size(); i++) { Boks b = (Boks)boksList.get(i); bokser.put(b.getBoksidI(), b); b.init(); if (b.maxBehindMp() > maxBehindMp) maxBehindMp = b.maxBehindMp(); } // Add all units we found only in boksbak (boxbehind) boksbakidSet.removeAll(boksidSet); Iterator iter = boksbakidSet.iterator(); while (iter.hasNext()) { Integer boksbakid = (Integer)iter.next(); String kat = (String)boksKat.get(boksbakid); if (kat == null) { errl("Error! kat not found for boksid: " + boksbakid); } boolean isSW = (kat.equals("SW") || kat.equals("GW") || kat.equals("GSW")); Boks b = new Boks(boksbakid.intValue(), 0, null, bokser, isSW, true); if (!bokser.containsKey(b.getBoksidI())) boksList.add(b); bokser.put(b.getBoksidI(), b); if (DEBUG_OUT) outl("Adding boksbak("+b.getBoksid()+"): <b>"+b.getName()+"</b><br>"); } if (DEBUG_OUT) outl("Begin processing, maxBehindMp: <b>"+maxBehindMp+"</b><br>"); for (int level=1; level <= maxBehindMp; level++) { boolean done = true; for (int i=0; i < boksList.size(); i++) { Boks b = (Boks)boksList.get(i); if (b.proc_mp(level)) done = false; } for (int i=0; i < boksList.size(); i++) { Boks b = (Boks)boksList.get(i); b.removeFromMp(); } if (!done) { if (DEBUG_OUT) outl("Level: <b>"+level+"</b>, state changed.<br>"); } } // Finally, we check the uplink ports, this will normally only be uplinks to -gw for (int i=0; i < boksList.size(); i++) { Boks b = (Boks)boksList.get(i); b.proc_mp(Boks.PROC_UPLINK_LEVEL); } if (DEBUG_OUT) outl("<b>BEGIN REPORT</b><br>"); for (int i=0; i < boksList.size(); i++) { Boks b = (Boks)boksList.get(i); if (DEBUG_OUT) b.report(); b.guess(); } HashMap boksMp = new HashMap(); for (int i=0; i < boksList.size(); i++) { Boks b = (Boks)boksList.get(i); b.addToMp(boksMp); } if (DEBUG_OUT) outl("Report done.<br>"); // Now we loop through all the ports we've found boksbak (boxbehind) for, and update the table with these int newcnt=0,updcnt=0,resetcnt=0; ArrayList swport = new ArrayList(); HashMap swrecMap = new HashMap(); Map swrecSwportidMap = new HashMap(); rs = Database.query("SELECT swportid,netboxid,link,speed,duplex,ifindex,portname,to_netboxid,trunk,hexstring FROM swport JOIN module USING(moduleid) LEFT JOIN swportallowedvlan USING (swportid) ORDER BY netboxid,ifindex"); ResultSetMetaData rsmd = rs.getMetaData(); while (rs.next()) { HashMap hm = getHashFromResultSet(rs, rsmd); String link = rs.getString("link"); if (link == null || link.toLowerCase().equals("y")) swport.add(hm); String key = rs.getString("netboxid")+":"+rs.getString("ifindex"); swrecMap.put(key, hm); swrecSwportidMap.put(rs.getString("swportid"), hm); } if (DEBUG_OUT) outl("boksMp listing....<br>"); iter = boksMp.entrySet().iterator(); while (iter.hasNext()) { Map.Entry me = (Map.Entry)iter.next(); String key = (String)me.getKey(); //Integer boksbak = (Integer)me.getValue(); BoksMpBak bmp = (BoksMpBak)me.getValue(); StringTokenizer st = new StringTokenizer(key, ":"); String boksid; String ifindex; try { boksid = st.nextToken(); ifindex = st.nextToken(); } catch (Exception e) { errl("Exception: " + e.getMessage() + " Key: " + key + " bmp: " + bmp); e.printStackTrace(System.err); return; } //outl(boksNavn.get(new Integer(boksid)) + " Modul: " + modul + " Port: " + port + " Link: " + boksNavn.get(boksbak) + "<br>"); if (swrecMap.containsKey(key)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -