snmpcollector.java

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

JAVA
1,443
字号
//// 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 for lame SNMP hosts// 2003 Oct 20: Added minval and maxval code for mibObj RRDs// 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///package org.opennms.netmgt.collectd;import java.io.File;import java.io.IOException;import java.lang.reflect.UndeclaredThrowableException;import java.net.InetAddress;import java.net.SocketException;import java.net.UnknownHostException;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.TreeMap;import org.apache.log4j.Category;import org.apache.log4j.Priority;import org.exolab.castor.xml.MarshalException;import org.exolab.castor.xml.ValidationException;import org.opennms.core.utils.ThreadCategory;import org.opennms.netmgt.EventConstants;import org.opennms.netmgt.capsd.DbIpInterfaceEntry;import org.opennms.netmgt.config.DataCollectionConfigFactory;import org.opennms.netmgt.config.DatabaseConnectionFactory;import org.opennms.netmgt.config.SnmpPeerFactory;import org.opennms.netmgt.poller.monitors.NetworkInterface;import org.opennms.netmgt.rrd.RrdException;import org.opennms.netmgt.rrd.RrdUtils;import org.opennms.netmgt.utils.AlphaNumeric;import org.opennms.netmgt.utils.BarrierSignaler;import org.opennms.netmgt.utils.EventProxy;import org.opennms.netmgt.utils.EventProxyException;import org.opennms.netmgt.utils.ParameterMap;import org.opennms.netmgt.utils.SnmpResponseHandler;import org.opennms.netmgt.xml.event.Event;import org.opennms.protocols.snmp.SnmpObjectId;import org.opennms.protocols.snmp.SnmpPduBulk;import org.opennms.protocols.snmp.SnmpPduPacket;import org.opennms.protocols.snmp.SnmpPduRequest;import org.opennms.protocols.snmp.SnmpPeer;import org.opennms.protocols.snmp.SnmpSMI;import org.opennms.protocols.snmp.SnmpSession;import org.opennms.protocols.snmp.SnmpSyntax;import org.opennms.protocols.snmp.SnmpVarBind;/** * <P> * The SnmpCollector class ... * </P> *  * @author <A HREF="mailto:mike@opennms.org">Mike Davidson </A> * @author <A HREF="http://www.opennms.org/">OpenNMS </A> *  */final class SnmpCollector implements ServiceCollector {    /**     * Name of monitored service.     */    private static final String SERVICE_NAME = "SNMP";    /**     * 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'";    /**     * SQL statement to retrieve interface's 'issnmpprimary' table information.     */    private static final String SQL_GET_ISSNMPPRIMARY = "SELECT ifindex,issnmpprimary FROM ipinterface WHERE nodeid=?";    /**     * /** SQL statement to retrieve node's system object id.     */    private static final String SQL_GET_NODESYSOID = "SELECT nodesysoid FROM node WHERE nodeid=? AND nodetype!='D'";    /**     * SQL statement to check for SNMPv2 for a node     */    private static final String SQL_CHECK_SNMPV2 = "SELECT ifservices.serviceid FROM service, ifservices WHERE servicename='SNMPv2' AND ifservices.serviceid = service.serviceid AND nodeid=?";    /**     * SQL statement to fetch the ifIndex, ifName, and ifDescr values for all     * interfaces associated with a node     */    private static final String SQL_GET_SNMP_INFO = "SELECT DISTINCT snmpifindex, snmpiftype, snmpifname, snmpifdescr,snmpphysaddr " + "FROM snmpinterface, ipinterface " + "WHERE ipinterface.nodeid=snmpinterface.nodeid " + "AND ifindex = snmpifindex " + "AND ipinterface.nodeid=? " + "AND (ipinterface.ismanaged!='D')";    /**     * Default object to collect if "oid" property not available.     */    private static final String DEFAULT_OBJECT_IDENTIFIER = ".1.3.6.1.2.1.1.2"; // MIB-II                                                                                // System                                                                                // Object                                                                                // Id    /**     * Object identifier used to retrieve interface count.     */    private static final String INTERFACES_IFNUMBER = ".1.3.6.1.2.1.2.1"; // MIB-II                                                                            // interfaces.ifNumber    /**     * Valid values for the 'snmpStorageFlag' attribute in datacollection-config     * xml file.     *      * "primary" = only primary SNMP interface should be collected and stored     * "all" = all primary SNMP interfaces should be collected and stored     */    private static String SNMP_STORAGE_PRIMARY = "primary";    private static String SNMP_STORAGE_ALL = "all";    private static String SNMP_STORAGE_SELECT = "select";    /**     * This defines the default maximum number of variables the collector is     * permitted to pack into a single outgoing PDU. This value is intentionally     * kept relatively small in order to communicate successfully with the     * largest possible number of agents.     */    private static int DEFAULT_MAX_VARS_PER_PDU = 10;    /**     * Max number of variables permitted in a single outgoing SNMP PDU request..     */    private int m_maxVarsPerPdu;    /**     * Path to SNMP RRD file repository.     */    private String m_rrdPath;    /**     * Local host name     */    private String m_host;    /* -------------------------------------------------------------- */    /* Attribute key names */    /* -------------------------------------------------------------- */    /**     * Interface attribute key used to store the interface's JoeSNMP SnmpPeer     * object.     */    static final String SNMP_PEER_KEY = "org.opennms.netmgt.collectd.SnmpCollector.SnmpPeer";    /**     * Interface attribute key used to store the number of interfaces configured     * on the remote host.     */    static final String INTERFACE_COUNT_KEY = "org.opennms.netmgt.collectd.SnmpCollector.ifCount";    /**     * Interface attribute key used to store the map of IfInfo objects which     * hold data about each interface on a particular node.     */    static String IF_MAP_KEY = "org.opennms.netmgt.collectd.SnmpCollector.ifMap";    /**     * Interface attribute key used to store a NodeInfo object which holds data     * about the node being polled.     */    static String NODE_INFO_KEY = "org.opennms.netmgt.collectd.SnmpCollector.nodeInfo";    /**     * Interface attribute key used to store the data collection scheme to be     * followed. Two possible values: SNMP_STORAGE_PRIMARY = "primary"     * SNMP_STORAGE_ALL = "all" SNMP_STORAGE_SELECT = "select"     */    static String SNMP_STORAGE_KEY = "org.opennms.netmgt.collectd.SnmpCollector.snmpStorage";    /**     * Interface attribute key used to store configured value for the maximum     * number of variables permitted in a single outgoing SNMP PDU request.     */    static String MAX_VARS_PER_PDU_STORAGE_KEY = "org.opennms.netmgt.collectd.SnmpCollector.maxVarsPerPdu";    /**     * <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 collector.     * </P>     *      * <P>     * During initialization the SNMP collector:     *  - Initializes various configuration factories. - Verifies access to the     * database - Verifies access to RRD file repository - Verifies access to     * JNI RRD shared library - Determines if SNMP to be stored for only the     * node'sprimary interface or for all interfaces.     * </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";        }        // Initialize the SnmpPeerFactory        //        try {            SnmpPeerFactory.reload();        } catch (MarshalException ex) {            if (log.isEnabledFor(Priority.FATAL))                log.fatal("initialize: Failed to load SNMP configuration", ex);            throw new UndeclaredThrowableException(ex);        } catch (ValidationException ex) {            if (log.isEnabledFor(Priority.FATAL))                log.fatal("initialize: Failed to load SNMP configuration", ex);            throw new UndeclaredThrowableException(ex);        } catch (IOException ex) {            if (log.isEnabledFor(Priority.FATAL))                log.fatal("initialize: Failed to load SNMP configuration", ex);            throw new UndeclaredThrowableException(ex);        }        // Initialize the DataCollectionConfigFactory        //        try {            DataCollectionConfigFactory.reload();        } catch (MarshalException ex) {

⌨️ 快捷键说明

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