📄 handlernettinfo.java
字号:
/* * $Id: HandlerNettinfo.java 3763 2006-11-28 10:36:33Z 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) mg.netboxid,sysname,ip,catid,roomid,sw_ver,serial,gwport.ifindex,gwport.interface,vlan.vlan,netaddr,prefix.prefixid,nettype,netident,gwport.to_netboxid,gwport.to_swportid,mg.module AS to_module,port AS to_port FROM gwport JOIN module AS mg USING(moduleid) JOIN netbox USING(netboxid) JOIN device ON (netbox.deviceid=device.deviceid) LEFT JOIN gwportprefix USING(gwportid) LEFT JOIN prefix ON (gwportprefix.prefixid=prefix.prefixid) LEFT JOIN vlan USING(vlanid) LEFT JOIN swport ON (gwport.to_swportid=swportid) LEFT JOIN module AS ms ON (ms.moduleid=swport.moduleid) 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("to_module", rs.getString("to_module")); hm.put("to_port", rs.getString("to_port")); 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,module.netboxid,netbox.sysname,netbox.ip,netbox.roomid AS room,sw_ver AS software,serial,netbox2.sysname AS to_sysname,module.module,a.port,a.ifindex,vlan.vlan,direction,module.up,a.speed,a.duplex,a.media,module.up,a.trunk,a.portname,a.to_netboxid,module2.module AS to_module,b.port AS to_port FROM swport AS a JOIN module USING (moduleid) JOIN netbox USING (netboxid) JOIN device ON (netbox.deviceid=device.deviceid) JOIN swportvlan USING (swportid) JOIN vlan USING(vlanid) LEFT JOIN swport AS b ON (a.to_swportid = b.swportid) LEFT JOIN module AS module2 ON (b.moduleid = module2.moduleid) 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,active AS up,handler AS portname,version,vlan FROM service JOIN netbox USING (netboxid) JOIN prefix USING (prefixid) JOIN vlan USING(vlanid)"); 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,ifindex,arp.ip,mac AS portname,cam.start_time,cam.end_time,vlan FROM cam JOIN netbox USING(netboxid) JOIN arp USING(mac) JOIN prefix ON(arp.prefixid=prefix.prefixid) JOIN vlan USING(vlanid) WHERE cam.end_time='infinity' and arp.end_time='infinity' AND vlan IS NOT NULL"); 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()); sysnameMap.put(boksid, sysname); katMap.put(boksid, rs.getString("catid")); } topologiMap = new HashMap[] { portMap, rootMap, sysnameMap, katMap }; cachedTopologi = new Object[] { new Long(System.currentTimeMillis()), topologiMap }; com.getContext().setAttribute(TOPOLOGI_MAP, cachedTopologi); } topologiMap = (HashMap[])cachedTopologi[1]; return topologiMap; } private static final int SORT_SYSNAME = 0; private static final int SORT_IFINDEX = 10; private static final int SORT_PORT = 20; private Collection sort(Collection c, final int sortOn) { List l = new ArrayList(c); Collections.sort(l, new Comparator() { public int compare(Object o1, Object o2) { if (!(o1 instanceof HashMap) || !(o2 instanceof HashMap)) return 0; HashMap m1 = (HashMap)o1; HashMap m2 = (HashMap)o2; switch (sortOn) { case SORT_SYSNAME: { String s1 = (String)sysnameMap.get(m1.containsKey("to_netboxid") ? m1.get("to_netboxid") : ""); String s2 = (String)sysnameMap.get(m2.containsKey("to_netboxid") ? m2.get("to_netboxid") : ""); return s1.compareTo(s2); } case SORT_IFINDEX: { String if1 = (String)m1.get("ifindex"); String if2 = (String)m2.get("ifindex"); Integer i1 = new Integer(if1 != null && !"null".equals(if1) ? if1 : "0"); Integer i2 = new Integer(if2 != null && !"null".equals(if2) ? if2 : "0"); int cmp = i1.compareTo(i2); if (cmp != 0) return cmp; } case SORT_PORT: { String p1 = (String)m1.get("port"); String p2 = (String)m2.get("port"); try { Integer i1 = new Integer(p1 != null && !"null".equals(p1) ? p1 : "0"); Integer i2 = new Integer(p2 != null && !"null".equals(p2) ? p2 : "0"); return i1.compareTo(i2); } catch (NumberFormatException exp) { return p1 == null ? 0 : p1.compareTo(p2); } } } return 0; } }); return l; } private boolean showdetails = false; /* [/ni.visTopologi] *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -