How to Create Agent Plugins for RHQ

SampleComponent.java

package br.eti.fernandoribeiro.sample;

import org.rhq.core.domain.configuration.Configuration;
import org.rhq.core.domain.configuration.PropertySimple;
import org.rhq.core.domain.measurement.AvailabilityType;
import org.rhq.core.pluginapi.inventory.ResourceComponent;
import org.rhq.core.pluginapi.inventory.ResourceContext;
import org.rhq.core.pluginapi.operation.OperationFacet;
import org.rhq.core.pluginapi.operation.OperationResult;
import org.rhq.plugins.platform.PlatformComponent;

public final class SampleComponent implements ResourceComponent<PlatformComponent>, OperationFacet {

  @Override
  public AvailabilityType getAvailability() {
    return AvailabilityType.UP;
  }

  @Override
  public OperationResult invokeOperation(final String name, final Configuration parameters) {

    if ("sampleOperation".equals(name)) {
      final PropertySimple sampleProperty = parameters.getSimple("sampleProperty");

      ...
    } else {
      throw new UnsupportedOperationException("The operation " + name + " is not supported");
    }

  }

  @Override
  public void start(final ResourceContext<PlatformComponent> context) {
  }

  @Override
  public void stop() {
  }

}

SampleDiscoveryComponent.java

package br.eti.fernandoribeiro.sample;

import java.util.HashSet;
import java.util.Set;

import org.rhq.core.domain.configuration.Configuration;
import org.rhq.core.pluginapi.inventory.DiscoveredResourceDetails;
import org.rhq.core.pluginapi.inventory.ResourceDiscoveryComponent;
import org.rhq.core.pluginapi.inventory.ResourceDiscoveryContext;
import org.rhq.plugins.platform.PlatformComponent;

public final class SampleDiscoveryComponent implements ResourceDiscoveryComponent<PlatformComponent> {

  @Override
  public Set<DiscoveredResourceDetails> discoverResources(final ResourceDiscoveryContext<PlatformComponent> context) {
    return new HashSet<DiscoveredResourceDetails>();
  }

}

rhq-plugin.xml

<?xml version="1.0" encoding="UTF-8"?>
<rhq:plugin description="Sample Plugin" displayName="Sample" name="Sample" package="br.eti.fernandoribeiro.sample" version="1.0" xmlns:rhq="urn:xmlns:rhq-plugin" xmlns:c="urn:xmlns:rhq-configuration">
  <rhq:server class="SampleComponent" description="A sample component" discovery="SampleDiscoveryComponent" name="SampleComponent">
    <rhq:plugin-configuration>
      <c:simple-property description="A sample property" displayName="Sample Property" name="sampleProperty" required="true" />
        <c:constraint>
          <c:integer-constraint minimum="0" maximum="32767" /> <!-- float and regex also supported -->
        </c:constraint>
      </c:simple-property>
    </rhq:plugin-configuration>
    <rhq:operation name="sampleOperation" description="Performs the operation" />
  </rhq:server>
</rhq:plugin>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s