Solstice Enterprise Manager 4.1 Developing Java Applications Doc Set ContentsPreviousIndex


Appendix A

Using the Java Alarm and Topology APIs Together

In some scenarios, it is useful to use the Java Alarm and Java Topology APIs together. For example, you may want to provide visual representation of alarms. This can be done by using the Java Topology API to create the topology node representation and the Alarm API to highlight the affected nodes.

This appendix provides a sample program that shows you how to use the Topology and Alarm APIs together (see the following code example). The program gets all the alarms pertaining to the specified node.

CODE EXAMPLE A-1 GetAlarmsForNode.java 
 * Copyright 10/30/98 Sun Microsystems, Inc. All Rights Reserved.
 */
import com.sun.em.api.common.*;
import com.sun.em.api.topology.*;
import com.sun.em.api.alarm.*;
import com.sun.em.api.pmi.Platform;
/*
  GetAlarmsForNode <servername> <mis-name> <username> <password> <name>
  <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.
  <name>     - is the name of the node.
 */
public class GetAlarmsForNode
{
  static void usage() {
    System.err.println("Usage:\n"+ "GetAlarmsForNode <servername> <mis-name> <username> <password> name");
    System.err.println("\t-Run the example with <servername> as the
	 remote server and <misname> \n\t where EM mis is running.");
    System.err.println("\t name = Node name ");
    System.exit(-1);
  }
  public static void main(String[] args) {
    if (args.length < 5) {
      usage();
    }
    Platform platform = null;
    try {
      //Instantiate the Platform object
      platform = new Platform(args[0],args[1],args[2],args[3]);
      //Instantiate a AlarmLog Object for a log named "AlarmLog"
      LogName logName = new LogName(args[1], "AlarmLog");
      System.out.println("AlarmLog instantiation");
      AlarmLog log = new AlarmLog(platform, logName);
      //Instantiate the EMTopoPlatform
      EMTopoPlatform topoPlatform = new EMTopoPlatform(platform);
      //Create a list of toponode ids to be used to retrieve the 
//managed objects. This is a list since there could be more
      //than one node with same name.
      EMTopoNodeDn dns[] = null;
      dns = EMTopoNode.findNodesByName(topoPlatform,args[4]);
      int ids[] = new int[dns.length];
      for(int k=0; k < dns.length; k++)
        ids[k] = dns[k].getUniqueId(); // get the toponode id
      //Get all the managed object names for the given toponode ids
      MOName[] monames = EMTopoNode.findMOsByNodes(topoPlatform, args[1] , ids, false);
      //Create a query object to get all alarms for the list of  
      //managed objects specified.
      FilterItem filterItem =  new FilterItem(AlarmRecordAttribute.MANAGED_OBJECT_INSTANCE,
     RelationCriteria.EQUAL, monames);
                                             
      Filter filter = new Filter(filterItem);
      GenericQuery query = new GenericQuery(filter);
      //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.ACK_OPERATOR);
      attrSet.add(AlarmRecordAttribute.MANAGED_OBJECT_INSTANCE);
      attrSet.add(AlarmRecordAttribute.LOG_NAME);
      attrSet.add(AlarmRecordAttribute.MANAGED_OBJECT_INSTANCE);
      //Get all the alarms satisfying the query created above.
      System.out.println("AlarmLog query begins");
      AlarmRecord[] alarms = log.getAlarms(query, attrSet);
      //Now print out the result
      //------------------------
      printAlarmRecord(alarms, attrSet);
    }
    catch (Exception e) {
      e.printStackTrace();
      System.exit(-1);
    }
    System.out.println("Done.");
    System.exit(0);
  }
  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 = "@(#)GetAlarmsForNode.java   1.3 98/10/30 Sun Microsystems";
}




Sun Microsystems, Inc.
Copyright information. All rights reserved.
Doc Set  |   Contents   |   Previous   |   Index