Trigger Service HOWTO - Using Triggers To Notify By Email

In this HowTo, we will use a GT4/MDS4 Trigger as a way to monitor resource information from another GT4 service. This HowTo is an extension of this tutorial, which we will refer to in this document as the base tutorial, this document, being an extension.

Basically we will use everything from the base tutorial, but here we will add to the action script to enable an email notification, essentially by substituting the contents of the current document with Step 3 - Set up the trigger action script of the base tutorial. So if you have not followed the base tutorial, please refer to it.

1. Prerequisites

2. Overview of the process

The following are all the steps required for setting up the Trigger Service. This document will cover Step 3 (by adding extra functionality) and Step 5 only. For details on the other steps, see this tutorial.

  1. Set up the GT4 environment
  2. Write the trigger configuration file
  3. Set up the trigger action script
  4. Create the trigger
  5. Test the trigger

3. Step 3 - Set up the trigger action script

  1. Write the first trigger action script. The action script can be written in almost anything, but it must output valid XML to stdout. We will be using a Perl script to parse a bit of the trigger output and format this information, then write it to a log file.

    You may copy the contents of the following Perl script. It is not necessarily the best way to write a script, but it will give you an example of what is possible!

    This action script will be created here:

    $GLOBUS_LOCATION/libexec/trigger/our-simple-trigger-action.pl

    #!/usr/bin/perl
    
    # CHANGE THIS to where you want to place your log file.  
    # DO NOT use an environment variable in this example; it should be the full path.
    $logfile = "/your-install-location/our_new_entry_detected";
    
    # Grab the date
    $date = `date`; chomp($date);
    
    # Collect the trigger input
    $string = <STDIN>;
    
    # Check on the log file
    $OUTFILE = $logfile;if(!open(OUTFILE, ">$logfile"))
    {warn "Couldn't open $logfile: $!";}
    
    # Split the output to separate lines
    $string =~ s/</ggg</g;
    my @records = split("ggg",$string);
    
    our @newrecs;
    
    # Parse each line of input for signs of FreeCPUs
    foreach $record (@records)
    {
    if($record =~ /FreeCPUs=\W+(\d+)\W+/) {
    $str = "$date :: FreeCPUs: $1\n";
    # This step adds the occurences to an array, and you
    # can decide what you want to do with it.
    push @newrecs, $str;
    }
    }
    
    # Output the first line of the array to the logfile.
    print OUTFILE ($newrecs[0]);
    `sh /your-install-location/libexec/trigger/trigger-action-send-mail.sh $logfile`;
    
    # NOTE, in the case that the FreeCPU occurences are not identical, 
    #you can decide which values are more important for your purposes.
    
    # All trigger action scripts must output valid XML.
    print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
    <OurActionScriptOutput>
    <OurDataDetected>true</OurDataDetected>
    </OurActionScriptOutput>";
            

    There are a few modifications from the action script in the original base tutorial:

    1. We rewrite the log file rather than append to it:

      open(OUTFILE, ">$logfile"))

      replaces

      open(OUTFILE, ">>$logfile"))
    2. We add a line where we invoke the email script:

      `sh /your-install-location/libexec/trigger/trigger-action-send-mail.sh $logfile`;
  2. Edit the email script. You can download one from here: email script. The sample below highlights some relevant sections from this script. You can put this file where you wish; we've put it with the other trigger action scripts:

    $GLOBUS_LOCATION/libexec/trigger/trigger-action-send-mail.sh

    #!/bin/bash
    
    # USER_EMAIL="foo@foo.com"
    USER_EMAIL="your-email-address"
    
    # The mailer used for sending mail. For example:
    MAILER="mailx"
    
    # Customize the e-mail's subject line here
    EMAIL_SUBJECT="*** The Configured Trigger has fired  ***"
    
    # send the mail
    cat $1 | $MAILER -s "$EMAIL_SUBJECT" $USER_EMAIL
            
  3. Modify the permissions on the action script. Be sure the the action script and the email script are executable:

    chmod a+x $GLOBUS_LOCATION/libexec/trigger/our-simple-trigger-action.sh
    chmod a+x $GLOBUS_LOCATION/libexec/trigger/trigger-action-send-mail.sh
  4. Edit the jndi-config.xml file. Remember in our trigger configuration file, we specified the name of our action script ("our-simple-trigger") using the <trigger:actionScript> node.

    Now we must edit the file: $GLOBUS_LOCATION/etc/globus_wsrf_mds_trigger/jndi-config.xml

    Find the executableMappings section of the file, and we will add our trigger action script mapping to the file.

    ...
    <jndiConfig>
      <global>
      
        <resource name="triggerConfiguration">
          <resourceParams>
              <parameter>  1
              <name>executableMappings</name>
              <value>test-trigger=test-trigger-action.sh, 
              glue-trigger=glue-trigger-action.sh, 
              echo-trigger=echo-trigger-action.sh,  2
              our-simple-trigger=our-simple-trigger-action.sh
          </value>
          </parameter>
          </resourceParams>
        </resource>
      </global>
    </jndiConfig>
    ...
            
    1 PLEASE note that this section may be commented out. You must uncomment it!
    2 Don't forget to add a comma here.

4. Step 5 - Test the trigger

  1. Now that our trigger is registered, it's ready to perform actions. We set our trigger to fire about every two minutes or so to let us know how many FreeCPUs there are and we set our action script to parse and log this information to a convenient location: $GLOBUS_LOCATION/our_new_entry_detected.

    Now we want to check that file to see if a new entry/log has been recorded.

  2. After a few minutes, if everything went properly, we should be able to open the file: $GLOBUS_LOCATION/our_new_entry_detected and see something like the following:

    Fri Dec  8 15:33:34 PST 2006 :: FreeCPUs: 81 

    In addition, if you properly configured the email script, you should get an email to the address you specified containing the content of this logfile in the body of the email.

5. Congratulations!

If all went well, then you have successfully set up a trigger providing email notifications!