Contact Us 1-800-596-4880

Scripting Examples

This page provides examples of using scripts. You can copy and modify these example scripts for your own use. The scripts include placeholders where you must substitute your own values or parameters.

When to Use Scripts

Scripts are useful for tasks you want to automate. Often, you need to perform tasks across several, or even many, Mule servers. You may also need to do these tasks on a regular basis, such as daily or weekly. While you can perform them manually, it makes sense to consider writing a script to take care of this work.

There are several good uses for scripts.

  • Customize an alert escalation action. When the console detects an alert, you can customize the escalation action that is performed. For example, you might want to send an email to one or more users, log a message about the alert condition, or perform an SNMP escalation. See the examples section below for sample scripts to perform SNMP escalation and to send email.

  • Download log files from multiple Mule servers.

  • Change some Mule server configuration property values. You might want to change certain configuration properties that can be set via bulk actions.

  • Monitor certain server actions, whether alerts or other actions, and send regularly scheduled reports or email notifications.

  • Restart a server.

To help with scripts, the console provides a set of helper utility classes. These helper classes are specifically designed to make it easy for you to send email to a user, send email to a user group listed in the console, and trigger SNMP escalations.

Alerting Examples

The console includes a set of classes to help you take actions when alerts are raised within the console. Out of the box there is support for sending emails or SNMP traps when an alert occurs. You can also build your own custom actions.

If you choose to build a custom action, you can configure the action using the AlertActionBuilder class. This class allows you to listen for alerts for particular servers, filter the alerts, and then take some specific action.

The basic format for any custom action is as follows:

  • Specify a name for your alert action.

  • Specify the servers to which the action applies.

  • Optionally, specify a filter.

  • Specify an action to take.

  • Install the action.

The following is the code for the AlertActionBuilder class:

import com.mulesoft.console.alert.builder.AlertActionBuilder;
new AlertActionBuilder()
    .name("EmailAction")
    .servers("test")
    .filter(...)
    .doAction(...)
    .install(applicationContext);

The AlertActionBuilder methods that are invoked are as follows:

  • name(): The name for your alert action (required). When the install method is called, it uses this name to determine if there is a previously installed action with the same name. If it finds a matching action, then the install method uninstalls the previously installed action and then installs this new action.

  • servers(): An optional list of server names. If specified, actions are only taken for the specified servers.

  • filter(): An AlertFilter that filters out some alerts (based on the filter condition) before taking an action

  • doAction(): An AlertAction to take when an alert is raised.

Example Email Alerts

You can configure email alert actions using the EmailActionBuilder class. Here is a simple example that emails an alert:

import com.mulesoft.console.alert.builder.AlertFactory
import com.mulesoft.console.alert.builder.AlertActionBuilder;
import com.mulesoft.console.alert.builder.EmailActionBuilder;

new AlertActionBuilder()
    .name("EmailAction")
    .doAction(
           new EmailActionBuilder()
                .server("localhost")
                .port(8025)
                .from("mmc@mulesoft.com")
                .to("you@mulesoft.com")
                .build())
    .install(applicationContext);

Using the methods on the EmailActionBuilder class, you can customize the SMTP server, the port, the from and to addresses, along with specifying the subject and body of the message.

You can customize how the subject and body are generated using the subject() and body() methods of the EmailActionBuilder class. For example:

import com.mulesoft.console.alert.builder.AlertFactory
import com.mulesoft.console.alert.builder.AlertActionBuilder;
import com.mulesoft.console.alert.builder.EmailActionBuilder;

def action = new AlertActionBuilder()
    .name("EmailAction")
    .doAction(
           new EmailActionBuilder()
                .server("localhost")
                .port(8025)
                .from("mmc@mulesoft.com")
                .to("you@mulesoft.com")
                .subject({ "Test subject: ${it.description}" } as AlertFactory)
                .body({ "Test body: ${it.description}" } as AlertFactory)
                .build())
    .install(applicationContext);
"Email alert successful installed"

In the above code example, the subject() and body() methods take an AlertFactory that generates a String variable from a RaisedAlert.

Here is a more complex example that specifies a user name and password, and also specifies that SSL be used with Gmail:

import com.mulesoft.console.alert.builder.AlertFactory
import com.mulesoft.console.alert.builder.AlertActionBuilder;
import com.mulesoft.console.alert.builder.EmailActionBuilder;

new AlertActionBuilder()
    .name("EmailAction")
    .servers("test")
    .doAction(
           new EmailActionBuilder()
                .useSSL()
                .server("smtp.gmail.com")
                .port(465)
                .from("xxx@gmail.com")
                .username("xxx@gmail.com")
                .password("xxx")
                .to("xxx@xxx.com")
                .subject({ "You have an alert! ${it.description}" } as AlertFactory)
                .body({ "${it.description}" } as AlertFactory)
                .build())
    .install(applicationContext);
"Email alert successful installed"

Inserting Expressions in Email Subject or Body

You can insert an expression in an email subject or body by placing the expression itself within curly braces, proceeded by the dollar sign ($) symbol; that is:

${<expression>}

The above examples do this for both subject and body; for example:

{{.subject({ "You have an alert! ${it.description}" } }...

An expression enclosed as shown above is converted into a string.

Script to Perform SNMP Escalation for an Alert

The SnmpActionBuilder class includes methods allowing you to create scripts that send an SNMP trap from a raised alert. You must specify three parameters: an SNMP OID (object identifier), a local address to bind to, and a target address to send to.

import com.mulesoft.console.alert.builder.AlertFactory
import com.mulesoft.console.alert.builder.AlertActionBuilder;
import com.mulesoft.console.alert.builder.SnmpActionBuilder;
new AlertActionBuilder()
            .name("SnmpAction")
            .servers("MyServer")
            .doAction(
                new SnmpActionBuilder()
                    .oid("1.3.6")
                    .localAddress("udp:127.0.0.1/40162")
                    .targetAddress("udp:127.0.0.1/40163")
                    .build())
            .install(applicationContext);
"SNMP trap alert successful installed"

By default the SnmpActionBuilder sends the value of RaisedAlert.getDescription as the value of your SNMP trap. You can customize this value, however, by using an AlertFactory. For instance, suppose you want to send the current memory usage as the value. You might set up the code as follows:

import com.mulesoft.console.alert.builder.AlertFactory
import com.mulesoft.console.alert.builder.AlertActionBuilder;
import com.mulesoft.console.alert.builder.SnmpActionBuilder;
new AlertActionBuilder()
            .name("SnmpAction")
            .servers("MyServer")
            .doAction(
                new SnmpActionBuilder()
                    .oid("1.3.6")
                    .localAddress("udp:127.0.0.1/40162")
                    .targetAddress("udp:127.0.0.1/40163")
                    .value({ it.getActualValue() } as AlertFactory)
                    .build())
            .install(applicationContext);
"SNMP trap alert successful installed"

The getActualValue method returns the current memory usage for raised memory alerts.

Script to Implement a Custom Action

The following is a sample script that implements a custom action when an alert occurs. In this sample script, the custom action merely prints a message. However, you can easily substitute your own action. To use this script, write your own custom action and then paste the script into the administration shell.

import com.mulesoft.common.server.ServerHealthEvent
import org.mule.galaxy.event.annotation.Async
import org.mule.galaxy.event.annotation.BindToEvent
import org.mule.galaxy.event.annotation.OnEvent

@BindToEvent("com.mulesoft.common.server.events.ServerHealthEvent")
class AnnoTestListener {

    @Async @OnEvent
    public void onServerHealthEvent(ServerHealthEvent event) {
        println "===> server health alert"
    }

}

// main block
def em = applicationContext.getBean('eventManager')
assert em != null

// remove the previous listener, if any
eventManager.listeners.each {
    if (it.class.simpleName == AnnoTestListener.class.simpleName) {
        eventManager.removeListener(it)
        println "Removed previous listener"
    }
}
em.addListener(new AnnoTestListener())

Script to Restart a Server

The following example script can be used to restart a server. In this example, the server to be restarted is called MyServer. To use this script, you would change MyServer to the name of your own server.

svrMgr = applicationContext.getBean("serverManager")
svr = svrMgr.getServerByName("MyServer", false)
foo = svrMgr.restartServerNow(svr.getId())