📄 collectdconfigfactory.java
字号:
/** * This method is used to determine if the named interface is included in * the passed package's url includes. If the interface is found in any of * the URL files, then a value of true is returned, else a false value is * returned. * * <pre> * The file URL is read and each entry in this file checked. Each line * in the URL file can be one of - * <IP><space>#<comments> * or * <IP> * or * #<comments> * * Lines starting with a '#' are ignored and so are characters after * a '<space>#' in a line. * </pre> * * @param addr * The interface to test against the package's URL * @param url * The url file to read * * @return True if the interface is included in the url, false otherwise. */ private boolean interfaceInUrl(String addr, String url) { boolean bRet = false; // get list of IPs in this URL java.util.List iplist = (java.util.List) m_urlIPMap.get(url); if (iplist != null && iplist.size() > 0) { bRet = iplist.contains(addr); } return bRet; } /** * This method is used to determine if the named interface is included in * the passed package definition. If the interface belongs to the package * then a value of true is returned. If the interface does not belong to the * package a false value is returned. * * <strong>Note: </strong>Evaluation of the interface against a package * filter will only work if the IP is already in the database. * * @param iface * The interface to test against the package. * @param pkg * The package to check for the inclusion of the interface. * * @return True if the interface is included in the package, false * otherwise. */ public synchronized boolean interfaceInPackage(String iface, Package pkg) { Category log = ThreadCategory.getInstance(this.getClass()); boolean filterPassed = interfaceInFilter(iface, pkg, log); if (!filterPassed) return false; // // Ensure that the interface is in the specific list or // that it is in the include range and is not excluded // long addr = IPSorter.convertToLong(iface); Enumeration eincs = pkg.enumerateIncludeRange(); boolean has_range_include = hasIncludeRange(addr, eincs); boolean has_specific = hasSpecific(pkg, addr); has_specific = hasSpecificUrl(iface, pkg, has_specific); boolean has_range_exclude = hasExcludeRange(pkg, addr, has_specific); boolean packagePassed = has_specific || (has_range_include && !has_range_exclude); log.debug("interfaceInPackage: Interface " + iface + " passed filter and specific/range for package " + pkg.getName() + "?: " + packagePassed); return packagePassed; } /** * Returns true if the service is part of the package and the status of the * service is set to "on". Returns false if the service is not in the * package or it is but the status of the service is set to "off". * * @param svcName * The service name to lookup. * @param pkg * The package to lookup up service. */ public synchronized boolean serviceInPackageAndEnabled(String svcName, org.opennms.netmgt.config.collectd.Package pkg) { boolean result = false; Enumeration esvcs = pkg.enumerateService(); while (result == false && esvcs.hasMoreElements()) { Service tsvc = (Service) esvcs.nextElement(); if (tsvc.getName().equalsIgnoreCase(svcName)) { // Ok its in the package. Now check the // status of the service String status = tsvc.getStatus(); if (status.equals("on")) result = true; } } return result; } /** * Returns true if the specified interface is included by at least one * package which has the specified service and that service is enabled (set * to "on"). * * @param ipAddr * IP address of the interface to lookup * @param svcName * The service name to lookup * * @return true if Collectd config contains a package which includes the * specified interface and has the specified service enabled. */ public synchronized boolean lookupInterfaceServicePair(String ipAddr, String svcName) { boolean result = false; Enumeration pkgs = m_config.enumeratePackage(); while (pkgs.hasMoreElements() && result == false) { org.opennms.netmgt.config.collectd.Package pkg = (org.opennms.netmgt.config.collectd.Package) pkgs.nextElement(); // Does the package include the interface? // if (interfaceInPackage(ipAddr, pkg)) { // Yes, now see if package includes // the service and service is enabled // if (serviceInPackageAndEnabled(svcName, pkg)) { // Thats all we need to know... result = true; } } } return result; } /** * This method is responsbile for determining the node's primary SNMP * interface from the specified list of InetAddress objects. * * @param addressList * List of InetAddress objects representing all the interfaces * belonging to a particular node which support the "SNMP" * service and have a valid ifIndex. * * @param strict * Boolean variable which requires an interface to be part of a * Collectd package to be eligible as a primary SNMP interface * * @return InetAddress object of the primary SNMP interface or null if none * of the node's interfaces are eligible. */ public synchronized InetAddress determinePrimarySnmpInterface(List addressList, boolean strict) { Category log = ThreadCategory.getInstance(CollectdConfigFactory.class); InetAddress primaryIf = null; // For now hard-coding primary interface address selection method to MIN String method = SELECT_METHOD_MIN; // To be selected as the the primary SNMP interface for a node // the interface must be included by a Collectd package if strict // is true, and that package must include the SNMP service and // the service must be enabled. // // Iterate over interface list and test each interface // Iterator iter = addressList.iterator(); while (iter.hasNext()) { InetAddress ipAddr = (InetAddress) iter.next(); if (log.isDebugEnabled()) log.debug("determinePrimarySnmpIf: checking interface " + ipAddr.getHostAddress()); primaryIf = compareAndSelectPrimaryCollectionInterface("SNMP", ipAddr, primaryIf, method, strict); } if (log.isDebugEnabled()) if (primaryIf != null) log.debug("determinePrimarySnmpInterface: candidate primary SNMP interface: " + primaryIf.getHostAddress()); else log.debug("determinePrimarySnmpInterface: no candidate primary SNMP interface found"); return primaryIf; } /** * Utility method which compares two InetAddress objects based on the * provided method (MIN/MAX) and returns the InetAddress which is to be * considered the primary interface. * * NOTE: In order for an interface to be considered primary, if strict is * true, it must be included by a Collectd package which supports the * specified service. This method will return null if the 'oldPrimary' * address is null and the 'currentIf' address does not pass the Collectd * package check, if strict is true.. * * @param svcName * Service name * @param currentIf * Interface with which to compare the 'oldPrimary' address. * @param oldPrimary * Primary interface to be compared against the 'currentIf' * address. * @param method * Comparison method to be used (either "min" or "max") * @param strict * require interface to be part of a Collectd package * * @return InetAddress object of the primary interface based on the provided * method or null if neither address is eligible to be primary. */ public synchronized InetAddress compareAndSelectPrimaryCollectionInterface(String svcName, InetAddress currentIf, InetAddress oldPrimary, String method, boolean strict) { InetAddress newPrimary = null; if (oldPrimary == null && strict) { if (lookupInterfaceServicePair(currentIf.getHostAddress(), svcName)) return currentIf; else return oldPrimary; } if (oldPrimary == null) return currentIf; long current = IPSorter.convertToLong(currentIf.getAddress()); long primary = IPSorter.convertToLong(oldPrimary.getAddress()); if (method.equals(SELECT_METHOD_MIN)) { // Smallest address wins if (current < primary) { // Replace the primary interface with the current // interface only if the current interface is managed! if (strict) { if (lookupInterfaceServicePair(currentIf.getHostAddress(), svcName)) newPrimary = currentIf; } else { newPrimary = currentIf; } } } else { // Largest address wins if (current > primary) { // Replace the primary interface with the current // interface only if the current interface is managed! if (strict) { if (lookupInterfaceServicePair(currentIf.getHostAddress(), svcName)) newPrimary = currentIf; } else { newPrimary = currentIf; } } } if (newPrimary != null) return newPrimary; else return oldPrimary; }private boolean hasExcludeRange(Package pkg, long addr, boolean has_specific) { boolean has_range_exclude = false; Enumeration eex = pkg.enumerateExcludeRange(); while (!has_range_exclude && !has_specific && eex.hasMoreElements()) { ExcludeRange rng = (ExcludeRange) eex.nextElement(); long start = IPSorter.convertToLong(rng.getBegin()); if (addr > start) { long end = IPSorter.convertToLong(rng.getEnd()); if (addr <= end) { has_range_exclude = true; } } else if (addr == start) { has_range_exclude = true; } } return has_range_exclude;}private boolean hasSpecificUrl(String iface, Package pkg, boolean has_specific) { Enumeration eurl = pkg.enumerateIncludeUrl(); while (!has_specific && eurl.hasMoreElements()) { has_specific = interfaceInUrl(iface, (String) eurl.nextElement()); } return has_specific;}private boolean hasSpecific(Package pkg, long addr) { boolean has_specific = false; Enumeration espec = pkg.enumerateSpecific(); while (!has_specific && espec.hasMoreElements()) { long speca = IPSorter.convertToLong(espec.nextElement().toString()); if (speca == addr) has_specific = true; } return has_specific;}private boolean hasIncludeRange(long addr, Enumeration eincs) { boolean has_range_include = false; while (!has_range_include && eincs.hasMoreElements()) { IncludeRange rng = (IncludeRange) eincs.nextElement(); long start = IPSorter.convertToLong(rng.getBegin()); if (addr > start) { long end = IPSorter.convertToLong(rng.getEnd()); if (addr <= end) { has_range_include = true; } } else if (addr == start) { has_range_include = true; } } return has_range_include;} private boolean interfaceInFilter(String iface, Package pkg, Category log) { boolean filterPassed = false; // get list of IPs in this package java.util.List ipList = (java.util.List) m_pkgIpMap.get(pkg); if (ipList != null && ipList.size() > 0) { filterPassed = ipList.contains(iface); } else { log.debug("interfaceInFilter: ipList contains no data"); } if (!filterPassed) log.debug("interfaceInFilter: Interface " + iface + " passed filter for package " + pkg.getName() + "?: false"); return filterPassed; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -