modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheVirtualHostServiceComponent.java | 7 modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheVirtualHostServiceDiscoveryComponent.java | 20 - modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/AugeasConfigurationApache.java | 24 +- modules/plugins/apache/src/main/java/org/rhq/plugins/apache/parser/ApacheParserImpl.java | 4 modules/plugins/apache/src/main/java/org/rhq/plugins/apache/util/HttpdAddressUtility.java | 114 ++++++++-- modules/plugins/augeas/src/main/java/org/rhq/augeas/util/Glob.java | 4 6 files changed, 126 insertions(+), 47 deletions(-)
New commits: commit 3e03058e6b276b66affd1529d684882db3cc3a13 Author: Lukas Krejci lkrejci@redhat.com Date: Fri Nov 26 14:48:32 2010 +0100
A cherry-pick and adaptation of the original fix ae99b5bc0bf42909308a9d1efc09cee77d06ffc1 in release-3.0.0 branch:
a couple of robustness enhancements to the apache plugin: BZ 656449 - use the matching algorithm between SNMP values and augeas nodes also when matching the vhost node by resource key, because resource key in RHQ 3 is based on the SNMP value. BZ 652247 - Log the SNMP errors only on DEBUG level during discovery because SNMP isn't required for it to work. BZ 656476 - Do not fail the discovery if a non-existent directory is used in an Include directive BZ 652247, BZ 656491 - Do not choke on invalid/unresolvable hostnames in VirtualHost or ServerName directives
diff --git a/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheVirtualHostServiceComponent.java b/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheVirtualHostServiceComponent.java index 9b5c975..a1365da 100644 --- a/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheVirtualHostServiceComponent.java +++ b/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheVirtualHostServiceComponent.java @@ -21,8 +21,6 @@ package org.rhq.plugins.apache; import java.io.File; import java.net.InetAddress; import java.net.MalformedURLException; -import java.net.URI; -import java.net.URISyntaxException; import java.net.URL; import java.net.UnknownHostException; import java.util.ArrayList; @@ -338,13 +336,16 @@ public class ApacheVirtualHostServiceComponent implements ResourceComponent<Apac
String serverName = null; int pipeIdx = resourceKey.indexOf('|'); - if (pipeIdx >= 0) { + //the resource key always contains the '|' so we're only checking for non-empty + //server names + if (pipeIdx > 0) { serverName = resourceKey.substring(0, pipeIdx); }
String[] addrs = resourceKey.substring(pipeIdx + 1).split(" "); List<AugeasNode> nodes = tree.matchRelative(tree.getRootNode(), "<VirtualHost"); List<AugeasNode> virtualHosts = new ArrayList<AugeasNode>(); + boolean matching = false;
for (AugeasNode node : nodes) { diff --git a/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheVirtualHostServiceDiscoveryComponent.java b/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheVirtualHostServiceDiscoveryComponent.java index 6f24ca7..1b00e9d 100644 --- a/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheVirtualHostServiceDiscoveryComponent.java +++ b/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheVirtualHostServiceDiscoveryComponent.java @@ -86,6 +86,7 @@ public class ApacheVirtualHostServiceDiscoveryComponent implements ResourceDisco String firstAddress = vhost.hosts.get(0);
String resourceKey = createResourceKey(vhost.serverName, vhost.hosts); + String resourceName = resourceKey; //this'll get overridden below if we find a better value using the address variable
Configuration pluginConfiguration = context.getDefaultPluginConfiguration();
@@ -94,7 +95,7 @@ public class ApacheVirtualHostServiceDiscoveryComponent implements ResourceDisco String scheme = address.scheme; String hostToPing = address.host; int portToPing = address.port; - if (address.isPortWildcard()) { + if (address.isPortWildcard() || !address.isPortDefined()) { Address serverAddress = serverComponent.getAddressUtility().getMainServerSampleAddress(tree, hostToPing, 0); if (serverAddress != null) { portToPing = serverAddress.port; @@ -122,21 +123,14 @@ public class ApacheVirtualHostServiceDiscoveryComponent implements ResourceDisco PropertySimple urlProp = new PropertySimple(ApacheVirtualHostServiceComponent.URL_CONFIG_PROP, url); pluginConfiguration.put(urlProp);
- } - - if (address != null) { File rtLogFile = new File(logsDir, address.host + address.port + RT_LOG_FILE_NAME_SUFFIX); - + PropertySimple rtLogProp = new PropertySimple( ApacheVirtualHostServiceComponent.RESPONSE_TIME_LOG_FILE_CONFIG_PROP, rtLogFile.toString()); pluginConfiguration.put(rtLogProp); - } - - String resourceName; - if (address != null) { - resourceName = address.host + ":" + address.port; - } else { - resourceName = resourceKey; + + //redefine the resourcename using the virtual host sample address + resourceName = address.toString(false); }
discoveredResources.add(new DiscoveredResourceDetails(resourceType, resourceKey, resourceName, null, null, @@ -323,7 +317,7 @@ public class ApacheVirtualHostServiceDiscoveryComponent implements ResourceDisco
return ret; } catch (Exception e) { - log.warn("Error while trying to contact SNMP of the apache server " + serverResourceKey, e); + log.debug("Error while trying to contact SNMP of the apache server " + serverResourceKey, e); return null; } } diff --git a/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/AugeasConfigurationApache.java b/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/AugeasConfigurationApache.java index 3e51757..d4f7dd2 100644 --- a/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/AugeasConfigurationApache.java +++ b/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/AugeasConfigurationApache.java @@ -31,10 +31,11 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern;
+import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.rhq.augeas.config.AugeasModuleConfig; import org.rhq.augeas.util.Glob; import org.rhq.core.domain.configuration.Configuration; -import org.rhq.plugins.apache.ApachePluginLifecycleListener; import org.rhq.plugins.apache.ApacheServerComponent; import org.rhq.rhqtransform.AugeasRhqException; import org.rhq.rhqtransform.impl.PluginDescriptorBasedAugeasConfiguration; @@ -59,6 +60,8 @@ public class AugeasConfigurationApache extends PluginDescriptorBasedAugeasConfig private final Pattern includePattern = Pattern.compile(INCLUDE_FILES_PATTERN); private final Pattern serverRootPattern = Pattern.compile(SERVER_ROOT_PATTERN);
+ private static final Log LOG = LogFactory.getLog(AugeasConfigurationApache.class); + private String serverRootPath; private AugeasModuleConfig module; private List<File> allConfigFiles; @@ -109,7 +112,7 @@ public class AugeasConfigurationApache extends PluginDescriptorBasedAugeasConfig files.addAll(Glob.match(root, expression));
for (File fl : files){ - if (fl.exists()) { + if (fl.exists() && fl.isFile()) { foundIncludes.add(fl.getAbsolutePath());
FileInputStream fstream = new FileInputStream(fl); @@ -162,18 +165,23 @@ public class AugeasConfigurationApache extends PluginDescriptorBasedAugeasConfig
for (File configFile : files) { if (!configFile.isAbsolute()) { - throw new IllegalStateException( - "Configuration files inclusion patterns contain a non-absolute file."); + LOG.warn("Configuration files inclusion patterns contain a non-absolute file: " + configFile); + continue; } + if (!configFile.exists()) { - throw new IllegalStateException( - "Configuration files inclusion patterns refer to a non-existent file."); + LOG.warn("Configuration files inclusion patterns refer to a non-existent file: " + configFile); + continue; } + if (configFile.isDirectory()) { - throw new IllegalStateException("Configuration files inclusion patterns refer to a directory."); + LOG.warn("Configuration files inclusion patterns refer to a directory: " + configFile); + continue; } - if (!module.getConfigFiles().contains(configFile.getAbsolutePath())) + + if (!module.getConfigFiles().contains(configFile.getAbsolutePath())) { module.addConfigFile(configFile.getAbsolutePath()); + } } } } diff --git a/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/parser/ApacheParserImpl.java b/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/parser/ApacheParserImpl.java index f33f8fd..686625e 100644 --- a/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/parser/ApacheParserImpl.java +++ b/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/parser/ApacheParserImpl.java @@ -24,7 +24,9 @@ public class ApacheParserImpl implements ApacheParser{ if (directive.getName().equals(INCLUDE_DIRECTIVE)){ List<File> files = getIncludeFiles(directive.getValuesAsString()); for (File fl : files){ - ApacheConfigReader.searchFile(fl.getAbsolutePath(), this); + if (fl.exists() && fl.isFile()) { + ApacheConfigReader.searchFile(fl.getAbsolutePath(), this); + } } } directive.setParentNode(stack.getLastDirective()); diff --git a/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/util/HttpdAddressUtility.java b/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/util/HttpdAddressUtility.java index b5cdedf..0710178 100644 --- a/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/util/HttpdAddressUtility.java +++ b/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/util/HttpdAddressUtility.java @@ -82,7 +82,7 @@ public enum HttpdAddressUtility { address = getLocalhost(address.port); }
- updateWithServerName(address, ag, false); + updateWithServerName(address, ag);
return address; } @@ -106,7 +106,7 @@ public enum HttpdAddressUtility { addr = getLocalhost(addr.port); }
- updateWithServerName(addr, ag, false); + updateWithServerName(addr, ag);
return addr; } @@ -217,21 +217,68 @@ public enum HttpdAddressUtility {
Address o = (Address) other;
- if (this.host == null) { - return o.host == null && this.port == o.port; - } else { - return this.host.equals(o.host) && this.port == o.port; + return safeEquals(host, o.host) && this.port == o.port; + } + + /** + * This differs from equals in the way that it considers wildcard values: + * <ul> + * <li>wildcard host matches any host + * <li>default host matches default host + * <li>wildcard port matches any port + * <li>undefined port matches undefined port + * </ul> + * The addresses match if both address and port match. + * + * @param other the address to match + * @param whether to match the scheme as well + * @return true if the addresses match according to the rules described above, false otherwise + */ + public boolean matches(Address other, boolean matchSchemes) { + if (matchSchemes && !safeEquals(scheme, other.scheme)) { + return false; + } + + if (!WILDCARD.equals(host) && !WILDCARD.equals(other.host) && !safeEquals(host, other.host)) { + return false; } + + if (PORT_WILDCARD_VALUE != port && PORT_WILDCARD_VALUE != other.port && port != other.port) { + return false; + } + + return true; }
@Override public String toString() { - if (port == NO_PORT_SPECIFIED_VALUE) return scheme + "://" + host; - else { - String portSpec = port == PORT_WILDCARD_VALUE ? WILDCARD : String.valueOf(port); + return toString(true); + } + + public String toString(boolean includeScheme) { + StringBuilder bld = new StringBuilder(); + + if (includeScheme) { + bld.append(scheme).append("://"); + } + + bld.append(host); + + if (port != NO_PORT_SPECIFIED_VALUE) { + bld.append(":");
- return scheme + "://" + host + ":" + portSpec; + if (port == PORT_WILDCARD_VALUE) { + bld.append(WILDCARD); + } else { + bld.append(port); + } } + + return bld.toString(); + } + + private static boolean safeEquals(Object a, Object b) { + return a == null ? b == null : a.equals(b); } }
@@ -270,9 +317,9 @@ public enum HttpdAddressUtility { return null; addr.host = serverAddr.host; } - + if (serverName != null) { - updateWithServerName(addr, serverName, true); + updateWithServerName(addr, serverName); }
return addr; @@ -351,7 +398,7 @@ public enum HttpdAddressUtility { } }
- private static void updateWithServerName(Address address, ApacheDirectiveTree config, boolean updatePort) throws UnknownHostException { + private static void updateWithServerName(Address address, ApacheDirectiveTree config) throws UnknownHostException { //check if there is a ServerName directive List<ApacheDirective> serverNameNodes = config.search("/ServerName");
@@ -360,21 +407,46 @@ public enum HttpdAddressUtility { //be the case if the server listens on more than one interfaces. if (serverNameNodes.size() > 0) { String serverName = serverNameNodes.get(0).getValuesAsString(); - updateWithServerName(address, serverName, updatePort); + updateWithServerName(address, serverName); } }
- private static void updateWithServerName(Address address, String serverName, boolean updatePort) throws UnknownHostException { + private static void updateWithServerName(Address address, String serverName) throws UnknownHostException { + //the configuration may be invalid and/or the hostname can be unresolvable. + //we try to match the address with the servername first by IP address + //but if that fails (i.e. the hostname couldn't be resolved to an IP) + //we try to simply match the hostnames themselves. + Address serverAddr = Address.parse(serverName); - InetAddress addrFromServerName = InetAddress.getByName(serverAddr.host); - InetAddress addrFromAddress = InetAddress.getByName(address.host); + String ipFromServerName = null; + String ipFromAddress = null; + String hostFromServerName = null; + String hostFromAddress = null; + boolean lookupFailed = false; + + try { + InetAddress addrFromServerName = InetAddress.getByName(serverAddr.host); + ipFromServerName = addrFromServerName.getHostAddress(); + hostFromServerName = addrFromServerName.getHostName(); + } catch (UnknownHostException e) { + ipFromServerName = serverAddr.host; + hostFromServerName = serverAddr.host; + lookupFailed = true; + }
- if (addrFromAddress.equals(addrFromServerName)) { + try { + InetAddress addrFromAddress = InetAddress.getByName(address.host); + ipFromAddress = addrFromAddress.getHostAddress(); + hostFromAddress = addrFromAddress.getHostName(); + } catch (UnknownHostException e) { + ipFromAddress = address.host; + hostFromAddress = address.host; + lookupFailed = true; + } + + if (ipFromAddress.equals(ipFromServerName) || (lookupFailed && (hostFromAddress.equals(hostFromServerName)))) { address.scheme = serverAddr.scheme; address.host = serverAddr.host; - if (updatePort) { - address.port = serverAddr.port; - } } } } diff --git a/modules/plugins/augeas/src/main/java/org/rhq/augeas/util/Glob.java b/modules/plugins/augeas/src/main/java/org/rhq/augeas/util/Glob.java index fddf06e..d41d2a2 100644 --- a/modules/plugins/augeas/src/main/java/org/rhq/augeas/util/Glob.java +++ b/modules/plugins/augeas/src/main/java/org/rhq/augeas/util/Glob.java @@ -26,6 +26,7 @@ package org.rhq.augeas.util; import java.io.File; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Iterator; import java.util.List; import org.apache.commons.logging.Log; @@ -129,7 +130,8 @@ public class Glob { globPattern = new File(globPattern).getAbsolutePath(); File[] files = parentPath.listFiles(new GlobFilter(globPattern)); if (files == null) { - throw new IllegalStateException("Could not list files in " + parentPath); + log.debug("Could list files in " + parentPath); + return Collections.emptyList(); } return Arrays.asList(files); }
rhq-commits@lists.fedorahosted.org