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

📄 handlernettinfo.java

📁 监控大型网络的软件。能够自动发现拓扑结构
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * $Id: HandlerNettinfo.java 4167 2007-08-22 09:35:21Z mortenv $ * * 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 *  * Author: Kristian Eide <kreide@gmail.com> */import java.io.PrintStream;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.util.ArrayList;import java.util.Collection;import java.util.Collections;import java.util.Comparator;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.Database.Database;import no.ntnu.nav.util.util;class HandlerNettinfo{	private final String gfxRoot;	public HandlerNettinfo(String[] Is, Com Icom, int InNum, int InTempNr)	{		s = Is;		com = Icom;		tempNr = InTempNr;		num = InNum;		gfxRoot = com.getReq().getContextPath() + "/gfx";	}	public String begin()	{		/************************************************************		* Level 1 handler											*		* user.*													*		************************************************************/		if (s.length >= 2)		{			try {				if (s[1].equals("visTopologi")) { visTopologi(); return null; }			} catch (SQLException e) {				com.outl("<br>\nSQLException: " + e.getMessage() + "<br>");				PrintStream ps = com.getPStream();				e.printStackTrace(ps);				com.outl("Done printing stack trace.<br>");				return null;			} catch (Exception e) {				com.outl("<br>\nException: " + e.getMessage() + "<br>");				PrintStream ps = com.getPStream();				e.printStackTrace(ps);				com.outl("Done printing stack trace.<br>");				return null;			}				/*				 * Kall opp metode med riktig navn				 *				 *				 */			try {				java.lang.reflect.Method metode = this.getClass().getDeclaredMethod(s[1], new Class[0]);				metode.invoke(this, new Object[0]);			} catch (NoSuchMethodException e) {				com.outl("NoSuchMethodException: " + e.getMessage());				com.outl("<!--Invalid tag: "+s[1]+"-->");			} catch (IllegalAccessException e) {				com.outl("IllegalAccessException: " + e.getMessage());				com.outl("<!--Inaccessible tag: "+s[1]+"-->");			} catch (java.lang.reflect.InvocationTargetException e) {				com.outl("<br>\nInvocationTargetException: " + e.getMessage() + "<br>");				PrintStream ps = com.getPStream();				e.printStackTrace(ps);				com.outl("Done printing stack trace.<br>");			}		}		return null;	}	/************************************************************	* Level 2 handler											*	* user.<>.*													*	************************************************************/	/************************************************************	* Level 1 functions											*	* user.*													*	************************************************************/	HashMap sysnameMap = null;	private synchronized HashMap[] getPortMap() throws SQLException {		final long AGE_LIMIT = 1000*60*15; // 5 mins		final String TOPOLOGI_MAP = "no.ntnu.nav.navAdmin.topologiMap";		HashMap[] topologiMap;		Object[] cachedTopologi = (Object[])com.getContext().getAttribute(TOPOLOGI_MAP);		long age=Long.MAX_VALUE;		if (cachedTopologi != null) age = System.currentTimeMillis() - ((Long)cachedTopologi[0]).longValue();		if (age > AGE_LIMIT) {			com.getContext().removeAttribute(TOPOLOGI_MAP);			System.gc();			HashMap portMap = null;			HashMap rootMap = null;			HashMap sysnameMap = null;			HashMap katMap = null;			portMap = new HashMap();			rootMap = new HashMap();			sysnameMap = new HashMap();			katMap = new HashMap();			String domainSuffix = com.getNavConf().get("DOMAIN_SUFFIX");			if (domainSuffix == null) domainSuffix = "";			// Hent gw'ene som danner røttene i treet			ResultSet rs = Database.query(					"SELECT DISTINCT ON (sysname, vlan.vlanid) \n" + 					"       mg.netboxid,\n" + 					"       sysname,\n" + 					"       ip,\n" + 					"       catid,\n" + 					"       roomid,\n" + 					"       sw_ver,\n" + 					"       serial,\n" + 					"       gwport.ifindex,\n" + 					"       gwport.interface,\n" + 					"       vlan.vlan,\n" + 					"       netaddr,\n" + 					"       prefix.prefixid,\n" + 					"       nettype,\n" + 					"       netident,\n" + 					"       gwport.to_netboxid,\n" + 					"       gwport.to_swportid,\n" + 					"       mg.module AS to_module,\n" + 					"       port AS to_port,\n" + 					"       swport.interface AS to_interface\n" + 					"FROM gwport\n" + 					"JOIN module AS mg USING (moduleid)\n" + 					"JOIN netbox USING (netboxid)\n" + 					"JOIN device ON (netbox.deviceid = device.deviceid)\n" + 					"LEFT JOIN gwportprefix USING (gwportid)\n" + 					"LEFT JOIN prefix ON (gwportprefix.prefixid = prefix.prefixid)\n" + 					"LEFT JOIN vlan USING (vlanid)\n" + 					"LEFT JOIN swport ON (gwport.to_swportid = swportid)\n" + 					"LEFT JOIN module AS ms ON (ms.moduleid = swport.moduleid)\n" + 					"ORDER BY sysname, vlan.vlanid, gwport.interface");			ResultSetMetaData rsmd = rs.getMetaData();			while (rs.next()) {				//HashMap hm = getHashFromResultSet(rs, rsmd, false);				String boksid = rs.getString("netboxid");				String boksbak = rs.getString("to_netboxid");				String kat = rs.getString("catid");				String vlan = rs.getString("vlan");				// GSW'er er både GW og SW i samme enhet				String gsw = "";				if (kat.equals("GSW")) {					boksbak = boksid;					// Need this to avoid the root being under the loopback interface					if (vlan != null && vlan.equals("0")) gsw = "r";				}				String key = boksid+":0";				List l;				if ( (l=(List)portMap.get(key)) == null) portMap.put(key, l=new ArrayList());				HashMap hm = new HashMap();				hm.put("netboxid", boksbak);				hm.put("to_netboxid", boksbak+gsw);				hm.put("vlan", rs.getString("vlan"));				hm.put("direction", "n");				hm.put("ifindex", rs.getString("ifindex"));				hm.put("port", rs.getString("interface"));				hm.put("interface", rs.getString("interface"));				hm.put("to_module", rs.getString("to_module"));				hm.put("to_port", rs.getString("to_port"));				hm.put("to_interface", rs.getString("to_interface"));				hm.put("to_catid", "gwport");				hm.put("netaddr", rs.getString("netaddr"));				hm.put("prefixid", rs.getString("prefixid"));				hm.put("nettype", rs.getString("nettype"));				hm.put("netident", rs.getString("netident"));				// For search				String sysname = rs.getString("sysname");				if (sysname.endsWith(domainSuffix)) sysname = sysname.substring(0, sysname.length()-domainSuffix.length());				hm.put("sysname", sysname);				hm.put("ip", rs.getString("ip"));				hm.put("room", rs.getString("roomid"));				hm.put("software", rs.getString("sw_ver"));				hm.put("serial", rs.getString("serial"));				l.add(hm);				if (!rootMap.containsKey(boksid)) {					hm = new HashMap();					hm.put("vlan", "0");					hm.put("direction", "n");					hm.put("netboxid", boksid);					hm.put("to_netboxid", boksid);					hm.put("catid", kat);					rootMap.put(boksid, hm);				}			}			// Hent hele swport			rs = Database.query(					"SELECT a.swportid, \n" + 					"       module.netboxid,\n" + 					"       netbox.sysname,\n" + 					"       netbox.ip,\n" + 					"       netbox.roomid AS room,\n" + 					"       sw_ver AS software, \n" + 					"       serial, \n" + 					"       netbox2.sysname AS to_sysname,\n" + 					"       module.module,\n" + 					"       a.port,\n" + 					"       a.interface,\n" + 					"       a.ifindex,\n" + 					"       vlan.vlan,\n" + 					"       direction,\n" + 					"       module.up,\n" + 					"       a.speed,\n" + 					"       a.duplex, \n" + 					"       a.media,\n" + 					"       module.up,\n" + 					"       a.trunk,\n" + 					"       a.portname,\n" + 					"       a.to_netboxid,\n" + 					"       module2.module AS to_module,\n" + 					"       b.port AS to_port,\n" + 					"       b.interface AS to_interface\n" + 					"FROM swport AS a \n" + 					"JOIN module USING (moduleid)\n" + 					"JOIN netbox USING (netboxid)\n" + 					"JOIN device ON (netbox.deviceid = device.deviceid)\n" + 					"JOIN swportvlan USING (swportid)\n" + 					"JOIN vlan USING (vlanid)\n" + 					"LEFT JOIN swport AS b ON (a.to_swportid = b.swportid)\n" + 					"LEFT JOIN module AS module2 ON (b.moduleid = module2.moduleid)\n" + 					"LEFT JOIN netbox AS netbox2 ON (a.to_netboxid = netbox2.netboxid)");						rsmd = rs.getMetaData();			while (rs.next()) {				String key = rs.getString("netboxid")+":"+rs.getString("vlan");				List l;				if ( (l=(List)portMap.get(key)) == null) portMap.put(key, l=new ArrayList());				HashMap hm = getHashFromResultSet(rs, rsmd, false);				if (rs.getString("to_netboxid") == null) {					hm.put("to_netboxid", "cam"+rs.getString("netboxid")+":"+rs.getString("ifindex"));				}				l.add(hm);			}			// Hent servicer			rs = Database.query(					"SELECT netboxid,\n" + 					"       active AS up,\n" + 					"       handler AS portname,\n" + 					"       version,\n" + 					"       vlan\n" + 					"FROM service\n" + 					"JOIN netbox USING (netboxid)\n" + 					"JOIN prefix USING (prefixid)\n" + 					"JOIN vlan USING(vlanid)\n" + 					"");			rsmd = rs.getMetaData();			while (rs.next()) {				String key = rs.getString("netboxid")+":"+rs.getString("vlan");				List l;				if ( (l=(List)portMap.get(key)) == null) portMap.put(key, l=new ArrayList());				HashMap hm = getHashFromResultSet(rs, rsmd, false);				hm.put("direction", "n");				hm.put("nodeType", "service");				l.add(hm);			}			// Hent cam			rs = Database.query(					"SELECT cam.netboxid,\n" + 					"       ifindex,\n" + 					"       arp.ip,\n" + 					"       REPLACE(mac::text, \':\', \'\') AS portname,\n" + 					"       cam.start_time,\n" + 					"       cam.end_time,\n" + 					"       vlan\n" + 					"FROM cam\n" + 					"JOIN netbox USING (netboxid)\n" + 					"JOIN arp USING (mac)\n" + 					"JOIN prefix ON (arp.prefixid = prefix.prefixid)\n" + 					"JOIN vlan USING (vlanid)\n" + 					"WHERE cam.end_time=\'infinity\'\n" + 					"  AND arp.end_time=\'infinity\'\n" + 					"  AND vlan IS NOT NULL\n" + 					""); 			rsmd = rs.getMetaData();			while (rs.next()) {				String key = "cam"+rs.getString("netboxid")+":"+rs.getString("ifindex")+":"+rs.getString("vlan");				List l;				if ( (l=(List)portMap.get(key)) == null) portMap.put(key, l=new ArrayList());				HashMap hm = getHashFromResultSet(rs, rsmd, false);				hm.put("parentKey", key);				hm.put("direction", "o");				hm.put("nodeType", "cam");				l.add(hm);			}			// Hent sysname,kat			rs = Database.query("SELECT netboxid,sysname,catid FROM netbox");			while (rs.next()) {				String boksid = rs.getString("netboxid");				String sysname = rs.getString("sysname");				if (sysname.endsWith(domainSuffix)) sysname = sysname.substring(0, sysname.length()-domainSuffix.length());

⌨️ 快捷键说明

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