modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheServerComponent.java
| 2
modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheServerDiscoveryComponent.java
| 2
modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheVirtualHostServiceComponent.java
| 29 -
modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheVirtualHostServiceDiscoveryComponent.java
| 34 +
modules/plugins/apache/src/main/java/org/rhq/plugins/apache/util/HttpdAddressUtility.java
| 281 ++++++----
modules/plugins/augeas/src/main/java/org/rhq/rhqtransform/impl/PluginDescriptorBasedAugeasConfiguration.java
| 2
6 files changed, 229 insertions(+), 121 deletions(-)
New commits:
commit 3617607ac9b93a68a790a8c465679cb74f9f11f1
Author: Lukas Krejci <lkrejci(a)redhat.com>
Date: Fri Jun 25 13:56:43 2010 +0200
BZ 601176 - Adding support for '*' as both host and port spec in Listen, and
VirtualHost directives.
Enhanced the detection of the address a server or vhost is reachable on, thus
improving the out-of-the-box availability detection.
diff --git
a/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheServerComponent.java
b/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheServerComponent.java
index 38ce9dc..0e98116 100644
---
a/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheServerComponent.java
+++
b/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheServerComponent.java
@@ -390,7 +390,7 @@ public class ApacheServerComponent implements
AugeasRHQComponent<PlatformCompone
ApacheParser parser = new
ApacheParserImpl(parserTree,getServerRoot().getAbsolutePath());
ApacheConfigReader.buildTree(getHttpdConfFile().getAbsolutePath(),
parser);
- addr = getAddressUtility().getVirtualHostSampleAddress(parserTree,
vhostDefs[0], serverName);
+ addr = getAddressUtility().getVirtualHostSampleAddress(parserTree,
vhostDefs[0], serverName, false);
} catch (Exception e) {
report.setStatus(CreateResourceStatus.FAILURE);
report.setErrorMessage("Wrong format of virtual host resource
name.");
diff --git
a/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheServerDiscoveryComponent.java
b/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheServerDiscoveryComponent.java
index 8f6deb7..edffb23 100644
---
a/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheServerDiscoveryComponent.java
+++
b/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheServerDiscoveryComponent.java
@@ -235,7 +235,7 @@ public class ApacheServerDiscoveryComponent implements
ResourceDiscoveryComponen
* @throws Exception
*/
private static String getUrl(ApacheDirectiveTree serverConfig, String version) throws
Exception {
- Address addr =
HttpdAddressUtility.get(version).getMainServerSampleAddress(serverConfig);
+ Address addr =
HttpdAddressUtility.get(version).getMainServerSampleAddress(serverConfig, null, 0);
return addr == null ? null : "http://" + addr.host + ":" +
addr.port + "/";
}
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 533ebd4..e44fef5 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
@@ -516,11 +516,17 @@ public class ApacheVirtualHostServiceComponent implements
ResourceComponent<Apac
ApacheServerComponent parent = resourceContext.getParentResourceComponent();
if (vhostAddressStrings.length == 1 &&
MAIN_SERVER_RESOURCE_KEY.equals(vhostAddressStrings[0])) {
-
vhostAddresses.add(parent.getAddressUtility().getMainServerSampleAddress(tree));
+ HttpdAddressUtility.Address serverAddr =
parent.getAddressUtility().getMainServerSampleAddress(tree, null, 0);
+ if (serverAddr != null) {
+ vhostAddresses.add(serverAddr);
+ }
} else {
for (int i = 0; i < vhostAddressStrings.length; ++i) {
-
vhostAddresses.add(parent.getAddressUtility().getVirtualHostSampleAddress(tree,
vhostAddressStrings[i],
- vhostServerName));
+ HttpdAddressUtility.Address vhostAddr =
parent.getAddressUtility().getVirtualHostSampleAddress(tree, vhostAddressStrings[i],
+ vhostServerName, true);
+ if (vhostAddr != null) {
+ vhostAddresses.add(vhostAddr);
+ }
}
}
@@ -540,7 +546,10 @@ public class ApacheVirtualHostServiceComponent implements
ResourceComponent<Apac
String fullPort = portValue.toString();
int snmpPort =
Integer.parseInt(fullPort.substring(fullPort.lastIndexOf(".") + 1));
- if (snmpPort == 0) snmpPort = 80;
+
+ //the snmp represent wildcard ports as 0 and so does
HttpdAddressUtility.Address class
+ //we can therefore match the Address generated from the snmp values and
the Addresses
+ //stored for this vhost directly.
HttpdAddressUtility.Address snmpAddress = new
HttpdAddressUtility.Address(snmpHost, snmpPort);
@@ -562,8 +571,10 @@ public class ApacheVirtualHostServiceComponent implements
ResourceComponent<Apac
}
private int matchRate(List<HttpdAddressUtility.Address> addresses,
HttpdAddressUtility.Address addressToCheck) throws UnknownHostException {
- if (addresses.contains(addressToCheck)) {
- return 3;
+ for(HttpdAddressUtility.Address a : addresses) {
+ if (HttpdAddressUtility.isAddressConforming(addressToCheck, a.host, a.port))
{
+ return 3;
+ }
}
//try to get the IP of the address to check
@@ -572,8 +583,10 @@ public class ApacheVirtualHostServiceComponent implements
ResourceComponent<Apac
for(InetAddress ip : ipAddresses) {
HttpdAddressUtility.Address newCheck = new
HttpdAddressUtility.Address(ip.getHostAddress(), addressToCheck.port);
- if (addresses.contains(newCheck)) {
- return 2;
+ for(HttpdAddressUtility.Address a : addresses) {
+ if (HttpdAddressUtility.isAddressConforming(newCheck, a.host, a.port)) {
+ return 2;
+ }
}
}
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 75e9f35..b308dd4 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
@@ -49,6 +49,8 @@ import org.rhq.plugins.apache.util.HttpdAddressUtility.Address;
*/
public class ApacheVirtualHostServiceDiscoveryComponent implements
ResourceDiscoveryComponent<ApacheServerComponent> {
+ private static final String COULD_NOT_DETERMINE_THE_VIRTUAL_HOST_ADDRESS = "***
Could not determine the virtual host address ***";
+
public static final String LOGS_DIRECTORY_NAME = "logs";
private static final String RT_LOG_FILE_NAME_SUFFIX = "_rt.log";
@@ -97,12 +99,38 @@ public class ApacheVirtualHostServiceDiscoveryComponent implements
ResourceDisco
Configuration pluginConfiguration = context.getDefaultPluginConfiguration();
- Address address =
serverComponent.getAddressUtility().getVirtualHostSampleAddress(tree, firstAddress,
serverName);
+ Address address =
serverComponent.getAddressUtility().getVirtualHostSampleAddress(tree, firstAddress,
serverName, false);
if (address != null) {
- String url = "http://" + address.host + ":" +
address.port + "/";
+ String hostToPing = address.host;
+ int portToPing = address.port;
+ if (address.isPortWildcard()) {
+ Address serverAddress =
serverComponent.getAddressUtility().getMainServerSampleAddress(tree, hostToPing, 0);
+ if (serverAddress != null) {
+ portToPing = serverAddress.port;
+ } else {
+ portToPing = Address.PORT_WILDCARD_VALUE;
+ }
+ }
+ if (address.isHostDefault() || address.isHostWildcard()) {
+ Address serverAddress =
serverComponent.getAddressUtility().getMainServerSampleAddress(tree, null, portToPing);
+
+ if (serverAddress != null) {
+ hostToPing = serverAddress.host;
+ } else {
+ hostToPing = null;
+ }
+ }
+
+ String url;
+ if (hostToPing != null && portToPing !=
Address.PORT_WILDCARD_VALUE && portToPing != Address.NO_PORT_SPECIFIED_VALUE) {
+ url = "http://" + hostToPing + ":" + portToPing +
"/";
+ } else {
+ url = COULD_NOT_DETERMINE_THE_VIRTUAL_HOST_ADDRESS;
+ }
PropertySimple urlProp = new
PropertySimple(ApacheVirtualHostServiceComponent.URL_CONFIG_PROP, url);
pluginConfiguration.put(urlProp);
+
}
File rtLogFile = new File(logsDir, address.host + address.port +
RT_LOG_FILE_NAME_SUFFIX);
@@ -113,7 +141,7 @@ public class ApacheVirtualHostServiceDiscoveryComponent implements
ResourceDisco
String resourceName;
if (serverName != null) {
- resourceName = address.host + ":" + address.port;
+ resourceName = address.toString();
} else {
resourceName = resourceKey;
}
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 1aea3b2..261c446 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
@@ -25,11 +25,11 @@ package org.rhq.plugins.apache.util;
import java.net.InetAddress;
import java.net.UnknownHostException;
+import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.rhq.augeas.node.AugeasNode;
import org.rhq.plugins.apache.parser.ApacheDirective;
import org.rhq.plugins.apache.parser.ApacheDirectiveTree;
@@ -41,71 +41,54 @@ import org.rhq.plugins.apache.parser.ApacheDirectiveTree;
public enum HttpdAddressUtility {
APACHE_1_3 {
- public Address getMainServerSampleAddress(ApacheDirectiveTree ag) {
+ public Address getMainServerSampleAddress(ApacheDirectiveTree ag, String
limitToHost, int limitToPort) {
try {
- Address addr = null;
+ List<ApacheDirective> ports = ag.search("/Port");
+ List<ApacheDirective> bindAddresses =
ag.search("/BindAddress");
+ List<ApacheDirective> listens = ag.search("/Listen");
- //check if there is a ServerName directive
- List<ApacheDirective> serverNameNodes =
ag.search("/ServerName");
-
- if (serverNameNodes.size() > 0) {
- String serverName = serverNameNodes.get(0).getValuesAsString();
- addr = Address.parse(serverName);
- } else {
- List<ApacheDirective> ports = ag.search("/Port");
- List<ApacheDirective> bindAddresses =
ag.search("/BindAddress");
- List<ApacheDirective> listens =
ag.search("/Listen");
-
- String port = null;
- String bindAddress = null;
- String listen = null;
-
- if (ports.size() > 0) {
- List<String>values = ports.get(0).getValues();
- if (values.size()>0)
- port = values.get(0);
- }
-
- if (bindAddresses.size() > 0) {
- List<String>values = bindAddresses.get(0).getValues();
- if (values.size()>0)
- bindAddress = values.get(0);
- }
-
- if (listens.size() > 0) {
- List<String>values = listens.get(0).getValues();
- if (values.size()>0)
- listen = values.get(0);
- }
-
- String host = null;
- int portToUse = -1;
-
- if (bindAddress != null &&
!"*".equals(bindAddress)) {
- host = bindAddress;
- }
-
- if (port != null) {
- portToUse = Integer.parseInt(port);
+ String port = "80"; //this is the default in apache 1.3
+ String bindAddress = null;
+
+ List<Address> addressesToMatch = new ArrayList<Address>();
+
+ if (ports.size() > 0) {
+ List<String>values = ports.get(0).getValues();
+ if (values.size()>0)
+ port = values.get(0);
+ }
+
+ if (bindAddresses.size() > 0) {
+ List<String>values = bindAddresses.get(0).getValues();
+ if (values.size()>0)
+ bindAddress = values.get(0);
+ }
+
+ //listen directives take precedence over port/bindaddress combo
+ if (listens.size() > 0) {
+ for(ApacheDirective l : listens) {
+ addressesToMatch.add(parseListen(l.getValues().get(0)));
}
-
- if (listen != null) {
- Address tmp = Address.parse(listen);
- host = tmp.host;
- portToUse = tmp.port;
- if (portToUse == -1) {
- host = null;
- portToUse = Integer.parseInt(tmp.host);
+ } else {
+ addressesToMatch.add(new Address(bindAddress,
Integer.parseInt(port)));
+ }
+
+ for (Address address : addressesToMatch) {
+ if (isAddressConforming(address, limitToHost, limitToPort)) {
+ if (!address.isPortDefined() || address.isPortWildcard()) {
+ address.port = 80;
}
+ if (address.host == null || address.isHostDefault() ||
address.isHostWildcard()) {
+ address = getLocalhost(address.port);
+ }
+
+ updateWithServerName(address, ag);
+
+ return address;
}
-
- if (host == null) {
- host = InetAddress.getLocalHost().getHostName();
- }
-
- addr = new Address(host, portToUse);
}
- return addr;
+
+ return null;
} catch (Exception e) {
log.info("Failed to obtain main server address. Is augeas installed
and correct lens in use?");
@@ -114,34 +97,23 @@ public enum HttpdAddressUtility {
}
},
APACHE_2_x {
- public Address getMainServerSampleAddress(ApacheDirectiveTree ag) {
+ public Address getMainServerSampleAddress(ApacheDirectiveTree ag, String
limitToHost, int limitToPort) {
try {
- Address addr = null;
-
- //check if there is a ServerName directive
- List<ApacheDirective> serverNameNodes =
ag.search("/ServerName");
-
- if (serverNameNodes.size() > 0) {
- String serverName = serverNameNodes.get(0).getValuesAsString();
- addr = Address.parse(serverName);
- } else {
- //there has to be at least one Listen directive
- ApacheDirective listen = ag.search("/Listen").get(0);
-
- String address = listen.getValues().get(0);
-
- addr = Address.parse(address);
-
- if (addr.port == -1) {
- addr.port = Integer.parseInt(addr.host);
- try {
- addr.host = InetAddress.getLocalHost().getHostName();
- } catch (UnknownHostException e) {
- throw new IllegalStateException("Unable to get the
localhost address.", e);
+ for(ApacheDirective n : ag.search("/Listen")) {
+ Address addr = parseListen(n.getValues().get(0));
+ if (isAddressConforming(addr, limitToHost, limitToPort)) {
+ if (addr.host == null || addr.isHostDefault() ||
addr.isHostWildcard()) {
+ addr = getLocalhost(addr.port);
}
+
+ updateWithServerName(addr, ag);
+
+ return addr;
}
}
- return addr;
+
+ //there has to be at least one Listen directive
+ throw new IllegalStateException("Could find a listen address on port
" + limitToPort);
} catch (Exception e) {
log.info("Failed to obtain main server address. Is augeas installed
and correct lens in use?");
@@ -159,7 +131,12 @@ public enum HttpdAddressUtility {
public static class Address {
public String host;
public int port = -1;
-
+
+ public static final String WILDCARD = "*";
+ public static final String DEFAULT_HOST = "_default_";
+ public static final int PORT_WILDCARD_VALUE = 0;
+ public static final int NO_PORT_SPECIFIED_VALUE = -1;
+
public Address(String host, int port) {
this.host = host;
this.port = port;
@@ -174,21 +151,45 @@ public enum HttpdAddressUtility {
*/
public static Address parse(String address) {
int lastColonIdx = address.lastIndexOf(':');
- if (lastColonIdx == -1) {
+ if (lastColonIdx == NO_PORT_SPECIFIED_VALUE) {
return new Address(address, -1);
} else {
int lastRightBracketPos = address.lastIndexOf(']');
if (lastColonIdx > lastRightBracketPos) {
String host = address.substring(0, lastColonIdx);
- int port = Integer.parseInt(address.substring(lastColonIdx + 1));
+ String portSpec = address.substring(lastColonIdx + 1);
+
+ int port = NO_PORT_SPECIFIED_VALUE;
+ if (WILDCARD.equals(portSpec)) {
+ port = PORT_WILDCARD_VALUE;
+ } else {
+ port = Integer.parseInt(portSpec);
+ }
+
return new Address(host, port);
} else {
//this is an IP6 address without a port spec
- return new Address(address, -1);
+ return new Address(address, NO_PORT_SPECIFIED_VALUE);
}
}
}
+ public boolean isPortWildcard() {
+ return port == PORT_WILDCARD_VALUE;
+ }
+
+ public boolean isPortDefined() {
+ return port != NO_PORT_SPECIFIED_VALUE;
+ }
+
+ public boolean isHostWildcard() {
+ return WILDCARD.equals(host);
+ }
+
+ public boolean isHostDefault() {
+ return DEFAULT_HOST.equals(host);
+ }
+
@Override
public int hashCode() {
int hash = port;
@@ -210,15 +211,28 @@ public enum HttpdAddressUtility {
return this.host.equals(o.host) && this.port == o.port;
}
}
+
+ @Override
+ public String toString() {
+ if (port == NO_PORT_SPECIFIED_VALUE) return host;
+ else {
+ String portSpec = port == PORT_WILDCARD_VALUE ? WILDCARD :
String.valueOf(port);
+
+ return host + ":" + portSpec;
+ }
+ }
}
/**
* This just constructs a first available address under which the server or one of
its virtual hosts can be reached.
*
* @param ag the tree of the httpd configuration
+ * @param limitToHost if non-null and different from {@link Address#DEFAULT_HOST} and
{@link Address#WILDCARD},
+ * the sample address is looked for only for the given host
+ * @param limitToPort if > 0, the sample address is looked for only for the given
port
* @return the address or null on failure
*/
- public abstract Address getMainServerSampleAddress(ApacheDirectiveTree ag);
+ public abstract Address getMainServerSampleAddress(ApacheDirectiveTree ag, String
limitToHost, int limitToPort);
/**
* This constructs an address on which given virtual host can be accessed.
@@ -226,29 +240,22 @@ public enum HttpdAddressUtility {
* @param ag the augeas tree of the httpd configuration
* @param virtualHost the port or address:port of the virtual host
* @param serverName the server name for the namebased virtual hosts (or null if the
virtual host is ip based)
- *
+ * @param snmpModuleCompatibleMode if true, generates a sample address in the same
way as snmp module. Namely
+ * deals with the host name wildcard the same way as snmp module (i.e. by assuming it
means "localhost", even though it doesn't have to).
* @return the address on which the virtual host can be accessed or null on error
*/
- public Address getVirtualHostSampleAddress(ApacheDirectiveTree ag, String
virtualHost, String serverName) {
+ public Address getVirtualHostSampleAddress(ApacheDirectiveTree ag, String
virtualHost, String serverName, boolean snmpModuleCompatibleMode) {
Address addr = Address.parse(virtualHost);
- if (addr.port == -1) {
- //just port specified
- Address serverAddr = getMainServerSampleAddress(ag);
-
+ if (addr.isHostDefault() || addr.isHostWildcard()) {
+ Address serverAddr = null;
+ if (snmpModuleCompatibleMode) {
+ serverAddr = getLocalhost(addr.port);
+ } else {
+ serverAddr = getMainServerSampleAddress(ag, null, addr.port);
+ }
if (serverAddr == null)
return null;
-
- addr.port = Integer.parseInt(addr.host);
addr.host = serverAddr.host;
- } else {
- String host = addr.host;
- if ("*".equals(host) || "_default_".equals(host)) {
- Address serverAddr = getMainServerSampleAddress(ag);
- if (serverAddr == null)
- return null;
- host = serverAddr.host;
- }
- addr.host = host;
}
if (serverName != null) {
@@ -259,9 +266,69 @@ public enum HttpdAddressUtility {
} else {
addr.host = serverName;
}
- addr.host = addr.host.replaceAll("\\*",
"replaced-wildcard");
}
return addr;
}
+
+ private static Address parseListen(String listenValue) {
+ Address ret = Address.parse(listenValue);
+ if (!ret.isPortDefined()) {
+ try {
+ ret.port = Integer.parseInt(ret.host);
+ } catch (NumberFormatException e) {
+ return null;
+ }
+ ret.host = null;
+ }
+
+ return ret;
+ }
+
+ public static boolean isAddressConforming(Address listen, String limitingHost, int
limitingPort) {
+ if (Address.DEFAULT_HOST.equals(limitingHost) ||
Address.WILDCARD.equals(limitingHost)) {
+ limitingHost = null;
+ }
+
+ boolean hostOk = limitingHost == null;
+ boolean portOk = limitingPort <= 0;
+
+ //listen.host == null means that server listens on all addresses
+ if (!hostOk && (listen.host == null || limitingHost.equals(listen.host)))
{
+ hostOk = true;
+ }
+
+ if (!portOk && limitingPort == listen.port) {
+ portOk = true;
+ }
+
+ return hostOk && portOk;
+ }
+
+ private static Address getLocalhost(int port) {
+ try {
+ return new Address(InetAddress.getLocalHost().getHostAddress(), port);
+ } catch (UnknownHostException e) {
+ //well, this is bad, we can get address of the localhost. let's use the
force...
+ return new Address("127.0.0.1", port);
+ }
+ }
+
+ private static void updateWithServerName(Address address, ApacheDirectiveTree config)
throws UnknownHostException {
+ //check if there is a ServerName directive
+ List<ApacheDirective> serverNameNodes =
config.search("/ServerName");
+
+ //if there is a ServerName directive, check that the address
+ //we're returning indeed corresponds to it. This might not
+ //be the case if the server listens on more than one interfaces.
+ if (serverNameNodes.size() > 0) {
+ String serverName = serverNameNodes.get(0).getValuesAsString();
+ InetAddress addrFromServerName = InetAddress.getByName(serverName);
+ InetAddress addrFromAddress = InetAddress.getByName(address.host);
+
+ if (addrFromAddress.equals(addrFromServerName)) {
+ address.host = serverName;
+ }
+ }
+ }
}
diff --git
a/modules/plugins/augeas/src/main/java/org/rhq/rhqtransform/impl/PluginDescriptorBasedAugeasConfiguration.java
b/modules/plugins/augeas/src/main/java/org/rhq/rhqtransform/impl/PluginDescriptorBasedAugeasConfiguration.java
index 8f3e470..cd15ef2 100644
---
a/modules/plugins/augeas/src/main/java/org/rhq/rhqtransform/impl/PluginDescriptorBasedAugeasConfiguration.java
+++
b/modules/plugins/augeas/src/main/java/org/rhq/rhqtransform/impl/PluginDescriptorBasedAugeasConfiguration.java
@@ -129,7 +129,7 @@ public class PluginDescriptorBasedAugeasConfiguration implements
AugeasConfigura
}
public static List<String> getGlobList(PropertySimple list) {
- if (list != null) {
+ if (list != null && list.getStringValue() != null) {
return Arrays.asList(list.getStringValue().split("\\s*\\|\\s*"));
} else {
return Collections.emptyList();