datacollectionconfigfactory.java
来自「opennms得相关源码 请大家看看」· Java 代码 · 共 687 行 · 第 1/2 页
JAVA
687 行
//// 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 Oct 20: Added minval and maxval parameters to mibObj for 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///// 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.net.InetAddress;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 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.collectd.MibObject;import org.opennms.netmgt.config.collectd.DatacollectionConfig;import org.opennms.netmgt.config.collectd.Group;import org.opennms.netmgt.config.collectd.Groups;import org.opennms.netmgt.config.collectd.MibObj;import org.opennms.netmgt.config.collectd.SystemDef;import org.opennms.netmgt.config.collectd.SystemDefChoice;/** * This class is the main respository for SNMP data collection configuration * information used by the SNMP service monitor. When this class is loaded it * reads the snmp data collection configuration into memory. * * <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:weave@oculan.com">Weave </a> * @author <a href="http://www.opennms.org/">OpenNMS </a> * */public final class DataCollectionConfigFactory { /** * The singleton instance of this factory */ private static DataCollectionConfigFactory m_singleton = null; /** * The config class loaded from the config file */ private DatacollectionConfig m_config; /** * This member is set to true if the configuration file has been loaded. */ private static boolean m_loaded = false; /** * Map of group maps indexed by SNMP collection name. */ private Map m_collectionGroupMap; /** * Map of SnmpCollection objects indexed by data collection name */ private Map m_collectionMap; /** * 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 DataCollectionConfigFactory(String configFile) throws IOException, MarshalException, ValidationException { InputStream cfgIn = new FileInputStream(configFile); m_config = (DatacollectionConfig) Unmarshaller.unmarshal(DatacollectionConfig.class, new InputStreamReader(cfgIn)); cfgIn.close(); // Build collection map which is a hash map of Collection // objects indexed by collection name...also build // collection group map which is a hash map indexed // by collection name with a hash map as the value // containing a map of the collections's group names // to the Group object containing all the information // for that group. So the associations are: // // CollectionMap // collectionName -> Collection // // CollectionGroupMap // collectionName -> groupMap // // GroupMap // groupMapName -> Group // // This is parsed and built at initialization for // faster processing at run-timne. // m_collectionMap = new HashMap(); m_collectionGroupMap = new HashMap(); java.util.Collection collections = m_config.getSnmpCollectionCollection(); Iterator citer = collections.iterator(); while (citer.hasNext()) { org.opennms.netmgt.config.collectd.SnmpCollection collection = (org.opennms.netmgt.config.collectd.SnmpCollection) citer.next(); // Build group map for this collection Map groupMap = new HashMap(); Groups groups = collection.getGroups(); java.util.Collection groupList = groups.getGroupCollection(); Iterator giter = groupList.iterator(); while (giter.hasNext()) { Group group = (Group) giter.next(); groupMap.put(group.getName(), group); } m_collectionGroupMap.put(collection.getName(), groupMap); m_collectionMap.put(collection.getName(), collection); } } /** * 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.DATA_COLLECTION_CONF_FILE_NAME); ThreadCategory.getInstance(DataCollectionConfigFactory.class).debug("init: config file path: " + cfgFile.getPath()); m_singleton = new DataCollectionConfigFactory(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(); } /** * 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 DataCollectionConfigFactory getInstance() { if (!m_loaded) throw new IllegalStateException("The factory has not been initialized"); return m_singleton; } /** * Converts the internet address to a long value so that it can be compared * using simple opertions. The address is converted in network byte order * (big endin) and allows for comparisions like <, >, <=, >=, * ==, and !=. * * @param addr * The address to convert to a long * * @return The address as a long value. * */ private static long toLong(InetAddress addr) { byte[] baddr = addr.getAddress(); long result = ((long) baddr[0] & 0xffL) << 24 | ((long) baddr[1] & 0xffL) << 16 | ((long) baddr[2] & 0xffL) << 8 | ((long) baddr[3] & 0xffL); return result; } /** * This method returns the list of MIB objects associated with a particular * system object id, IP address, and ifType for the specified collection. * * @param cName * name of the data collection from which to retrieve oid * information. * @param aSysoid * system object id to look up in the collection * @param anAddress * IP address to look up in the collection * @param ifType * Interface type (-1 indicates that only node-level objects * should be retrieved. * * @return a list of MIB objects */ public List getMibObjectList(String cName, String aSysoid, String anAddress, int ifType) { Category log = ThreadCategory.getInstance(getClass()); if (log.isDebugEnabled()) log.debug("getMibObjectList: collection: " + cName + " sysoid: " + aSysoid + " address: " + anAddress + " ifType: " + ifType); if (aSysoid == null) { if (log.isDebugEnabled()) log.debug("getMibObjectList: aSysoid parameter is NULL..."); return new ArrayList(); } // Retrieve the appropriate Collection object // org.opennms.netmgt.config.collectd.SnmpCollection collection = (org.opennms.netmgt.config.collectd.SnmpCollection) m_collectionMap.get(cName); if (collection == null) { return new ArrayList(); } // // First build a list of SystemDef objects which "match" the passed // sysoid and IP address parameters. The SystemDef object must match // on both the sysoid AND the IP address. // // SYSOID MATCH // // A SystemDef object's sysoid value may be a complete system object // identifier or it may be a mask (a partial sysoid). // // If the sysoid is not a mask, the 'aSysoid' string must equal the // sysoid value exactly in order to match. // // If the sysoid is a mask, the 'aSysoid' string need only start with // the sysoid mask value in order to match // // For example, a sysoid mask of ".1.3.6.1.4.1.11." would match any // Hewlett-Packard product which had this sysoid prefix (which should // include all of them). // // IPADDRESS MATCH // // In order to match on IP Address one of the following must be true: // // The SystemDef's IP address list (ipList) must contain the 'anAddress' // parm (must be an exact match) // // OR // // The 'anAddress' parm must have the same prefix as one of the // SystemDef's IP address mask list (maskList) entries. // // NOTE: A SystemDef object which contains an empty IP list and // an empty Mask list matches ALL IP addresses (default is INCLUDE). // List systemList = new ArrayList(); Enumeration e = collection.getSystems().enumerateSystemDef(); SystemDef system = null; while (e.hasMoreElements()) { system = (SystemDef) e.nextElement(); // Match on sysoid? boolean bMatchSysoid = false; // Retrieve sysoid for this SystemDef and/ set the isMask boolean. boolean isMask = false; String currSysoid = null; SystemDefChoice sysChoice = system.getSystemDefChoice(); if (sysChoice.getSysoid() != null) currSysoid = sysChoice.getSysoid(); else if (sysChoice.getSysoidMask() != null) { currSysoid = sysChoice.getSysoidMask(); isMask = true; } if (currSysoid != null) { if (isMask) { // SystemDef's sysoid is a mask, 'aSysoid' need only // start with the sysoid mask in order to match if (aSysoid.startsWith(currSysoid)) { if (log.isDebugEnabled())
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?