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

📄 networkdiscovery.java

📁 Network Administration Visualized 网络管理可视化源码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/* * $Id: networkDiscovery.java 3554 2006-07-17 13:54:16Z 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> *  * Please, if you want to hack this code but cannot read Norwegian, use the * mailing lists and have someone put in the effort of translating the * comments in this file :-P *  */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() {	}	// Main metoden	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;		/*		if (!configFile.startsWith("-")) {			beginOptions = 1;			try {				cp = new ConfigParser(configFile);			} catch (IOException e) {				nu.outl("Error, could not read config file: " + configFile);				return;			}		}		*/		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);		}	}	/*	private String mpToIfindex(String modul, String port)	{		int m,p;		try {			if (modul.startsWith("Fa")) m = Integer.parseInt(modul.substring(2, modul.length()));			else if (modul.startsWith("Gi")) m = 100+Integer.parseInt(modul.substring(2, modul.length()));			else m = Integer.parseInt(modul);			p = Integer.parseInt(port);		} catch (NumberFormatException e) {			outl("ERROR, NumberFormatExeption: Modul: " + modul + " Port: " + port);			return null;		}		return String.valueOf(m)+String.valueOf(p);	}	*/	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>");		// Vis dato		{			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"); // Må være med da sysname kan være null !!			boksNavn.put(new Integer(rs.getInt("netboxid")), (sysname==null?"&lt;null&gt;":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"));		}		// Endret for å få med GSW		//rs = Database.query("SELECT swp_netbox.netboxid,catid,swp_netbox.module,port,swp_netbox.to_netboxid,swp_netbox.to_module,swp_netbox.to_port,module.netboxid AS gwnetboxid FROM swp_netbox JOIN netbox USING(netboxid) JOIN prefix USING(prefixid) LEFT JOIN gwport ON (rootgwid=gwportid) LEFT JOIN module USING (moduleid) WHERE gwportid IS NOT NULL OR catid='GSW' ORDER BY netboxid,module,port");		//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) JOIN prefix USING(prefixid) LEFT JOIN gwportprefix USING(prefixid) 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");		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 = rs.getInt("boksid");		int previd = 0;		while (rs.next()) {			int boksid = rs.getInt("netboxid");			if (boksid != previd) {				// Ny boks				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("port"),				rs.getString("to_netboxid"),				rs.getString("to_ifindex")				//rs.getString("to_port")			};			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();		}		// Legg til alle enheter vi bare har funnet i boksbak		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>");			}		}		// Til slutt sjekker vi uplink-portene, dette vil normalt kun gjelde uplink mot -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>");		/*		// Vi må vite hvilke bokser som har trunker ut fra seg, dvs. det kjører flere vlan		HashSet boksWithTrunk = new HashSet();		rs = Database.query("SELECT DISTINCT netboxid FROM swport JOIN module USING(moduleid) WHERE trunk='t'");		while (rs.next()) boksWithTrunk.add(rs.getString("netboxid"));		*/		/*		// Vi trenger en oversikt over hvilket vlan de forskjellige boksene er på		HashMap boksVlan = new HashMap();		rs = Database.query("SELECT netboxid,vlan FROM netbox JOIN prefix USING (prefixid) JOIN vlan USING(vlanid) WHERE vlanid IS NOT NULL");		while (rs.next()) {			boksVlan.put(rs.getString("netboxid"), rs.getString("vlan"));		}		*/		// Nå går vi gjennom alle portene vi har funnet boksbak for, og oppdaterer tabellen med dette		int newcnt=0,updcnt=0,resetcnt=0;		ArrayList swport = new ArrayList();		HashMap swrecMap = new HashMap();		Map swrecSwportidMap = new HashMap();

⌨️ 快捷键说明

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