modules/core/client-api/src/main/java/org/rhq/core/clientapi/agent/metadata/PluginMetadataManager.java | 21 + modules/core/client-api/src/main/java/org/rhq/core/clientapi/agent/metadata/PluginMetadataParser.java | 77 ++++++- modules/plugins/nagios/src/main/java/org/rhq/plugins/nagios/NagiosMonitorChildTypeComponent.java | 109 +++++----- modules/plugins/nagios/src/main/java/org/rhq/plugins/nagios/NagiosMonitorChildTypeDiscovery.java | 103 ++++----- 4 files changed, 201 insertions(+), 109 deletions(-)
New commits: commit 29ff68ac16801f0da8cadbdc7b37f8cd6dcbdc33 Author: Alexander Kiefer alexander.kiefer@dillinger.biz Date: Fri Oct 15 01:20:13 2010 +0200
Made some changes to enable the usage of child type discovery and component classes
diff --git a/modules/core/client-api/src/main/java/org/rhq/core/clientapi/agent/metadata/PluginMetadataManager.java b/modules/core/client-api/src/main/java/org/rhq/core/clientapi/agent/metadata/PluginMetadataManager.java index 0c5e0b6..f2dc59b 100644 --- a/modules/core/client-api/src/main/java/org/rhq/core/clientapi/agent/metadata/PluginMetadataManager.java +++ b/modules/core/client-api/src/main/java/org/rhq/core/clientapi/agent/metadata/PluginMetadataManager.java @@ -55,7 +55,7 @@ public class PluginMetadataManager { private Object typesLock = new Object();
private Map<String, PluginMetadataParser> parsersByPlugin = new HashMap<String, PluginMetadataParser>(); - + public PluginMetadataManager() { }
@@ -89,13 +89,28 @@ public class PluginMetadataManager { }
public String getDiscoveryClass(ResourceType resourceType) { + PluginMetadataParser parser = this.parsersByPlugin.get(resourceType.getPlugin()); - return (parser != null) ? parser.getDiscoveryComponentClass(resourceType) : null; + + //TODO: make it more generic without hardcoded comparison + if (parser.getDescriptor().getName().equals("NagiosMonitor")) { + return (parser != null) ? parser.getChildTypeDiscoveryComponentClass(resourceType) : null; + } else { + return (parser != null) ? parser.getDiscoveryComponentClass(resourceType) : null; + } + }
public String getComponentClass(ResourceType resourceType) { PluginMetadataParser parser = this.parsersByPlugin.get(resourceType.getPlugin()); - return (parser != null) ? parser.getComponentClass(resourceType) : null; + + //TODO: make it more generic without hardcoded comparison + if (parser.getDescriptor().getName().equals("NagiosMonitor")) { + return (parser != null) ? parser.getChildTypeComponentClass(resourceType) : null; + } else { + return (parser != null) ? parser.getComponentClass(resourceType) : null; + } + }
/** diff --git a/modules/core/client-api/src/main/java/org/rhq/core/clientapi/agent/metadata/PluginMetadataParser.java b/modules/core/client-api/src/main/java/org/rhq/core/clientapi/agent/metadata/PluginMetadataParser.java index 686b16b..95a8943 100644 --- a/modules/core/client-api/src/main/java/org/rhq/core/clientapi/agent/metadata/PluginMetadataParser.java +++ b/modules/core/client-api/src/main/java/org/rhq/core/clientapi/agent/metadata/PluginMetadataParser.java @@ -80,6 +80,13 @@ public class PluginMetadataParser { private Map<ResourceType, String> discoveryClasses = new HashMap<ResourceType, String>(); private Map<ResourceType, String> componentClasses = new HashMap<ResourceType, String>();
+ private String childTypeDiscoveryClass; + private String childTypeComponentClass; + + //Hashmaps for ChildType discovery and component classes + private Map<ResourceType, String> childTypeDiscoveryClasses = new HashMap<ResourceType, String>(); + private Map<ResourceType, String> childTypeComponentClasses = new HashMap<ResourceType, String>(); + // a map keyed on plugin name that contains the parsers for all other known plugin descriptors // this map is managed by this parser's PluginMetadataManager and is how the manager shares information // from other plugins to this parser instance @@ -87,6 +94,10 @@ public class PluginMetadataParser {
public PluginMetadataParser(PluginDescriptor descriptor, Map<String, PluginMetadataParser> parsersByPlugin) throws InvalidPluginDescriptorException { + + this.childTypeDiscoveryClass = null; + this.childTypeComponentClass = null; + this.pluginDescriptor = descriptor; this.parsersByPlugin = parsersByPlugin; parseDescriptor(); @@ -189,7 +200,23 @@ public class PluginMetadataParser { .setCreateDeletePolicy(convertCreateDeletePolicy(serverDescriptor.getCreateDeletePolicy())); serverResourceType.setSingleton(serverDescriptor.isSingleton());
- parseResourceDescriptor(serverDescriptor, serverResourceType, null, null, null); + String childTypeDiscovery = null; + String childTypeClass = null; + + //get childType discovery and component class names + this.childTypeDiscoveryClass = serverDescriptor.getChildTypeDiscovery(); + this.childTypeComponentClass = serverDescriptor.getChildTypeClass(); + + //Check if it is the Nagios plugin and if the serverResourceType is a child + //Only nagios plugin decriptor has child type discovery and component class tags + if (this.childTypeDiscoveryClass != null && this.childTypeComponentClass != null + && parentServerType != null) { + parseResourceDescriptor(serverDescriptor, serverResourceType, this.childTypeDiscoveryClass, + this.childTypeComponentClass, null); + } else { + parseResourceDescriptor(serverDescriptor, serverResourceType, null, null, null); + } + LOG.debug("Parsed server Resource type: " + serverResourceType); } else if ((sourcePlugin.length() > 0) && (sourceServer.length() > 0)) { // using Embedded extension model - the defined type is actually a copy of another plugin's server type @@ -307,7 +334,16 @@ public class PluginMetadataParser { .getCreateDeletePolicy())); serviceResourceType.setSingleton(serviceDescriptor.isSingleton());
- parseResourceDescriptor(serviceDescriptor, serviceResourceType, null, null, null); + //Check if it is the Nagios plugin and if the serviceResourceType is a child + //Only nagios plugin decriptor has child type discovery and component class tags + if (this.childTypeDiscoveryClass != null && this.childTypeComponentClass != null && parentType != null) { + parseResourceDescriptor(serviceDescriptor, serviceResourceType, this.childTypeDiscoveryClass, + this.childTypeComponentClass, null); + } else { + parseResourceDescriptor(serviceDescriptor, serviceResourceType, null, null, null); + + } + LOG.debug("Parsed service Resource type: " + serviceResourceType);
if ((serviceResourceType.getProcessScans() != null) && (serviceResourceType.getProcessScans().size() > 0)) { @@ -597,13 +633,44 @@ public class PluginMetadataParser { return this.componentClasses.get(resourceType); }
+ /** + * Returns the fully qualified name of the resource discovery component class for the given Child ResourceType. + * + * @param resourceType the ResourceType + * @return the resource discovery component class name + */ + public String getChildTypeDiscoveryComponentClass(ResourceType resourceType) { + return this.childTypeDiscoveryClasses.get(resourceType); + } + + /** + * Returns the fully qualified name of the resource component class for the given Child ResourceType. + * + * @param resourceType the ResourceType + * @return the resource component class name + */ + public String getChildTypeComponentClass(ResourceType resourceType) { + return this.childTypeComponentClasses.get(resourceType); + } + private void registerResourceTypeAndComponentClasses(ResourceType resourceType, String discoveryClass, String componentClass) { this.resourceTypes.add(resourceType); - this.componentClasses.put(resourceType, componentClass); - if (discoveryClass != null) { - this.discoveryClasses.put(resourceType, discoveryClass); + + //if it is the child resource type of Nagios plugin + if (discoveryClass.equals(this.childTypeDiscoveryClass) && componentClass.equals(this.childTypeComponentClass)) { + //Put the childresource type and its discovery and component classes to the childtype map + this.childTypeComponentClasses.put(resourceType, componentClass); + if (discoveryClass != null) { + this.childTypeDiscoveryClasses.put(resourceType, discoveryClass); + } + } else { + this.componentClasses.put(resourceType, componentClass); + if (discoveryClass != null) { + this.discoveryClasses.put(resourceType, discoveryClass); + } } + }
private Map<String, ServerDescriptor> getPluginServerDescriptors(String pluginName) {
commit e24a63de7dc5d32cd1a6dee328353b441986d17a Author: Alexander Kiefer alexander.kiefer@dillinger.biz Date: Fri Oct 15 01:18:59 2010 +0200
Made the child type discovery and component classes ready to work
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 index ddb5a50..facde60 100755 --- 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 @@ -21,12 +21,17 @@ package org.rhq.plugins.nagios;
import java.util.HashSet; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern;
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory;
import org.rhq.core.domain.configuration.Configuration; import org.rhq.core.domain.measurement.AvailabilityType; +import org.rhq.core.domain.measurement.DataType; +import org.rhq.core.domain.measurement.MeasurementDataNumeric; +import org.rhq.core.domain.measurement.MeasurementDataTrait; import org.rhq.core.domain.measurement.MeasurementDefinition; import org.rhq.core.domain.measurement.MeasurementReport; import org.rhq.core.domain.measurement.MeasurementScheduleRequest; @@ -38,6 +43,8 @@ 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; +import org.rhq.plugins.nagios.data.NagiosSystemData; +import org.rhq.plugins.nagios.error.NagiosException;
/** * @@ -59,57 +66,57 @@ public class NagiosMonitorChildTypeComponent implements ResourceComponent, Measu
@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); - // } - // } + 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); + } + }
}
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 index 32c8f65..b4f961f 100755 --- 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 @@ -19,18 +19,23 @@ package org.rhq.plugins.nagios; * 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.resource.ResourceType; 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.ResourceComponent; import org.rhq.core.pluginapi.inventory.ResourceDiscoveryComponent; import org.rhq.core.pluginapi.inventory.ResourceDiscoveryContext; +import org.rhq.plugins.nagios.network.NetworkConnection; import org.rhq.plugins.nagios.reply.LqlReply; +import org.rhq.plugins.nagios.request.LqlResourceTypeRequest;
/** * @@ -45,40 +50,40 @@ public class NagiosMonitorChildTypeDiscovery implements ResourceDiscoveryCompone * 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); - // } + 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;
@@ -90,11 +95,11 @@ public class NagiosMonitorChildTypeDiscovery implements ResourceDiscoveryCompone * 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); + LqlResourceTypeRequest resourceTypeRequest = new LqlResourceTypeRequest(); + LqlReply resourceTypeReply; + + NetworkConnection connection = new NetworkConnection(nagiosIp, nagiosPort); + resourceTypeReply = connection.sendAndReceive(resourceTypeRequest);
return null; } @@ -102,18 +107,16 @@ public class NagiosMonitorChildTypeDiscovery implements ResourceDiscoveryCompone @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; + 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; }
}
rhq-commits@lists.fedorahosted.org