modules/core/plugin-api/src/main/java/org/rhq/core/pluginapi/upgrade/ResourceUpgradeContext.java | 12 modules/core/plugin-container/src/main/java/org/rhq/core/pc/inventory/InventoryManager.java | 3 modules/core/plugin-container/src/main/java/org/rhq/core/pc/upgrade/ResourceUpgradeDelegate.java | 7 modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheServerComponent.java | 7 modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheServerDiscoveryComponent.java | 123 ++++ modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheVirtualHostServiceComponent.java | 56 +- modules/plugins/apache/src/main/java/org/rhq/plugins/apache/ApacheVirtualHostServiceDiscoveryComponent.java | 260 ++++++---- modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/ApacheAugeasNode.java | 12 modules/plugins/apache/src/main/java/org/rhq/plugins/apache/util/HttpdAddressUtility.java | 90 ++- 9 files changed, 390 insertions(+), 180 deletions(-)
New commits: commit cec52a70ca813ed0138ab1b2f59efc364953b393 Author: Lukas Krejci lkrejci@redhat.com Date: Sat Oct 16 01:15:18 2010 +0200
Removing the code dealing with the legacy resource keys and implement a ResourceUpgradeFacet for virtual hosts so that we can finally move away from the insufficient legacy resource keys.
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 a0f2120..9b5c975 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 @@ -345,79 +345,46 @@ public class ApacheVirtualHostServiceComponent implements ResourceComponent<Apac String[] addrs = resourceKey.substring(pipeIdx + 1).split(" "); List<AugeasNode> nodes = tree.matchRelative(tree.getRootNode(), "<VirtualHost"); List<AugeasNode> virtualHosts = new ArrayList<AugeasNode>(); - boolean updated = false; - -//BZ 612189 - uncomment this algo once the resource upgrade is in place -// for (AugeasNode node : nodes) { -// updated = false; -// List<AugeasNode> serverNameNodes = tree.matchRelative(node, "ServerName/param"); -// String tempServerName = null; -// -// if (!(serverNameNodes.isEmpty())) { -// tempServerName = serverNameNodes.get(0).getValue(); -// } -// if (tempServerName == null & serverName == null) -// updated = true; -// if (tempServerName != null & serverName != null) -// if (tempServerName.equals(serverName)){ -// updated = true; -// } -// -// -// if (updated){ -// updated = false; -// List<AugeasNode> params = node.getChildByLabel("param"); -// for (AugeasNode nd : params) { -// updated = false; -// for (String adr : addrs) { -// if (adr.equals(nd.getValue())) -// updated = true; -// } -// if (!updated) -// break; -// } -// -// if (updated) -// virtualHosts.add(node); -// } -// } - - //BZ 612189 - remove this once resource upgrade is in place - HttpdAddressUtility.Address resourceKeyAddress = HttpdAddressUtility.Address.parse(resourceKey); - for(AugeasNode node : nodes) { + boolean matching = false; + + for (AugeasNode node : nodes) { + matching = false; List<AugeasNode> serverNameNodes = tree.matchRelative(node, "ServerName/param"); - List<AugeasNode> vhostAddressNodes = node.getChildByLabel("param"); + String tempServerName = null; + + if (!(serverNameNodes.isEmpty())) { + tempServerName = serverNameNodes.get(0).getValue(); + } + if (tempServerName == null & serverName == null) { + matching = true; + }
- String vhostServerName = serverNameNodes.isEmpty() ? null : serverNameNodes.get(0).getValue(); - String vhostAddressDef = vhostAddressNodes.isEmpty() ? null : vhostAddressNodes.get(0).getValue(); + if (tempServerName != null & serverName != null) { + if (tempServerName.equals(serverName)) { + matching = true; + } + }
- if (vhostAddressDef != null) { - HttpdAddressUtility.Address vhostAddress = HttpdAddressUtility.Address.parse(vhostAddressDef); - if (vhostServerName != null) { - HttpdAddressUtility.Address vhostServerAddress = HttpdAddressUtility.Address.parse(vhostServerName); - vhostAddress.host = vhostServerAddress.host; + if (matching) { + List<AugeasNode> params = node.getChildByLabel("param"); + for (AugeasNode nd : params) { + matching = false; + for (String adr : addrs) { + if (adr.equals(nd.getValue())) { + matching = true; + } + } + if (!matching) { + break; + } } - - if (resourceKeyAddress.equals(vhostAddress)) { + + if (matching) { virtualHosts.add(node); } } } - - //BZ 612189 - remove this once we have resource upgrade - //ok, one final attempt... the legacy resource key format for the MainServer is just a host:port as with the rest of the vhosts, let's try that - try { - String serverUrl = resourceContext.getParentResourceComponent().getServerUrl(); - URI serverUri = new URI(serverUrl); - String expectedResourceKey = serverUri.getHost() + ":" + serverUri.getPort(); - - if (expectedResourceKey.equals(resourceKey)) { - return tree.getRootNode(); - } - } catch (URISyntaxException e) { - log.warn("Failed to parse the server URL when trying to match the vhost with the main server.", e); - } - + if (virtualHosts.size() == 0) { throw new IllegalStateException("Could not find virtual host configuration in augeas for virtual host: " + resourceKey); @@ -544,14 +511,6 @@ public class ApacheVirtualHostServiceComponent implements ResourceComponent<Apac vhostServerName, true); if (vhostAddr != null) { vhostAddresses.add(vhostAddr); - } else { - //this is not to choke on the old style resource keys for the main server. without this, we'd never be able - //to match the main server with its snmp index below. - HttpdAddressUtility.Address addr = HttpdAddressUtility.Address.parse(vhostAddressStrings[i]); - vhostAddr = parent.getAddressUtility().getMainServerSampleAddress(tree, addr.host, addr.port); - if (vhostAddr != null) { - vhostAddresses.add(vhostAddr); - } } } } @@ -618,7 +577,7 @@ public class ApacheVirtualHostServiceComponent implements ResourceComponent<Apac return snmpWwwServiceIndex; }
- private static int matchRate(List<HttpdAddressUtility.Address> addresses, HttpdAddressUtility.Address addressToCheck) { + public static int matchRate(List<HttpdAddressUtility.Address> addresses, HttpdAddressUtility.Address addressToCheck) { for(HttpdAddressUtility.Address a : addresses) { if (HttpdAddressUtility.isAddressConforming(addressToCheck, a.host, a.port, true)) { return 3; 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 8251658..6f24ca7 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 @@ -21,23 +21,25 @@ package org.rhq.plugins.apache; import java.io.File; import java.net.InetAddress; import java.net.URI; -import java.net.URISyntaxException; import java.net.UnknownHostException; -import java.util.Iterator; +import java.util.ArrayList; +import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; import java.util.Set;
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.rhq.augeas.node.AugeasNode; import org.rhq.core.domain.configuration.Configuration; import org.rhq.core.domain.configuration.PropertySimple; import org.rhq.core.domain.resource.ResourceType; +import org.rhq.core.domain.resource.ResourceUpgradeReport; import org.rhq.core.pluginapi.inventory.DiscoveredResourceDetails; import org.rhq.core.pluginapi.inventory.InvalidPluginConfigurationException; import org.rhq.core.pluginapi.inventory.ResourceDiscoveryComponent; import org.rhq.core.pluginapi.inventory.ResourceDiscoveryContext; +import org.rhq.core.pluginapi.upgrade.ResourceUpgradeContext; +import org.rhq.core.pluginapi.upgrade.ResourceUpgradeFacet; import org.rhq.plugins.apache.parser.ApacheDirective; import org.rhq.plugins.apache.parser.ApacheDirectiveTree; import org.rhq.plugins.apache.util.HttpdAddressUtility; @@ -54,7 +56,7 @@ import org.rhq.plugins.www.snmp.SNMPValue; * @author Ian Springer * @author Lukas Krejci */ -public class ApacheVirtualHostServiceDiscoveryComponent implements ResourceDiscoveryComponent<ApacheServerComponent> { +public class ApacheVirtualHostServiceDiscoveryComponent implements ResourceDiscoveryComponent<ApacheServerComponent>, ResourceUpgradeFacet<ApacheServerComponent> {
private static final String COULD_NOT_DETERMINE_THE_VIRTUAL_HOST_ADDRESS = "*** Could not determine the virtual host address ***";
@@ -69,37 +71,25 @@ public class ApacheVirtualHostServiceDiscoveryComponent implements ResourceDisco
Set<DiscoveredResourceDetails> discoveredResources = new LinkedHashSet<DiscoveredResourceDetails>();
- //BZ 612189 - prepare for the legacy overrides. We need to revert to the old-style resource keys until - //resource upgrade functionality is ready. - SnmpWwwServiceIndexes snmpDiscoveries = getSnmpDiscoveries(context); - ApacheServerComponent serverComponent = context.getParentResourceComponent(); ApacheDirectiveTree tree = serverComponent.loadParser(); //first define the root server as one virtual host - discoverMainServer(context, discoveredResources, snmpDiscoveries); + discoverMainServer(context, discoveredResources);
ResourceType resourceType = context.getResourceType();
File configPath = serverComponent.getServerRoot(); File logsDir = new File(configPath, LOGS_DIRECTORY_NAME);
- List<ApacheDirective> virtualHosts = tree.search("/<VirtualHost"); - - for (ApacheDirective node : virtualHosts) { - List<String> hosts = node.getValues(); - String firstAddress = hosts.get(0); - - List<ApacheDirective> serverNames = node.getChildByName("ServerName"); - String serverName = null; - if (serverNames.size() > 0) { - serverName = serverNames.get(0).getValuesAsString(); - } + for(VHostSpec vhost : VHostSpec.detect(tree)) { + + String firstAddress = vhost.hosts.get(0);
- String resourceKey = createResourceKey(serverName, hosts); + String resourceKey = createResourceKey(vhost.serverName, vhost.hosts);
Configuration pluginConfiguration = context.getDefaultPluginConfiguration();
- Address address = serverComponent.getAddressUtility().getVirtualHostSampleAddress(tree, firstAddress, serverName, false); + Address address = serverComponent.getAddressUtility().getVirtualHostSampleAddress(tree, firstAddress, vhost.serverName, false); if (address != null) { String scheme = address.scheme; String hostToPing = address.host; @@ -143,17 +133,11 @@ public class ApacheVirtualHostServiceDiscoveryComponent implements ResourceDisco }
String resourceName; - if (serverName != null) { + if (address != null) { resourceName = address.host + ":" + address.port; } else { resourceName = resourceKey; } - - //BZ 612189 - remove this once we have resource upgrade - if (snmpDiscoveries != null) { - String legacyResourceKey = getLegacyResourceKey(context, resourceKey, snmpDiscoveries); - resourceKey = legacyResourceKey != null ? legacyResourceKey : resourceKey; - }
discoveredResources.add(new DiscoveredResourceDetails(resourceType, resourceKey, resourceName, null, null, pluginConfiguration, null)); @@ -162,9 +146,56 @@ public class ApacheVirtualHostServiceDiscoveryComponent implements ResourceDisco return discoveredResources; }
+ public ResourceUpgradeReport upgrade(ResourceUpgradeContext<ApacheServerComponent> inventoriedResource) { + String resourceKey = inventoriedResource.getResourceKey(); + + if (ApacheVirtualHostServiceComponent.MAIN_SERVER_RESOURCE_KEY.equals(resourceKey) || + resourceKey.contains("|")) { + //a new style resource key. we're done. + return null; + } + + String newResourceKey = null; + + ApacheServerComponent serverComponent = inventoriedResource.getParentResourceComponent(); + + ApacheDirectiveTree tree = serverComponent.loadParser(); + + List<VHostSpec> vhosts = VHostSpec.detect(tree);
+ SnmpWwwServiceIndexes snmpIndexes = getSnmpDiscoveries(serverComponent, inventoriedResource.getParentResourceContext().getResourceKey()); + + for (VHostSpec vhost : vhosts) { + String legacyResourceKey = createLegacyResourceKey(serverComponent, vhost.serverName, vhost.hosts, snmpIndexes); + if (resourceKey.equals(legacyResourceKey)) { + newResourceKey = createResourceKey(vhost.serverName, vhost.hosts); + break; + } + } + + if (newResourceKey == null) { + //the last thing to check is whether the inventoried vhost isn't in fact the main server + String serverUrl = serverComponent.getServerUrl(); + HttpdAddressUtility.Address serverAddress = HttpdAddressUtility.Address.parse(serverUrl); + HttpdAddressUtility.Address vhostAddress = HttpdAddressUtility.Address.parse(resourceKey); + + if (ApacheVirtualHostServiceComponent.matchRate(Collections.singletonList(serverAddress), vhostAddress) > 0) { + newResourceKey = ApacheVirtualHostServiceComponent.MAIN_SERVER_RESOURCE_KEY; + } + } + + if (newResourceKey != null) { + ResourceUpgradeReport report = new ResourceUpgradeReport(); + report.setNewResourceKey(newResourceKey); + + return report; + } else { + return null; + } + } + private void discoverMainServer(ResourceDiscoveryContext<ApacheServerComponent> context, - Set<DiscoveredResourceDetails> discoveredResources, SnmpWwwServiceIndexes snmpDiscoveries) throws Exception { + Set<DiscoveredResourceDetails> discoveredResources) throws Exception {
ResourceType resourceType = context.getResourceType(); Configuration mainServerPluginConfig = context.getDefaultPluginConfiguration(); @@ -175,8 +206,6 @@ public class ApacheVirtualHostServiceDiscoveryComponent implements ResourceDisco String mainServerUrl = context.getParentResourceContext().getPluginConfiguration().getSimple( ApacheServerComponent.PLUGIN_CONFIG_PROP_URL).getStringValue();
- String key = null; - if (mainServerUrl != null && !"null".equals(mainServerUrl)) { PropertySimple mainServerUrlProp = new PropertySimple(ApacheVirtualHostServiceComponent.URL_CONFIG_PROP, mainServerUrl); @@ -195,21 +224,9 @@ public class ApacheVirtualHostServiceDiscoveryComponent implements ResourceDisco PropertySimple rtLogProp = new PropertySimple( ApacheVirtualHostServiceComponent.RESPONSE_TIME_LOG_FILE_CONFIG_PROP, rtLogFile.toString()); mainServerPluginConfig.put(rtLogProp); - - //BZ 612189 - remove this once we have resource upgrade - key = host + ":" + port; }
- //BZ 612189 - this can simply the MAIN_SERVER_RESOURCE_KEY only once we have resource upgrade - if (key == null) { - key = ApacheVirtualHostServiceComponent.MAIN_SERVER_RESOURCE_KEY; - } - - //BZ 612189 - remove this once we have resource upgrade - if (snmpDiscoveries != null) { - String legacyKey = getLegacyResourceKey(context, key, snmpDiscoveries); - key = legacyKey != null ? legacyKey : key; - } + String key = ApacheVirtualHostServiceComponent.MAIN_SERVER_RESOURCE_KEY;
DiscoveredResourceDetails mainServer = new DiscoveredResourceDetails(resourceType, key, "Main", null, null, @@ -217,45 +234,43 @@ public class ApacheVirtualHostServiceDiscoveryComponent implements ResourceDisco discoveredResources.add(mainServer); }
- /** - * @deprecated remove this once we have resource upgrade - * @param discoveryContext - * @param newStyleResourceKey - * @param snmpDiscoveries - * @return - */ - @Deprecated - private String getLegacyResourceKey(ResourceDiscoveryContext<ApacheServerComponent> discoveryContext, String newStyleResourceKey, SnmpWwwServiceIndexes snmpDiscoveries) { - int snmpWwwServiceIndex = ApacheVirtualHostServiceComponent.getMatchingWwwServiceIndex(discoveryContext.getParentResourceComponent(), newStyleResourceKey, snmpDiscoveries.names, snmpDiscoveries.ports); - - if (snmpWwwServiceIndex < 1) { - return null; - } else { - String host = snmpDiscoveries.names.get(snmpWwwServiceIndex - 1).toString(); - String fullPort = snmpDiscoveries.ports.get(snmpWwwServiceIndex - 1).toString(); - - // The port value will be in the form "1.3.6.1.2.1.6.XXXXX", - // where "1.3.6.1.2.1.6" represents the TCP protocol ID, - // and XXXXX is the actual port number - String port = fullPort.substring(fullPort.lastIndexOf(".") + 1); - return host + ":" + port; + public static String createResourceKey(String serverName, List<String> hosts) { + StringBuilder keyBuilder = new StringBuilder(); + if (serverName != null) { + keyBuilder.append(serverName); + } + keyBuilder.append("|"); //always do this so that we have a clear distinction between old and new style resource keys + keyBuilder.append(hosts.get(0)); + + for (int i = 1; i < hosts.size(); ++i){ + keyBuilder.append(" ").append(hosts.get(i)); } + + return keyBuilder.toString(); }
- public static String createResourceKey(String serverName, List<String> hosts) { -//BZ 612189 - swap the impls once resource upgrade is in place -// StringBuilder keyBuilder = new StringBuilder(); -// if (serverName != null) { -// keyBuilder.append(serverName).append("|"); -// } -// keyBuilder.append(hosts.get(0)); -// -// -// for (int i = 1; i < hosts.size(); ++i){ -// keyBuilder.append(" ").append(hosts.get(i)); -// } -// -// return keyBuilder.toString(); + private static String createLegacyResourceKey(ApacheServerComponent serverComponent, String serverName, List<String> hosts, SnmpWwwServiceIndexes snmpIndexes) { + if (snmpIndexes != null) { + String newStyleResourceKey = createResourceKey(serverName, hosts); + String legacyResourceKey = null; + + int snmpWwwServiceIndex = ApacheVirtualHostServiceComponent.getMatchingWwwServiceIndex(serverComponent, newStyleResourceKey, snmpIndexes.names, snmpIndexes.ports); + + if (snmpWwwServiceIndex > 0) { + String host = snmpIndexes.names.get(snmpWwwServiceIndex - 1).toString(); + String fullPort = snmpIndexes.ports.get(snmpWwwServiceIndex - 1).toString(); + + // The port value will be in the form "1.3.6.1.2.1.6.XXXXX", + // where "1.3.6.1.2.1.6" represents the TCP protocol ID, + // and XXXXX is the actual port number + String port = fullPort.substring(fullPort.lastIndexOf(".") + 1); + legacyResourceKey = host + ":" + port; + } + + if (legacyResourceKey != null) { + return legacyResourceKey; + } + }
//try to derive the same resource key as the SNMP would have... this is to prevent the duplication of //vhost resources after the SNMP was configured - how I wish resource upgrade made it to 3.0 to prevent this @@ -279,18 +294,15 @@ public class ApacheVirtualHostServiceDiscoveryComponent implements ResourceDisco }
/** - * @deprecated remove this once we have resource upgrade * @param discoveryContext * @return */ - @Deprecated - private SnmpWwwServiceIndexes getSnmpDiscoveries(ResourceDiscoveryContext<ApacheServerComponent> discoveryContext) { + private static SnmpWwwServiceIndexes getSnmpDiscoveries(ApacheServerComponent serverComponent, String serverResourceKey) { try { - SNMPSession snmpSession = discoveryContext.getParentResourceComponent().getSNMPSession(); + SNMPSession snmpSession = serverComponent.getSNMPSession(); List<SNMPValue> nameValues; List<SNMPValue> portValues; - SNMPValue descValue; - + try { nameValues = snmpSession.getColumn(SNMPConstants.COLUMN_VHOST_NAME); } catch (SNMPException e) { @@ -305,35 +317,46 @@ public class ApacheVirtualHostServiceDiscoveryComponent implements ResourceDisco "Error getting SNMP column: " + SNMPConstants.COLUMN_VHOST_PORT + ": " + e.getMessage(), e); }
- try { - // Just get the first one - they are all the same. - descValue = snmpSession.getNextValue(SNMPConstants.COLUMN_VHOST_DESC); - } catch (SNMPException e) { - throw new Exception("Error getting SNMP value: " + SNMPConstants.COLUMN_VHOST_DESC + ": " + e.getMessage(), - e); - } - SnmpWwwServiceIndexes ret = new SnmpWwwServiceIndexes(); ret.names = nameValues; ret.ports = portValues; - ret.desc = descValue;
return ret; } catch (Exception e) { - log.warn("Error while trying to contact SNMP of the apache server " + discoveryContext.getParentResourceContext().getResourceKey(), e); + log.warn("Error while trying to contact SNMP of the apache server " + serverResourceKey, e); return null; } }
- /** - * @deprecated remove this once we have resource upgrade - * - * @author Lukas Krejci - */ - @Deprecated private static class SnmpWwwServiceIndexes { public List<SNMPValue> names; public List<SNMPValue> ports; - public SNMPValue desc; + } + + private static class VHostSpec { + public String serverName; + public List<String> hosts; + + public static List<VHostSpec> detect(ApacheDirectiveTree config) { + List<ApacheDirective> virtualHosts = config.search("/<VirtualHost"); + + List<VHostSpec> ret = new ArrayList<VHostSpec>(virtualHosts.size()); + + for(ApacheDirective dir : virtualHosts) { + ret.add(new VHostSpec(dir)); + } + + return ret; + } + + public VHostSpec(ApacheDirective vhostDirective) { + hosts = vhostDirective.getValues(); + + List<ApacheDirective> serverNames = vhostDirective.getChildByName("ServerName"); + serverName = null; + if (serverNames.size() > 0) { + serverName = serverNames.get(0).getValuesAsString(); + } + } } } \ No newline at end of file
commit dd1e15c2f1b564b0f547508fbc472a0f97ec8cc6 Author: Lukas Krejci lkrejci@redhat.com Date: Sat Oct 16 01:13:17 2010 +0200
Adding the ResourceContext of the parent resource to the ResourceUpgradeContext so that upgrade() methods of the discovery components can use that information.
diff --git a/modules/core/plugin-api/src/main/java/org/rhq/core/pluginapi/upgrade/ResourceUpgradeContext.java b/modules/core/plugin-api/src/main/java/org/rhq/core/pluginapi/upgrade/ResourceUpgradeContext.java index 35f5029..c56d280 100644 --- a/modules/core/plugin-api/src/main/java/org/rhq/core/pluginapi/upgrade/ResourceUpgradeContext.java +++ b/modules/core/plugin-api/src/main/java/org/rhq/core/pluginapi/upgrade/ResourceUpgradeContext.java @@ -44,17 +44,18 @@ import org.rhq.core.system.SystemInfo; * * @author Lukas Krejci */ -@SuppressWarnings("unchecked") +@SuppressWarnings("rawtypes") public class ResourceUpgradeContext<T extends ResourceComponent> extends ResourceContext<T> {
private final Configuration resourceConfiguration; private final String name; private final String description; + private ResourceContext<?> parentResourceContext;
/** * @see ResourceContext#ResourceContext(Resource, ResourceComponent, ResourceDiscoveryComponent, SystemInfo, File, File, String, EventContext, OperationContext, ContentContext, Executor, PluginContainerDeployment) */ - public ResourceUpgradeContext(Resource resource, T parentResourceComponent, + public ResourceUpgradeContext(Resource resource, ResourceContext<?> parentResourceContext, T parentResourceComponent, ResourceDiscoveryComponent resourceDiscoveryComponent, SystemInfo systemInfo, File temporaryDirectory, File dataDirectory, String pluginContainerName, EventContext eventContext, OperationContext operationContext, ContentContext contentContext, Executor availCollectorThreadPool, @@ -64,15 +65,14 @@ public class ResourceUpgradeContext<T extends ResourceComponent> extends Resourc pluginContainerName, eventContext, operationContext, contentContext, availCollectorThreadPool, pluginContainerDeployment);
+ this.parentResourceContext = parentResourceContext; this.resourceConfiguration = resource.getResourceConfiguration(); this.name = resource.getName(); this.description = resource.getDescription(); }
- public ResourceUpgradeContext(Resource resource, ResourceDiscoveryComponent discoveryComponent, ResourceContext<T> context, Executor availCollectorThreadPool) { - this(resource, context.getParentResourceComponent(), discoveryComponent, context.getSystemInformation(), context.getTemporaryDirectory(), - context.getDataDirectory(), context.getPluginContainerName(), context.getEventContext(), context.getOperationContext(), - context.getContentContext(), availCollectorThreadPool, context.getPluginContainerDeployment()); + public ResourceContext<?> getParentResourceContext() { + return parentResourceContext; }
public Configuration getResourceConfiguration() { diff --git a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/inventory/InventoryManager.java b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/inventory/InventoryManager.java index 0259c69..2e15f8e 100644 --- a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/inventory/InventoryManager.java +++ b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/inventory/InventoryManager.java @@ -1568,10 +1568,11 @@ public class InventoryManager extends AgentService implements ContainerService, }
public <T extends ResourceComponent> ResourceUpgradeContext<T> createResourceUpgradeContext(Resource resource, - T parentComponent, ResourceDiscoveryComponent<T> discoveryComponent) { + ResourceContext<?> parentResourceContext, T parentComponent, ResourceDiscoveryComponent<T> discoveryComponent) { File pluginDataDir = new File(this.configuration.getDataDirectory(), resource.getResourceType().getPlugin());
return new ResourceUpgradeContext<T>(resource, // the resource itself + parentResourceContext, //the context of its parent resource parentComponent, // its parent component discoveryComponent, // the discovery component (this is actually the proxy to it) SystemInfoFactory.createSystemInfo(), // for native access diff --git a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/upgrade/ResourceUpgradeDelegate.java b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/upgrade/ResourceUpgradeDelegate.java index 06d2cd0..b3b8716 100644 --- a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/upgrade/ResourceUpgradeDelegate.java +++ b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/upgrade/ResourceUpgradeDelegate.java @@ -36,6 +36,7 @@ import org.rhq.core.pc.PluginContainer; import org.rhq.core.pc.inventory.InventoryManager; import org.rhq.core.pc.inventory.ResourceContainer; import org.rhq.core.pluginapi.inventory.ResourceComponent; +import org.rhq.core.pluginapi.inventory.ResourceContext; import org.rhq.core.pluginapi.inventory.ResourceDiscoveryComponent; import org.rhq.core.pluginapi.upgrade.ResourceUpgradeContext; import org.rhq.core.pluginapi.upgrade.ResourceUpgradeFacet; @@ -158,12 +159,14 @@ public class ResourceUpgradeDelegate {
ResourceComponent<T> parentResourceComponent = resourceContainer.getResourceContext() .getParentResourceComponent(); - + Resource parentResource = resourceContainer.getResource().getParentResource();
ResourceContainer parentResourceContainer = (parentResource != null) ? inventoryManager .getResourceContainer(resourceContainer.getResource().getParentResource()) : null;
+ ResourceContext<?> parentResourceContext = parentResourceContainer == null ? null : parentResourceContainer.getResourceContext(); + Resource resource = resourceContainer.getResource();
ResourceDiscoveryComponent<ResourceComponent<T>> discoveryComponent = PluginContainer.getInstance() @@ -175,7 +178,7 @@ public class ResourceUpgradeDelegate { }
ResourceUpgradeContext<ResourceComponent<T>> upgradeContext = inventoryManager.createResourceUpgradeContext( - resource, parentResourceComponent, discoveryComponent); + resource, parentResourceContext, parentResourceComponent, discoveryComponent);
ResourceUpgradeRequest request = new ResourceUpgradeRequest(resource.getId());
commit 9bf39010bcf60ad0cd52876be3f1255329cf656e Author: Lukas Krejci lkrejci@redhat.com Date: Wed Oct 13 15:37:36 2010 +0100
BZ 535786 - Adding the SNMP host and port detection during the apache server discovery.
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 a1b08a7..a5e19e5 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 @@ -18,12 +18,19 @@ */ package org.rhq.plugins.apache;
+import java.io.BufferedReader; import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; import java.net.URI; +import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.regex.Pattern;
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -162,6 +169,16 @@ public class ApacheServerDiscoveryComponent implements ResourceDiscoveryComponen serverConfigFile.getParent() + File.separator + "*")); }
+ List<InetSocketAddress> snmpAddresses = findSNMPAddresses(serverConfig, new File(serverRoot)); + if (snmpAddresses != null && snmpAddresses.size() > 0) { + InetSocketAddress addr = snmpAddresses.get(0); + int port = addr.getPort(); + InetAddress host = addr.getAddress() == null ? InetAddress.getLocalHost() : addr.getAddress(); + + pluginConfig.put(new PropertySimple(ApacheServerComponent.PLUGIN_CONFIG_PROP_SNMP_AGENT_HOST, host.getHostAddress())); + pluginConfig.put(new PropertySimple(ApacheServerComponent.PLUGIN_CONFIG_PROP_SNMP_AGENT_PORT, port)); + } + discoveredResources.add(createResourceDetails(discoveryContext, pluginConfig, process.getProcessInfo(), binaryInfo)); } @@ -445,4 +462,106 @@ public class ApacheServerDiscoveryComponent implements ResourceDiscoveryComponen
return serverRoot + "||" + httpdConf; } + + private static List<InetSocketAddress> findSNMPAddresses(ApacheDirectiveTree tree, File serverRoot) { + List<InetSocketAddress> ret = new ArrayList<InetSocketAddress>(); + + List<ApacheDirective> confs = tree.search("/SNMPConf"); + + if (confs.size() == 0) { + log.info("SNMPConf directive not found. Skipping SNMP configuration."); + return ret; + } + + String confDirName = confs.get(0).getValuesAsString(); + if (confDirName == null || confDirName.isEmpty()) { + log.warn("The SNMPConf directive seems to not have a value. Skipping SNMP configuration."); + return ret; + } + + + File confDir = new File(confDirName); + + if (!confDir.isAbsolute()) { + confDir = new File(serverRoot, confDirName); + } + + File snmpdConf = new File(confDir, "snmpd.conf"); + + if (!snmpdConf.exists()) { + log.warn("Could not find a snmpd.conf file under the configured directory '" + confDirName + + "'. Skipping SNMP configuration."); + return ret; + } + + try { + String agentAddressLine = findSNMPAgentAddressConfigLine(snmpdConf); + + if (agentAddressLine == null) { + log.warn("Could not find the 'agentaddress' property in the snmpd.conf. Skipping SNMP configuration."); + return ret; + } + + int specStartIdx = agentAddressLine.indexOf("agentaddress") + "agentaddress".length() + 1; + + while (Character.isWhitespace(agentAddressLine.charAt(specStartIdx))) + specStartIdx++; + + String spec = agentAddressLine.substring(specStartIdx); + + String[] addrs = spec.split(","); + + try { + for (String addr : addrs) { + if (addr.startsWith("udp") || addr.startsWith("tcp")) { + //this contains the transport spec - either "udp:" or "tcp:" + addr = addr.substring(4); + } + + int atIdx = addr.indexOf('@'); + String port = addr; + String host = null; + if (atIdx > 0) { + host = addr.substring(atIdx + 1); + port = addr.substring(0, atIdx); + } + + InetSocketAddress address = null; + if (host != null) { + address = new InetSocketAddress(host, Integer.parseInt(port)); + } else { + address = new InetSocketAddress(Integer.parseInt(port)); + } + + ret.add(address); + } + } catch (Exception e) { + log.warn("Failed to parse the SNMP 'agentaddress' configuration property: " + + agentAddressLine, e); + } + } catch (IOException e) { + log.warn("Failed to read in the configured snmpd.conf file: " + snmpdConf.getAbsolutePath(), e); + } + + return ret; + } + + private static String findSNMPAgentAddressConfigLine(File snmpdConf) throws IOException { + BufferedReader rdr = new BufferedReader(new FileReader(snmpdConf)); + + try { + Pattern search = Pattern.compile("^\s*agentaddress.*"); + String line; + + while ((line = rdr.readLine()) != null) { + if (search.matcher(line).matches()) { + return line; + } + } + + return null; + } finally { + rdr.close(); + } + } }
commit b7e3bccde3c4c36be6e9aab7299ff2d060e0475f Author: Lukas Krejci lkrejci@redhat.com Date: Wed Oct 13 15:41:07 2010 +0100
BZ 642622 - Adding the ability to extract the scheme and port from the ServerName so that proper ping URLs can be discovered.
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 bea7c23..a1b08a7 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 @@ -262,7 +262,7 @@ public class ApacheServerDiscoveryComponent implements ResourceDiscoveryComponen */ private static String getUrl(ApacheDirectiveTree serverConfig, String version) throws Exception { Address addr = HttpdAddressUtility.get(version).getMainServerSampleAddress(serverConfig, null, 0); - return addr == null ? null : "http://" + addr.host + ":" + addr.port + "/"; + return addr == null ? null : addr.toString(); }
@Nullable 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 b2f76bc..8251658 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 @@ -124,7 +124,7 @@ public class ApacheVirtualHostServiceDiscoveryComponent implements ResourceDisco
String url; if (hostToPing != null && portToPing != Address.PORT_WILDCARD_VALUE && portToPing != Address.NO_PORT_SPECIFIED_VALUE) { - url = "http://" + hostToPing + ":" + portToPing + "/"; + url = scheme + "://" + hostToPing + ":" + portToPing + "/"; } else { url = COULD_NOT_DETERMINE_THE_VIRTUAL_HOST_ADDRESS; } @@ -133,16 +133,18 @@ public class ApacheVirtualHostServiceDiscoveryComponent implements ResourceDisco pluginConfiguration.put(urlProp);
} - - 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); - + + 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 (serverName != null) { - resourceName = address.toString(); + resourceName = address.host + ":" + address.port; } 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 c5589fd..b5cdedf 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); + updateWithServerName(address, ag, false);
return address; } @@ -90,7 +90,7 @@ public enum HttpdAddressUtility {
return null; } catch (Exception e) { - log.info("Failed to obtain main server address. Is augeas installed and correct lens in use?"); + log.warn("Failed to obtain main server address.", e);
return null; } @@ -106,7 +106,7 @@ public enum HttpdAddressUtility { addr = getLocalhost(addr.port); }
- updateWithServerName(addr, ag); + updateWithServerName(addr, ag, false);
return addr; } @@ -115,7 +115,7 @@ public enum HttpdAddressUtility { //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?"); + log.warn("Failed to obtain main server address.", e);
return null; } @@ -131,6 +131,7 @@ public enum HttpdAddressUtility { public static class Address { public String host; public int port = -1; + public String scheme = "http";
public static final String WILDCARD = "*"; public static final String DEFAULT_HOST = "_default_"; @@ -142,6 +143,10 @@ public enum HttpdAddressUtility { this.port = port; }
+ public Address(String scheme, String host, int port) { + this(host, port); + this.scheme = scheme; + } /** * A simple parser of the provided address into host and port * sections. @@ -150,6 +155,13 @@ public enum HttpdAddressUtility { * @return an instance of Address with host and port set accordingly */ public static Address parse(String address) { + String scheme = "http"; + int schemeSpecIdx = address.indexOf("://"); + if (schemeSpecIdx >= 0) { + scheme = address.substring(0, schemeSpecIdx); + address = address.substring(schemeSpecIdx + "://".length()); + } + int lastColonIdx = address.lastIndexOf(':'); if (lastColonIdx == NO_PORT_SPECIFIED_VALUE) { return new Address(address, -1); @@ -166,10 +178,10 @@ public enum HttpdAddressUtility { port = Integer.parseInt(portSpec); }
- return new Address(host, port); + return new Address(scheme, host, port); } else { //this is an IP6 address without a port spec - return new Address(address, NO_PORT_SPECIFIED_VALUE); + return new Address(scheme, address, NO_PORT_SPECIFIED_VALUE); } } } @@ -214,11 +226,11 @@ public enum HttpdAddressUtility {
@Override public String toString() { - if (port == NO_PORT_SPECIFIED_VALUE) return host; + if (port == NO_PORT_SPECIFIED_VALUE) return scheme + "://" + host; else { String portSpec = port == PORT_WILDCARD_VALUE ? WILDCARD : String.valueOf(port);
- return host + ":" + portSpec; + return scheme + "://" + host + ":" + portSpec; } } } @@ -245,30 +257,29 @@ public enum HttpdAddressUtility { * @return the address on which the virtual host can be accessed or null on error */ public Address getVirtualHostSampleAddress(ApacheDirectiveTree ag, String virtualHost, String serverName, boolean snmpModuleCompatibleMode) { - Address addr = Address.parse(virtualHost); - if (addr.isHostDefault() || addr.isHostWildcard()) { - Address serverAddr = null; - if (snmpModuleCompatibleMode) { - serverAddr = getLocalhost(addr.port); - } else { - serverAddr = getMainServerSampleAddress(ag, null, addr.port); + try { + Address addr = Address.parse(virtualHost); + 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.host = serverAddr.host; } - if (serverAddr == null) - return null; - addr.host = serverAddr.host; - }
- if (serverName != null) { - int colonIdx = serverName.indexOf(':'); - if (colonIdx >= 0) { - addr.host = serverName.substring(0, colonIdx); - addr.port = Integer.parseInt(serverName.substring(colonIdx + 1)); - } else { - addr.host = serverName; + if (serverName != null) { + updateWithServerName(addr, serverName, true); } - }
- return addr; + return addr; + } catch (Exception e) { + log.warn("Failed to obtain virtual host address.", e); + return null; + } }
private static Address parseListen(String listenValue) { @@ -335,12 +346,12 @@ public enum HttpdAddressUtility { 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... + //well, this is bad, we can't 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 { + private static void updateWithServerName(Address address, ApacheDirectiveTree config, boolean updatePort) throws UnknownHostException { //check if there is a ServerName directive List<ApacheDirective> serverNameNodes = config.search("/ServerName");
@@ -349,11 +360,20 @@ 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(); - InetAddress addrFromServerName = InetAddress.getByName(serverName); - InetAddress addrFromAddress = InetAddress.getByName(address.host); - - if (addrFromAddress.equals(addrFromServerName)) { - address.host = serverName; + updateWithServerName(address, serverName, updatePort); + } + } + + private static void updateWithServerName(Address address, String serverName, boolean updatePort) throws UnknownHostException { + Address serverAddr = Address.parse(serverName); + InetAddress addrFromServerName = InetAddress.getByName(serverAddr.host); + InetAddress addrFromAddress = InetAddress.getByName(address.host); + + if (addrFromAddress.equals(addrFromServerName)) { + address.scheme = serverAddr.scheme; + address.host = serverAddr.host; + if (updatePort) { + address.port = serverAddr.port; } } }
commit eb45a242496d529a1ac3ee63807d8ea67237708e Author: Lukas Krejci lkrejci@redhat.com Date: Wed Oct 13 15:43:42 2010 +0200
BZ 642610 - directives found in included files aren't duplicated anymore
diff --git a/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/ApacheAugeasNode.java b/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/ApacheAugeasNode.java index 2e0f2a2..6cb168e 100644 --- a/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/ApacheAugeasNode.java +++ b/modules/plugins/apache/src/main/java/org/rhq/plugins/apache/augeas/ApacheAugeasNode.java @@ -121,18 +121,6 @@ public class ApacheAugeasNode extends AugeasNodeLazy implements AugeasNode { childNodes.add(node); }
- public List<AugeasNode> getChildByLabel(String labelName) { - List<AugeasNode> nodes = super.getChildByLabel(labelName); - - if (includedNodes != null) { - for (AugeasNode node : includedNodes) { - if (node.getLabel().equals(labelName)) - nodes.add(node); - } - } - return nodes; - } - public void setPath(String path) throws AugeasTreeException { this.path = path;
commit a3455824e924722ee018b6fca916da31a2c5e859 Author: Lukas Krejci lkrejci@redhat.com Date: Wed Oct 13 15:42:17 2010 +0200
BZ 642591 - make sure we always have the same resource keys as in old plugin version.
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 c1f6323..4c61682 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 @@ -590,6 +590,13 @@ public class ApacheServerComponent implements AugeasRHQComponent<PlatformCompone }
/** + * @return The url the server is pinged for availability or null if the url is not set. + */ + public @Nullable String getServerUrl() { + return resourceContext.getPluginConfiguration().getSimpleValue(PLUGIN_CONFIG_PROP_URL, null); + } + + /** * Returns the httpd.conf file * @return A File object that represents the httpd.conf file or null in case of error */ 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 d97d9fc..a0f2120 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,6 +21,8 @@ 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; @@ -345,40 +347,77 @@ public class ApacheVirtualHostServiceComponent implements ResourceComponent<Apac List<AugeasNode> virtualHosts = new ArrayList<AugeasNode>(); boolean updated = false;
- for (AugeasNode node : nodes) { - updated = false; +//BZ 612189 - uncomment this algo once the resource upgrade is in place +// for (AugeasNode node : nodes) { +// updated = false; +// List<AugeasNode> serverNameNodes = tree.matchRelative(node, "ServerName/param"); +// String tempServerName = null; +// +// if (!(serverNameNodes.isEmpty())) { +// tempServerName = serverNameNodes.get(0).getValue(); +// } +// if (tempServerName == null & serverName == null) +// updated = true; +// if (tempServerName != null & serverName != null) +// if (tempServerName.equals(serverName)){ +// updated = true; +// } +// +// +// if (updated){ +// updated = false; +// List<AugeasNode> params = node.getChildByLabel("param"); +// for (AugeasNode nd : params) { +// updated = false; +// for (String adr : addrs) { +// if (adr.equals(nd.getValue())) +// updated = true; +// } +// if (!updated) +// break; +// } +// +// if (updated) +// virtualHosts.add(node); +// } +// } + + //BZ 612189 - remove this once resource upgrade is in place + HttpdAddressUtility.Address resourceKeyAddress = HttpdAddressUtility.Address.parse(resourceKey); + for(AugeasNode node : nodes) { List<AugeasNode> serverNameNodes = tree.matchRelative(node, "ServerName/param"); - String tempServerName = null; - - if (!(serverNameNodes.isEmpty())) { - tempServerName = serverNameNodes.get(0).getValue(); - } - if (tempServerName == null & serverName == null) - updated = true; - if (tempServerName != null & serverName != null) - if (tempServerName.equals(serverName)){ - updated = true; - } + List<AugeasNode> vhostAddressNodes = node.getChildByLabel("param"); + + String vhostServerName = serverNameNodes.isEmpty() ? null : serverNameNodes.get(0).getValue(); + String vhostAddressDef = vhostAddressNodes.isEmpty() ? null : vhostAddressNodes.get(0).getValue(); + + if (vhostAddressDef != null) { + HttpdAddressUtility.Address vhostAddress = HttpdAddressUtility.Address.parse(vhostAddressDef); + if (vhostServerName != null) { + HttpdAddressUtility.Address vhostServerAddress = HttpdAddressUtility.Address.parse(vhostServerName); + vhostAddress.host = vhostServerAddress.host; + }
- - if (updated){ - updated = false; - List<AugeasNode> params = node.getChildByLabel("param"); - for (AugeasNode nd : params) { - updated = false; - for (String adr : addrs) { - if (adr.equals(nd.getValue())) - updated = true; - } - if (!updated) - break; - } - - if (updated) - virtualHosts.add(node); + if (resourceKeyAddress.equals(vhostAddress)) { + virtualHosts.add(node); } - } - + } + } + + //BZ 612189 - remove this once we have resource upgrade + //ok, one final attempt... the legacy resource key format for the MainServer is just a host:port as with the rest of the vhosts, let's try that + try { + String serverUrl = resourceContext.getParentResourceComponent().getServerUrl(); + URI serverUri = new URI(serverUrl); + String expectedResourceKey = serverUri.getHost() + ":" + serverUri.getPort(); + + if (expectedResourceKey.equals(resourceKey)) { + return tree.getRootNode(); + } + } catch (URISyntaxException e) { + log.warn("Failed to parse the server URL when trying to match the vhost with the main server.", e); + } + if (virtualHosts.size() == 0) { throw new IllegalStateException("Could not find virtual host configuration in augeas for virtual host: " + resourceKey); @@ -505,6 +544,14 @@ public class ApacheVirtualHostServiceComponent implements ResourceComponent<Apac vhostServerName, true); if (vhostAddr != null) { vhostAddresses.add(vhostAddr); + } else { + //this is not to choke on the old style resource keys for the main server. without this, we'd never be able + //to match the main server with its snmp index below. + HttpdAddressUtility.Address addr = HttpdAddressUtility.Address.parse(vhostAddressStrings[i]); + vhostAddr = parent.getAddressUtility().getMainServerSampleAddress(tree, addr.host, addr.port); + if (vhostAddr != null) { + vhostAddresses.add(vhostAddr); + } } } } 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 4a32e27..b2f76bc 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 @@ -19,7 +19,10 @@ package org.rhq.plugins.apache;
import java.io.File; +import java.net.InetAddress; import java.net.URI; +import java.net.URISyntaxException; +import java.net.UnknownHostException; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; @@ -37,6 +40,7 @@ import org.rhq.core.pluginapi.inventory.ResourceDiscoveryComponent; import org.rhq.core.pluginapi.inventory.ResourceDiscoveryContext; import org.rhq.plugins.apache.parser.ApacheDirective; import org.rhq.plugins.apache.parser.ApacheDirectiveTree; +import org.rhq.plugins.apache.util.HttpdAddressUtility; import org.rhq.plugins.apache.util.HttpdAddressUtility.Address; import org.rhq.plugins.www.snmp.SNMPException; import org.rhq.plugins.www.snmp.SNMPSession; @@ -91,23 +95,13 @@ public class ApacheVirtualHostServiceDiscoveryComponent implements ResourceDisco serverName = serverNames.get(0).getValuesAsString(); }
- StringBuilder keyBuilder = new StringBuilder(); - if (serverName != null) { - keyBuilder.append(serverName).append("|"); - } - keyBuilder.append(firstAddress); - - - for (int i=1;i<hosts.size();i++){ - keyBuilder.append(" ").append(hosts.get(i)); - } - - String resourceKey = keyBuilder.toString(); + String resourceKey = createResourceKey(serverName, hosts);
Configuration pluginConfiguration = context.getDefaultPluginConfiguration();
Address address = serverComponent.getAddressUtility().getVirtualHostSampleAddress(tree, firstAddress, serverName, false); if (address != null) { + String scheme = address.scheme; String hostToPing = address.host; int portToPing = address.port; if (address.isPortWildcard()) { @@ -178,6 +172,9 @@ public class ApacheVirtualHostServiceDiscoveryComponent implements ResourceDisco
String mainServerUrl = context.getParentResourceContext().getPluginConfiguration().getSimple( ApacheServerComponent.PLUGIN_CONFIG_PROP_URL).getStringValue(); + + String key = null; + if (mainServerUrl != null && !"null".equals(mainServerUrl)) { PropertySimple mainServerUrlProp = new PropertySimple(ApacheVirtualHostServiceComponent.URL_CONFIG_PROP, mainServerUrl); @@ -196,9 +193,15 @@ public class ApacheVirtualHostServiceDiscoveryComponent implements ResourceDisco PropertySimple rtLogProp = new PropertySimple( ApacheVirtualHostServiceComponent.RESPONSE_TIME_LOG_FILE_CONFIG_PROP, rtLogFile.toString()); mainServerPluginConfig.put(rtLogProp); + + //BZ 612189 - remove this once we have resource upgrade + key = host + ":" + port; }
- String key = ApacheVirtualHostServiceComponent.MAIN_SERVER_RESOURCE_KEY; + //BZ 612189 - this can simply the MAIN_SERVER_RESOURCE_KEY only once we have resource upgrade + if (key == null) { + key = ApacheVirtualHostServiceComponent.MAIN_SERVER_RESOURCE_KEY; + }
//BZ 612189 - remove this once we have resource upgrade if (snmpDiscoveries != null) { @@ -237,6 +240,42 @@ public class ApacheVirtualHostServiceDiscoveryComponent implements ResourceDisco } }
+ public static String createResourceKey(String serverName, List<String> hosts) { +//BZ 612189 - swap the impls once resource upgrade is in place +// StringBuilder keyBuilder = new StringBuilder(); +// if (serverName != null) { +// keyBuilder.append(serverName).append("|"); +// } +// keyBuilder.append(hosts.get(0)); +// +// +// for (int i = 1; i < hosts.size(); ++i){ +// keyBuilder.append(" ").append(hosts.get(i)); +// } +// +// return keyBuilder.toString(); + + //try to derive the same resource key as the SNMP would have... this is to prevent the duplication of + //vhost resources after the SNMP was configured - how I wish resource upgrade made it to 3.0 to prevent this + //kind of guessing being necessary. + String host = hosts.get(0); + HttpdAddressUtility.Address hostAddr = HttpdAddressUtility.Address.parse(host); + if (serverName != null) { + HttpdAddressUtility.Address serverAddr = HttpdAddressUtility.Address.parse(serverName); + hostAddr.host = serverAddr.host; + } + + //the SNMP module seems to resolve the IPs to hostnames. + try { + InetAddress hostName = InetAddress.getByName(hostAddr.host); + hostAddr.host = hostName.getHostName(); + } catch (UnknownHostException e) { + log.debug("Host " + hostAddr.host + " is not resolvable.", e); + } + + return hostAddr.host + ":" + hostAddr.port; + } + /** * @deprecated remove this once we have resource upgrade * @param discoveryContext @@ -279,7 +318,7 @@ public class ApacheVirtualHostServiceDiscoveryComponent implements ResourceDisco
return ret; } catch (Exception e) { - log.warn("Error while trying to contact SNMP of the apache server " + discoveryContext.getParentResourceContext().getResourceKey()); + log.warn("Error while trying to contact SNMP of the apache server " + discoveryContext.getParentResourceContext().getResourceKey(), e); return null; } }
commit 15a2bbe9f0a9ba43cccb2065ced149a8abb92710 Author: Lukas Krejci lkrejci@redhat.com Date: Wed Oct 13 14:33:57 2010 +0100
BZ 642588 - config is reloaded if ServerRoot is redefined in config
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 ca8f972..bea7c23 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 @@ -141,6 +141,8 @@ public class ApacheServerDiscoveryComponent implements ResourceDiscoveryComponen if (!serverRoots.isEmpty()) { serverRoot = AugeasNodeValueUtil.unescape(serverRoots.get(0).getValuesAsString()); serverRootProp.setValue(serverRoot); + //reparse the configuration with the new ServerRoot + serverConfig = loadParser(serverConfigFile.getAbsolutePath(), serverRoot); }
serverUrl = getUrl(serverConfig, binaryInfo.getVersion());