📄 snmpmonitor.java
字号:
//// 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://// 2003 Jan 31: Added the ability to imbed RRA information in poller packages.// 2003 Jan 31: Cleaned up some unused imports.// 2003 Jan 29: Added response times to certain monitors.//// 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.poller.monitors;import java.io.IOException;import java.lang.reflect.UndeclaredThrowableException;import java.math.BigInteger;import java.net.InetAddress;import java.net.SocketException;import java.util.Map;import java.util.regex.Pattern;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.config.PollerConfig;import org.opennms.netmgt.config.SnmpPeerFactory;import org.opennms.netmgt.utils.ParameterMap;import org.opennms.netmgt.utils.SnmpResponseHandler;import org.opennms.protocols.snmp.SnmpCounter64;import org.opennms.protocols.snmp.SnmpInt32;import org.opennms.protocols.snmp.SnmpObjectId;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.SnmpUInt32;import org.opennms.protocols.snmp.SnmpVarBind;/** * <P> * This class is designed to be used by the service poller framework to test the * availability of the SNMP service on remote interfaces. The class implements * the ServiceMonitor interface that allows it to be used along with other * plug-ins by the service poller framework. * </P> * * @author <A HREF="mailto:tarus@opennms.org">Tarus Balog </A> * @author <A HREF="mailto:mike@opennms.org">Mike Davidson </A> * @author <A HREF="http://www.opennms.org/">OpenNMS </A> * */final public class SnmpMonitor extends IPv4Monitor { /** * Name of monitored service. */ private static final String SERVICE_NAME = "SNMP"; /** * <P> * The default port on which the host is checked to see if it supports SNMP. * </P> */ private static int DEFAULT_PORT = 161; /** * Default object to collect if "oid" property not available. */ private static final String DEFAULT_OBJECT_IDENTIFIER = ".1.3.6.1.2.1.1.2.0"; // MIB-II // System // Object // Id /** * Interface attribute key used to store the interface's JoeSNMP SnmpPeer * object. */ static final String SNMP_PEER_KEY = "org.opennms.netmgt.collectd.SnmpCollector.SnmpPeer"; /** * Constant for less-than operand */ static final String LESS_THAN = "<"; static final String GREATER_THAN = ">"; static final String LESS_THAN_EQUALS = "<="; static final String GREATER_THAN_EQUALS = ">="; static final String EQUALS = "="; static final String NOT_EQUAL = "!="; static final String MATCHES = "~"; /** * <P> * Returns the name of the service that the plug-in monitors ("SNMP"). * </P> * * @return The service that the plug-in monitors. */ public String serviceName() { return SERVICE_NAME; } /** * <P> * Initialize the service monitor. * </P> * * @param parameters * Not currently used. * * @exception RuntimeException * Thrown if an unrecoverable error occurs that prevents the * plug-in from functioning. * */ public void initialize(PollerConfig pollerConfig, Map parameters) { // Log4j category // Category log = ThreadCategory.getInstance(getClass()); // 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); } return; } /** * <P> * Called by the poller framework when an interface is being added to the * scheduler. Here we perform any necessary initialization to prepare the * NetworkInterface object for polling. * </P> * * @param iface * The network interface to be initialized. * * @exception RuntimeException * Thrown if an unrecoverable error occurs that prevents the * interface from being monitored. */ public void initialize(NetworkInterface iface) { // Log4j category // 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 configured SNMP parms for this interface //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -