ifcollector.java

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

JAVA
571
字号
//// 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: added support lame snmp hosts when ifTable not supported// 2003 Jan 31: Cleaned up some unused imports.//// 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///// Tab Size = 8//package org.opennms.netmgt.capsd;import java.lang.reflect.UndeclaredThrowableException;import java.net.InetAddress;import java.net.NoRouteToHostException;import java.net.UnknownHostException;import java.util.ArrayList;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import java.util.TreeMap;import org.apache.log4j.Category;import org.opennms.core.utils.ThreadCategory;import org.opennms.netmgt.capsd.snmp.IfTableEntry;import org.opennms.netmgt.capsd.snmp.IpAddrTable;import org.opennms.netmgt.config.CapsdConfigFactory;import org.opennms.netmgt.config.SnmpPeerFactory;import org.opennms.protocols.snmp.SnmpInt32;import org.opennms.protocols.snmp.SnmpPeer;import org.opennms.protocols.snmp.SnmpSMI;/** * This class is designed to collect all the relevant information from the * target designated during construction. The target is initially polled using * all the configured plugins, then tested for SMB and SNMP. If either of those * plugins were detected then an additional collection of the SMB/SNMP * information is preformed. If any node has multiple interfaces in it then * addition probes of those interfaces are performed. The SNMP/SMB collections * are preformed only once though. *  * @author <a href="mailto:weave@oculan.com">Weave </a> * @author <a href="http://www.opennms.org">OpenNMS </a> *  */final class IfCollector implements Runnable {    /**     * The primary target internet address     */    private final InetAddress m_target;    /**     * The SMB collector. If the interface is determine to have SMB connectivity     * then the collector is run.     */    private IfSmbCollector m_smbCollector;    /**     * If the interface is determined to have SNMP capabilities then the     * collector is run.     */    private IfSnmpCollector m_snmpCollector;    /**     * The list of supported protocols on this interface.     */    private List m_protocols;    /**     * The list of sub-targets found via SNMP. Indexed by InetAddress     */    private Map m_subTargets;    /**     * List of SnmpInt32 objects representing each of the unnamed/non-IP     * interfaces found via SNMP     */    private List m_nonIpInterfaces;    /**     * Boolean flag which indicates if SNMP collection is to be done.     */    private boolean m_doSnmpCollection;    private Set m_previouslyProbed;    /**     * This class is used to encapsulate the supported protocol information     * discovered for an interface. The is the combination of the protocol name     * and the in/out qualifiers for the plugin.     *      * @author <a href="mailto:weave@oculan.com">Weave </a>     *      */    static final class SupportedProtocol {        /**         * The protocol name         */        private final String m_name;        /**         * The map of qualifiers from the plugin that discovered this protocol.         */        private final Map m_qualifiers;        /**         * Creates a new supported protocol based upon the protocol string and         * the qualifier map.         *          * @param protoName         *            The name of the protocol.         * @param qualifiers         *            The protocol qualifiers.         */        SupportedProtocol(String protoName, Map qualifiers) {            m_name = protoName;            m_qualifiers = qualifiers;        }        /**         * Returns the name of the discovered protocol.         */        String getProtocolName() {            return m_name;        }        /**         * Returns the map of qualifiers from the plugin that discovered this         * protocol.         */        Map getQualifiers() {            return m_qualifiers;        }    }    /**     * This method is used to <em>probe</em> the target addresses using the     * configured list of protocol specifications from the Configuration     * Manager. The list of supported protocols are added to the supports list.     * Any failures in the plugins are logged and discarded.     *      * @param target     *            The target to probe     * @param supports     *            The supported protocols (SupportedProtocol)     *      */    private static void probe(InetAddress target, List supports) {        Category log = ThreadCategory.getInstance(IfCollector.class);        String logAddr = target.getHostAddress();        CapsdConfigFactory.ProtocolInfo[] plugins = CapsdConfigFactory.getInstance().getProtocolSpecification(target);                // First run the plugins to find out all the capabilities        // for the interface        //        for (int i = 0; i < plugins.length; i++) {            log.debug(logAddr + " testing plugin " + plugins[i].getProtocol());            if (plugins[i].autoEnabled()) {                log.debug(logAddr + " protocol " + plugins[i].getProtocol() + " is auto enabled");                supports.add(new SupportedProtocol(plugins[i].getProtocol(), null));                continue;            }            try {                Plugin p = plugins[i].getPlugin();                Map q = plugins[i].getParameters();                boolean r = p.isProtocolSupported(target, q);                log.debug(logAddr + " protocol " + plugins[i].getProtocol() + " supported? " + (r ? "true" : "false"));                if (r)                    supports.add(new SupportedProtocol(plugins[i].getProtocol(), q));            } catch (UndeclaredThrowableException utE) {                Throwable t = utE.getUndeclaredThrowable();                if (t instanceof NoRouteToHostException) {                    if (CapsdConfigFactory.getInstance().getAbortProtocolScansFlag()) {                        log.warn("IfCollector: No route to host " + logAddr + ", aborting protocol scans.");                        break; // Break out of plugin loop                    } else {                        log.warn("IfCollector: No route to host " + logAddr + ", continuing protocol scans.");                    }                } else {                    log.warn("IfCollector: Caught undeclared throwable exception when testing for protocol " + plugins[i].getProtocol() + " on host " + logAddr, utE);                }            } catch (Throwable t) {                log.warn("IfCollector: Caught an exception when testing for protocol " + plugins[i].getProtocol() + " on host " + logAddr, t);            }            log.debug(logAddr + " plugin " + plugins[i].getProtocol() + " completed!");        }    }    /**     * Default constructor. This constructor is disallowed since the collector     * must have a target IP address to collect on. This constructor will always     * throw an Unsupported Operation Exception.     *      */    IfCollector() {        throw new UnsupportedOperationException("default construction not available!");    }    /**     * Constructs a new collector instance. The collector's target is passed as     * an argument to the constructor. Very little initialization is preformed     * in the constructor. The main work of the class is preformed in the     * {@link #run run}method. This provides a well known interface that can be     * collected in a thread pool or directly invoked.     *      * @param addr     *            The target of the poll.     * @param doSnmpCollection     *            Flag which indicates if SNMP collection should be done.     *      */    IfCollector(InetAddress addr, boolean doSnmpCollection) {        this(addr, doSnmpCollection, new HashSet());    }        IfCollector(InetAddress addr, boolean doSnmpCollection, Set previouslyProbed) {        m_target = addr;        m_doSnmpCollection = doSnmpCollection;        m_smbCollector = null;        m_snmpCollector = null;        m_protocols = new ArrayList(8);        m_subTargets = null;        m_nonIpInterfaces = null;        m_previouslyProbed = previouslyProbed;    }    /**     * Returns the target of this collection     */    InetAddress getTarget() {        return m_target;    }    /**     * Returns the supported protocols for this interface.     */    List getSupportedProtocols() {        return m_protocols;    }    /**     * Returns true if this target had additional interfaces found by SNMP     */    boolean hasAdditionalTargets() {        return m_subTargets != null && !m_subTargets.isEmpty();    }    /**     * Returns the map of additional interface targets. The keys are instances

⌨️ 快捷键说明

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