Tag Archives: RHQ

How to Support Manual Add in RHQ Agent Plugins

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.ManualAddFacet;
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>, ManualAddFacet<PlatformComponent> {

  @Override
  public DiscoveredResourceDetails discoverResource(final Configuration pluginConfiguration, final ResourceDiscoveryContext<PlatformComponent> context) {
    return null;
  }

}

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" supportsManualAdd="true">
    ...
  </rhq:server>
</rhq:plugin>
Advertisements

Issue with RHQ 4.5.1

If you get the classloading issues mentioned here, just change the <RHQ Install Dir>/conf/agent-configuration.xml file:

<node name="${rhq.agent.preferences-node}">
  <map>
    ...
    <entry key="rhq.agent.plugins.root-plugin-classloader" value="\"\"" />
  </map>
</node>

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>

How to Get the History of an Operation for a Resource with the RHQ REST API

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

@SuppressWarnings("serial")
@WebServlet(name = "SampleRhqServlet", value = "/")
public final class SampleRhqServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
    final Client client = ClientBuilder.newClient();

    client.register(HttpAuthenticationFeature.basic("rhqadmin", "rhqadmin"));

    final Response res = client.target("http://localhost:7080/rest/1/operation/history.xml?resourceId=1").get();

    resp.getWriter().println(res.readEntity(String.class));

    client.close();
  }

}

How to Delete the History of an Operation with the RHQ REST API

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;

import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

@SuppressWarnings("serial")
@WebServlet(name = "SampleRhqServlet", value = "/")
public final class SampleRhqServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) {
    final Client client = ClientBuilder.newClient();

    client.register(HttpAuthenticationFeature.basic("rhqadmin", "rhqadmin"));

    client.target("http://localhost:7080/rest/1/operation/history/1").delete();

    client.close();
  }

}

How to Get the History of an Operation with the RHQ REST API

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

@SuppressWarnings("serial")
@WebServlet(name = "SampleRhqServlet", value = "/")
public final class SampleRhqServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
    final Client client = ClientBuilder.newClient();

    client.register(HttpAuthenticationFeature.basic("rhqadmin", "rhqadmin"));

    final Response res = client.target("http://localhost:7080/rest/1/operation/history/1.xml").get();

    resp.getWriter().println(res.readEntity(String.class));

    client.close();
  }

}

How to Submit an Operation with the RHQ REST API

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

import org.rhq.enterprise.server.rest.domain.OperationRest;

@SuppressWarnings("serial")
@WebServlet(name = "SampleRhqServlet", value = "/")
public final class SampleRhqServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
    final Client client = ClientBuilder.newClient();

    client.register(HttpAuthenticationFeature.basic("rhqadmin", "rhqadmin"));

    final OperationRest operation = new OperationRest();

    operation.setName("sampleOperation");

    operation.setReadyToSubmit(true);

    operation.setResourceId(1);

    final Response res = client.target("http://localhost:7080/rest/1/operation/1").put(Entity.entity(operation));

    resp.getWriter().println(res.readEntity(String.class));

    client.close();
  }

}

How to Get an Operation with the RHQ REST API

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

@SuppressWarnings("serial")
@WebServlet(name = "SampleRhqServlet", value = "/")
public final class SampleRhqServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
    final Client client = ClientBuilder.newClient();

    client.register(HttpAuthenticationFeature.basic("rhqadmin", "rhqadmin"));

    final Response res = client.target("http://localhost:7080/rest/1/operation/1.xml").get();

    resp.getWriter().println(res.readEntity(String.class));

    client.close();
  }

}

How to Delete an Operation with the RHQ REST API

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;

import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

@SuppressWarnings("serial")
@WebServlet(name = "SampleRhqServlet", value = "/")
public final class SampleRhqServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) {
    final Client client = ClientBuilder.newClient();

    client.register(HttpAuthenticationFeature.basic("rhqadmin", "rhqadmin"));

    client.target("http://localhost:7080/rest/1/operation/1").delete();

    client.close();
  }

}