📄 collectdconfigfactory.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://// 2004 Dec 21: Changed determination of primary SNMP interface// 2003 Nov 11: Merged changes from Rackspace project// 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.config;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.StringWriter;import java.io.FileWriter;import java.net.InetAddress;import java.util.Enumeration;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import org.apache.log4j.Category;import org.apache.log4j.Priority;import org.exolab.castor.xml.Marshaller;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.config.collectd.CollectdConfiguration;import org.opennms.netmgt.config.collectd.ExcludeRange;import org.opennms.netmgt.config.collectd.IncludeRange;import org.opennms.netmgt.config.collectd.Service;import org.opennms.netmgt.filter.Filter;import org.opennms.netmgt.utils.IPSorter;import org.opennms.netmgt.utils.IpListFromUrl;import org.opennms.netmgt.config.collectd.Package;/** * @author <a href="mailto:jamesz@opennms.com">James Zuo</a> * @author <a href="mailto:mike@opennms.org">Mike Davidson</a> * @author <a href="mailto:sowmya@opennms.org">Sowmya Nataraj</a> * @author <a href="http://www.opennms.org/">OpenNMS</a> *//** * This is the singleton class used to load the configuration for the OpenNMS * Collection Daemon from the collectd-configuration xml file. * * A mapping of the configured URLs to the iplist they contain is built at * init() time so as to avoid numerous file reads. * * <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. */public final class CollectdConfigFactory { private final static String SELECT_METHOD_MIN = "min"; private final static String SELECT_METHOD_MAX = "max"; /** * The singleton instance of this factory */ private static CollectdConfigFactory m_singleton = null; /** * This member is set to true if the configuration file has been loaded. */ private static boolean m_loaded = false; /** * The config class loaded from the config file */ private CollectdConfiguration m_config; /** * A mapping of the configured URLs to a list of the specific IPs configured * in each - so as to avoid file reads */ private Map m_urlIPMap; /** * A mapping of the configured package to a list of IPs selected via filter * rules, so as to avoid repetetive database access. */ private Map m_pkgIpMap; /** * A boolean flag to indicate If a filter rule against the local NMS server * has to be used. */ private static boolean m_verifyServer; /** * Name of the local NMS server. */ private static String m_localServer; /** * Go through the configuration and build a mapping of each configured URL * to a list of IPs configured in that URL - done at init() time so that * repeated file reads can be avoided */ private void createUrlIpMap() { m_urlIPMap = new HashMap(); Enumeration pkgEnum = m_config.enumeratePackage(); while (pkgEnum.hasMoreElements()) { org.opennms.netmgt.config.collectd.Package pkg = (org.opennms.netmgt.config.collectd.Package) pkgEnum.nextElement(); Enumeration urlEnum = pkg.enumerateIncludeUrl(); while (urlEnum.hasMoreElements()) { String urlname = (String) urlEnum.nextElement(); java.util.List iplist = IpListFromUrl.parse(urlname); if (iplist.size() > 0) { m_urlIPMap.put(urlname, iplist); } } } } /** * This method is used to establish package agaist iplist mapping, with * which, the iplist is selected per package via the configured filter rules * from the database. */ private void createPackageIpListMap() { Category log = ThreadCategory.getInstance(this.getClass()); // Multiple threads maybe asking for the m_pkgIpMap field so create // with temp map then assign when finished. Map pkgIpMap = new HashMap(); Enumeration pkgEnum = m_config.enumeratePackage(); while (pkgEnum.hasMoreElements()) { Package pkg = (Package) pkgEnum.nextElement(); // // Get a list of ipaddress per package agaist the filter rules from // database and populate the package, IP list map. // Filter filter = new Filter(); StringBuffer filterRules = new StringBuffer(pkg.getFilter().getContent()); try { if (m_verifyServer) { filterRules.append(" & (serverName == "); filterRules.append('\"'); filterRules.append(m_localServer); filterRules.append('\"'); filterRules.append(")"); } if (log.isDebugEnabled()) log.debug("createPackageIpMap: package is " + pkg.getName() + ". filer rules are " + filterRules.toString()); List ipList = filter.getIPList(filterRules.toString()); if (ipList.size() > 0) { pkgIpMap.put(pkg, ipList); } } catch (Throwable t) { if (log.isEnabledFor(Priority.ERROR)) { log.error("createPackageIpMap: failed to map package: " + pkg.getName() + " to an IP List", t); } } } m_pkgIpMap = pkgIpMap; } /** * This method is used to rebuild the package agaist iplist mapping when * needed. When a node gained service event occurs, collectd has to * determine which package the ip/service combination is in, but if the * interface is a newly added one, the package iplist should be rebuilt so * that collectd could know which package this ip/service pair is in. */ public synchronized void rebuildPackageIpListMap() { createPackageIpListMap(); } /** * Private constructor * * @exception java.io.IOException * Thrown if the specified config file cannot be read * @exception org.exolab.castor.xml.MarshalException * Thrown if the file does not conform to the schema. * @exception org.exolab.castor.xml.ValidationException * Thrown if the contents do not match the required schema. */ private CollectdConfigFactory(String configFile) throws IOException, MarshalException, ValidationException { InputStream cfgIn = new FileInputStream(configFile); m_config = (CollectdConfiguration) Unmarshaller.unmarshal(CollectdConfiguration.class, new InputStreamReader(cfgIn)); cfgIn.close(); createUrlIpMap(); OpennmsServerConfigFactory.init(); m_verifyServer = OpennmsServerConfigFactory.getInstance().verifyServer(); m_localServer = OpennmsServerConfigFactory.getInstance().getServerName(); createPackageIpListMap(); } /** * Load the config from the default config file and create the singleton * instance of this factory. * * @exception java.io.IOException * Thrown if the specified config file cannot be read * @exception org.exolab.castor.xml.MarshalException * Thrown if the file does not conform to the schema. * @exception org.exolab.castor.xml.ValidationException * Thrown if the contents do not match the required schema. */ public static synchronized void init() throws IOException, MarshalException, ValidationException { if (m_loaded) { // init already called - return // to reload, reload() will need to be called return; } File cfgFile = ConfigFileConstants.getFile(ConfigFileConstants.COLLECTD_CONFIG_FILE_NAME); ThreadCategory.getInstance(CollectdConfigFactory.class).debug("init: config file path: " + cfgFile.getPath()); m_singleton = new CollectdConfigFactory(cfgFile.getPath()); m_loaded = true; } /** * Reload the config from the default config file * * @exception java.io.IOException * Thrown if the specified config file cannot be read/loaded * @exception org.exolab.castor.xml.MarshalException * Thrown if the file does not conform to the schema. * @exception org.exolab.castor.xml.ValidationException * Thrown if the contents do not match the required schema. */ public static synchronized void reload() throws IOException, MarshalException, ValidationException { m_singleton = null; m_loaded = false; init(); } /** * Saves the current in-memory configuration to disk and reloads */ public synchronized void saveCurrent() throws MarshalException, IOException, ValidationException { File cfgFile = ConfigFileConstants.getFile(ConfigFileConstants.COLLECTD_CONFIG_FILE_NAME); //marshall to a string first, then write the string to the file. This way the original config //isn't lost if the xml from the marshall is hosed. StringWriter stringWriter = new StringWriter(); Marshaller.marshal(m_config, stringWriter); if (stringWriter.toString()!=null) { FileWriter fileWriter = new FileWriter(cfgFile); fileWriter.write(stringWriter.toString()); fileWriter.flush(); fileWriter.close(); } reload(); } /** * Return the singleton instance of this factory. * * @return The current factory instance. * * @throws java.lang.IllegalStateException * Thrown if the factory has not yet been initialized. */ public static synchronized CollectdConfigFactory getInstance() { if (!m_loaded) throw new IllegalStateException("The factory has not been initialized"); return m_singleton; } /** * Return the collectd configuration object. */ public synchronized CollectdConfiguration getConfiguration() { return m_config; } public synchronized org.opennms.netmgt.config.collectd.Package getPackage(String name) { Enumeration packageEnum=m_config.enumeratePackage(); while(packageEnum.hasMoreElements()) { org.opennms.netmgt.config.collectd.Package thisPackage=( org.opennms.netmgt.config.collectd.Package)packageEnum.nextElement(); if(thisPackage.getName().equals(name)) { return thisPackage; } } return null; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -