modules/core/client-api/src/main/resources/rhq-plugin.xsd | 17 modules/core/plugin-container/src/main/java/org/rhq/core/pc/inventory/ChildResourceTypeDiscoveryRunner.java | 451 +++++----- modules/core/plugin-container/src/main/java/org/rhq/core/pc/inventory/InventoryManager.java | 52 - modules/plugins/nagios/src/main/java/org/rhq/plugins/nagios/NagiosMonitorChildTypeComponent.java | 183 ++++ modules/plugins/nagios/src/main/java/org/rhq/plugins/nagios/NagiosMonitorChildTypeDiscovery.java | 119 ++ modules/plugins/nagios/src/main/resources/META-INF/rhq-plugin.xml | 2 6 files changed, 579 insertions(+), 245 deletions(-)
New commits: commit b90a7253bdeb889575d764725be7921c6fb05e18 Author: Alexander Kiefer alexander.kiefer@dillinger.biz Date: Tue Aug 17 21:26:02 2010 +0200
Created discovery/component classes for child ResourceType discovery and updated plugin descriptor
diff --git a/modules/plugins/nagios/src/main/java/org/rhq/plugins/nagios/NagiosMonitorChildTypeComponent.java b/modules/plugins/nagios/src/main/java/org/rhq/plugins/nagios/NagiosMonitorChildTypeComponent.java new file mode 100755 index 0000000..ddb5a50 --- /dev/null +++ b/modules/plugins/nagios/src/main/java/org/rhq/plugins/nagios/NagiosMonitorChildTypeComponent.java @@ -0,0 +1,183 @@ +package org.rhq.plugins.nagios; + +/* + * RHQ Management Platform + * Copyright (C) 2005-2008 Red Hat, Inc. + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +import java.util.HashSet; +import java.util.Set; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.rhq.core.domain.configuration.Configuration; +import org.rhq.core.domain.measurement.AvailabilityType; +import org.rhq.core.domain.measurement.MeasurementDefinition; +import org.rhq.core.domain.measurement.MeasurementReport; +import org.rhq.core.domain.measurement.MeasurementScheduleRequest; +import org.rhq.core.domain.resource.ResourceCategory; +import org.rhq.core.domain.resource.ResourceType; +import org.rhq.core.pluginapi.inventory.ChildResourceTypeDiscoveryFacet; +import org.rhq.core.pluginapi.inventory.InvalidPluginConfigurationException; +import org.rhq.core.pluginapi.inventory.ResourceComponent; +import org.rhq.core.pluginapi.inventory.ResourceContext; +import org.rhq.core.pluginapi.measurement.MeasurementFacet; +import org.rhq.plugins.nagios.controller.NagiosManagementInterface; + +/** + * + * @author Alexander Kiefer + * + */ +public class NagiosMonitorChildTypeComponent implements ResourceComponent, MeasurementFacet, + ChildResourceTypeDiscoveryFacet { + + private final Log log = LogFactory.getLog(this.getClass()); + + public static final String DEFAULT_NAGIOSIP = "127.0.0.1"; + public static final String DEFAULT_NAGIOSPORT = "6557"; + + private ResourceContext context; + private NagiosManagementInterface nagiosManagementInterface; + private String nagiosHost; + private int nagiosPort; + + @Override + public void getValues(MeasurementReport report, Set<MeasurementScheduleRequest> metrics) throws Exception { + // NagiosSystemData nagiosSystemData = null; + // String serviceName = this.context.getResourceType().getName(); + // log.info("getValues() of ResourceType: " + serviceName); + // + // try { + // //Getting all Nagios system information + // nagiosSystemData = nagiosManagementInterface.createNagiosSystemData(); + // } catch (Exception e) { + // log.warn(" Can not get information from Nagios: ", e); + // return; + // } + // + // //iterating over the metrics + // for (MeasurementScheduleRequest req : metrics) { + // try { // Don't let one bad egg spoil the cake + // + // String[] splitter = req.getName().split("\|"); + // String property = splitter[1]; + // String pattern = splitter[2]; + // + // if (log.isDebugEnabled()) { + // log.debug("Name of Metric: " + property); + // log.debug("RegEx: " + pattern); + // } + // + // // Get "raw" data from nagios data structures - we need to pick our value below + // String value = nagiosSystemData.getSingleHostServiceMetric(property, serviceName, "localhost") + // .getValue(); // TODO use 'real' host + // + // Pattern p = Pattern.compile(pattern); + // Matcher m = p.matcher(value); + // if (m.matches()) { + // String val = m.group(1); // Our metric is always in the first match group. + // + // // We have a match, now dispatch by dataType of the request + // if (req.getDataType() == DataType.MEASUREMENT) { + // MeasurementDataNumeric res = new MeasurementDataNumeric(req, Double.valueOf(val)); + // report.addData(res); + // } else if (req.getDataType() == DataType.TRAIT) { + // MeasurementDataTrait res = new MeasurementDataTrait(req, val); + // report.addData(res); + // } else + // log.error("Unknown DataType for request " + req); + // } else { + // log.warn("Pattern >>" + pattern + "<< did not match for input >>" + value + "<< and request: >>" + // + req.getName()); + // } + // } catch (NagiosException e) { + // log.error(e); + // } + // } + + } + + @Override + public Set<ResourceType> discoverChildResourceTypes() { + + log.info("<nagiosMonitorComponent>discoverChildResourceTypes called"); + + ResourceType parentType = this.context.getResourceType(); + ResourceType resourceType = new ResourceType("NewNagiosChildNo3", parentType.getPlugin(), + ResourceCategory.SERVICE, parentType); + + //Create measurement definition for new created ResourceType + MeasurementDefinition measurementDef = new MeasurementDefinition(resourceType, resourceType.getName() + + "Metric"); + + //Add new MeasurementDefinition to the resourceType + resourceType.addMetricDefinition(measurementDef); + + Set<ResourceType> resourceTypes = new HashSet<ResourceType>(); + resourceTypes.add(resourceType); + + return resourceTypes; + } + + public void start(ResourceContext context) throws InvalidPluginConfigurationException, Exception { + //get context of this component instance + this.context = context; + + //get config + if (context.getParentResourceComponent() instanceof NagiosMonitorComponent) { + NagiosMonitorComponent parent = (NagiosMonitorComponent) context.getParentResourceComponent(); + + nagiosHost = parent.getNagiosHost(); + nagiosPort = parent.getNagiosPort(); + } else { + Configuration conf = context.getPluginConfiguration(); + nagiosHost = conf.getSimpleValue("nagiosHost", DEFAULT_NAGIOSIP); + String tmp = conf.getSimpleValue("nagiosPort", DEFAULT_NAGIOSPORT); + nagiosPort = Integer.parseInt(tmp); + } + + //Interface class to the nagios system + nagiosManagementInterface = new NagiosManagementInterface(nagiosHost, nagiosPort); + + //log.info("nagios Plugin started"); + + } + + @Override + public void stop() { + // TODO Auto-generated method stub + + } + + @Override + public AvailabilityType getAvailability() { + if (context.getParentResourceComponent() instanceof NagiosMonitorComponent) { + return AvailabilityType.UP; // TODO get from parent? + } else { + boolean available = false; + if (nagiosManagementInterface != null) + available = nagiosManagementInterface.pingNagios(); + + if (available) + return AvailabilityType.UP; + } + return AvailabilityType.DOWN; + } + +} diff --git a/modules/plugins/nagios/src/main/java/org/rhq/plugins/nagios/NagiosMonitorChildTypeDiscovery.java b/modules/plugins/nagios/src/main/java/org/rhq/plugins/nagios/NagiosMonitorChildTypeDiscovery.java new file mode 100755 index 0000000..32c8f65 --- /dev/null +++ b/modules/plugins/nagios/src/main/java/org/rhq/plugins/nagios/NagiosMonitorChildTypeDiscovery.java @@ -0,0 +1,119 @@ +package org.rhq.plugins.nagios; + +/* + * RHQ Management Platform + * Copyright (C) 2005-2008 Red Hat, Inc. + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +import java.util.Set; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.rhq.core.domain.configuration.Configuration; +import org.rhq.core.pluginapi.inventory.DiscoveredResourceDetails; +import org.rhq.core.pluginapi.inventory.InvalidPluginConfigurationException; +import org.rhq.core.pluginapi.inventory.ManualAddFacet; +import org.rhq.core.pluginapi.inventory.ResourceDiscoveryComponent; +import org.rhq.core.pluginapi.inventory.ResourceDiscoveryContext; +import org.rhq.plugins.nagios.reply.LqlReply; + +/** + * + * @author Alexander Kiefer + * + */ +public class NagiosMonitorChildTypeDiscovery implements ResourceDiscoveryComponent, ManualAddFacet { + + private final Log log = LogFactory.getLog(this.getClass()); + + /** + * Don run the auto-discovery for the services below the NagiosMonitor server type. + */ + public Set<DiscoveredResourceDetails> discoverResources(ResourceDiscoveryContext discoveryContext) throws Exception { + // Set<DiscoveredResourceDetails> discoveredResources = new HashSet<DiscoveredResourceDetails>(); + // + // // If we have no parent, it means the NagioMonitoring server type is not yet up. + // ResourceComponent tmpComponent = discoveryContext.getParentResourceComponent(); + // if (tmpComponent == null || !(tmpComponent instanceof NagiosMonitorComponent)) + // return discoveredResources; + // + // NagiosMonitorComponent parentComponent = (NagiosMonitorComponent) tmpComponent; + // String nagiosHost = parentComponent.getNagiosHost(); + // int nagiosPort = parentComponent.getNagiosPort(); + // + // //Method requests available nagios services an returns the names of them + // LqlReply resourceTypeReply = getResourceTypeInformation(nagiosHost, nagiosPort); + // + // // the resource type we are interested in this invocation + // ResourceType wanted = discoveryContext.getResourceType(); + // //for each available service + // for (int i = 0; i < resourceTypeReply.getLqlReply().size(); i++) { + // + // String nagiosType = resourceTypeReply.getLqlReply().get(i); + // if (!nagiosType.equals(wanted.getName())) + // continue; + // + // //create new DiscoveredResourceDetails instance + // DiscoveredResourceDetails detail = new DiscoveredResourceDetails( + // //new ResourceType instance per service + // wanted, "nagiosKey@" + "Nr:" + i + ":" + resourceTypeReply.getLqlReply().get(i), "Nagios@" + "Nr:" + i + // + ":" + resourceTypeReply.getLqlReply().get(i), null, "NagiosService: " + // + resourceTypeReply.getLqlReply().get(i), null, null); + // + // //add DiscoveredResourceDetails instance to Set + // discoveredResources.add(detail); + // log.info("Discovered a nagios service: " + detail); + // } + + return null; + + } + + /** + * Don't run the auto-discovery of this "nagios" server type, + * as we probably won't have one on each platform. Rather have the admin + * explicitly add it to one platform. + */ + private LqlReply getResourceTypeInformation(String nagiosIp, int nagiosPort) { + // LqlResourceTypeRequest resourceTypeRequest = new LqlResourceTypeRequest(); + // LqlReply resourceTypeReply; + // + // NetworkConnection connection = new NetworkConnection(nagiosIp, nagiosPort); + // resourceTypeReply = connection.sendAndReceive(resourceTypeRequest); + + return null; + } + + @Override + public DiscoveredResourceDetails discoverResource(Configuration configuration, + ResourceDiscoveryContext resourceDiscoveryContext) throws InvalidPluginConfigurationException { + // + // String nagiosHost = configuration.getSimpleValue("nagiosHost", NagiosMonitorComponent.DEFAULT_NAGIOSIP); + // String nagiosPort = configuration.getSimpleValue("nagiosPort", NagiosMonitorComponent.DEFAULT_NAGIOSPORT); + // + // DiscoveredResourceDetails detail = new DiscoveredResourceDetails(resourceDiscoveryContext.getResourceType(), + // "nagios@" + nagiosHost + ":" + nagiosPort, "Nagios@" + nagiosHost + ":" + nagiosPort, null, + // "Nagios server @ " + nagiosHost + ":" + nagiosPort, configuration, null); + // log.info("Adding NagiosMonitor " + detail); + // + // return detail; + + return null; + } + +} diff --git a/modules/plugins/nagios/src/main/resources/META-INF/rhq-plugin.xml b/modules/plugins/nagios/src/main/resources/META-INF/rhq-plugin.xml index 1e937d8..059bc1d 100644 --- a/modules/plugins/nagios/src/main/resources/META-INF/rhq-plugin.xml +++ b/modules/plugins/nagios/src/main/resources/META-INF/rhq-plugin.xml @@ -14,6 +14,8 @@ <server name="NagiosMonitor" discovery="NagiosMonitorDiscovery" class="NagiosMonitorComponent" + childTypeDiscovery="NagiosMonitorChildTypeDiscovery" + childTypeClass="NagiosMonitorChildTypeComponent" description="Interface to Nagios monitoring system. Nagios needs to be equipped with mk_livestatus module for this plugin to work" supportsManualAdd="true"
commit 5f40275d55d20dbeade4e12f67e018fb85a2e788 Author: Alexander Kiefer alexander.kiefer@dillinger.biz Date: Tue Aug 17 21:23:23 2010 +0200
Enlarged .xsd schema to prepare child ResourceType discovery of plugin
diff --git a/modules/core/client-api/src/main/resources/rhq-plugin.xsd b/modules/core/client-api/src/main/resources/rhq-plugin.xsd index 6225b8e..69f296e 100644 --- a/modules/core/client-api/src/main/resources/rhq-plugin.xsd +++ b/modules/core/client-api/src/main/resources/rhq-plugin.xsd @@ -614,6 +614,23 @@ </xs:documentation> </xs:annotation> </xs:attribute> + + <xs:attribute name="childTypeDiscovery" use="optional" type="rhq:classNameType"> + xs:annotation + xs:documentation + The discovery component class used to discover this child resource. + </xs:documentation> + </xs:annotation> + </xs:attribute> + + <xs:attribute name="childTypeClass" type="rhq:classNameType"> + xs:annotation + xs:documentation + The resource component class that is used to represent instances of this child resource. + </xs:documentation> + </xs:annotation> + </xs:attribute> + xs:anyAttribute/ </xs:complexType>
commit 0ba46e33aa18c0be94c6271c5ef4f71cddd884d0 Author: Alexander Kiefer alexander.kiefer@dillinger.biz Date: Tue Aug 17 21:21:48 2010 +0200
Added try/catch-Block to catch Exceptions that will be thrown in case of error
diff --git a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/inventory/ChildResourceTypeDiscoveryRunner.java b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/inventory/ChildResourceTypeDiscoveryRunner.java index d13d8ee..7567324 100644 --- a/modules/core/plugin-container/src/main/java/org/rhq/core/pc/inventory/ChildResourceTypeDiscoveryRunner.java +++ b/modules/core/plugin-container/src/main/java/org/rhq/core/pc/inventory/ChildResourceTypeDiscoveryRunner.java @@ -1,221 +1,230 @@ -package org.rhq.core.pc.inventory; - -/* - * RHQ Management Platform - * Copyright (C) 2005-2008 Red Hat, Inc. - * All rights reserved. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation version 2 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - */ - -import java.util.HashSet; -import java.util.Set; -import java.util.concurrent.Callable; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import org.rhq.core.clientapi.agent.PluginContainerException; -import org.rhq.core.domain.measurement.AvailabilityType; -import org.rhq.core.domain.resource.Resource; -import org.rhq.core.domain.resource.ResourceCategory; -import org.rhq.core.domain.resource.ResourceType; -import org.rhq.core.pc.PluginContainer; -import org.rhq.core.pc.util.ComponentUtil; -import org.rhq.core.pc.util.FacetLockType; -import org.rhq.core.pluginapi.inventory.ChildResourceTypeDiscoveryFacet; -import org.rhq.core.util.exception.ThrowableUtil; - -public class ChildResourceTypeDiscoveryRunner implements Callable<Set<ResourceType>>, Runnable { - - private Log log = LogFactory.getLog(ChildResourceTypeDiscoveryRunner.class); - - //Default Ctor - public ChildResourceTypeDiscoveryRunner() { - - } - - public void run() { - try { - call(); - } catch (Exception e) { - log.error("Could not get measurement report.", e); - } - } - - public Set<ResourceType> call() { - - if (log.isDebugEnabled()) { - log.info("<ChildResourceTypeDiscoveryRunner>call() called"); - } - - //Set<ResourceTypes> for the ResourceTypes which shall be added later - Set<ResourceType> resourceTypes = null; - - long start = System.currentTimeMillis(); - - //get InventoryManager instance - InventoryManager im = PluginContainer.getInstance().getInventoryManager(); - - if (log.isDebugEnabled()) { - log.info("InventoryManager instance created"); - } - - //Get current plattform - Resource platform = im.getPlatform(); - if (log.isDebugEnabled()) { - log.info("Platform returned with name: " + platform.getName()); - } - - // Next discover all other services and non-top-level servers - Set<Resource> children = platform.getChildResources(); - if (log.isDebugEnabled()) { - log.info("Platform " + platform.getName() + " has " + children.size() + " ChildResources"); - } - - if (children != null) { - for (Resource child : children) { - if (log.isDebugEnabled()) { - log.debug("Name of server: " + child.getName()); - log.debug("Id of server: " + child.getId()); - log.debug("Category of server: " + child.getResourceType().getCategory().toString()); - } - - //Check if really is of Category SERVER because our Plugin has to be of that category - if (child.getResourceType().getCategory() == ResourceCategory.SERVER) { - - if (log.isDebugEnabled()) { - log.info("Server " + child.getName() + "has passed the Server Category test succesfull"); - } - - ResourceContainer container = im.getResourceContainer(child.getId()); - - if (log.isDebugEnabled()) { - log.info("Server " + child.getName() + " is running in ResourceContainer " - + container.toString()); - } - - if (container.getResourceComponentState() != ResourceContainer.ResourceComponentState.STARTED - || container.getAvailability() == null - || container.getAvailability().getAvailabilityType() == AvailabilityType.DOWN) { - // Don't collect metrics for resources that are down - if (log.isDebugEnabled()) { - log.info("ChildType not discoverd for inactive resource component: " - + container.getResource()); - } - } else { - - try { - - //Get Facet Component - ChildResourceTypeDiscoveryFacet discoveryComponent = ComponentUtil.getComponent(child - .getId(), ChildResourceTypeDiscoveryFacet.class, FacetLockType.READ, 30 * 1000, true, - true); - - //get Set<ResourceType> --> all the ChildResourceTypes which shall be added dynamically - resourceTypes = discoverChildResourceTypes(discoveryComponent); - - if (log.isDebugEnabled()) { - log.info("Container.getResource(): " + container.getResource().getName()); - log.info("Container.getResource().getResourceType(): " - + container.getResource().getResourceType().getName()); - - } - - //all the ChildResourceTypes which are already part of the plugin - Set<ResourceType> currentChildTypes = container.getResource().getResourceType() - .getChildResourceTypes(); - - Set<ResourceType> newTypesToAdd = new HashSet<ResourceType>(); - - if (log.isDebugEnabled()) { - log.info("Size of HashSet<ResourceType> after initialisation: " + newTypesToAdd.size()); - } - - //Check all types with were added by the plugin - for (ResourceType newTypetoAdd : resourceTypes) { - //Check all ChildResourceTypes that already exist - boolean childAllreadyExists = false; - - for (ResourceType alreadyExistingType : currentChildTypes) { - //Check if name and plugin of the types are equal - //Necessary because equal-named ChildResourceTypes can belong to different plugins - if (newTypetoAdd.getName().equals(alreadyExistingType.getName()) - && newTypetoAdd.getPlugin().equals(alreadyExistingType.getPlugin())) { - log.info("The ResourceType " + newTypetoAdd.getName() - + " already exists for the Plugin " + newTypetoAdd.getPlugin()); - - //if ResourceType allready exists set boolean var to true - childAllreadyExists = true; - } - } - - //Check if type does not allready exist, only add ResourceType to set if so - if (!childAllreadyExists) { - if (log.isDebugEnabled()) { - log.info("new ChildResourceType " + newTypetoAdd.getName() - + " added to Set<ResourceTypes>"); - } - newTypesToAdd.add(newTypetoAdd); - } - } - - //Create a new ResourceType in the DB for the selected type - //call InventoryManager method only if Set<ResourceType> contains at least one element - if (newTypesToAdd.size() > 0) { - im.createNewResourceType(newTypesToAdd); - } - - } catch (PluginContainerException pce) { - // This is expected when the ResourceComponent does not implement the ChildResourceTypeDiscoveryFacet - log.warn("Error submitting service scan: " + pce.getMessage()); - } catch (Exception e) { - throw new RuntimeException("Error submitting service scan", e); - } - } - } - } - - } else { - log.info("Set<ResourceType> was returned with value null"); - } - - return null; - } - - /** - * - * @param discoveryComponent - * @return - */ - private Set<ResourceType> discoverChildResourceTypes(ChildResourceTypeDiscoveryFacet discoveryComponent) { - - Set<ResourceType> resourceTypes = null; - try { - long start = System.currentTimeMillis(); - resourceTypes = discoveryComponent.discoverChildResourceTypes(); - long duration = (System.currentTimeMillis() - start); - - if (duration > 2000) { - log.info("[PERF] Discovery of childResourceTypes for [" + discoveryComponent + "] took [" + duration - + "ms]"); - } - } catch (Throwable t) { - - log.warn("Failure to discover childResourceType data - cause: " + ThrowableUtil.getAllMessages(t)); - } - - return resourceTypes; - } -} +package org.rhq.core.pc.inventory; + +/* + * RHQ Management Platform + * Copyright (C) 2005-2008 Red Hat, Inc. + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.Callable; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.rhq.core.clientapi.agent.PluginContainerException; +import org.rhq.core.domain.measurement.AvailabilityType; +import org.rhq.core.domain.resource.Resource; +import org.rhq.core.domain.resource.ResourceCategory; +import org.rhq.core.domain.resource.ResourceType; +import org.rhq.core.pc.PluginContainer; +import org.rhq.core.pc.util.ComponentUtil; +import org.rhq.core.pc.util.FacetLockType; +import org.rhq.core.pluginapi.inventory.ChildResourceTypeDiscoveryFacet; +import org.rhq.core.util.exception.ThrowableUtil; + +public class ChildResourceTypeDiscoveryRunner implements Callable<Set<ResourceType>>, Runnable { + + private Log log = LogFactory.getLog(ChildResourceTypeDiscoveryRunner.class); + + //Default Ctor + public ChildResourceTypeDiscoveryRunner() { + + } + + public void run() { + try { + call(); + } catch (Exception e) { + log.error("Could not get measurement report.", e); + } + } + + public Set<ResourceType> call() { + + if (log.isDebugEnabled()) { + log.info("<ChildResourceTypeDiscoveryRunner>call() called"); + } + + //Set<ResourceTypes> for the ResourceTypes which shall be added later + Set<ResourceType> resourceTypes = null; + + long start = System.currentTimeMillis(); + + //get InventoryManager instance + InventoryManager im = PluginContainer.getInstance().getInventoryManager(); + + if (log.isDebugEnabled()) { + log.info("InventoryManager instance created"); + } + + //Get current plattform + Resource platform = im.getPlatform(); + if (log.isDebugEnabled()) { + log.info("Platform returned with name: " + platform.getName()); + } + + // Next discover all other services and non-top-level servers + Set<Resource> children = platform.getChildResources(); + if (log.isDebugEnabled()) { + log.info("Platform " + platform.getName() + " has " + children.size() + " ChildResources"); + } + + if (children != null) { + for (Resource child : children) { + if (log.isDebugEnabled()) { + log.debug("Name of server: " + child.getName()); + log.debug("Id of server: " + child.getId()); + log.debug("Category of server: " + child.getResourceType().getCategory().toString()); + } + + //Check if really is of Category SERVER because our Plugin has to be of that category + if (child.getResourceType().getCategory() == ResourceCategory.SERVER) { + + if (log.isDebugEnabled()) { + log.info("Server " + child.getName() + "has passed the Server Category test succesfull"); + } + + ResourceContainer container = im.getResourceContainer(child.getId()); + + if (log.isDebugEnabled()) { + log.info("Server " + child.getName() + " is running in ResourceContainer " + + container.toString()); + } + + if (container.getResourceComponentState() != ResourceContainer.ResourceComponentState.STARTED + || container.getAvailability() == null + || container.getAvailability().getAvailabilityType() == AvailabilityType.DOWN) { + // Don't collect metrics for resources that are down + if (log.isDebugEnabled()) { + log.info("ChildType not discoverd for inactive resource component: " + + container.getResource()); + } + } else { + + try { + + //Get Facet Component + ChildResourceTypeDiscoveryFacet discoveryComponent = ComponentUtil.getComponent(child + .getId(), ChildResourceTypeDiscoveryFacet.class, FacetLockType.READ, 30 * 1000, true, + true); + + //get Set<ResourceType> --> all the ChildResourceTypes which shall be added dynamically + resourceTypes = discoverChildResourceTypes(discoveryComponent); + + if (log.isDebugEnabled()) { + log.info("Container.getResource(): " + container.getResource().getName()); + log.info("Container.getResource().getResourceType(): " + + container.getResource().getResourceType().getName()); + + } + + //all the ChildResourceTypes which are already part of the plugin + Set<ResourceType> currentChildTypes = container.getResource().getResourceType() + .getChildResourceTypes(); + + if (log.isDebugEnabled()) { + log.info("Current existing types:"); + for (ResourceType type : currentChildTypes) { + log.info(type.getName()); + } + } + + Set<ResourceType> newTypesToAdd = new HashSet<ResourceType>(); + + if (log.isDebugEnabled()) { + log.info("Size of HashSet<ResourceType> after initialisation: " + newTypesToAdd.size()); + } + + //Check all types with were added by the plugin + for (ResourceType newTypetoAdd : resourceTypes) { + //Check all ChildResourceTypes that already exist + boolean childAllreadyExists = false; + + for (ResourceType alreadyExistingType : currentChildTypes) { + //Check if name and plugin of the types are equal + //Necessary because equal-named ChildResourceTypes can belong to different plugins + if (newTypetoAdd.getName().equals(alreadyExistingType.getName()) + && newTypetoAdd.getPlugin().equals(alreadyExistingType.getPlugin())) { + log.info("The ResourceType " + newTypetoAdd.getName() + + " already exists for the Plugin " + newTypetoAdd.getPlugin()); + + //if ResourceType already exists set boolean var to true + childAllreadyExists = true; + } + } + + //Check if type does not already exist, only add ResourceType to set if so + if (!childAllreadyExists) { + if (log.isDebugEnabled()) { + log.info("new ChildResourceType " + newTypetoAdd.getName() + + " added to Set<ResourceTypes>"); + } + newTypesToAdd.add(newTypetoAdd); + } + } + + //Create a new ResourceType in the DB for the selected type + //call InventoryManager method only if Set<ResourceType> contains at least one element + if (newTypesToAdd.size() > 0) { + + //pass the Set<Resourcetype> to the inventory manager + im.createNewResourceType(newTypesToAdd); + } + + } catch (PluginContainerException pce) { + // This is expected when the ResourceComponent does not implement the ChildResourceTypeDiscoveryFacet + log.warn("Error submitting service scan: " + pce.getMessage()); + } catch (Exception e) { + throw new RuntimeException("Error submitting service scan", e); + } + } + } + } + + } else { + log.info("Set<ResourceType> was returned with value null"); + } + + return null; + } + + /** + * + * @param discoveryComponent + * @return + */ + private Set<ResourceType> discoverChildResourceTypes(ChildResourceTypeDiscoveryFacet discoveryComponent) { + + Set<ResourceType> resourceTypes = null; + try { + long start = System.currentTimeMillis(); + resourceTypes = discoveryComponent.discoverChildResourceTypes(); + long duration = (System.currentTimeMillis() - start); + + if (duration > 2000) { + log.info("[PERF] Discovery of childResourceTypes for [" + discoveryComponent + "] took [" + duration + + "ms]"); + } + } catch (Throwable t) { + + log.warn("Failure to discover childResourceType data - cause: " + ThrowableUtil.getAllMessages(t)); + } + + return resourceTypes; + } +} 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 5daf22a..ec7bb6e 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 @@ -607,8 +607,6 @@ public class InventoryManager extends AgentService implements ContainerService, MergeResourceResponse mergeResourceResponse; Resource resource = null; boolean resourceAlreadyExisted = false; - Throwable startError = null; - try { ResourceContainer parentResourceContainer = getResourceContainer(parentResourceId); ResourceComponent parentResourceComponent = parentResourceContainer.getResourceComponent(); @@ -700,14 +698,7 @@ public class InventoryManager extends AgentService implements ContainerService, if (log.isDebugEnabled()) { log.debug("Activating resource [" + resource + "]..."); } - // if it fails to start keep going, we already have the resource in inventory and - // need to coordinate with the server. The new resource will be unavailable but at least - // it will be accessible and editable by the user. Report the start exception at the end. - try { - activateResource(resource, resourceContainer, newPluginConfig); - } catch (Throwable t) { - startError = t; - } + activateResource(resource, resourceContainer, newPluginConfig);
// NOTE: We don't mess with inventory status - that's the server's responsibility.
@@ -723,12 +714,6 @@ public class InventoryManager extends AgentService implements ContainerService, newResources.add(resource); postProcessNewlyCommittedResources(newResources); performServiceScan(resource.getId()); - - if (null != startError) { - throw new PluginContainerException("The resource [" + resource - + "] has been added but could not be started. Verify the supplied configuration values: ", - startError); - } }
// Catch any other RuntimeExceptions or Errors, so the server doesn't have to worry about deserializing or @@ -883,6 +868,9 @@ public class InventoryManager extends AgentService implements ContainerService, if (!configuration.isInsideAgent()) { return true; } + if (report.getAddedRoots().isEmpty()) { + return true; // nothing to do + }
ResourceSyncInfo syncInfo; try { @@ -2176,12 +2164,6 @@ public class InventoryManager extends AgentService implements ContainerService, log.info("Got unknown resource: " + syncInfo.getId()); } else { Resource resource = container.getResource(); - if (log.isDebugEnabled()) { - log.debug("Local Resource: id=" + resource.getId() + ", status=" + resource.getInventoryStatus() - + ", mtime=" + resource.getMtime()); - log.debug("Sync Resource: " + syncInfo.getId() + ", status=" + syncInfo.getInventoryStatus() - + ", mtime=" + syncInfo.getMtime()); - }
if (resource.getInventoryStatus() != InventoryStatus.COMMITTED && syncInfo.getInventoryStatus() == InventoryStatus.COMMITTED) { @@ -2454,9 +2436,31 @@ public class InventoryManager extends AgentService implements ContainerService, DiscoveryServerService serverService = configuration.getServerServices().getDiscoveryServerService();
if (serverService != null) { - //Call method to add a new ResourceType in the server DB - serverService.addNewResourceType(resourceTypes);
+ try { + //Pass Set<ResourceType> to the server and do persistence there + serverService.addNewResourceType(resourceTypes); + + //Add new ResourceTypes to the local inventory of the IM too + for (ResourceType childType : resourceTypes) { + if (log.isDebugEnabled()) { + log.info("Current child ResourceType: " + childType.getName()); + } + Set<ResourceType> parentTypes = childType.getParentResourceTypes(); + + for (ResourceType parentType : parentTypes) { + if (log.isDebugEnabled()) { + log.info("Current parent ResourceType where child type will be added: " + + parentType.getName()); + } + //Hang the new child ResourceType in the tree + parentType.addChildResourceType(childType); + } + } + + } catch (Exception e) { + log.error(e); + } } }
rhq-commits@lists.fedorahosted.org