Tuesday, December 15, 2009

How to write an alert sender plugin for RHQ/Jopr (Part 2)

Now that we have seen the "theoretical side" of writing an alert sender, lets just start and code one up.

First grab RHQ from GIT and then change into
modules/enterprise/server/plugins.


  • Run this little script:

    mkdir alert-url
    cd alert-url
    mkdir -p src/main/java/org/rhq/enterprise/server/plugins/alertUrl
    mkdir -p src/main/resources/META-INF

  • copy over the pom.xml from alert-email and change email to url in it:

    cp ../alert-email/pom.xml pom.xml,1
    sed -e 's/email/url/' < pom.xml,1 > pom.xml

  • Lets edit the plugin descriptor

    vi src/main/resources/META-INF/rhq-serverplugin.xml

    <alert-plugin
    name="alert-url"
    displayName="Alert:Url"
    xmlns="urn:xmlns:rhq-serverplugin.alert"
    xmlns:c="urn:xmlns:rhq-configuration"
    xmlns:serverplugin="urn:xmlns:rhq-serverplugin"
    package="org.rhq.enterprise.server.plugins.alertUrl"
    description="Alert sender plugin"
    version="1.0"
    >

    This is just the generic header. Now lets set the target host name on the server-wide preferences and

    <serverplugin:plugin-configuration>
    <c:simple-property name="hostName"
    default="localhost" />
    </serverplugin:plugin-configuration>

    Now the name of the sender and its alert sender class

    <short-name>Url</short-name>
     
    <!-- Class that does the actual sending -->
    <plugin-class>UrlSender</plugin-class>

    And now finally the Information that is specific to an alert definition. Lets just set the port here for demonstation purposes.

    <alert-configuration>
    <c:simple-property name="port" type="integer"/>
    </alert-configuration>
    </alert-plugin>

  • Now lets implement the Java sender class

    $ cd src/main/java/org/rhq/enterprise/server/plugins/alertUrl
    $ vi UrlSender.java

    package org.rhq.enterprise.server.plugins.alertUrl;
     
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
     
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
     
    import org.rhq.core.domain.alert.Alert;
    import org.rhq.enterprise.server.alert.AlertManagerLocal;
    import org.rhq.enterprise.server.plugin.pc.alert.AlertSender;
    import org.rhq.enterprise.server.plugin.pc.alert.ResultState;
    import org.rhq.enterprise.server.plugin.pc.alert.SenderResult;
    import org.rhq.enterprise.server.util.LookupUtil;
    import org.rhq.enterprise.server.xmlschema.generated.serverplugin.perspective.ResourceTaskElement;
     
    public class UrlSender extends AlertSender{
     
    @Override
    public SenderResult send(Alert alert) {

    So this was just the bolier plate code - now lets get the work done by first getting our data that the user entered in the UI

    String hostname = preferences.getSimpleValue("hostname","localhost");
    String portS = alertParameters.getSimpleValue("port","80");

    Then construct an URL and URL connection

    HttpURLConnection conn = null;
    try {
    URL url = new URL("http://" + hostname + ":" + portS);
    conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setDoOutput(true);
    conn.setDoInput(true);
    OutputStream out = conn.getOutputStream();

    Then we get the message together - you see, you can even call into the EJB layer to get things done - and send it off.
         
    AlertManagerLocal alertManager = LookupUtil.getAlertManager();
    StringBuilder b = new StringBuilder("Alert: '");
    b.append(alert.getAlertDefinition().getResource().getName());
    b.append("' (");
    b.append(alert.getAlertDefinition().getResource().getId());
    b.append("): ");
    b.append(alertManager.prettyPrintAlertConditions(alert));
     
    out.write(b.toString().getBytes());
    out.flush();
    conn.connect();
    } catch (Exception e) {
    return new SenderResult(ResultState.FAILURE,"Sending failed " + e.getMessage());
    }
    finally {
    if (conn!=null)
    conn.disconnect();
    }
     
    return new SenderResult(ResultState.SUCCESS,"Sending to " + hostname + ":" + portS + " worked");
    }
    }

    After this was done, we return our operation result in a new SenderResult object - that's all.

    Now just change back to the root directory (the one with the pom.xml file) and fire "mvn install". This leaves you with a jar archive in target/ that you can then deploy into a (running) server:

    Deploying the plugin



    You can deploy the plugin in three ways, where the first one is probably the best, when developing such a plugin:

    • run mvn install above with option -Pdev - this will automatically deploy into the development containter
    • copy the plugin manually into the dropbox at $RHQ_SERVER/plugins (that directory is in parallel to bin/ and jbossas/ )
    • Deploy via GUI:


      • Go to the plugins page
        Bildschirmfoto 2009-12-15 um 21.00.37.png


        and select server plugins at the top tab
      • Scroll down to the upload box, select add, select the plugin in the open dialog and then when done, click on "upload".
      • Press "scan for updates" at the bottom of the plugin list above - it should now show up in the list of plugins


    Configuring it



    Click on "Alert:Url" in the list of plugins. This will bring you to the plugin details. And then on "Configure Alert:Url" (configuring could also happen from the Administration->System Configuration->Settings menu).

    You will see a list of plugins where the Alert:Url one is selected on the left and the current site wide preferences on the right:

    Bildschirmfoto 2009-12-15 um 21.41.18.png


    If you click on Edit, you can edit the host to some other value than the default.

    Using it in an alert definition




    Go to the usual screen for alert definitions of a resource - in the preliminary gui, there is a link To Alert sender plugins; click it.

    You will come to a screen with a drop down - select Url from it

    THIS UI IS CURRENTLY WORK IN PROGRESS

    Click on "Go" and then in the configuration enter the port number to use. Click on "submit it".

    Done

    Congratulations


    you have just written and deployed your first alert sender plugin for RHQ and Jopr




No comments: