8. Monitoring

8.1. Getting Resource Availability

Many operations should only occur if the target resource is running, such as deploying a new web application. Including an availability check in a larger script is helpful for controlling when state-dependent operations are run.
The script steps are:
  1. Search for the resource. In this case, the script just looks for any resource which matches the resource type (Linux platform), and uses the first match for the availability scan.
  2. Get the current availability status.

Example 20. Current Availability

// get the resource ID
criteria = new ResourceCriteria();

var res = ResourceManager.findResourcesByCriteria(criteria);

// check the current availability
The JBoss ON server returns the resource information, its current status, and the time what the current status began (meaning, if the server is up, the time the server started).
rhqadmin@localhost:7080$ exec -f /export/myscripts/test.js
        availabilityType: UP
                      id: 10192
                resource: Resource[id=10001, uuid=null, type=<null>, key=null, name=null, parent=<null>]
	       startTime: 1335974397214

8.2. Getting Specific Metrics

The monitoring information in JBoss ON is not a live reading. There are two reasons for that: scans are periodic, not streaming, and the information for baselines and averages are processed (aggregated).
The findLiveData method is a way to pull in the current, un-average, live reading of a given metric.
The script steps are:
  1. Search for the available metric definitions, based on the resource type and then filtered to a single metric. This example grabs the free memory metric for the Linux platform.
  2. Search for the resource.
  3. Get the current reading for the metric.
  4. Print the data to the terminal.

Example 21. Annotated Example

// search for the resource
criteria = new ResourceCriteria();
var resources = ResourceManager.findResourcesByCriteria(criteria);

// search for the resource type to use in the metrics definition
var rt = ResourceTypeManager.getResourceTypeByNameAndPlugin("Linux", "Platforms");

// search for the metric definition
var mdc = MeasurementDefinitionCriteria();
mdc.addFilterDisplayName("Free Memory");
var mdefs =  MeasurementDefinitionManager.findMeasurementDefinitionsByCriteria(mdc);

//get the data
var metrics = MeasurementDataManager.findLiveData(resources.get(0).id, [mdefs.get(0).id]);

// as a nice little display, print the retrieved metrics value
if( metrics !=null ) {
        println(" Metric value for " + resources.get(0).id + " is " + metrics );
With this example, the current, live reading for the metric is printed to the screen.
rhqadmin@localhost:7080$ exec -f /export/myscripts/test.js
 Metric value for 10001 is [MeasurementDataNumeric[value=[6.3932239872E10], MeasurementData [MeasurementDataPK: timestamp=[Tue May 08 20:10:15 EDT 2012], scheduleId=[10002]]]]

8.3. Exporting Metric Data for a Resource

Raw metrics data are only saved in the database for a week by default. After that, only the processed (aggregated) data are saved. It can be useful to export raw measurements to a CSV or text file so that long-term historical data can be preserved.
The script steps are:
  1. Search for the available metric definitions, based on the resource type. In this example, it is for the Linux platform.
  2. Search for the resource.
  3. Set a date range for the metric information. This is configured in seconds, relative to the time the script is run.
  4. Set up the file information to which to write the data.
  5. Iterate through all the metric definitions for the resource, and print the data to the given CSV file.

Example 22. Exporting All Metrics Definitions for a Linux Server

// search for the available metrics definitions
var rt = ResourceTypeManager.getResourceTypeByNameAndPlugin("Linux","Platforms")
var mdc = MeasurementDefinitionCriteria();
var mdefs =  MeasurementDefinitionManager.findMeasurementDefinitionsByCriteria(mdc);

// search for the resource
criteria = new ResourceCriteria();
var resources = ResourceManager.findResourcesByCriteria(criteria);

// give the date range for the metrics collection
// this is in seconds
var start = new Date() - 8* 3600 * 1000;
var end = new Date()

// setup up the CSV to dump the data to
exporter.file = '/opt/myfile.csv'
exporter.format = 'csv'

// iterate through the metrics definitions for the resource
// and export all the collected metrics for all definitions
// within the given date range
if( mdefs != null ) {
  if( mdefs.size() > 1 ) {
     for( i =0; i < mdefs.size(); ++i) {
          mdef = mdefs.get(i);
          var data = MeasurementDataManager.findDataForResource(resources.get(0).id,[mdef.id],start,end,"")

	  exporter.write(data.get(0)); // write the data to the CSV file
  else if( mdefs.size() == 1 ) {
     mdef = mdefs.get(0);
     var data = MeasurementDataManager.findDataForResource(resources.get(0).id,[mdef.get(0).id],start,end,60)

8.4. Getting Baseline Calculations

Baselines are the normal operating ranges for a specific resource, based on its own performance.
Getting a baseline is really easy; all it requires is the resource ID.
rhqadmin@localhost:7080$ MeasurementBaselineManager.findBaselinesForResource(10001)
one row
        computeTime: Tue May 08 21:28:05 EDT 2012
                 id: 10001
                max: 6.4005419008E10
               mean: 6.3933904981333336E10
                min: 6.380064768E10
           schedule: [MeasurementSchedule, id=10002]
        userEntered: true