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

📄 getboksmacs.java

📁 监控大型网络的软件。能够自动发现拓扑结构
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * getBoksMacs *  * $LastChangedRevision: 4096 $ * * $LastChangedDate: 2007-07-13 11:44:19 +0200 (fre, 13 jul 2007) $ * * Copyright 2002-2004 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 */import java.io.*;import java.util.*;import java.net.*;import java.text.*;import java.sql.*;import no.ntnu.nav.ConfigParser.*;import no.ntnu.nav.Database.*;import no.ntnu.nav.SimpleSnmp.*;import no.ntnu.nav.logger.*;import no.ntnu.nav.Path;/** * getBoksMacs collects CAM and CDP data from routers/switches * * @version $LastChangedRevision: 4096 $ $LastChangedDate: 2007-07-13 11:44:19 +0200 (fre, 13 jul 2007) $ * @author Kristian Eide &lt;kreide@online.no&gt; */class getBoksMacs{	public static final String dbConfigFile = (Path.sysconfdir + "/db.conf").replace('/', File.separatorChar);	public static final String configFile = (Path.sysconfdir + "/getBoksMacs.conf").replace('/', File.separatorChar);	public static final String watchMacsFile = (Path.sysconfdir + "/watchMacs.conf").replace('/', File.separatorChar);	public static final String scriptName = "getBoksMacs";	public static final String logFile = (Path.localstatedir + "/log/getBoksMacs.log").replace('/', File.separatorChar);	public static int NUM_THREADS = 24;	public static final int SHOW_TOP = 25;	public static final boolean DB_COMMIT = true;	public static final boolean DUMP_CAM = true;	/*	public static final boolean DB_COMMIT = false;	public static final boolean DUMP_CAM = false;	*/	// Felles datastrukturer som bare skal leses fra	static HashMap macBoksId = new HashMap();	static HashMap boksIdName = new HashMap();	static HashMap boksidKat = new HashMap();	static HashMap boksidType = new HashMap();	static HashMap sysnameMap = new HashMap();	static HashMap spanTreeBlocked = new HashMap();	static HashSet cdpBoks = new HashSet();	static HashSet foundBoksBakSwp = new HashSet();	static Set downBoksid = new HashSet();	static Map vlanBoksid = new HashMap();	// For CAM-logger	static HashMap unclosedCam = new HashMap();	static HashSet safeCloseBoksid = new HashSet();	static HashSet watchMacs = new HashSet();	public static void main(String[] args) throws SQLException	{		String cf = null;		String qNetbox = null;		// Check arguments		if (args.length > 0) {			try {				NUM_THREADS = Integer.parseInt(args[0]);			} catch (NumberFormatException e) {				// Assume this argument is a netbox name				qNetbox = args[0].trim();				System.out.println("Overriding netbox: " + qNetbox);				// Is next arg number of threads?				if (args.length > 1) {					try {						NUM_THREADS = Integer.parseInt(args[1]);					} catch (NumberFormatException ee) {						// Assume this argument is the name of the config file						File f = new File(args[0]);						if (f.exists() && !f.isDirectory()) {							cf = f.getAbsolutePath();							System.out.println("Overriding configfile: " + f.getAbsolutePath());						}						// Is next arg number of threads?						if (args.length > 2) {							try {								NUM_THREADS = Integer.parseInt(args[2]);								System.out.println("Overriding number of threads: " + NUM_THREADS);							} catch (NumberFormatException eee) {								System.out.println("Error, unrecognized argument: " + args[2]);								return;							}						}					}				}			}			if (NUM_THREADS > 128) {				System.out.println("Error, more than 128 threads not recommended, re-compile needed.");				return;			}		}		// Init logger		Log.init(logFile, "getBoksData");		Log.i("INIT", "============ getBoksData starting ============");		Log.i("INIT", "Running with " + NUM_THREADS + " thread"+(NUM_THREADS>1?"s":"")+".");		ConfigParser cp, dbCp;		try {			if (cf == null) cf = configFile;			cp = new ConfigParser(cf);		} catch (IOException e) {			Log.e("INIT", "Could not read config file: " + cf);			return;		}		try {			dbCp = new ConfigParser(dbConfigFile);		} catch (IOException e) {			Log.e("INIT", "Could not read 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)))) {			Log.e("INIT", "Could not connect to database!");			return;		}		// Set MAX_MISSCNT		int MAX_MISSCNT = 3;		if (cp != null) {			String s = cp.get("MaxMisscnt");			if (s != null) {				try {					MAX_MISSCNT = Integer.parseInt(s);				} catch (NumberFormatException e) {					errl("Warning, MaxMisscnt must be a number: " + s);				}			}		}		// Load watchMacs		try {			int wmcnt=0;			BufferedReader bf = new BufferedReader(new FileReader(watchMacsFile));			String s;			while ( (s=bf.readLine()) != null) {				s = s.trim();				if (s.length() != 12 || s.startsWith("#")) continue;				watchMacs.add(s);				wmcnt++;			}			outl("watchMacs read: " + wmcnt);		} catch (IOException e) {			outl("Could not read watchMacs.conf");		}		long dumpBeginTime,dumpUsedTime;		outl("Dumping data from tables...");		// Hent kobling mellom mac<->boksid og mac<->sysName		//ResultSet rs = Database.query("SELECT mac,boksid,sysName FROM boksmac NATURAL JOIN boks");		out("  netboxmac...");		dumpBeginTime = System.currentTimeMillis();		ResultSet rs = Database.query("SELECT netboxid,REPLACE(mac::text, ':', '') AS mac FROM netboxmac");		while (rs.next()) {			macBoksId.put(rs.getString("mac"), rs.getString("netboxid"));		}		dumpUsedTime = System.currentTimeMillis() - dumpBeginTime;		outl(dumpUsedTime + " ms.");		// Hent kobling mellom boksid<->sysName og motsatt		out("  boks...");		dumpBeginTime = System.currentTimeMillis();		rs = Database.query("SELECT netboxid,sysName,catid,typename FROM netbox LEFT JOIN type USING(typeid)");		while (rs.next()) {			boksIdName.put(rs.getString("netboxid"), rs.getString("sysname"));			boksidKat.put(rs.getString("netboxid"), rs.getString("catid").toUpperCase());			boksidType.put(rs.getString("netboxid"), rs.getString("typename"));			String sysname = rs.getString("sysname");			sysnameMap.put(sysname, rs.getString("netboxid"));			// Ta med denne også for sikkerhets skyld			String kat = rs.getString("catid").toLowerCase();			if (isNetel(kat)) {				// Stripp etter første '.'				int i;				if (sysname != null && (i=sysname.indexOf('.')) != -1) {					sysname = sysname.substring(0, i);					sysnameMap.put(sysname, rs.getString("netboxid"));				}			}		}		// Alle (HP) stacknames		rs = Database.query("SELECT netboxid,val AS stackname FROM netboxinfo WHERE var='stackName'");		while (rs.next()) {			sysnameMap.put(rs.getString("stackname"), rs.getString("netboxid"));		}		// Og så alle "ekte" sysname		rs = Database.query("SELECT netboxid,val AS sysname FROM netboxinfo WHERE var='sysname'");		while (rs.next()) {			sysnameMap.put(rs.getString("sysname"), rs.getString("netboxid"));		}		rs = Database.query("SELECT netboxid FROM netbox WHERE up!='y'");		while (rs.next()) {			downBoksid.add(rs.getString("netboxid"));		}		// Hent alle bokser der kat='GW'		QueryBoks.boksGwSet = new HashSet();		rs = Database.query("SELECT netboxid FROM netbox WHERE catid='GW'");		while (rs.next()) {			QueryBoks.boksGwSet.add(rs.getString("netboxid"));		}		// Hent alle bokser med cdp		rs = Database.query("SELECT netboxid FROM netbox JOIN type USING(typeid) WHERE cdp='t'");		while (rs.next()) {			cdpBoks.add(rs.getString("netboxid"));		}		dumpUsedTime = System.currentTimeMillis() - dumpBeginTime;		outl(dumpUsedTime + " ms.");		// Mapping fra boksid, port og modul til swportid i swport		out("  swport...");		dumpBeginTime = System.currentTimeMillis();		QueryBoks.swportidMap = new HashMap();		QueryBoks.swportNetboxSet = new HashSet();		rs = Database.query("SELECT swportid,netboxid,ifindex FROM swport JOIN module USING(moduleid)");		while (rs.next()) {			String key = rs.getString("netboxid")+":"+rs.getString("ifindex");			QueryBoks.swportidMap.put(key, rs.getString("swportid"));			QueryBoks.swportNetboxSet.add(rs.getString("netboxid"));		}		dumpUsedTime = System.currentTimeMillis() - dumpBeginTime;		outl(dumpUsedTime + " ms.");		// Hent alle vlan som er blokkert av spanning-tree		out("  swportblocked...");		dumpBeginTime = System.currentTimeMillis();		rs = Database.query("SELECT swportid,netboxid,ifindex,cs_at_vlan,swportblocked.vlan FROM swportblocked JOIN swport USING(swportid) JOIN module USING(moduleid) JOIN netbox USING(netboxid) JOIN type USING(typeid)");		while (rs.next()) {			String vlan = (rs.getBoolean("cs_at_vlan") ? rs.getString("vlan") : "");			String key = rs.getString("netboxid")+":"+vlan;			HashMap blockedIfind;			if ( (blockedIfind=(HashMap)spanTreeBlocked.get(key)) == null) spanTreeBlocked.put(key, blockedIfind = new HashMap());			blockedIfind.put(rs.getString("ifindex"), rs.getString("swportid"));		}		dumpUsedTime = System.currentTimeMillis() - dumpBeginTime;		outl(dumpUsedTime + " ms.");		// Hent alle aktive vlan		out("  vlan...");		dumpBeginTime = System.currentTimeMillis();		// Get VLANs from netbox_vtpvlan and swportvlan, or fall back to using swportallowedvlan		{			rs = Database.query("SELECT netboxid,vtpvlan FROM netbox_vtpvlan");			while (rs.next()) {				Set s;				String boksid = rs.getString("netboxid");				if ( (s=(Set)vlanBoksid.get(boksid)) == null) vlanBoksid.put(boksid, s = new TreeSet());				s.add(new Integer(rs.getInt("vtpvlan")));			}			Set vtpBoksid = vlanBoksid.keySet();			rs = Database.query("SELECT DISTINCT netboxid,vlan.vlan FROM module JOIN swport USING(moduleid) JOIN swportvlan USING(swportid) JOIN vlan USING(vlanid)");			while (rs.next()) {				Set s;				String boksid = rs.getString("netboxid");				if ( (s=(Set)vlanBoksid.get(boksid)) == null) vlanBoksid.put(boksid, s = new TreeSet());				s.add(new Integer(rs.getInt("vlan")));			}						rs = Database.query("SELECT DISTINCT vlan FROM vlan WHERE vlan IS NOT NULL");			List tmp = new ArrayList();			while (rs.next()) tmp.add(new Integer(rs.getInt("vlan")));			int[] vlanList = new int[tmp.size()];			{				int i=0;				for (Iterator it=tmp.iterator(); it.hasNext(); i++) vlanList[i] = ((Integer)it.next()).intValue();			}			rs = Database.query("SELECT netboxid,hexstring FROM swport JOIN module USING(moduleid) JOIN swportallowedvlan USING (swportid)");			while (rs.next()) {				String boksid = rs.getString("netboxid");				if (!vtpBoksid.contains(boksid)) {					Set s;					String hexstring = rs.getString("hexstring");					if (hexstring == null || hexstring.length() == 0) continue;					if ( (s=(Set)vlanBoksid.get(boksid)) == null) vlanBoksid.put(boksid, s = new TreeSet());					for (int i=0; i < vlanList.length; i++) {						if (isAllowedVlan(hexstring, vlanList[i])) {							s.add(new Integer(vlanList[i]));						}					}				}			}		}		rs = Database.query("SELECT DISTINCT netboxid,vlan FROM swport JOIN module USING(moduleid) WHERE trunk='f' AND vlan IS NOT NULL");		while (rs.next()) {			Set s;			String boksid = rs.getString("netboxid");			if ( (s=(Set)vlanBoksid.get(boksid)) == null) vlanBoksid.put(boksid, s = new TreeSet());			s.add(new Integer(rs.getInt("vlan")));		}		dumpUsedTime = System.currentTimeMillis() - dumpBeginTime;		outl(dumpUsedTime + " ms.");		// Alt fra swp_boks for duplikatsjekking		out("  swp_boks...");		dumpBeginTime = System.currentTimeMillis();		HashSet swp = new HashSet();		HashMap swp_d = new HashMap();		rs = Database.query("SELECT swp_netboxid,netboxid,ifindex,to_netboxid,to_swportid,misscnt FROM swp_netbox");		ResultSetMetaData rsmd = rs.getMetaData();		//rs = Database.query("SELECT swp_boksid,boksid,modul,port,boksbak FROM swp_boks JOIN boks USING (boksid) WHERE sysName='sb-sw'");		while (rs.next()) {			String key = rs.getString("netboxid")+":"+rs.getString("ifindex")+":"+rs.getString("to_netboxid");			swp.add(key);			HashMap hm = getHashFromResultSet(rs, rsmd, false);			swp_d.put(key, hm);			// Vi trenger å vite om det befinner seg en GW|SW|KANT bak en gitt enhet			String boksBakKat = (String)boksidKat.get(rs.getString("to_netboxid"));			if (boksBakKat == null || isNetel(boksBakKat)) {				foundBoksBakSwp.add(rs.getString("netboxid")+":"+rs.getString("ifindex"));			}		}		dumpUsedTime = System.currentTimeMillis() - dumpBeginTime;		outl(dumpUsedTime + " ms.");		// Fetch OID db		out("  OID db...");		dumpBeginTime = System.currentTimeMillis();		QueryBoks.oidDb = new HashMap();		Map oidDb = QueryBoks.oidDb;		rs = Database.query("SELECT netboxid,oidkey,snmpoid FROM netbox JOIN netboxsnmpoid USING(netboxid) JOIN snmpoid USING(snmpoidid)");		while (rs.next()) {			Map m;			String nid = rs.getString("netboxid");			if ( (m=(Map)oidDb.get(nid)) == null) oidDb.put(nid, m = new HashMap());			m.put(rs.getString("oidkey"), rs.getString("snmpoid"));		}		dumpUsedTime = System.currentTimeMillis() - dumpBeginTime;		outl(dumpUsedTime + " ms.");		// netboxid+ifindex -> vlan		QueryBoks.vlanMap = new HashMap();		Map vlanMap = QueryBoks.vlanMap;		rs = Database.query("SELECT netboxid,ifindex,vlan FROM swport JOIN module USING(moduleid) WHERE trunk='f' AND vlan IS NOT NULL");		while (rs.next()) {

⌨️ 快捷键说明

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