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

📄 queryboks.java

📁 监控大型网络的软件。能够自动发现拓扑结构
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/* * QueryBoks *  * $LastChangedRevision: 3934 $ * * $LastChangedDate: 2007-03-14 11:12:34 +0100 (ons, 14 mar 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.logger.*;import no.ntnu.nav.util.*;import no.ntnu.nav.netboxinfo.*;import no.ntnu.nav.Database.*;import no.ntnu.nav.SimpleSnmp.*;/** * Actual data collection via SNMP. * * @version $LastChangedRevision: 3934 $ $LastChangedDate: 2007-03-14 11:12:34 +0100 (ons, 14 mar 2007) $ * @author Kristian Eide &lt;kreide@online.no&gt; */public class QueryBoks extends Thread{	public static boolean DB_COMMIT = false;	// Felles datastrukturer som bare skal leses fra	public static HashMap macBoksId;	public static HashMap boksIdName;	public static HashMap boksidKat;	public static HashMap boksidType;	public static HashMap sysnameMap;	public static Set downBoksid;	public static HashMap spanTreeBlocked;	public static HashSet cdpBoks;	// Inneholder alle boksid'er som er av kat=GW	public static HashSet boksGwSet;	// Mapping fra boksid+ifindex swportid i swport	public static HashMap swportidMap;	public static Set swportNetboxSet;	// Denne inneholder alle "boksid:ifindex" fra swport som er trunk-porter	//public static HashSet boksIfindexTrunkSet;	// Mengde av vlan som må sjekkes på Cisco-boksene	public static Map vlanBoksid;	// Hvilke porter det står en GW|SW|EDGE bak, som gitt i swp_boks-tabellen	static HashSet foundBoksBakSwp;	public static void setFoundBoksBakSwp(HashSet hs) { foundBoksBakSwp = hs; }	// OID db	public static Map oidDb;	public static Map vlanMap;	public static Map interfaceMap;	public static Map mpMap;	// For CAM-loggeren	public static HashMap unclosedCam;	public static HashSet safeCloseBoksid;	public static HashSet watchMacs;	// Per-tråd variabler for CAM	private List camInsertQueue = new ArrayList();	private List camResetQueue = new ArrayList();	private Set dupeMacSet = new HashSet(); // For å unngå at duplikate rapporteringer gir duplikater i tabellen	private int camNewCnt;	// Hvor mange records i swp_boks som er resatt	private static int swpResetMisscnt = 0;	synchronized private static void swpIncResetMisscnt() {		swpResetMisscnt++;	}	public static int getSwpResetMisscnt() { return swpResetMisscnt; }	// Hvor mange records i cam som er resatt	private static int camResetMisscnt = 0;	synchronized private static void camIncResetMisscnt() {		camResetMisscnt++;	}	public static int getCamResetMisscnt() { return camResetMisscnt; }	// Køen som inneholder alle boksene, delt mellom trådene	Stack bdStack;	// Hvilke tråder som er ferdig	static boolean[] threadDone;	// Rapport når en boks er ferdigbehandlet	static ArrayList boksReport = new ArrayList();	// Objekt-spesifikke data	int num;	String id;	int antBd;	HashSet swp;	HashMap swp_d;	HashSet foundCDPMp = new HashSet();	Map oidkeys;	SimpleSnmp sSnmp;	static long lastActivity = System.currentTimeMillis();	// Konstruktør	public QueryBoks(int num, String id, Stack bdStack, int antBd, HashSet swp, HashMap swp_d)	{		this.num = num;		this.id = id;		this.bdStack = bdStack;		this.antBd = antBd;		this.swp = swp;		this.swp_d = swp_d;		Log.setDefaultSubsystem("QUERYBOKS");		this.setName("QueryBoks-" + id);	}	public static void initThreadDone(final int NUM_THREADS)	{		threadDone = new boolean[NUM_THREADS];		for (int i=0; i < threadDone.length; i++) {			threadDone[i] = false;		}	}	private String getOid(String oidkey) {		if (!oidkeys.containsKey(oidkey)) {			Log.d("GET_OID", "This device does not support this oidkey: " + oidkey);			return null;		}		return (String)oidkeys.get(oidkey);	}	public void run()	{		Log.setDefaultSubsystem("QUERY_NETBOX");		Log.setThreadId(id);		long beginTime = System.currentTimeMillis();		while (true) {			BoksData bd;			int bdRemaining;			synchronized (bdStack) {				if (!bdStack.empty()) {					bd = (BoksData)bdStack.pop();					bdRemaining = bdStack.size();				} else {					// Stack er tom, altså er vi ferdig					break;				}			}			Log.setNetbox(bd.sysName);			String ip = bd.ip;			String cs_ro = bd.cs_ro;			String boksId = bd.boksId;			String boksType = bd.boksType;			String sysName = bd.sysName;			String kat = bd.kat;			String vendor = bd.vendor;			boolean csAtVlan = bd.csAtVlan;			boolean cdp = bd.cdp;			oidkeys = (Map)oidDb.get(boksId);			if (oidkeys == null) {				Log.d("RUN", "Missing OID keys for netbox: " + boksId + ", skipping " + sysName);				continue;			}			sSnmp = SimpleSnmp.simpleSnmpFactory(bd.vendor, bd.boksType);			sSnmp.setHost(ip);			sSnmp.setCs_ro(cs_ro);			Log.d("RUN", "Now working with("+boksId+"): " + sysName + " ("+ boksType +") ("+ ip +") ("+ bdRemaining +" of "+ antBd+" left)");			long boksBeginTime = System.currentTimeMillis();			// Liste over alle innslagene vi evt. skal sette inn i swp_boks			List netboxList = new ArrayList();			// Liste over porter der vi har funnet boks via CDP			foundCDPMp.clear();			// OK, prøv å spørre			try {				if (cdp) {					List l = processCDP(boksId);					netboxList.addAll(l);				}								if (kat.equalsIgnoreCase("GW")) {					// GW'er behandles annerledes, vi skal oppdatere boksbak og evt. swportbak i gwport					for (Iterator netboxIt = netboxList.iterator(); netboxIt.hasNext();) {						PortBoks pm = (PortBoks)netboxIt.next();						String remoteIf = pm.getRemoteIf();												if (boksGwSet.contains(pm.getToNetboxid())) continue;												String to_swportid = (String)interfaceMap.get(pm.getToNetboxid()+":"+remoteIf);						if (pm.getRemoteIf() != null && to_swportid == null) {							if (swportNetboxSet.contains(pm.getToNetboxid())) {								Log.i("RUN", "Cannot find swport: ("+pm.getToNetboxid()+") "+boksIdName.get(pm.getToNetboxid())+" If: " + pm.getRemoteIf() + " (" + boksId + ")");							} else {								Log.i("RUN", "Link, but no swports, for: ("+pm.getToNetboxid()+") "+boksIdName.get(pm.getToNetboxid())+" If: " + pm.getRemoteIf() + " (" + boksId + ")");							}							continue;						}						// OK, da er vi klar, oppdater gwport!						if (boksType.equals("cat6kMsfc") || // MSFC							boksType.equals("cat6kMsfc2") || // MSFC1							boksType.equals("cisWSX5302") || boksType.equals("Cis-WX5302") ) { // RSM														Database.update("UPDATE gwport SET to_netboxid = '"+pm.getToNetboxid()+"', to_swportid = "+to_swportid+" WHERE gwportid IN (SELECT gwportid FROM module JOIN gwport USING(moduleid) JOIN gwportprefix USING(gwportid) WHERE netboxid = '"+boksId+"')");							if (DB_COMMIT) Database.commit(); else Database.rollback();							Log.d("RUN", "["+boksType+"] Ifindex: " + pm.getIfindex() + " Interface: " + remoteIf + ", " + boksIdName.get(pm.getToNetboxid()) );							continue;						}											Database.update("UPDATE gwport SET to_netboxid = '"+pm.getToNetboxid()+"', to_swportid = "+to_swportid+" WHERE moduleid IN (SELECT moduleid FROM module WHERE netboxid = '"+boksId+"') AND ifindex='" + pm.getIfindex() + "'");						if (DB_COMMIT) Database.commit(); else Database.rollback();												Log.d("RUN", "[GW] Ifindex: " + pm.getIfindex() + " Interface: " + remoteIf + ", " + boksIdName.get(pm.getToNetboxid()) );					}									long boksUsedTime = System.currentTimeMillis() - boksBeginTime;					synchronized (boksReport) {						boksReport.add(new BoksReport((int)boksUsedTime, bd));					}					continue;				}								// Hent inn boksbak via matching mot MAC-adresser				List macList = new ArrayList();				// tag								macList.addAll(processMacEntry(boksId, ip, cs_ro, boksType, csAtVlan));								/*					if (boksTypegruppe.equals("cat1900-sw")) {					macListe = processCisco1900(boksId, ip, cs_ro, boksType, ifindexMp);					} else					if (boksTypegruppe.equals("catmeny-sw")) {					macListe = processCisco1Q(boksId, ip, cs_ro, boksType);					} else					if (boksTypegruppe.equals("cat-sw") || boksTypegruppe.equals("ios-sw")) {					// Cisco utstyr der man må hente per vlan					macListe = processCisco2Q(boksId, ip, cs_ro, boksTypegruppe, boksType, ifindexMp);				} else				if (boksTypegruppe.equals("3hub") || boksTypegruppe.equals("3ss") || boksTypegruppe.equals("3ss9300")) {				// Alt 3Com utstyr				macListe = process3Com(boksId, ip, cs_ro, boksTypegruppe, boksType);				} else				if (boksTypegruppe.equals("hpsw")) {				// Alt HP utstyr				macListe = processHP(boksId, ip, cs_ro, boksTypegruppe, boksType);				} else {					outl("  Error, unknown typegruppe: " + boksTypegruppe);					}				*/								// Før MAC-listen kan legges til boksListe må alle konflikter med CDP tas bort				for (Iterator macIt = macList.iterator(); macIt.hasNext();) {					PortBoks pm = (PortBoks)macIt.next();					String ifindex = pm.getIfindex();					if (foundCDPMp.contains(ifindex)) {						// Vi har funnet CDP på denne porten, støtter denne også CDP tar vi den bort						if (cdpBoks.contains(pm.getToNetboxid())) {							Log.d("RUN", "[CDP-DEL] ifindex: " + ifindex + ", " + boksIdName.get(pm.getToNetboxid()) );							continue;						}					}					netboxList.add(pm);				}			} catch (SQLException se) {				Log.d("RUN", "SQLException: " + se.getMessage());				System.err.println("SQLException: " + se.getMessage());				if (se.getMessage() != null && se.getMessage().indexOf("Exception: java.net.SocketException") != -1) {					// Mistet kontakten med serveren, abort					Log.d("RUN", "Lost contact with backend, fatal error!");					System.err.println("QueryBoks.run(): Exiting...");					System.exit(2);				}				se.printStackTrace(System.err);			} catch (TimeoutException te) {				Log.d("RUN", "*** GIVING UP ON: " + sysName + ", typename: " + boksType + " ***");				continue;			} catch (NullPointerException exp) {				Log.w("RUN", "NullPointerException, aborting thread. Exception: " + exp.getMessage());				exp.printStackTrace(System.err);			} catch (Exception exp) {				Log.e("RUN", "Fatal error, aborting. Exception: " + exp.getMessage());				exp.printStackTrace(System.err);				System.exit(1);			} finally {				sSnmp.destroy();				sSnmp = null;			}			Collections.sort(netboxList);			int newCnt=0,dupCnt=0;			List printList = new ArrayList();			for (Iterator netboxIt = netboxList.iterator(); netboxIt.hasNext();) {				PortBoks pm = (PortBoks)netboxIt.next();				String key = boksId+":"+pm;				String new_to_swportid = (String)interfaceMap.get(pm.getToNetboxid()+":"+pm.getRemoteIf());				// En enhet kan ikke ha link til seg selv				if (boksId.equals(pm.getToNetboxid())) continue;				// Dersom boksen bak er nede skal vi ikke endre				if (downBoksid.contains(pm.getToNetboxid())) continue;				if (pm.getRemoteIf() != null && new_to_swportid == null) {					if (swportNetboxSet.contains(pm.getToNetboxid())) {						Log.i("RUN", "Cannot find swport: ("+pm.getToNetboxid()+") "+boksIdName.get(pm.getToNetboxid())+" If: " + pm.getRemoteIf() + " (" + boksId + ")");					} else {						Log.i("RUN", "Link, but no swports, for: ("+pm.getToNetboxid()+") "+boksIdName.get(pm.getToNetboxid())+" If: " + pm.getRemoteIf() + " (" + boksId + ")");					}				}				// Sjekk om dette er en duplikat				if (swp.contains(key)) {					String swp_boksid = null, to_swportid = null;					int misscnt=0;					synchronized (swp_d) {						if (swp_d.containsKey(key)) {							HashMap hm = (HashMap)swp_d.remove(key);							swp_boksid = (String)hm.get("swp_netboxid");							misscnt = Integer.parseInt((String)hm.get("misscnt"));							to_swportid = (String)hm.get("to_swportid");						} else {							// Dup							continue;						}					}					if (swp_boksid != null && misscnt > 0) {						// Nå må vi også resette misscnt i recorden i swp_boks						try {							String[] updateFields = {								"misscnt", "0"							};							String[] condFields = {								"swp_netboxid", swp_boksid							};							Database.update("swp_netbox", updateFields, condFields);							if (DB_COMMIT) Database.commit(); else Database.rollback();						} catch (SQLException e) {							Log.d("RUN", "Reseting swp_netboxid: " + swp_boksid + " in swp_netbox, SQLException: " + e.getMessage() );						}						swpIncResetMisscnt();					}					if (new_to_swportid != null) {						// Nå må vi sjekke om ifindex feltet har endret seg						if (!new_to_swportid.equals(to_swportid)) {							try {								if (swp_boksid == null || swp_boksid.length() == 0) {									System.err.println("swp_boksid null for " + key + " swp: " + swp.contains(key) + " swp_d: " + swp_d.get(key));								}								String[] upd = {									"to_swportid", new_to_swportid								};								String[] where = {									"swp_netboxid", swp_boksid								};								Database.update("swp_netbox", upd, where);								if (DB_COMMIT) Database.commit(); else Database.rollback();							} catch (SQLException e) {								System.err.println("Update modulbak/portbak in swp_boks, swp_boksid: " + swp_boksid + ", to_ifindex: " + pm.getRemoteIf() + "\n  SQLException: " + e.getMessage() );								e.printStackTrace(System.err);							}						}					}					//String s = "T"+id+":    [DUP] Modul: " + pm.getModulS() + " Port: " + pm.getPortS() + ", " + getBoksMacs.boksIdName.get(pm.getBoksId());					//printList.add(s);					dupCnt++;					continue;				}				// Legg til i listen så vi ikke får duplikater				synchronized (swp) {					swp.add(key);				}				//outl("T"+id+":    ["+pm.getSource()+"] Modul: " + pm.getModulS() + " Port: " + pm.getPortS() + ", " + getBoksMacs.boksIdName.get(pm.getBoksId()) );				boolean verify1=true, verify2=true;				if ((verify1=verifyNetboxid(boksId)) && (verify2=verifyNetboxid(pm.getToNetboxid()))) {					String[] ins = {						"netboxid", boksId,						"ifindex", pm.getIfindex(),						"to_netboxid", pm.getToNetboxid(),						"to_swportid", new_to_swportid					};										try {						Database.insert("swp_netbox", ins);						if (DB_COMMIT) Database.commit(); else Database.rollback();						newCnt++;					} catch (SQLException e) {						Log.d("RUN", "Insert into swp_netbox ("+key+"), SQLException: " + e.getMessage() );						e.printStackTrace(System.err);					}				} else {					if (!verify1) Log.d("VERIFY_NETBOXID", "Verify netboxid ("+boksId+") failed");					if (!verify2) Log.d("VERIFY_NETBOXID", "Verify to netboxid ("+pm.getToNetboxid()+") failed");

⌨️ 快捷键说明

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