Setting up the Rules

The Service Performance Manager provides you with another set of API to define a set of rules that, once registered with the server, gets automatically triggered when the threshold conditions are met.

Defining a Rule Definition Object and Set its Properties

Procedure

  1. The following code snippet helps you create a MutableRuleDef object and set the properties of the instance.
    RuleFactory factory = new RuleFactory();
    String ruleName = "APIDemoRule"; 
    MutableRuleDef ruleDef = factory.newRuleDef(ruleName + Math.round(Math.random()));
    ruleDef.setScheduleName("schedule1");
    ruleDef.setUserName("userName1");
  2. To set the threshold conditions at which a rule gets evaluated, define a query object, and set its query type to STREAMING. Rules are nothing but continuously evaluating queries.
    QueryFactory qfac = QueryFactory.INSTANCE;
    //***********************Set Condition*************
    QueryByFilterDef setCondition = 
    qfac.newQueryByFilterDef(SCHEMA_NAME, CUBE_DEV, 
    DIM_HIERARCHY_DEMO, MEASUREMENT_HITCOUNT);
    setCondition.setName("RuleService");
    setCondition.setQueryType(QueryType.STREAMING);
    setCondition.setBatchSize(6);
    Filter eqFilter = QueryFactory.INSTANCE.newEqFilter(
                      MetricQualifier.DIMENSION_LEVEL, 
                      DIM_LEVEL_SERVICE);
    Filter gtFilter = QueryFactory.INSTANCE.newGtFilter(
                      FilterKeyQualifier.MEASUREMENT_NAME, 
                      MEASUREMENT_HITCOUNT, 41.0);
    Filter ltFilter = QueryFactory.INSTANCE.newLtFilter(
                      FilterKeyQualifier.MEASUREMENT_NAME,
                      MEASUREMENT_HITCOUNT, 45.0);
    AndFilter andFilter = QueryFactory.INSTANCE.newAndFilter();
    andFilter.addFilter(eqFilter, gtFilter, ltFilter);
    setCondition.setFilter(andFilter);
    //*************************Clear Condition*************
    QueryByFilterDef clearCondition= 
    qfac.newQueryByFilterDef(SCHEMA_NAME, CUBE_DEV, 
    DIM_HIERARCHY_DEMO, MEASUREMENT_HITCOUNT);
    clearCondition.setName("ClearCondition");
    clearCondition.setQueryType(QueryType.STREAMING);
    clearCondition.setBatchSize(6);
    Filter eqFilter1 = QueryFactory.INSTANCE.newEqFilter(
                       MetricQualifier.DIMENSION_LEVEL,
                       DIM_LEVEL_SERVICE);
    Filter gtFilter1 = QueryFactory.INSTANCE.newGtFilter(
                       FilterKeyQualifier.MEASUREMENT_NAME, 
                       MEASUREMENT_HITCOUNT, 47.0);
    Filter ltFilter1 = QueryFactory.INSTANCE.newLtFilter(
                       FilterKeyQualifier.MEASUREMENT_NAME, 
                       MEASUREMENT_HITCOUNT, 50.0);
    AndFilter andFilter2 = QueryFactory.INSTANCE.newAndFilter();
    andFilter2.addFilter(eqFilter1, gtFilter1, ltFilter1);
    clearCondition.setFilter(andFilter2);
    Note: Rules across measurements and hierarchies in the conditions are not supported. For example, you cannot create a rule with a condition, such as:
    application.HitCount > 200 AND node.AverageUsedMemory > 600 MB

    OR

    application.SuccessCount > application.HitCount
  3. Set and clear conditions can be thought of upper and lower bounds for the threshold values. Once these conditions are defined, you need to register them with the rule.
    ruleDef.setSetCondition(setCondition);
    ruleDef.setClearCondition(clearCondition);
    Note: You cannot set a rule on alerts hierarchy.
  4. After setting the threshold conditions for set and clear, define actions to be taken when these conditions are met. Actions have to be first setup on the Service Performance Manager server. Some actions such as "Send-Email" are already set up on the server. Refer to Creating Custom Actions for details on how to set up custom actions on the server. First, initialize the action definitions using session.getAllActionFunctionDescriptors().
    //Get a handle to a pre-defined action function descriptor.
    session.getAllActionFunctionDescriptors();
    ActionFunctionDescriptor sendToSessionActionFn =         ActionFunctionsRepository.INSTANCE.getFunctionDescriptor        ("SendToNamedSession");
    //Get a handle to the action's function parameters and provide values to its parameters.
    FunctionParam param = sendToSessionActionFn.getFunctionParam("session-name");
    ActionFunctionDescriptorImpl.FunctionParamValueImpl 
    paramValue = new 
    FunctionDescriptorImpl.FunctionParamValueImpl();
    paramValue.setName(param.getName());
    paramValue.setDataType(param.getDataType());
    paramValue.setIndex(param.getIndex());
    paramValue.setDescription(param.getDescription());
    paramValue.setValue(sessionName);
    //Bind the function parameter value to the action function.
    sendToSessionActionFn.addFunctionParamValue(paramValue);
  5. Add the time-based constraints to control how many times and how frequently the actions must get triggered.
    InvokeConstraint invokeConstraint1 = factory.newInvokeConstraint(Constraint.TIMED);
    MutableTimeBasedConstraint tbc = (MutableTimeBasedConstraint) invokeConstraint1;
    tbc.setInvocationFrequency(1000);
    tbc.setMaxInvocationCount(5);
    tbc.setTimeConstraint(TimeBasedConstraint.Constraint.TILL_CONDITION_CLEARS);
  6. After defining the actions, register the actions with a rule either to fire when the set condition is met or to fire when the clear condition is met.
    ActionDef setSendSessionAction = 
    factory.newSetActionDef(ruleDef, sendToSessionActionFn, invokeConstraint1);
    /A convenience method that clones the passed in action definition and sets it as an action for the clear condition.
    ActionDef clearSessionAction = 
    factory.newClearActionDef(ruleDef, setSendSessionAction);