snmpthresholder.java

来自「opennms得相关源码 请大家看看」· Java 代码 · 共 1,059 行 · 第 1/4 页

JAVA
1,059
字号
//// This file is part of the OpenNMS(R) Application.//// OpenNMS(R) is Copyright (C) 2002-2003 The OpenNMS Group, Inc.  All rights reserved.// OpenNMS(R) is a derivative work, containing both original code, included code and modified// code that was published under the GNU General Public License. Copyrights for modified // and included code are below.//// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.//// Modifications://// 2005 Jan 03: minor mod to support lame SNMP hosts// 2003 Jan 31: Cleaned up some unused imports.// 2002 Oct 22: Added a threshold rearm event.// 2002 Jul 08: Modified code to allow for Threshold-based event notifications.//// Original code base Copyright (C) 1999-2001 Oculan Corp.  All rights reserved.//// This program 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.//// This program 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 this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.//       // For more information contact: //      OpenNMS Licensing       <license@opennms.org>//      http://www.opennms.org///      http://www.opennms.com///package org.opennms.netmgt.threshd;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.lang.NullPointerException;import java.lang.reflect.UndeclaredThrowableException;import java.net.InetAddress;import java.net.UnknownHostException;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Properties;import org.apache.log4j.Category;import org.apache.log4j.Priority;import org.opennms.core.utils.ThreadCategory;import org.opennms.netmgt.EventConstants;import org.opennms.netmgt.capsd.DbIpInterfaceEntry;import org.opennms.netmgt.config.DatabaseConnectionFactory;import org.opennms.netmgt.config.ThresholdingConfigFactory;import org.opennms.netmgt.config.threshd.Threshold;import org.opennms.netmgt.poller.monitors.NetworkInterface;import org.opennms.netmgt.rrd.RrdException;import org.opennms.netmgt.rrd.RrdUtils;import org.opennms.netmgt.utils.EventProxy;import org.opennms.netmgt.utils.EventProxyException;import org.opennms.netmgt.utils.IfLabel;import org.opennms.netmgt.utils.ParameterMap;import org.opennms.netmgt.utils.RrdFileConstants;import org.opennms.netmgt.xml.event.Event;import org.opennms.netmgt.xml.event.Events;import org.opennms.netmgt.xml.event.Log;import org.opennms.netmgt.xml.event.Parm;import org.opennms.netmgt.xml.event.Parms;import org.opennms.netmgt.xml.event.Value;import org.opennms.protocols.snmp.SnmpSession;/** * <P> * The SnmpThresholder class ... * </P> *  * @author <A HREF="mailto:mike@opennms.org">Mike Davidson </A> * @author <A HREF="http://www.opennms.org/">OpenNMS </A> *  */final class SnmpThresholder implements ServiceThresholder {    /**     * SQL statement to retrieve interface's 'ipinterface' table information.     */    private static final String SQL_GET_NODEID = "SELECT nodeid,ifindex,issnmpprimary FROM ipinterface WHERE ipAddr=? AND ismanaged!='D'";    /**     * Name of monitored service.     */    private static final String SERVICE_NAME = "SNMP";    /**     * Default thresholding interval (in milliseconds).     *      */    private static final int DEFAULT_INTERVAL = 300000; // 300s or 5m        /**     * Default age before which a data point is considered "out of date"     */        private static final int DEFAULT_RANGE = 0; // 300s or 5m        /**     * Interface attribute key used to store the interface's node id     */    static final String NODE_ID_KEY = "org.opennms.netmgt.collectd.SnmpThresholder.NodeId";    /**     * Interface attribute key used to store the interface's node id     */    static final String RRD_REPOSITORY_KEY = "org.opennms.netmgt.collectd.SnmpThresholder.RrdRepository";    /**     * Interface attribute key used to store a map of node level ThresholdEntity     * objects keyed by datasource name.     */    static final String NODE_THRESHOLD_MAP_KEY = "org.opennms.netmgt.collectd.SnmpThresholder.NodeThresholdMap";    /**     * Interface attribute key used to store a map of interface level     * ThresholdEntity objects keyed by datasource name.     */    static final String BASE_IF_THRESHOLD_MAP_KEY = "org.opennms.netmgt.collectd.SnmpThresholder.IfThresholdMap";    /**     * We must maintain a map of interface level ThresholdEntity objects on a     * per interface basis in order to maintain separate exceeded counts and the     * like for each of a node's interfaces. This interface attribute key used     * to store a map of interface level ThresholdEntity object maps keyed by     * ifLabel. So it wil refer to a map of maps indexed by ifLabel.     */    static final String ALL_IF_THRESHOLD_MAP_KEY = "org.opennms.netmgt.collectd.SnmpThresholder.AllIfThresholdMap";    /**     * Local host name     */    private String m_host;    /**     * <P>     * Returns the name of the service that the plug-in collects ("SNMP").     * </P>     *      * @return The service that the plug-in collects.     */    public String serviceName() {        return SERVICE_NAME;    }    /**     * <P>     * Initialize the service thresholder.     * </P>     *      * @param parameters     *            Not currently used.     *      * @exception RuntimeException     *                Thrown if an unrecoverable error occurs that prevents the     *                plug-in from functioning.     *      */    public void initialize(Map parameters) {        // Log4j category        //        Category log = ThreadCategory.getInstance(getClass());        // Get local host name (used when generating threshold events)        //        try {            m_host = InetAddress.getLocalHost().getHostName();        } catch (UnknownHostException e) {            if (log.isEnabledFor(Priority.WARN))                log.warn("initialize: Unable to resolve local host name.", e);            m_host = "unresolved.host";        }        try {            RrdUtils.initialize();        } catch (RrdException e) {            if (log.isEnabledFor(Priority.ERROR))                log.error("initialize: Unable to initialize RrdUtils", e);            throw new RuntimeException("Unable to initialize RrdUtils", e);        }        if (log.isDebugEnabled())            log.debug("initialize: successfully instantiated JNI interface to RRD...");        return;    }    /**     * Responsible for freeing up any resources held by the thresholder.     */    public void release() {        // Nothing to release...    }    /**     * Responsible for performing all necessary initialization for the specified     * interface in preparation for thresholding.     *      * @param iface     *            Network interface to be prepped for thresholding.     * @param parameters     *            Key/value pairs associated with the package to which the     *            interface belongs..     *      */    public void initialize(NetworkInterface iface, Map parameters) {        Category log = ThreadCategory.getInstance(getClass());        // Get interface address from NetworkInterface        //        if (iface.getType() != NetworkInterface.TYPE_IPV4)            throw new RuntimeException("Unsupported interface type, only TYPE_IPV4 currently supported");        InetAddress ipAddr = (InetAddress) iface.getAddress();        // Retrieve the name of the thresholding group associated        // with this interface.        String groupName = ParameterMap.getKeyedString(parameters, "thresholding-group", "default");        // Get the threshold group's RRD repository path        //         String repository = null;        try {            repository = ThresholdingConfigFactory.getInstance().getRrdRepository(groupName);        } catch (IllegalArgumentException e) {            throw new RuntimeException("Thresholding group '" + groupName + "' does not exist.");        }        // Add RRD repository as an attribute of the interface for retrieval        // by the check() method.        //        iface.setAttribute(RRD_REPOSITORY_KEY, repository);        // Retrieve the collection of Threshold objects associated with        // the defined thresholding group and build two maps, one consisting        // of node level ThresholdEntity objects and another consisting of        // interface level ThresholdEntity objects both keyed by datasource        // name.        //        // Each ThresholdEntity can wrap one high Threshold and one low        // Threshold castor-generated object for a single datasource.        // If more than one high or more than one low threshold is defined        // for a single datasource a warning messages is generated. Only        // the first threshold in such a scenario will be used for thresholding.        //        Map nodeMap = new HashMap();        Map baseIfMap = new HashMap();        try {

⌨️ 快捷键说明

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