📄 capsdconfigfactory.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://// 2005 Mar 25: Fixed bug 1178 regarding designation of secondary SNMP// interfaces, as well as a few other minor bugs discovered// in testing the bug fix.// 2004 Dec 27: Updated code to determine primary SNMP interface to select// an interface from collectd-configuration.xml first, and if// none found, then from all interfaces on the node. In either// case, a loopback interface is preferred if available.// 2004 Jan 06: Added support for STATUS_SUSPEND abd STATUS_RESUME// 2003 Nov 11: Merged changes from Rackspace project// 2003 Sep 17: Fixed an SQL parameter problem.// 2003 Sep 16: Changed rescan information to let OpenNMS handle duplicate IPs.// 2003 Jan 31: Cleaned up some unused imports.// 2002 Aug 27: Fixed <range> tag. Bug #655//// 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.config;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.InetAddress;import java.net.MalformedURLException;import java.net.URL;import java.net.UnknownHostException;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.Enumeration;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.TreeMap;import org.apache.log4j.Category;import org.exolab.castor.xml.MarshalException;import org.exolab.castor.xml.Unmarshaller;import org.exolab.castor.xml.ValidationException;import org.opennms.core.utils.ThreadCategory;import org.opennms.netmgt.ConfigFileConstants;import org.opennms.netmgt.capsd.DbIfServiceEntry;import org.opennms.netmgt.capsd.DbIpInterfaceEntry;import org.opennms.netmgt.capsd.Plugin;import org.opennms.netmgt.config.capsd.CapsdConfiguration;import org.opennms.netmgt.config.capsd.IpManagement;import org.opennms.netmgt.config.capsd.Property;import org.opennms.netmgt.config.capsd.ProtocolConfiguration;import org.opennms.netmgt.config.capsd.ProtocolPlugin;import org.opennms.netmgt.config.capsd.Range;import org.opennms.netmgt.config.capsd.SmbAuth;import org.opennms.netmgt.config.capsd.SmbConfig;import org.opennms.netmgt.utils.IPSorter;/** * This is the singleton class used to load the configuration for the OpenNMS * Capsd service from the capsd-configuration xml file. * * <strong>Note: </strong>Users of this class should make sure the * <em>init()</em> is called before calling any other method to ensure the * config is loaded before accessing other convenience methods. * * @author <a href="mailto:mike@opennms.org">Mike Davidson </a> * @author <a href="http://www.opennms.org/">OpenNMS </a> */public final class CapsdConfigFactory { /** * The string indicating the start of the comments in a line containing the * IP address in a file URL */ private final static String COMMENT_STR = " #"; /** * This character at the start of a line indicates a comment line in a URL * file */ private final static char COMMENT_CHAR = '#'; /** * This member is set to true if the configuration file has been loaded. */ private static boolean m_loaded = false; /** * The singleton instance of this factory */ private static CapsdConfigFactory m_singleton = null; /** * The config class loaded from the config file */ private CapsdConfiguration m_config; /** * Maps url names to a list of addresses specified by the url. Initialized * at factory construction. */ private Map m_urlMap; /** * The SQL statement used to retrieve all non-deleted/non-forced unamanaged * IP interfaces from the 'ipInterface' table. */ final static String SQL_DB_RETRIEVE_IP_INTERFACE = "SELECT nodeid,ipaddr,ismanaged FROM ipinterface WHERE ipaddr!='0.0.0.0' AND isManaged!='D' AND isManaged!='F'"; /** * The SQL statement used to retrieve all non-deleted/non-forced unamanaged * IP interfaces from the 'ipInterface' table with the local OpenNMS server * restriction. */ final static String SQL_DB_RETRIEVE_IP_INTERFACE_IN_LOCAL_SERVER = "SELECT ip.nodeid, ip.ipaddr, ip.ismanaged " + "FROM ipinterface ip, servermap s " + "WHERE ip.ipaddr = s.ipaddr " + "AND ip.ipaddr!='0.0.0.0' " + "AND ip.isManaged!='D' " + "AND ip.isManaged!='F' " + "AND s.servername = ?"; /** * SQL statement to retrieve all non-deleted IP addresses from the * ipInterface table which support SNMP. */ private static String SQL_DB_RETRIEVE_SNMP_IP_INTERFACES = "SELECT DISTINCT ipinterface.nodeid,ipinterface.ipaddr,ipinterface.ifindex,ipinterface.issnmpprimary,snmpinterface.snmpiftype,snmpinterface.snmpifindex FROM ipinterface,ifservices,service,snmpinterface WHERE ipinterface.ismanaged!='D' AND ifservices.status!='D' AND ipinterface.ipaddr=ifservices.ipaddr AND ipinterface.ipaddr=snmpinterface.ipaddr AND ifservices.serviceid=service.serviceid AND service.servicename='SNMP' AND ipinterface.nodeid=snmpinterface.nodeid"; /** * SQL statement used to update the 'isSnmpPrimary' field of the ipInterface * table. */ private static String SQL_DB_UPDATE_SNMP_PRIMARY_STATE = "UPDATE ipinterface SET issnmpprimary=? WHERE nodeid=? AND ipaddr=? AND ismanaged!='D'"; /** * The SQL statement used to retrieve all non-deleted/non-forced unamanaged * services for a nodeid/ip from the 'ifservices' table. */ final static String SQL_DB_RETRIEVE_IF_SERVICES = "SELECT serviceid, status FROM ifservices WHERE nodeid=? AND ipaddr=? AND status!='D' AND status!='F'"; /** * The SQL statement which updates the 'isManaged' field in the ipInterface * table for a specific node/ipAddr pair */ final static String SQL_DB_UPDATE_IP_INTERFACE = "UPDATE ipinterface SET ismanaged=? WHERE nodeid=? AND ipaddr=? AND isManaged!='D' AND isManaged!='F'"; /** * The SQL statement which updates the 'status' field in the ifServices * table for a specific node/ipAddr pair */ final static String SQL_DB_UPDATE_ALL_SERVICES_FOR_NIP = "UPDATE ifservices SET status=? WHERE nodeid=? AND ipaddr=? AND status!='D' AND status!='F'"; /* * The SQL statement which updates the 'status' field in the ifServices * table for a specific node/ipAddr/service */ final static String SQL_DB_UPDATE_SERVICE_FOR_NIP = "UPDATE ifservices SET status=? WHERE nodeid=? AND ipaddr=? AND serviceid=? AND status!='D' AND status!='F'"; /** * The SQL statement used to determine if an IP address is already in the * ipInterface table and there is known. */ private static final String RETRIEVE_IPADDR_SQL = "SELECT ipaddr FROM ipinterface WHERE ipaddr=? AND ismanaged!='D'"; /** * The SQL statement used to determine if an IP address is already in the * ipInterface table and if so what its parent nodeid is. */ private static final String RETRIEVE_IPADDR_NODEID_SQL = "SELECT nodeid FROM ipinterface WHERE ipaddr=? AND ismanaged!='D'"; /** * The SQL statement used to load the currenly defined service table. */ private static final String SVCTBL_LOAD_SQL = "SELECT serviceID, serviceName FROM service"; /** * The SQL statement used to add a new entry into the service table */ private static final String SVCTBL_ADD_SQL = "INSERT INTO service (serviceID, serviceName) VALUES (?,?)"; /** * The SQL statement used to get the next value for a service identifier. * This is a sequence defined in the database. */ private static final String NEXT_SVC_ID_SQL = "SELECT nextval('serviceNxtId')"; /** * The SQL statement used to delete all entries from the outage table which * refer to the specified serviceId */ private static final String DELETE_OUTAGES_SQL = "DELETE FROM outages WHERE serviceID=?"; /** * The SQL statement used to mark all ifservices table entries which refer * to the specified serviceId as deleted. */ private static final String DELETE_IFSERVICES_SQL = "UPDATE ifservices SET status='D' WHERE serviceID=?"; /** * The map of service identifiers, mapped by the service id and name. The * identifier keys are integers and the names are string. The integers map * to strings and the strings map to integers. * */ private static final Map m_serviceIds = new HashMap(); /** * The map of capsd plugins. The plugins are indexed by both their class * name and the protocols that they support. */ private static final Map m_plugins = new TreeMap(); /** * The integer value that is used to represent the protocol scan * configuration. If this value is used the the plugin should be used to * scan the address. */ public static final Integer SCAN = new Integer(0); /** * This integer value is used to represent that the protocol plugin should * not be used to scan the interface. */ public static final Integer SKIP = new Integer(1); /** * This integer value is used to represent the state when the protocol * should be automatically set, with any status checks. */ public static final Integer AUTO_SET = new Integer(2); /** * This integer value is used to represent the primary snmp interface * ifindex in the ipinterface table for SNMP hosts that don't support * the MIB2 ipAddrTable */ public static final int LAME_SNMP_HOST_IFINDEX = -100; /** * This class is used to encapsulate the basic protocol information read * from the config file. The information includes the plugin, the protocol * name, the merged parameters to the plugin, and the action to be taken. * * @author <a href="mailto:weave@oculan.com">Weave </a> * @author <a href="http://www.opennms.org/">OpenNMS </a> * */ public final static class ProtocolInfo { /** * The plugin used to poll the interface */ private Plugin m_plugin; /** * The name of the protocol supported by the plugin. */ private String m_protocol; /** * the map or parameters passed to the plugin */ private Map m_parameters; /** * The integer value that represents the action that should be taken to * poll the interface. */ private Integer m_action; /** * Constructs a new protocol information element. * * @param proto * The protocol supported. * @param plugin * The plugin module * @param params * The parameters for the plugin. * @param action * The action to take. */ public ProtocolInfo(String proto, Plugin plugin, Map params, Integer action) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -