Sun GlassFish Enterprise Manager Monitoring Scripting Client 3.0 Installation and Quick Start Guide

Writing Scripts in the JavaScript Language for Monitoring Enterprise Server

Monitoring Scripting Client enables you to write clients in the JavaScript programming language to provide monitoring data about Sun GlassFish Enterprise Server.

The following topics are addressed here:

Obtaining Information About Events That Provide Monitoring Data

Components and services that are deployed in the Enterprise Server typically generate statistics that the Enterprise Server can gather at run time. To provide statistics to Enterprise Server, components define events for the operations that generate these statistics. At runtime, components send these events when performing the operations for which the events are defined. For example, to enable the number of received requests to be monitored, a component sends a “request received” event each time that the component receives a request.

Monitoring Scripting Client enables you to list all events that are provided for monitoring Enterprise Server. Detailed information about each of these events is provided to enable you to identify which events provide the statistics that you want to monitor.

Use this information to process appropriately the events of interest in JavaScript programs that you write for monitoring Enterprise Server.

ProcedureTo Obtain a List of Events That Provide Monitoring Data

  1. Ensure that the server is running.

    Remote subcommands require a running server.

  2. Ensure that monitoring is enabled for Enterprise Server.

    If monitoring for Enterprise Server is disabled, no events are listed.

    For information about how to enable monitoring for Enterprise Server, see To Enable Monitoring in Sun GlassFish Enterprise Server v3 Administration Guide.

  3. To include in the list events that are related to a container, ensure that the container is loaded.

    Events that are related to a container are listed only if the container is loaded. For example, to list events that are related to the JRuby container, you must ensure that the JRuby container is loaded by deploying a JRuby application in Enterprise Server.

  4. Run the list-probes subcommand.

    The signatures of all events for all installed components of Enterprise Server are displayed.

    An event signature consists of the event identifier (ID) followed in parentheses by a comma-separated list of the event's parameters. Each parameter is listed as its type followed by its name.

    For detailed information about the format of an event signature, see the help page for the list-probes subcommand.


Example 1–2 Listing All Events

This command lists all events for monitoring Enterprise Server. For better readability, some events that would listed by this example are not shown.


asadmin> list-probes
glassfish:jdbc:connection-pool:connectionRequestDequeuedEvent (java.lang.String 
poolName)
glassfish:jca:connection-pool:connectionsFreedEvent (java.lang.String poolName, 
int count)
glassfish:transaction:transaction-service:deactivated ()
glassfish:kernel:connections-keep-alive:incrementCountFlushesEvent (java.lang.String 
listenerName)
glassfish:kernel:file-cache:countInfoMissEvent (java.lang.String fileCacheName)
glassfish:ejb:timers:timerRemovedEvent ()
glassfish:jdbc:connection-pool:decrementNumConnFreeEvent (java.lang.String poolName)

...
glassfish:kernel:thread-pool:threadAllocatedEvent (java.lang.String monitoringId, 
java.lang.String threadPoolName, java.lang.String threadId)
glassfish:jca:connection-pool:connectionCreatedEvent (java.lang.String poolName)
glassfish:kernel:connection-queue:connectionAcceptedEvent (java.lang.String 
listenerName, int connection)

Command list-probes executed successfully.

See Also

You can also view the full syntax and options of the subcommand by typing asadmin help list-probes at the command line.

ProcedureTo Obtain Detailed Information About an Event That Provides Monitoring Data

The following detailed information is available about events for monitoring Enterprise Server:

  1. Ensure that the server is running.

    Remote subcommands require a running server.

  2. If necessary, obtain the event ID of the event for which you want detailed information.

    For details, see To Obtain a List of Events That Provide Monitoring Data.

  3. Specify the --details option of the list-probes subcommand and the ID of the event as the operand of the subcommand.


Example 1–3 Displaying Detailed Information About an Event

This example displays detailed information about the glassfish:web:web-module:webModuleStartedEvent event.


asadmin list-probes --details glassfish:web:web-module:webModuleStartedEvent

Information similar to the following is displayed.


Events       glassfish:web:web-module:webModuleStartedEvent(5GFP)

NAME
     glassfish:web:web-module:webModuleStartedEvent - web  module
     started event

SYNOPSIS
     glassfish:web:web-module:webModuleStartedEvent(
     java.lang.String appName,
     java.lang.String hostName)

DESCRIPTION
     This event is sent whenever an application has been  started
     (for example, as part of its deployment).

PARAMETERS
     appName

         The name of the web application that has been started.

     hostName
         The name of the virtual server on which the  application
         has been deployed.

Java EE 6           Last change: 19 Nov 2009                    1


Command list-probes executed successfully.

ProcedureTo Register a Script as a Listener for an Event

Registering a script as listener for an event enables the script to listen for the event and to receive callbacks from the Monitoring Scripting Client when the script receives the event. The script can then collect data from the event. Registering a script as listener for an event also specifies the event callback function that is to be called when the event is received. For information about writing an event callback function, see Writing an Event Callback Function.

  1. Create an array of the event parameters to pass to the event callback function.

    This array may contain any number of the event's parameters in any order.

  2. Invoke the scriptContainer.registerListener method.

    In the invocation of the scriptContainer.registerListener method, pass the following information as parameters to the method:

    • The event ID of the event

    • The array of event parameters that you created in the previous step

    • The name of the event callback function that is to be called when the event is received


Example 1–4 Registering a Script as a Listener for an Event

This example registers a script as a listener for the event glassfish:web:jsp:jspLoadedEvent. When this event is received, the event parameter hostName is passed to the jspLoaded() event callback function. For clarity, the declaration of the event callback function jspLoaded() is also shown in this example.

function jspLoaded(hostName) {
    ...
}

params = java.lang.reflect.Array.newInstance(java.lang.String, 1);
params[0]="hostName";

scriptContainer.registerListener('glassfish:web:jsp:jspLoadedEvent', 
    params, 'jspLoaded');

ProcedureTo Display Information From a Script

To provide statistics to system administrators, a script must display information when the script is run. Monitoring Scripting Client provides a preinstantiated object that has a method for displaying information from scripts. You must use this method to display updated information on standard output on the client system where the script is run. You cannot use the standard printing mechanisms of the JavaScript language because they write information to the server log.

  1. Invoke the client.print method.

    In the invocation of the client.print method, pass the text string to display as the parameter to the method.


Example 1–5 Displaying Information From a Script

This example displays a string similar to the following in standard output each time the function jspLoaded() is called.


js> jsp loaded event called on host = server and count = 1
var njspLoaded=0;

function jspLoaded(hostName) {
    njspLoaded = njspLoaded + 1;
    client.print( '\n js> jsp loaded event called on ' +
            'host = ' + hostName +
            ' and count = ' + njspLoaded);
}
...

Writing an Event Callback Function

An event callback function is a function in a script that Monitoring Scripting Client calls in response to an event.

In your event callback functions, provide code to generate statistics from the data in events. Typically, the following types of statistics can be generated from the data in events:

ProcedureTo Generate Counter Statistics

Counter statistics typically correspond to a single event. For example, to calculate the number of received requests, only one event is required, for example, a “request received” event. Every time that a “request received” event is sent, the number of received requests is increased by 1.

  1. Declare and initialize a variable.

  2. Increase or decrease the variable each time the appropriate event is received.


Example 1–6 Generating a Counter Statistic

This example declares and initializes to zero the variable njspLoaded. Each time the callback function jspLoaded() is invoked, the value of this counter is increased by 1.

For the complete listing of the script from which this example is extracted, see Example 1–8.

var njspLoaded=0;

function jspLoaded(hostName) {
    njspLoaded = njspLoaded + 1;
    ...
}
...

ProcedureTo Generate a Timer Statistic

Timer statistics typically correspond to multiple events. For example, to calculate the time to process a request, two events are required, for example, a “request received” event and a “request completed” event.

For operations that have a measurable duration, Monitoring Scripting Client provides pairs of events to indicate the start and the end of the operations. For example, to indicate the initiation and completion of an HTTP request that has been received by the web container, Monitoring Scripting Client provides the following pair of events:

Use pairs of events that indicate the start and end of an operation to generate a timer statistic.

  1. Write an event callback function to calculate the start time.

  2. Ensure that the function to calculate the start time is called when the “operation started” event is received.

    For details, see To Register a Script as a Listener for an Event.

  3. Write an event callback function to calculate the end time.

  4. Ensure that the function to calculate the end time is called when the “operation ended” event is received.

    For details, see To Register a Script as a Listener for an Event.


Example 1–7 Generating a Timer Statistic

This example uses the following events to measure the time to process web service requests:

The events for a single request are sent in the same thread of control. Therefore, the identity of the thread can be used as a key to associate the start event and the end event for the request.

For the complete listing of the script from which this example is extracted, see Example 1–9.

...
var startTime;
var object = new Object();
...

function requestStartEvent(appName,hostName,serverName,serverPort,contextPath,
    servletPath){

	...
        startTime = (new Date()).getTime();
	//insert the request time in Map
        key = java.lang.Thread.currentThread().getId();
        object[key] = startTime;
        ...
}
scriptContainer.registerListener('glassfish:web:http-service:requestStartEvent',
    request_params , 'requestStartEvent');
...
function requestEndEvent(appName,hostName,serverName,serverPort,contextPath,
    servletPath,statusCode){

...
        key = java.lang.Thread.currentThread().getId();
        startTime = object[key];
        if (startTime == null)
            client.print("Error getting the startTime for thread = " + key);
        else
            delete[key];
        totalTime = (new Date()).getTime() - startTime;
        ...
}
scriptContainer.registerListener('glassfish:web:http-service:requestEndEvent', 
    request1_params, 'requestEndEvent');