Solstice Enterprise Manager 4.1 Developing Java Applications |
Using the Java Alarm API
The Java Alarm API defines the classes and methods which allow you to create applications that perform alarm management operations. This chapter covers the most important aspects of using the API and provides detailed examples. For more information about the API, refer to Java API Reference.
The following topics are covered in this chapter:
- Section 3.1 Overview
- Section 3.2 Instantiating the AlarmLog Class
- Section 3.3 Creating Query Objects
- Section 3.4 Creating AlarmRecordAttributeSet Objects
- Section 3.5 Getting Alarms
- Section 3.6 Clearing and Acknowledging Alarms
- Section 3.7 Listening for Alarm Log Events
- Section 3.8 Sample Programs
These topics are arranged, as far as possible, to reflect the order you need to follow when creating alarm management applications. For example, before getting alarms, you must have associated an AlarmLog object with a log, created a query, and specified the attributes to be returned. This sequence becomes clear as you go through this chapter.
3.1 Overview
Alarms are events indicating abnormal conditions that the MIS logs (through the logging subsystem) into alarm logs (the default log object is AlarmLog). Each log entry (record) represents an alarm and is created based on the mapping provided in evv2oclist object in the MIS and the filter created for the log. An alarm:
- Can be logged.
- Has attributes that allows it to be acknowledged and cleared.
- Is recognized by and displayed in the Solstice Enterprise Manager (Solstice EM) Alarms window tool.
When the MIS receives an event, it uses a predefined filter to determine whether the event is an alarm and logs it accordingly as an alarm record. You can use the Alarms window to view and monitor alarms.
The Java Alarm API provides a programmatic interface to achieve functionality similar to the Alarms window. The Java Alarm API hides the implementation details of how logs stored in databases are accessed and processed, and provides the classes and methods that allow you to create alarm management applications. If the implementation details change, your application still works. See the following figure.
FIGURE 3-1 The Java Alarm API
Note The Java Alarm API works only on alarms that are derived from the GDMO object emAlarmRecord and does not work with user-defined alarms.
3.1.1 Alarm Management Tasks
The Java Alarm API allows you to perform the following alarm management tasks:
- Monitor alarms
- Acknowledge alarms
- Take actions based on alarms
- Clear alarms
These tasks can be performed using the following main classes of the Java Alarm API:
- AlarmLog class
- Query classes (FilterItem, Filter, and GenericQuery)
- Listener classes (such as AlarmLogListener and AlarmLogCreationListener)
FIGURE 3-2 Main "Players" in the Java Alarm APIEach instance of the AlarmLog class represents an alarm log and contains methods for accessing and modifying alarm information. The query classes provide the filtering mechanism needed by some of the AlarmLog class methods. The listener classes provide the mechanism for reacting to alarms.
3.1.2 Alarm Management Task Flow
When creating alarm management applications, keep in mind the following model of task flow, as illustrated in the following figure.
FIGURE 3-3 Flow of Tasks When Handling an Event3.2 Instantiating the AlarmLog Class
The first step to take when performing alarm operations is to instantiate the AlarmLog class. This class represents the MIS's log objects that contain alarms. Each instance of this class represents one log. See the following figure.
FIGURE 3-4 AlarmLog Objects
To Instantiate the AlarmLog Class
1. Create a LogName object as shown in the following example:
LogName logName = new LogName(MISName, "AlarmLog");
- The logName object allows you to hide the naming internals of the log. When creating the logName object, you must supply the following two arguments:
2. Create an AlarmLog object as shown in the following example:
AlarmLog log = new AlarmLog(platform, logName);
- The AlarmLog object constructor takes the following parameters:
- platform: An instance of the Platform class that represents the MIS to which the application is connected.
- logName: A LogName object representing a log's name.
Note The MIS name parameter used to create the LogName object and the MIS which the platform parameter represents when creating the AlarmLog object can be different.
3.3 Creating Query Objects
The Java Alarm API allows you to create filter objects that you can use to query alarm logs. For example, you can create queries to retrieve all alarm records that have a perceived severity that is major or critical.
To Create a Query Object
1. Create one or more FilterItem objects that define the conditions to check for in the query.
- This topic is described in Section 3.3.1 Creating FilterItem Objects.
2. Create a Filter object that combines the FilterItem objects you defined in Step1 based on a logical criteria (AND or OR).
- This topic is described in Section 3.3.2 Creating Filter Objects.
3. Create a GenericQuery object that combines the Filter objects you defined in Step2 based on a logical criteria (AND or OR). See the following figure.
- This topic is described in Section 3.3.3 Creating GenericQuery Objects.
FIGURE 3-5 Query ObjectsAs shown in FIGURE 3-5, the GenericQuery object is used to join various Filter objects and the Filter object is used to join various FilterItem objects. Constructing a GenericQuery object in this fashion hides the implementation details of filtering, allows FilterItem and Filter objects to be reused in the same application to create different queries, and allows you to create complicated queries.
3.3.1 Creating FilterItem Objects
A FilterItem object represents a condition in a query that affects only one alarm record attribute. For example, you might want to retrieve all alarm records whose PERCEIVED_SEVERITY attribute is equal to EMSeverity.MAJOR or EMSeverity.CRITICAL.
To create a FilterItem object, you need to provide the following three parameters:
- Attribute name: Name of the alarm record attribute to be checked by the condition. See Section 3.4 Creating AlarmRecordAttributeSet Objects for more information on alarm record attributes.
- Relational operator: A RelationCriteria object that defines a relation operator for the condition (=, >,<).
- Attribute values: One or more attribute values.
Following is an example of a code segment that creates a FilterItem object:
EMSeverity[] sev = {EMSeverity.MAJOR,EMSeverity.CRITICAL};FilterItem filterItem1 =
new FilterItem(AlarmRecordAttribute.PERCEIVED_SEVERITY, RelationCriteria.EQUAL, sev);This code creates a FilterItem object based on the severity of the alarm record.
The first statement in the code above defines the attribute values that will be used when creating the FilterItem object filterItem1.
The second statement creates the FilterItem object filterItem1. All alarm records whose perceived severity is major or critical will pass this filter.
3.3.2 Creating Filter Objects
A Filter object implements an alarm filter. You construct a filter by combining an instance of the LogicalCriteria object with a list of FilterItem objects. In other words, an alarm filter consists of one or more conditions combined together by the logical operators AND or OR.
To create a Filter object, you need to provide the following two parameters:
- LogicalCriteria object: The logical operator (AND or OR) to be used in the filter.
- FilterItem object: The object specifying a condition to check for. You can add more FilterItem objects using the addFilter method of the Filter object.
Note If you don't specify a logical criteria, AND is assumed by default.
Following is a code segment that creates a Filter object:
LogicalCriteria logCriteria1 = new LogicalCriteria OR;Filter filter = new Filter(logCriteria1, filterItem1);In this code segment, the first statement defines OR as the logical operator to be used when creating the filter.
The second statement creates the Filter object filter based on the logical operator defined in the previous statement and the FilterItem object defined in Section 3.3.1 Creating FilterItem Objects.
To add more FilterItem objects to the filter object, you can use the addFilter method as follows:
filter.addFilterItem(filterItem2);filter.addFilterItem(filterItem3);In this case, the filter object consists of three conditions combined by the OR operator (filterItem1 OR filterItem2 OR filterItem3).
3.3.3 Creating GenericQuery Objects
A GenericQuery object implements a generic alarm filter. You construct a GenericQuery object by combining a LogicalCriteria object with FilterItem objects.
To create a GenericQuery object, you need to provide the following two parameters:
- LogicalCriteria object: The logical operator (AND or OR) to be used in the filter.
- Filter object: The object specifying one or more conditions to be checked. You can add more Filter objects using the addFilter method of the GenericQuery object.
Note If you don't specify a LogicalCriteria object, AND is assumed by default.
Following is a code segment that creates a GenericQuery object:
GenericQuery query = new GenericQuery();query.addFilter(filter1);query.addFilter(filter2);In this code segment, the first statement creates a new GenericQuery object (query). The second and third statements add two Filter objects to the query. These two filters are combined using the AND operator.
3.4 Creating AlarmRecordAttributeSet Objects
Each record in an alarm log consists of 22 fields or attributes. However, in many instances you only need to retrieve or modify a subset of these attributes, which you can specify using the AlarmRecordAttributeSet objects. These objects are used by the API methods that retrieve or modify alarm record attributes.
The following table contains a list of the 22 alarm record attributes that are represented as variables in the AlarmRecordAttribute class:
To define a set of alarm record attributes, create an AlarmRecordAttributeSet object and add the required attributes (using the add method) as shown in the following code example:
3.5 Getting Alarms
Once the AlarmLog and query objects are created, you can perform alarm management operations. This section describes how to get:
- Alarm counts
- Alarms
- Alarms in batches
3.5.1 Getting Alarm Counts
To get the number of alarms in an alarm log that satisfy a particular query, use the getAlarms method of the AlarmLog class as shown in the following code segment:
count = log.getAlarmCount(query);This code segment gets the alarm count based on query. If query is null, the getAlarmCount method returns the total number of alarms in the alarm log.
3.5.2 Getting Alarms
To retrieve the attributes of alarms that satisfy a particular query, use the getAlarms method of the AlarmLog object as shown in the following code segment:
AlarmRecord[] alarms = log.getAlarms(query, attrSet);This code segment retrieves the alarm records that satisfy the query and stores them into the alarms array. The returned attributes contain the attributes specified by attrSet only.
3.5.3 Getting Alarms in Batches
It is often more efficient to retrieve alarm information in batches. This method frees the system to perform other operations while alarm information is being gathered.
To retrieve alarms in batches, you need to define a BatchListener object and pass it as an argument to the getAlarmsInBatches method. This method takes the following parameters:
- Query object: See Section 3.3.3 Creating GenericQuery Objects.
- Size: Batch size.
- BatchListener object: Implements methods (that you define) that tell the application what to do when alarm batches are received and when alarm batching is done.
- AlarmRecordAttributeSet object: See Section 3.4 Creating AlarmRecordAttributeSet Objects.
The following code example shows how to get alarms in batches.
CODE EXAMPLE 3-2 Getting Alarms in Batches
BatchListener batchList = new BatchListener("Critical"); System.out.println("Getting critical alarms in batches"); int batchId = log.getAlarmsInBatches(query, 5000, batchList, attrSet); System.out.println("Batch Id: " + batchId); .......... .......... .......... class BatchListener implements AlarmsBatchListener { String id = null; public BatchListener(String id) { this.id = id; } public void batchReceived(int callId, AlarmRecord[] record) { System.out.println("BatchId: " + callId); System.out.println( "Number of alarms received: " + record.length); } public void batchDone(int callId) { System.out.println("BatchId :" + callId); System.out.println("Batch complete"); } }Notice that this code segment also contains the implementation of the batchReceived and batchDone methods of the BatchListener class. These methods specify what to do when a batch is received and when batching is done.
3.6 Clearing and Acknowledging Alarms
Clearing and acknowledging alarms is a routine task that you perform when managing alarms. The AlarmLog class provides the setClearAlarms and setAckAlarms methods that you can use to clear and acknowledge alarms. The following code example shows how to acknowledge and clear alarms.
CODE EXAMPLE 3-3 Clearing and Acknowledging Alarms
................ AlarmRecord[] alarms = log.getAlarms(q1, attrSet); ............... AlarmRecordId alrmId =alarms[0].getLogRecordId(); AlarmRecordId[] alrm = {alrmId}; log.setAckAlarms(alrm, "meaningful text needed"); log.setClearAlarms(alrm, "meaningful text needed"); ............In this code segment, the setAckAlarms and setClearAlarms methods acknowledge and clear the alarm whose log id is specified by the alarms array (in this case the array contains only one id) by modifying the ACK_TEXT and CLEAR_TEXT attributes, respectively, of the corresponding alarm record in the corresponding log.
You can also clear alarms by sending an event using the sendClearAlarmsEvent method. This method sends an event that clears all alarms that originated from the same source all at once without having to clear them one-by-one, as in the setClearAlarms method.
log.sendClearAlarmsEvent(alrm);3.7 Listening for Alarm Log Events
One of the main tasks in alarm management is to define how your application reacts to alarm records being created, deleted, or modified. This task is done by creating listener objects that define how to react to such events. These objects will be notified of such events by the logging subsystem.
The Java Alarm API provides the following four interfaces for creating alarm event listener objects:
- AlarmLogCreationListener: Contains one method (alarmLogCreated). You can define the body of this method to perform the appropriate actions when an alarm log record is created.
- AlarmLogDeletionListener: Contains one method (alarmLogDeleted). You can define the body of this method to perform the appropriate actions when an alarm log record is deleted.
- AlarmLogModificationListener: Contains one method (alarmLogModified). You can define the body of this method to perform the appropriate actions when an alarm log record is modified.
- AlarmLogListener: Contains three methods (alarmLogCreated, alarmLogDeleted, and alarmLogModified). You can define the body of these methods to perform the appropriate actions when an alarm log record is created, deleted, or modified.
To Create an Alarm Event Listener
1. Use the setEventAttrSet method to specify the list of attributes to be returned.2. Use the addAlarmLogListener method to register to receive an event when the alarm log is created, deleted, or modified, as shown in the following example.
//Listen for objectCreationEvent//------------------------------log.setEventAttrSet(attrSet);System.out.println("Listening for alarm events");log.addAlarmLogListener(new EventListener());
Note If attributes other than the one specified in attrSet changes, an empty event is generated.
3. Fill the bodies of the AlarmLogListener methods with the appropriate code (see CODE EXAMPLE 3-5).3.8 Sample Programs
This section provides sample programs that perform alarm management.
3.8.1 AlarmBatch
This program gets alarms in batches. The query is based on perceivedSeverity, and the batchSize is 5000. See the following code example.
CODE EXAMPLE 3-4 AlarmBatch
/* * Copyright 10/30/98 Sun Microsystems, Inc. All Rights Reserved. */ /* AlarmBatch <servername> <mis-name> <username> <password> <servername> - is the machine name on which the server is running. <mis-name> - is the machine name on which the mis is running. <username> - is the user login name. <password> - is the password of the user login. */ import com.sun.em.api.alarm.*; import com.sun.em.api.common.*; import java.util.Enumeration; import java.util.Vector; import com.sun.em.api.pmi.*; public class AlarmBatch { static void usage() { System.err.println("Usage:"); System.err.println("AlarmBatch <servername> <mis-name> <username> <password > "); System.exit(-1); } public static void main(String[] args) { try { new AlarmBatch(args); } catch (Exception e) { e.printStackTrace(); } System.out.println("Done."); System.exit(0); } public AlarmBatch(String[] args) throws AlarmException { if (args.length < 4) usage(); //Create an attribute set: The set of alarm record attributes //in which you are interested. //-------------------------------------------------------------- AlarmRecordAttributeSet attrSet = new AlarmRecordAttributeSet(); attrSet.add(AlarmRecordAttribute.LOG_RECORD_ID); attrSet.add(AlarmRecordAttribute.PERCEIVED_SEVERITY); attrSet.add(AlarmRecordAttribute.PROBABLE_CAUSE); attrSet.add(AlarmRecordAttribute.CLEAR_STATE); attrSet.add(AlarmRecordAttribute.CLEAR_TIME); attrSet.add(AlarmRecordAttribute.ACK_TIME); attrSet.add(AlarmRecordAttribute.LOGGING_TIME); attrSet.add(AlarmRecordAttribute.EVENT_TYPE); attrSet.add(AlarmRecordAttribute.ACK_STATE); attrSet.add(AlarmRecordAttribute.MANAGED_OBJECT_INSTANCE); attrSet.add(AlarmRecordAttribute.ACK_OPERATOR); attrSet.add(AlarmRecordAttribute.ACK_TEXT); attrSet.add(AlarmRecordAttribute.CLEAR_OPERATOR); attrSet.add(AlarmRecordAttribute.CLEAR_TEXT); attrSet.add(AlarmRecordAttribute.ADDITIONAL_TEXT); attrSet.add(AlarmRecordAttribute.DISPLAY_OPERATOR); attrSet.add(AlarmRecordAttribute.DISPLAY_STATE); attrSet.add(AlarmRecordAttribute.DISPLAY_TIME); attrSet.add(AlarmRecordAttribute.EVENT_TIME); try { System.out.println("Connecting to " + args[1] + "..."); //Create a Platform object based on the arguments passed //------------------------------------------------------- Platform platform = new Platform(args[0],args[1] , args[2],args[3]); System.out.println("Platform instantiation complete "); //Instantiate a AlarmLog Object for a log named "AlarmLog" //-------------------------------------------------------- System.out.println("AlarmLog instantiation"); LogName logName = new LogName(args[1], "AlarmLog"); AlarmLog log = new AlarmLog(platform, logName); //Create a query //--------------- //Create array of perceived severity values to be used to //create a filter item EMSeverity[] sev = { EMSeverity.CRITICAL }; // Create a FilterItem based on perceivedSeverity FilterItem filterItem1 = new FilterItem(AlarmRecordAttribute.PERCEIVED_SEVERITY, RelationCriteria.EQUAL, sev); Filter filter = new Filter(filterItem1); GenericQuery query = new GenericQuery(filter); //Test batch mechanism //-------------------- // Create a listener object instance to handle to batch events BatchListener batchList = new BatchListener("Critical"); System.out.println("Getting critical alarms in batches"); //Get alarms in batches of 5000 each int batchId = log.getAlarmsInBatches(query, 5000, batchList, attrSet); System.out.println("Batch Id: " + batchId); //Stop batch //---------- System.out.println("Stopping the batch"); log.stopGetAlarmsInBatches(batchId); } catch(Exception e1) { e1.printStackTrace(); } } } // Implements a listener object to handle batch events //----------------------------------------------------- class BatchListener implements AlarmsBatchListener { String id = null; public BatchListener(String id) { this.id = id; } // When a batch is received the following method is called public void batchReceived(int callId, AlarmRecord[] record) { System.out.println("BatchId: " + callId); System.out.println( "Number of alarms received: " + record.length); } // When all batches have been received the following method is called public void batchDone(int callId) { System.out.println("BatchId :" + callId); System.out.println("Batch complete"); } private static final String sccsID = "@(#)AlarmBatch.java 1.5 98/10/30 Sun Microsystems"; }3.8.2 AlarmEvent
- Listens for alarm creation, deletion, and modification events.
- Prints the event data.
CODE EXAMPLE 3-5 AlarmEvent
/* * Copyright 10/30/98 Sun Microsystems, Inc. All Rights Reserved. */ /* AlarmEvent <servername> <mis-name> <username> <password> <servername> - is the machine name on which the server is running. <mis-name> - is the machine name on which the mis is running. <username> - is the user login name. <password> - is the password of the user login. */ import com.sun.em.api.alarm.*; import com.sun.em.api.common.*; import java.util.Enumeration; import com.sun.em.api.pmi.*; public class AlarmEvent { static void usage() { System.err.println("Usage:"); System.err.println("AlarmEvent <servername> <mis-name> <username> <password> "); System.exit(-1); } public static void main(String[] args) { try { new AlarmEvent(args); } catch (Exception e) { e.printStackTrace(); } System.out.println("Done."); System.exit(0); } public AlarmEvent(String[] args) throws AlarmException { Platform platform = null; if (args.length < 4) usage(); try { //Connect to a platform //--------------------- System.out.println("Connecting to " + args[1] + "..."); platform = new Platform(args[0],args[1] , args[2],args[3]); System.out.println("Platform instantiation complete "); //Instantiate a AlarmLog Object for a log named "AlarmLog" //-------------------------------------------------------- System.out.println("AlarmLog instantiation"); LogName logName = new LogName(args[1], "AlarmLog"); AlarmLog log = new AlarmLog(platform, logName); //Create an attribute set: The set of alarm record attributes //in which you are interested. //----------------------------------------------------------- AlarmRecordAttributeSet attrSet = new AlarmRecordAttributeSet(); attrSet.add(AlarmRecordAttribute.LOG_RECORD_ID); attrSet.add(AlarmRecordAttribute.PERCEIVED_SEVERITY); attrSet.add(AlarmRecordAttribute.PROBABLE_CAUSE); attrSet.add(AlarmRecordAttribute.CLEAR_STATE); attrSet.add(AlarmRecordAttribute.CLEAR_TIME); attrSet.add(AlarmRecordAttribute.ACK_TIME); attrSet.add(AlarmRecordAttribute.ACK_STATE); attrSet.add(AlarmRecordAttribute.LOGGING_TIME); attrSet.add(AlarmRecordAttribute.EVENT_TYPE); attrSet.add(AlarmRecordAttribute.ACK_OPERATOR); attrSet.add(AlarmRecordAttribute.MANAGED_OBJECT_INSTANCE); //Listen for alarm related events //------------------------------- log.setEventAttrSet(attrSet); System.out.println("Listening for alarm events"); log.addAlarmLogListener(new EventListener()); Object o = new Object(); synchronized(o) { try { o.wait(); } catch (InterruptedException ex) {} } } catch(Exception e1) { e1.printStackTrace(); } } } class EventListener implements AlarmLogListener { //Callback for alarm record creation //---------------------------------- public void alarmRecordCreated(AlarmLogEvent event) { System.out.println("Received Alarm CREATION"); try { //Print the event data //--------------------- AlarmRecord alr1 = event.getAlarmRecord(); System.out.println(alr1.toString()); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } //Callback for alarm record deletion //---------------------------------- public void alarmRecordDeleted(AlarmLogEvent event) { System.out.println("Received Alarm DELETION"); try { //Print the event data //--------------------- AlarmRecord alr1 = event.getAlarmRecord(); System.out.println(alr1.toString()); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } //Callback for alarm record modification //-------------------------------------- public void alarmRecordModified(AlarmLogEvent event) { System.out.println("Received Alarm MODIFICATION"); try { //Print the event data //--------------------- AlarmRecord alr1 = event.getAlarmRecord(); System.out.println(alr1.toString()); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } private static final String sccsID = "@(#)AlarmEvent.java 1.5 98/10/30 Sun Microsystems"; }3.8.3 AlarmClear
- Retrieves all alarms which have major or critical severity.
- Sets the display flag of the first alarm it retrieves.
- Acknowledges the first alarm it retrieves.
- Clears the first alarm it retrieves.
CODE EXAMPLE 3-6 AlarmClear
/* * Copyright 10/30/98 Sun Microsystems, Inc. All Rights Reserved. */ /* AlarmClear <servername> <mis-name> <username> <password> <servername> - is the machine name on which the server is running. <mis-name> - is the machine name on which the mis is running. <username> - is the user login name. <password> - is the password of the user login. */ import com.sun.em.api.alarm.*; import com.sun.em.api.common.*; import java.util.Enumeration; import java.util.Vector; import com.sun.em.api.pmi.*; public class AlarmClear { static void usage() { System.err.println("Usage:"); System.err.println("AlarmClear <servername> <mis-name> <username> <password> "); System.exit(-1); } public static void main(String[] args) { try { new AlarmClear(args); } catch (Exception e) { e.printStackTrace(); } System.out.println("Done."); System.exit(0); } public AlarmClear(String[] args) throws AlarmException { if (args.length < 4) usage(); //Create an attribute set: The set of alarm record attributes in //which you are interested. //-------------------------------------------------------------- AlarmRecordAttributeSet attrSet = new AlarmRecordAttributeSet(); attrSet.add(AlarmRecordAttribute.LOG_RECORD_ID); attrSet.add(AlarmRecordAttribute.PERCEIVED_SEVERITY); attrSet.add(AlarmRecordAttribute.PROBABLE_CAUSE); attrSet.add(AlarmRecordAttribute.CLEAR_STATE); attrSet.add(AlarmRecordAttribute.CLEAR_TIME); attrSet.add(AlarmRecordAttribute.CLEAR_TEXT); attrSet.add(AlarmRecordAttribute.CLEAR_OPERATOR); attrSet.add(AlarmRecordAttribute.ACK_STATE); attrSet.add(AlarmRecordAttribute.ACK_TIME); attrSet.add(AlarmRecordAttribute.ACK_TEXT); attrSet.add(AlarmRecordAttribute.LOGGING_TIME); attrSet.add(AlarmRecordAttribute.EVENT_TYPE); attrSet.add(AlarmRecordAttribute.MANAGED_OBJECT_INSTANCE); attrSet.add(AlarmRecordAttribute.LOG_NAME); attrSet.add(AlarmRecordAttribute.ADDITIONAL_TEXT); attrSet.add(AlarmRecordAttribute.DISPLAY_OPERATOR); attrSet.add(AlarmRecordAttribute.DISPLAY_STATE); attrSet.add(AlarmRecordAttribute.DISPLAY_TIME); attrSet.add(AlarmRecordAttribute.EVENT_TIME); try { //Create a query based on perceivedSeverity //-------------------------------- EMSeverity[] sev = { EMSeverity.MAJOR, EMSeverity.CRITICAL }; FilterItem filterItem1 = new FilterItem(AlarmRecordAttribute.PERCEIVED_SEVERITY, RelationCriteria.EQUAL, sev); Filter filter = new Filter(filterItem1); GenericQuery query = new GenericQuery(filter); //Connect to a platform //--------------------- System.out.println("Connecting to " + args[1] + "..."); Platform platform = new Platform(args[0],args[1] , args[2],args[3]); System.out.println("Platform instantiation complete "); //Instantiate a AlarmLog Object for a log named "AlarmLog" //-------------------------------------------------------- System.out.println("AlarmLog instantiation"); LogName logName = new LogName(args[1], "AlarmLog"); AlarmLog log = new AlarmLog(platform, logName); //Now retrieve the count of alarms satisfying the query //------------------------------------------------------ System.out.println("\n Getting alarm Count for the filter:\n"); System.out.println(query.toString()); int count = log.getAlarmCount(query); System.out.println("Count of Alarms: " + count); System.out.println("Getting alarms for the above filter\n"); AlarmRecord[] alarms = log.getAlarms(query, attrSet); //Now print out the result //------------------------ printAlarmRecord(alarms, attrSet); if (alarms.length == 0) { System.out.println("Stop the test as no alarms match the filter"); System.exit(-1); } try { //Get the log record Id of the first alarm record retrieved. //---------------------------------------------------------- AlarmRecordId alrmId =alarms[0].getLogRecordId(); AlarmRecordId[] alrm = {alrmId}; System.out.println("\n Get cleared alarm \n"); FilterItem filterItem2 = new FilterItem(AlarmRecordAttribute.LOG_RECORD_ID, RelationCriteria.EQUAL, alrm); Filter filter2 = new Filter(filterItem2); //Clear previous query //--------------------- query.clear(); query.addFilter(filter2); System.out.println(query.toString()); System.out.println("Getting alarms for the filter: "); query.toString(); alarms = log.getAlarms(query, attrSet); printAlarmRecord(alarms, attrSet); //Set the display flag //--------------------- String[] str2 = {""}; System.out.println("\n Set Display alarm \n"); log.setDisplayAlarms(alrm, str2); //Acknowledge the alarm //---------------------- System.out.println("\n Set Ack alarm \n"); log.setAckAlarms(alrm, str2); //Clear the first alarm retrieved. //-------------------------------- System.out.println("\n Clear the alarm"); log.setClearAlarms(alrm, null); } catch (AlarmAttributeNotSetException e) { e.printStackTrace(); } } catch(Exception e1) { e1.printStackTrace(); } } private static void printAlarmRecord(AlarmRecord[] alarms, AlarmRecordAttributeSet attrSet) throws AlarmException { System.out.println("Received : " + alarms.length + "Alarms "); int ii; for (ii=0; ii<alarms.length; ii++) { System.out.println("Alarm number:" + ii ); AlarmRecord alr1 = (AlarmRecord)alarms[ii]; System.out.println(alr1.toString()); } } private static final String sccsID = "@(#)AlarmClear.java 1.5 98/10/30 Sun Microsystems"; }3.8.4 AlarmDelete
- Queries to retrieve alarms based on perceivedSeverity from a log.
- Deletes the first alarm it retrieves.
CODE EXAMPLE 3-7 AlarmDelete
/* * Copyright 10/30/98 Sun Microsystems, Inc. All Rights Reserved. */ /* AlarmDelete <servername> <mis-name> <username> <password> <servername> - is the machine name on which the server is running. <mis-name> - is the machine name on which the mis is running. <username> - is the user login name. <password> - is the password of the user login. */ import com.sun.em.api.alarm.*; import com.sun.em.api.common.*; import com.sun.em.api.common.MOName; import java.util.Enumeration; import java.lang.reflect.Array; import com.sun.em.api.pmi.*; import java.util.*; import java.lang.Integer; import java.text.*; public class AlarmDelete { static void usage() { System.err.println("Usage:"); System.err.println("AlarmDelete <servername> <mis-name> <username> <password> "); System.exit(-1); } public static void main(String[] args) { try { new AlarmDelete(args); } catch (Exception e) { e.printStackTrace(); } System.out.println("Done."); System.exit(0); } public AlarmDelete(String[] args) throws AlarmException { if (args.length < 4) usage(); //Create an attribute set: The set of alarm record attributes //in which you are interested. //--------------------------------------------------------------- AlarmRecordAttributeSet attrSet = new AlarmRecordAttributeSet(); attrSet.add(AlarmRecordAttribute.LOG_RECORD_ID); attrSet.add(AlarmRecordAttribute.PERCEIVED_SEVERITY); attrSet.add(AlarmRecordAttribute.PROBABLE_CAUSE); attrSet.add(AlarmRecordAttribute.CLEAR_STATE); attrSet.add(AlarmRecordAttribute.CLEAR_TIME); attrSet.add(AlarmRecordAttribute.CLEAR_TEXT); attrSet.add(AlarmRecordAttribute.CLEAR_OPERATOR); attrSet.add(AlarmRecordAttribute.ACK_STATE); attrSet.add(AlarmRecordAttribute.ACK_TIME); attrSet.add(AlarmRecordAttribute.ACK_TEXT); attrSet.add(AlarmRecordAttribute.ACK_OPERATOR); attrSet.add(AlarmRecordAttribute.LOGGING_TIME); attrSet.add(AlarmRecordAttribute.EVENT_TYPE); attrSet.add(AlarmRecordAttribute.MANAGED_OBJECT_INSTANCE); attrSet.add(AlarmRecordAttribute.LOG_NAME); attrSet.add(AlarmRecordAttribute.ADDITIONAL_TEXT); attrSet.add(AlarmRecordAttribute.DISPLAY_OPERATOR); attrSet.add(AlarmRecordAttribute.DISPLAY_STATE); attrSet.add(AlarmRecordAttribute.DISPLAY_TIME); attrSet.add(AlarmRecordAttribute.EVENT_TIME); try { //Create a query based on perceivedSeverity //----------------------------------------- EMSeverity[] sev = { EMSeverity.MAJOR, EMSeverity.CRITICAL }; FilterItem filterItem1 = new FilterItem(AlarmRecordAttribute.PERCEIVED_SEVERITY, RelationCriteria.EQUAL, sev); Filter filter = new Filter(filterItem1); GenericQuery query = new GenericQuery(filter); //Connect to a platform //---------------------- System.out.println("Connecting to " + args[1] + "..."); Platform platform = new Platform(args[0],args[1] , args[2],args[3]); System.out.println("Platform instantiation complete "); //Instantiate a AlarmLog Object for a log named "AlarmLog" //-------------------------------------------------------- System.out.println("AlarmLog instantiation"); LogName logName = new LogName(args[1], "AlarmLog"); AlarmLog log = new AlarmLog(platform, logName); //Get the alarm count based on the query //--------------------------------------- System.out.println("\n Getting alarm Count for the filter:\n"); System.out.println(query.toString()); int count = log.getAlarmCount(query); System.out.println("Count of Alarms: " + count); //Get alarms based on the query //----------------------------- System.out.println("Getting alarms for the above filter\n"); AlarmRecord[] alarms = log.getAlarms(query, attrSet); //Now print out the result //------------------------ printAlarmRecord(alarms, attrSet); if (alarms.length == 0) { System.out.println("Stop the test as no alarms match the filter"); System.exit(-1); } //Delete alarms //------------ try { //Get the alarm Id of the first alarm and delete the alarm. //--------------------------------------------------------- AlarmRecordId alarmRcdId = alarms[0].getLogRecordId(); System.out.println("Delete the first alarm: " + alarmRcdId.toString()); AlarmRecordId[] alrmIdArray = {alarmRcdId}; log.deleteAlarms(alrmIdArray); //Try to get it back to make sure //------------------------------- FilterItem fdelete = new FilterItem(AlarmRecordAttribute.LOG_RECORD_ID, RelationCriteria.EQUAL, alrmIdArray); Filter filterDel = new Filter(LogicalCriteria.AND, fdelete); GenericQuery queryDel = new GenericQuery(filterDel); System.out.println("Try to get the alarm being deleted"); System.out.println(queryDel.toString()); AlarmRecord[] alrm = log.getAlarms(queryDel, attrSet); printAlarmRecord(alrm, attrSet); } catch (AlarmAttributeNotSetException e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } } private static void printAlarmRecord(AlarmRecord[] alarms, AlarmRecordAttributeSet attrSet) throws AlarmException { System.out.println("Received : " + alarms.length + "Alarms "); int ii; for (ii=0; ii<alarms.length; ii++) { System.out.println("Alarm number:" + ii ); AlarmRecord alr1 = (AlarmRecord)alarms[ii]; System.out.println(alr1.toString()); } } private static final String sccsID = "@(#)AlarmDelete.java 1.5 98/10/30 Sun Microsystems"; }3.8.5 AlarmQuery
- Queries to retrieve all alarms from a log.
- Queries to retrieve alarms based on perceivedSeverity from a log.
- Queries to retrieve alarms based on loggingTime.
- Gets alarm count based on a query.
CODE EXAMPLE 3-8 AlarmQuery
/* * Copyright 10/30/98 Sun Microsystems, Inc. All Rights Reserved. */ /* AlarmQuery <servername> <mis-name> <username> <password> <servername> - is the machine name on which the server is running. <mis-name> - is the machine name on which the mis is running. <username> - is the user login name. <password> - is the password of the user login. */ import com.sun.em.api.alarm.*; import com.sun.em.api.common.*; import com.sun.em.api.common.MOName; import java.util.Enumeration; import java.lang.reflect.Array; import com.sun.em.api.pmi.*; import java.util.*; import java.lang.Integer; import java.text.*; public class AlarmQuery { static void usage() { System.err.println("Usage:"); System.err.println("AlarmQuery <servername> <mis-name> <username> <password> "); System.exit(-1); } public static void main(String[] args) { try { new AlarmQuery(args); } catch (Exception e) { e.printStackTrace(); } System.out.println("Done."); System.exit(0); } public AlarmQuery(String[] args) throws AlarmException { if (args.length < 4) usage(); //Create an attribute set: The set of alarm record attributes //in which you are interested. //-------------------------------------------------------------- AlarmRecordAttributeSet attrSet = new AlarmRecordAttributeSet(); attrSet.add(AlarmRecordAttribute.LOG_RECORD_ID); attrSet.add(AlarmRecordAttribute.PERCEIVED_SEVERITY); attrSet.add(AlarmRecordAttribute.PROBABLE_CAUSE); attrSet.add(AlarmRecordAttribute.CLEAR_STATE); attrSet.add(AlarmRecordAttribute.CLEAR_TIME); attrSet.add(AlarmRecordAttribute.CLEAR_TEXT); attrSet.add(AlarmRecordAttribute.CLEAR_OPERATOR); attrSet.add(AlarmRecordAttribute.ACK_STATE); attrSet.add(AlarmRecordAttribute.ACK_TIME); attrSet.add(AlarmRecordAttribute.ACK_TEXT); attrSet.add(AlarmRecordAttribute.ACK_OPERATOR); attrSet.add(AlarmRecordAttribute.LOGGING_TIME); attrSet.add(AlarmRecordAttribute.EVENT_TYPE); attrSet.add(AlarmRecordAttribute.MANAGED_OBJECT_INSTANCE); attrSet.add(AlarmRecordAttribute.LOG_NAME); attrSet.add(AlarmRecordAttribute.ADDITIONAL_TEXT); attrSet.add(AlarmRecordAttribute.DISPLAY_OPERATOR); attrSet.add(AlarmRecordAttribute.DISPLAY_STATE); attrSet.add(AlarmRecordAttribute.DISPLAY_TIME); attrSet.add(AlarmRecordAttribute.EVENT_TIME); //Create array of perceived severity values try { // Create a FilterItem based on perceivedSeverity //------------------------------------------------ EMSeverity[] sev = { EMSeverity.MAJOR, EMSeverity.CRITICAL }; FilterItem filterItem1 = new FilterItem(AlarmRecordAttribute.PERCEIVED_SEVERITY, RelationCriteria.EQUAL, sev); Filter filter = new Filter(filterItem1); //Connect to a platform //--------------------- System.out.println("Connecting to " + args[1] + "..."); // Create a Platform object based on the arguments passed Platform platform = new Platform(args[0],args[1] , args[2],args[3]); System.out.println("Platform instantiation complete "); //Instantiate a AlarmLog Object for a log named "AlarmLog" //-------------------------------------------------------- System.out.println("AlarmLog instantiation"); LogName logName = new LogName(args[1], "AlarmLog"); AlarmLog log = new AlarmLog(platform, logName); //Get the total count of all existing alarms //------------------------------------------// Create a query with no filters GenericQuery query = new GenericQuery(); System.out.println("\n Getting alarm Count for the filter:\n"); System.out.println(query.toString()); int count = log.getAlarmCount(query); System.out.println("Count of Alarms: " + count); //Get alarms based on a query //---------------------------- // Add the filter created earlier to the query to change the query query.addFilter(filter); System.out.println("\n Getting alarm Count for the filter:\n"); System.out.println(query.toString()); // Get the count of existing alarms based on the query count = log.getAlarmCount(query); System.out.println("Count of Alarms: " + count); System.out.println("Getting alarms for the above filter\n"); //Get alarms based on the query AlarmRecord[] alarms = log.getAlarms(query, attrSet); //Now print out the result //------------------------ printAlarmRecord(alarms, attrSet); if (alarms.length == 0) { System.out.println("Stop the test as no alarms match the filter"); System.exit(-1); } //Getting alarms created after a specific date //-------------------------------------------- try { Date loggingTime = alarms[0].getLoggingTime(); System.out.println("Getting alarm Count for alarms created
after or on:" + loggingTime.toString()); FilterItem filterItem3 = new FilterItem(AlarmRecordAttribute.LOGGING_TIME, RelationCriteria.GREATER_THAN_OR_EQUAL, loggingTime); Filter filter1 = new Filter(filterItem3); //Add to the previous query query.addFilter(filter1); System.out.println(query.toString()); count = log.getAlarmCount(query); System.out.println("Received: " + count + " alarms matching the filter"); } catch (AlarmAttributeNotSetException e) { e.printStackTrace(); } //Getting alarms for perceived severity and clear State //----------------------------------------------------- FilterItem filterItem2 = new FilterItem(AlarmRecordAttribute.CLEAR_STATE, RelationCriteria.EQUAL, Boolean.TRUE); Filter filter2 = new Filter(filterItem2); query.addFilter(filter2); System.out.println("\n Getting alarm Count for the filter:\n"); System.out.println(query.toString()); count = log.getAlarmCount(query); System.out.println("Count of Alarms: " + count); } catch (Exception e) { e.printStackTrace(); } } private static void printAlarmRecord(AlarmRecord[] alarms, AlarmRecordAttributeSet attrSet) throws AlarmException { System.out.println("Received : " + alarms.length + "Alarms "); int ii; for (ii=0; ii<alarms.length; ii++) { System.out.println("Alarm number:" + ii ); AlarmRecord alr1 = (AlarmRecord)alarms[ii]; System.out.println(alr1.toString()); } } private static final String sccsID = "@(#)AlarmQuery.java 1.5 98/10/30 Sun Microsystems"; }
Sun Microsystems, Inc. Copyright information. All rights reserved. |
Doc Set | Contents | Previous | Next | Index |