📄 querynetbox.java
字号:
scheduleUpdateNetboxes(0); } Log.d("RUN", "Thread idle, done OID object processing ("+oidQ.size()+" remain), exiting..."); return; } } oidUpdObj = null; } nb.setNeedRestart(false); // Process netbox String netboxid = nb.getNetboxidS(); String ip = nb.getIp(); String cs_ro = nb.getCommunityRo(); String vendor = nb.getTypeT().getVendor(); String type = nb.getType(); String sysName = nb.getSysname(); String cat = nb.getCat(); int snmpVersion = nb.getSnmpVersion(); Log.setNetbox(sysName); sSnmp = SimpleSnmp.simpleSnmpFactory(vendor, type); //System.err.println("Created sSnmp: " + sSnmp); sSnmp.setHost(ip); sSnmp.setCs_ro(cs_ro); Log.d("RUN", "Now working with("+netboxid+"): " + sysName + ", type="+type+", ip="+ip+" (device "+ nb.getNum() +" of "+ netboxHigh+")"); long boksBeginTime = System.currentTimeMillis(); try { // Get DataContainer objects from each data-plugin. DataContainersImpl containers = getDataContainers(null); // Find handlers for this boks DeviceHandler[] deviceHandler = findDeviceHandlers(nb); if (deviceHandler == null) { nb.printSchedule(); throw new NoDeviceHandlerException(" No device handlers found for netbox: " + netboxid + " (cat: " + cat + " type: " + type + ")"); } Log.setDefaultSubsystem("QUERY_NETBOX_T"+tid); String dhNames = ""; for (int dhNum=0; dhNum < deviceHandler.length; dhNum++) { String[] ss = String.valueOf(deviceHandler[dhNum].getClass()).split("\\."); dhNames += (dhNum==0?"":",")+ss[ss.length-1]; } Log.d("RUN", " Found " + deviceHandler.length + " deviceHandlers ("+dhNames+"): " + netboxid + " (cat: " + cat + " type: " + type + ")"); boolean timeout = false; for (int dhNum=0; dhNum < deviceHandler.length; dhNum++) { try { deviceHandler[dhNum].handleDevice(nb, sSnmp, myCp, containers); if (nb.needRefetch() || nb.needRestart() || nb.isRemoved()) break; } catch (TimeoutException te) { Log.setDefaultSubsystem("QUERY_NETBOX_T"+tid); Log.d("RUN", "TimeoutException: " + te.getMessage()); Log.w("RUN", "GIVING UP ON: " + sysName + ", typeid: " + type ); timeout = true; } catch (Exception exp) { Log.w("RUN", "Fatal error from devicehandler, skipping. Exception: " + exp.getMessage()); exp.printStackTrace(System.err); } catch (Throwable e) { Log.w("RUN", "Fatal error from devicehandler, plugin is probably old and needs to be updated to new API: " + e.getMessage()); e.printStackTrace(System.err); } } if (!timeout && !nb.isRemoved()) { // Call the data handlers for all data plugins try { Map changedDeviceids = containers.callDataHandlers(nb); if (!changedDeviceids.isEmpty()) getDataContainers(changedDeviceids); } catch (Exception exp) { Log.w("RUN", "Fatal error from datahandler, skipping. Exception: " + exp.getMessage()); exp.printStackTrace(System.err); } catch (Throwable e) { Log.w("RUN", "Fatal error from datahandler, plugin is probably old and needs to be updated to new API: " + e.getMessage()); e.printStackTrace(System.err); } } } catch (NoDeviceHandlerException exp) { Log.d("RUN", exp.getMessage()); } Log.setDefaultSubsystem("QUERY_NETBOX_T"+tid); if (scheduleImmediatelyMap.containsKey(nb.getNetboxidS())) { // Send event that we are done String[] ss = (String[])scheduleImmediatelyMap.remove(nb.getNetboxidS()); String target = ss[0]; String subid = ss[1]; Log.i("RUN", "Done collecting immediate data for " + target + ", netbox: " + nb); Map varMap = new HashMap(); varMap.put("command", "runNetboxDone"); try { EventQ.createAndPostEvent("getDeviceData", target, nb.getDeviceid(), nb.getNetboxid(), Integer.parseInt(subid), "notification", Event.STATE_NONE, 0, 0, varMap); } catch (Exception e) { Log.e("RUN", "Exception when posting to eventq: " + e); e.printStackTrace(System.err); } } // If we need to recreate the netbox, set the unknown type /* if (nb.needRecreate()) { Log.d("RUN", "Recreating netbox: " + nb); nb.setType((Type)typeidMap.get(Type.UNKNOWN_TYPEID)); } */ // If netbox is removed, don't add it to the RunQ if (!nb.needRefetch() && !nb.isRemoved()) { // Store last collect time in netboxinfo try { NetboxInfo.put(nb.getNetboxidS(), null, "lastUpdated", String.valueOf(System.currentTimeMillis())); } catch (RuntimeException rte) { Log.w("RUN", "Database error while updating lastUpdated for " + nb.getSysname() + ", contact NAV support."); Log.d("RUN", "Got exception while setting lastUpdated: " + rte.getMessage()); } nb.reschedule(); /* // Don't reschedule if we just set the unknown type for this netbox as we want it to run immediately if (!nb.needRecreate()) { nb.reschedule(); } */ // Insert into queue addToRunQ(nb); Log.d("RUN", "Done processing netbox " + nb + " (nextrun: " + (nb.getNextRun()-System.currentTimeMillis())+ ")"); } else { Log.d("RUN", "Done, re-fetching nb from database: " + nb); synchronized (nbMap) { nbMap.remove(nb.getNetboxidS()); } scheduleUpdateNetboxes(0); } Log.setNetbox(null); long pc = ++nbProcessedCnt; if ((pc % 100) == 0) { Log.i("RUN", "** Processed " + pc + " netboxes (" + (pc%(netboxCnt+1)) + " of " + netboxCnt + ") **"); } if (nb.needRefetch() || !nb.needRestart()) { // Try to get a new netbox to process Object o = removeRunQHead(); if (o instanceof NetboxImpl) { nb = (NetboxImpl)o; Log.d("RUN", "Got new netbox: " + nb); } else { // We didn't get a netbox; exit the thread Log.d("RUN", "No new netbox available: " + o); break; } } } } catch (Throwable e) { Log.e("RUN", "Caught exception, should not happen: " + e.getMessage()); e.printStackTrace(System.err); } finally { Log.d("RUN", "Thread idle, exiting..."); //System.err.println("Thread idle, sSnmp: " + sSnmp); if (sSnmp != null) sSnmp.destroy(); sSnmp = null; System.gc(); Log.freeThread(); threadIdle(tid); } } private DataContainersImpl getDataContainers(Map changedDeviceids) { if (changedDeviceids == null) changedDeviceids = new HashMap(); DataContainersImpl dcs = new DataContainersImpl(); try { // Iterate over all data plugins synchronized (dataClassMap) { for (Iterator it=dataClassMap.entrySet().iterator(); it.hasNext();) { Map.Entry me = (Map.Entry)it.next(); String fn = (String)me.getKey(); Class c = (Class)me.getValue();; Object o = c.newInstance(); DataHandler dh = (DataHandler)o; Map m; if ( (m = (Map)persistentStorage.get(fn)) == null) persistentStorage.put(fn, m = Collections.synchronizedMap(new HashMap())); m.put("navCp", navCp); try { dh.init(m, changedDeviceids); } catch (Throwable th) { System.err.println("File: " + fn); th.printStackTrace(); continue; } dcs.addContainer(dh.dataContainerFactory()); } } } catch (InstantiationException e) { Log.w("GET_DATA_CONTAINERS", "GET_DATA_CONTAINERS", "Unable to instantiate handler for " + nb.getNetboxid() + ", msg: " + e.getMessage()); } catch (IllegalAccessException e) { Log.w("GET_DATA_CONTAINERS", "GET_DATA_CONTAINERS", "IllegalAccessException for " + nb.getNetboxid() + ", msg: " + e.getMessage()); } return dcs; } private DeviceHandler[] findDeviceHandlers(Netbox nb) { try { synchronized (deviceNetboxCache) { Class[] c; if ( (c=(Class[])deviceNetboxCache.get(nb.getNetboxidS() )) != null) { DeviceHandler[] dh = new DeviceHandler[c.length]; for (int i=0; i < c.length; i++) dh[i] = (DeviceHandler)c[i].newInstance(); return dh; } } // Iterate over all known plugins to find the set of handlers to process this boks // Look at DeviceHandler for docs on the algorithm for selecting handlers TreeMap dbMap = new TreeMap(); List alwaysHandleList = new ArrayList(); synchronized (deviceClassMap) { int high = 0; for (Iterator it=deviceClassMap.values().iterator(); it.hasNext();) { Class c = (Class)it.next(); Object o = c.newInstance(); DeviceHandler dh = (DeviceHandler)o; int v; try { v = dh.canHandleDevice(nb); if (v == DeviceHandler.NEVER_HANDLE) continue; } catch (Exception e) { Log.w("FIND_DEVICE_HANDLERS", "FIND_DEVICE_HANDLERS", "Error from DeviceHandler " + c + ", skipping: " + e.getMessage()); continue; } catch (Throwable e) { Log.w("FIND_DEVICE_HANDLERS", "FIND_DEVICE_HANDLERS", "Fatal error from DeviceHandler " + c + ", plugin is probably old and needs to be updated to new API: " + e.getMessage()); continue; } if (v == DeviceHandler.ALWAYS_HANDLE) { alwaysHandleList.add(c); } else { if (Math.abs(v) > high) { if (v > high) high = v; dbMap.put(new Integer(Math.abs(v)), c); } } } if (!dbMap.isEmpty() || !alwaysHandleList.isEmpty()) { SortedMap dbSMap = dbMap.tailMap(new Integer(high)); Class[] c = new Class[dbSMap.size() + alwaysHandleList.size()]; int j=dbSMap.size()-1; for (Iterator i=dbSMap.values().iterator(); i.hasNext(); j--) c[j] = (Class)i.next(); j = c.length - 1; for (Iterator i=alwaysHandleList.iterator(); i.hasNext(); j--) c[j] = (Class)i.next(); synchronized (deviceNetboxCache) { deviceNetboxCache.put(nb.getNetboxidS(), c); } // Call ourselves; this avoids duplicating the code for instatiating objects from the classes return findDeviceHandlers(nb); } } } catch (InstantiationException e) { Log.w("FIND_DEVICE_HANDLERS", "FIND_DEVICE_HANDLERS", "Unable to instantiate handler for " + nb.getNetboxid() + ", msg: " + e.getMessage()); } catch (IllegalAccessException e) { Log.w("FIND_DEVICE_HANDLERS", "FIND_DEVICE_HANDLERS", "IllegalAccessException for " + nb.getNetboxid() + ", msg: " + e.getMessage()); } return null; } static class UpdateDataTask extends TimerTask { public void run() { Log.setDefaultSubsystem("UPDATE_DATA"); updateTypes(false); updateNetboxes(); } } static class CheckRunQTask extends TimerTask { public void run() { Log.setDefaultSubsystem("CHECK_RUN_Q"); checkRunQ(); } } static class EventListener implements EventQListener { public void handleEvent(Event e) { Log.setDefaultSubsystem("HANDLE_EVENT"); if (!e.getEventtypeid().equals("notification")) { Log.d("HANDLE_EVENT", "Unknown eventtypeid: " + e.getEventtypeid()); e.dispose(); return; } String cmd = e.getVar("command"); if ("dumpRunq".equals(cmd)) { // Dump runq synchronized (nbRunQ) { Log.d("HANDLE_EVENT", "Dumping runQ: " + nbRunQ.size() + " entries"); for (Iterator it=nbRunQ.entrySet().iterator(); it.hasNext();) { Map.Entry me = (Map.Entry)it.next(); long curTime = System.currentTimeMillis(); Log.d("RUNQ", (((Long)me.getKey()).longValue()-curTime) + ": " + me.getValue()); } } } else if ("dumpOidLocks".equals(cmd)) { Log.d("HANDLE_EVENT", "Active OID locks: " + OidTester.getLockSet()); } else if ("updateFromDB".equals(cmd)) { // Update types/netboxes Log.d("HANDLE_EVENT", "Updating types/netboxes"); scheduleUpdateNetboxes(0); } else if ("runNetbox".equals(cmd)) { String netboxid = String.valueOf(e.getNetboxid()); if (netboxid != null) { runNetbox(netboxid, e.getVar("maxage"), e.getSource(), String.valueOf(e.getSubid())); } } else if ("dumpOidNetbox".equals(cmd)) { String netboxid = String.valueOf(e.getNetboxid()); if (netboxid != null) { NetboxImpl nb = (NetboxImpl)nbMap.get(netboxid); if (nb != null) nb.printSchedule(); } } else { Log.d("HANDLE_EVENT", "Unknown command: " + cmd); } e.dispose(); } } private static String format(long i, int n) { DecimalFormat nf = new DecimalFormat("#"); nf.setMinimumIntegerDigits(n); return nf.format(i); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -