snmpthresholder.java
来自「opennms得相关源码 请大家看看」· Java 代码 · 共 1,059 行 · 第 1/4 页
JAVA
1,059 行
Iterator iter = ThresholdingConfigFactory.getInstance().getThresholds(groupName).iterator(); while (iter.hasNext()) { Threshold thresh = (Threshold) iter.next(); // See if map entry already exists for this datasource // If not, create a new one. boolean newEntity = false; ThresholdEntity thresholdEntity = null; if (thresh.getDsType().equals("node")) { thresholdEntity = (ThresholdEntity) nodeMap.get(thresh.getDsName()); } else if (thresh.getDsType().equals("if")) { thresholdEntity = (ThresholdEntity) baseIfMap.get(thresh.getDsName()); } // Found entry? if (thresholdEntity == null) { // Nope, create a new one newEntity = true; thresholdEntity = new ThresholdEntity(); } try { // Set high/low threshold if (thresh.getType().equals(ThresholdEntity.HIGH_THRESHOLD)) thresholdEntity.setHighThreshold(thresh); else if (thresh.getType().equals(ThresholdEntity.LOW_THRESHOLD)) thresholdEntity.setLowThreshold(thresh); } catch (IllegalStateException e) { log.warn("Encountered duplicate " + thresh.getType() + " for datasource " + thresh.getDsName(), e); } // Add new entity to the map if (newEntity) { if (thresh.getDsType().equals("node")) nodeMap.put(thresh.getDsName(), thresholdEntity); else if (thresh.getDsType().equals("if")) baseIfMap.put(thresh.getDsName(), thresholdEntity); } } } catch (IllegalArgumentException e) { throw new RuntimeException("Thresholding group '" + groupName + "' does not exist."); } // Add node and interface thresholding maps as attributes of the // interface // for retrieval by the check() method. // iface.setAttribute(NODE_THRESHOLD_MAP_KEY, nodeMap); iface.setAttribute(BASE_IF_THRESHOLD_MAP_KEY, baseIfMap); // Now create an empty map which will hold interface level // ThresholdEntity objects for each of the node's interfaces. // This map will be keyed by the interface's iflabel and will // contain as a value a map of ThresholdEntity objects keyed // by datasource name. // iface.setAttribute(ALL_IF_THRESHOLD_MAP_KEY, new HashMap()); // Get database connection in order to retrieve the nodeid and // ifIndex from the database for this interface. // java.sql.Connection dbConn = null; try { dbConn = DatabaseConnectionFactory.getInstance().getConnection(); } catch (SQLException sqlE) { if (log.isEnabledFor(Priority.ERROR)) log.error("initialize: Failed getting connection to the database.", sqlE); throw new UndeclaredThrowableException(sqlE); } int nodeId = -1; int primaryIfIndex = -1; char isSnmpPrimary = DbIpInterfaceEntry.SNMP_NOT_ELIGIBLE; // All database calls wrapped in try/finally block so we make // certain that the connection will be closed when we are // finished. // try { // Prepare & execute the SQL statement to get the 'nodeid', // 'ifIndex' and 'isSnmpPrimary' fields from the ipInterface table. // PreparedStatement stmt = null; try { stmt = dbConn.prepareStatement(SQL_GET_NODEID); stmt.setString(1, ipAddr.getHostAddress()); // interface address ResultSet rs = stmt.executeQuery(); if (rs.next()) { nodeId = rs.getInt(1); if (rs.wasNull()) nodeId = -1; primaryIfIndex = rs.getInt(2); if (rs.wasNull()) primaryIfIndex = -1; String str = rs.getString(3); if (str != null) isSnmpPrimary = str.charAt(0); } rs.close(); } catch (SQLException sqle) { if (log.isDebugEnabled()) log.debug("initialize: SQL exception!!", sqle); throw new RuntimeException("SQL exception while attempting to retrieve node id for interface " + ipAddr.getHostAddress()); } finally { try { stmt.close(); } catch (Exception e) { // Ignore } } if (log.isDebugEnabled()) log.debug("initialize: db retrieval info: nodeid = " + nodeId + ", address = " + ipAddr.getHostAddress() + ", ifIndex = " + primaryIfIndex + ", isSnmpPrimary = " + isSnmpPrimary); // RuntimeException is thrown if any of the following are true: // - node id is invalid // - primaryIfIndex is invalid // - Interface is not the primary SNMP interface for the node // if (nodeId == -1) throw new RuntimeException("Unable to retrieve node id for interface " + ipAddr.getHostAddress()); if (primaryIfIndex == -1) // allow this for nodes without ipAddrTables // throw new RuntimeException("Unable to retrieve ifIndex for interface " + ipAddr.getHostAddress()); if (log.isDebugEnabled()) log.debug("initialize: db retrieval info: node " + nodeId + " does not have a legitimate primaryIfIndex. Assume node does not supply ipAddrTable and continue..."); if (isSnmpPrimary != DbIpInterfaceEntry.SNMP_PRIMARY) throw new RuntimeException("Interface " + ipAddr.getHostAddress() + " is not the primary SNMP interface for nodeid " + nodeId); } finally { // Done with the database so close the connection try { dbConn.close(); } catch (SQLException sqle) { if (log.isEnabledFor(Priority.INFO)) log.info("initialize: SQLException while closing database connection", sqle); } } // Add nodeId as an attribute of the interface for retrieval // by the check() method. // iface.setAttribute(NODE_ID_KEY, new Integer(nodeId)); // Debug // if (log.isDebugEnabled()) { log.debug("initialize: dumping node thresholds defined for " + ipAddr.getHostAddress() + "/" + groupName + ":"); Iterator iter = nodeMap.values().iterator(); while (iter.hasNext()) { log.debug((ThresholdEntity) iter.next()); } log.debug("initialize: dumping interface thresholds defined for " + ipAddr.getHostAddress() + "/" + groupName + ":"); iter = baseIfMap.values().iterator(); while (iter.hasNext()) { log.debug((ThresholdEntity) iter.next()); } } if (log.isDebugEnabled()) log.debug("initialize: initialization completed for " + ipAddr.getHostAddress()); return; } /** * Responsible for releasing any resources associated with the specified * interface. * * @param iface * Network interface to be released. */ public void release(NetworkInterface iface) { // Nothing to release... } /** * Perform threshold checking. * * @param iface * Network interface to be data collected. * @param eproxy * Eventy proxy for sending events. * @param parameters * Key/value pairs from the package to which the interface * belongs. */ public int check(NetworkInterface iface, EventProxy eproxy, Map parameters) { Category log = ThreadCategory.getInstance(getClass()); int thresholdingStatus = THRESHOLDING_UNKNOWN; InetAddress primary = (InetAddress) iface.getAddress(); SnmpSession session = null; // Get configuration parameters // String groupName = ParameterMap.getKeyedString(parameters, "thresholding-group", "default"); int interval = ParameterMap.getKeyedInteger(parameters, "interval", DEFAULT_INTERVAL); int range = ParameterMap.getKeyedInteger(parameters, "range", DEFAULT_RANGE); if (log.isDebugEnabled()) log.debug("check: service= " + SERVICE_NAME + " address= " + primary.getHostAddress() + " thresholding-group=" + groupName + " interval=" + interval + "ms range=" + range + " mS"); // RRD Repository attribute // String repository = (String) iface.getAttribute(RRD_REPOSITORY_KEY); if (log.isDebugEnabled()) log.debug("check: rrd repository=" + repository); // Nodeid attribute // Integer nodeId = (Integer) iface.getAttribute(NODE_ID_KEY); // node and interface ThresholdEntity map attributes // Map nodeMap = (Map) iface.getAttribute(NODE_THRESHOLD_MAP_KEY); Map baseIfMap = (Map) iface.getAttribute(BASE_IF_THRESHOLD_MAP_KEY); Map allIfMap = (Map) iface.getAttribute(ALL_IF_THRESHOLD_MAP_KEY); // ----------------------------------------------------------- // // Perform node-level threshold checking // // ----------------------------------------------------------- // Get File object representing the node directory File nodeDirectory = new File(repository + File.separator + nodeId.toString()); if (!RrdFileConstants.isValidRRDNodeDir(nodeDirectory)) { log.info("Node directory for " + nodeId + "/" + primary.getHostAddress() + " does not exist or is not a valid RRD node directory."); log.info("Threshold checking failed for primary SNMP interface " + primary.getHostAddress()); return THRESHOLDING_FAILED; } // Create empty Events object to hold any threshold // events generated during the thresholding check... Events events = new Events(); // Date stamp for all outgoing events Date dateStamp = new Date(); try { checkNodeDir(nodeDirectory, nodeId, primary, interval, range, dateStamp, nodeMap, events); } catch (IllegalArgumentException e) { log.info("check: Threshold checking failed for primary SNMP interface " + primary.getHostAddress(), e); return THRESHOLDING_FAILED; } // ----------------------------------------------------------- // // Perform interface-level threshold checking // // ----------------------------------------------------------- // Iterate over node directory contents and call // checkInterfaceDirectory() for any/all RRD interface // directories. // File[] files = nodeDirectory.listFiles(RrdFileConstants.INTERFACE_DIRECTORY_FILTER); if (files != null) { for (int i = 0; i < files.length; i++) { try { // Found interface directory... checkIfDir(files[i], nodeId, primary, interval, dateStamp, baseIfMap, allIfMap, events); } catch (IllegalArgumentException e) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?