Ejemplo n.º 1
0
  private Double getCalculatedBaselineValue(
      int conditionId,
      AlertConditionBaselineCategoryComposite composite,
      String optionStatus,
      Double threshold) {
    int baselineId = composite.getBaselineId();

    if (AlertConditionCacheUtils.isInvalidDouble(threshold)) {
      log.error(
          "Failed to calculate baseline for [conditionId="
              + conditionId
              + ", baselineId="
              + baselineId
              + "]: threshold was null");
    }

    // auto-unboxing of threshold is safe here
    Double baselineValue = 0.0;

    if (optionStatus == null) {
      log.error(
          "Failed to calculate baseline for [conditionId="
              + conditionId
              + ", baselineId="
              + baselineId
              + "]: optionStatus string was null");
    } else if (optionStatus.equals("min")) {
      baselineValue = composite.getMinValue();
    } else if (optionStatus.equals("mean")) {
      baselineValue = composite.getMeanValue();
    } else if (optionStatus.equals("max")) {
      baselineValue = composite.getMaxValue();
    } else {
      log.error(
          "Failed to calculate baseline for [conditionId="
              + conditionId
              + ", baselineId="
              + baselineId
              + "]: unrecognized optionStatus string of '"
              + optionStatus
              + "'");
      return null;
    }

    if (AlertConditionCacheUtils.isInvalidDouble(baselineValue)) {
      log.error(
          "Failed to calculate baseline for [conditionId="
              + conditionId
              + ", baselineId="
              + baselineId
              + "]: optionStatus string was '"
              + optionStatus
              + "', but the corresponding baseline value was null");
    }

    return threshold * baselineValue;
  }
Ejemplo n.º 2
0
  private void insertAlertConditionComposite(
      int agentId,
      AbstractAlertConditionCategoryComposite composite,
      AlertConditionCacheStats stats) {

    AlertCondition alertCondition = composite.getCondition();
    int alertConditionId =
        alertCondition
            .getId(); // auto-unboxing is safe here because as the PK it's guaranteed to be non-null

    AlertConditionCategory alertConditionCategory = alertCondition.getCategory();
    AlertConditionOperator alertConditionOperator =
        AlertConditionCacheUtils.getAlertConditionOperator(alertCondition);

    if (DataType.CALLTIME == composite.getDataType()) { // call-time cases start here
      if (alertConditionCategory == AlertConditionCategory.CHANGE) {
        AlertConditionChangesCategoryComposite changesComposite =
            (AlertConditionChangesCategoryComposite) composite;
        int scheduleId = changesComposite.getScheduleId();

        try {
          CallTimeDataCacheElement cacheElement =
              new CallTimeDataCacheElement(
                  alertConditionOperator,
                  CallTimeElementValue.valueOf(alertCondition.getOption()),
                  alertCondition.getComparator(),
                  alertCondition.getThreshold(),
                  alertConditionId,
                  alertCondition.getName());

          addTo(
              "callTimeDataCache",
              callTimeCache,
              scheduleId,
              cacheElement,
              alertConditionId,
              stats);
        } catch (InvalidCacheElementException icee) {
          log.info(
              "Failed to create CallTimeDataCacheElement with parameters: "
                  + AlertConditionCacheUtils.getCacheElementErrorString(
                      alertConditionId,
                      alertConditionOperator,
                      null,
                      alertCondition.getThreshold(),
                      icee));
        }
      } else if (alertConditionCategory == AlertConditionCategory.THRESHOLD) {
        AlertConditionScheduleCategoryComposite thresholdComposite =
            (AlertConditionScheduleCategoryComposite) composite;

        try {
          CallTimeDataCacheElement cacheElement =
              new CallTimeDataCacheElement(
                  alertConditionOperator,
                  CallTimeElementValue.valueOf(alertCondition.getOption()),
                  null,
                  alertCondition.getThreshold(),
                  alertConditionId,
                  alertCondition.getName());

          addTo(
              "measurementDataCache",
              callTimeCache,
              thresholdComposite.getScheduleId(),
              cacheElement,
              alertConditionId,
              stats);
        } catch (InvalidCacheElementException icee) {
          log.info(
              "Failed to create CallTimeDataCacheElement with parameters: "
                  + AlertConditionCacheUtils.getCacheElementErrorString(
                      alertConditionId,
                      alertConditionOperator,
                      null,
                      alertCondition.getThreshold(),
                      icee));
        }
      } // last call-time case
    } else if (alertConditionCategory
        == AlertConditionCategory.BASELINE) { // normal cases start here
      AlertConditionBaselineCategoryComposite baselineComposite =
          (AlertConditionBaselineCategoryComposite) composite;
      // option status for baseline gets set to "mean", but it's rather useless since the UI
      // current doesn't allow alerting off of other baseline properties such as "min" and "max"
      Double threshold = alertCondition.getThreshold();
      String optionStatus = alertCondition.getOption();

      /*
       * yes, calculatedValue may be null, but that's OK because the match
       * method for MeasurementBaselineCacheElement handles nulls just fine
       */
      Double calculatedValue =
          getCalculatedBaselineValue(alertConditionId, baselineComposite, optionStatus, threshold);

      try {
        MeasurementBaselineCacheElement cacheElement =
            new MeasurementBaselineCacheElement(
                alertConditionOperator, calculatedValue, alertConditionId, optionStatus);

        // auto-boxing (of alertConditionId) is always safe
        addTo(
            "measurementDataCache",
            measurementDataCache,
            baselineComposite.getScheduleId(),
            cacheElement,
            alertConditionId,
            stats);
      } catch (InvalidCacheElementException icee) {
        log.info(
            "Failed to create MeasurementBaselineCacheElement with parameters: "
                + AlertConditionCacheUtils.getCacheElementErrorString(
                    alertConditionId, alertConditionOperator, null, calculatedValue, icee));
      }
    } else if (alertConditionCategory == AlertConditionCategory.CHANGE) {
      AlertConditionChangesCategoryComposite changesComposite =
          (AlertConditionChangesCategoryComposite) composite;
      int scheduleId = changesComposite.getScheduleId();

      MeasurementDataNumeric numeric =
          measurementDataManager.getCurrentNumericForSchedule(scheduleId);

      try {
        MeasurementNumericCacheElement cacheElement =
            new MeasurementNumericCacheElement(
                alertConditionOperator,
                (numeric == null) ? null : numeric.getValue(),
                alertConditionId);

        addTo(
            "measurementDataCache",
            measurementDataCache,
            scheduleId,
            cacheElement,
            alertConditionId,
            stats);
      } catch (InvalidCacheElementException icee) {
        log.info(
            "Failed to create MeasurementNumericCacheElement with parameters: "
                + AlertConditionCacheUtils.getCacheElementErrorString(
                    alertConditionId, alertConditionOperator, null, numeric, icee));
      }
    } else if (alertConditionCategory == AlertConditionCategory.TRAIT) {
      AlertConditionTraitCategoryComposite traitsComposite =
          (AlertConditionTraitCategoryComposite) composite;
      String value = null;

      switch (alertConditionOperator) {
        case CHANGES:
          value = traitsComposite.getValue();
          break;
        case REGEX:
          value = traitsComposite.getCondition().getOption();
          break;
        default:
          log.error("Invalid operator for Trait condition: " + alertConditionOperator);
      }

      try {
        /*
         * don't forget special defensive handling to allow for null trait calculation;
         * this might happen if a newly committed resource has some alert template applied to
         * it for some trait that it has not yet gotten from the agent
         */
        MeasurementTraitCacheElement cacheElement =
            new MeasurementTraitCacheElement(alertConditionOperator, value, alertConditionId);

        addTo(
            "measurementTraitCache",
            measurementTraitCache,
            traitsComposite.getScheduleId(),
            cacheElement,
            alertConditionId,
            stats);
      } catch (InvalidCacheElementException icee) {
        log.info(
            "Failed to create MeasurementTraitCacheElement with parameters: "
                + AlertConditionCacheUtils.getCacheElementErrorString(
                    alertConditionId, alertConditionOperator, null, value, icee));
      }

    } else if (alertConditionCategory == AlertConditionCategory.THRESHOLD) {
      AlertConditionScheduleCategoryComposite thresholdComposite =
          (AlertConditionScheduleCategoryComposite) composite;
      Double thresholdValue = alertCondition.getThreshold();

      MeasurementNumericCacheElement cacheElement = null;
      try {
        cacheElement =
            new MeasurementNumericCacheElement(
                alertConditionOperator, thresholdValue, alertConditionId);
      } catch (InvalidCacheElementException icee) {
        log.info(
            "Failed to create MeasurementNumericCacheElement with parameters: "
                + AlertConditionCacheUtils.getCacheElementErrorString(
                    alertConditionId, alertConditionOperator, null, thresholdValue, icee));
      }

      if (cacheElement != null) {
        addTo(
            "measurementDataCache",
            measurementDataCache,
            thresholdComposite.getScheduleId(),
            cacheElement,
            alertConditionId,
            stats);
      }
    } else if (alertConditionCategory == AlertConditionCategory.EVENT) {
      AlertConditionEventCategoryComposite eventComposite =
          (AlertConditionEventCategoryComposite) composite;
      EventSeverity eventSeverity = EventSeverity.valueOf(alertCondition.getName());
      String eventDetails = alertCondition.getOption();

      EventCacheElement cacheElement = null;
      try {
        if (eventDetails == null) {
          cacheElement =
              new EventCacheElement(alertConditionOperator, eventSeverity, alertConditionId);
        } else {
          cacheElement =
              new EventCacheElement(
                  alertConditionOperator, eventDetails, eventSeverity, alertConditionId);
        }
      } catch (InvalidCacheElementException icee) {
        log.info(
            "Failed to create EventCacheElement with parameters: "
                + AlertConditionCacheUtils.getCacheElementErrorString(
                    alertConditionId, alertConditionOperator, eventDetails, eventSeverity, icee));
      }

      addTo(
          "eventsCache",
          eventsCache,
          eventComposite.getResourceId(),
          cacheElement,
          alertConditionId,
          stats);
    } else if (alertConditionCategory == AlertConditionCategory.DRIFT) {
      AlertConditionDriftCategoryComposite driftComposite =
          (AlertConditionDriftCategoryComposite) composite;

      String driftDefNameRegexStr = driftComposite.getCondition().getName();
      String driftPathNameRegexStr = driftComposite.getCondition().getOption();

      DriftCacheElement cacheElement = null;
      try {
        cacheElement =
            new DriftCacheElement(
                alertConditionOperator,
                driftDefNameRegexStr,
                driftPathNameRegexStr,
                alertConditionId);
      } catch (InvalidCacheElementException icee) {
        log.info(
            "Failed to create DriftCacheElement: id="
                + alertConditionId
                + ", operator="
                + alertConditionOperator
                + ", driftDefNameRegex="
                + driftDefNameRegexStr
                + ", driftPathNameRegex="
                + driftPathNameRegexStr);
      }
      addTo(
          "driftCache",
          driftCache,
          driftComposite.getResourceId(),
          cacheElement,
          alertConditionId,
          stats);
    } else if (alertConditionCategory == AlertConditionCategory.RANGE) {
      AlertConditionRangeCategoryComposite rangeComposite =
          (AlertConditionRangeCategoryComposite) composite;
      Double loValue = alertCondition.getThreshold();
      String hiValueStr = alertCondition.getOption();

      MeasurementNumericCacheElement cacheElement = null;
      try {
        if (hiValueStr == null) {
          throw new NumberFormatException("The range alert condition is missing the high value");
        }
        Double hiValue = Double.valueOf(hiValueStr);
        cacheElement =
            new MeasurementRangeNumericCacheElement(
                alertConditionOperator, loValue, hiValue, alertConditionId);
      } catch (InvalidCacheElementException icee) {
        log.info(
            "Failed to create MeasurementRangeNumericCacheElement with parameters: "
                + AlertConditionCacheUtils.getCacheElementErrorString(
                    alertConditionId, alertConditionOperator, hiValueStr, loValue, icee));
      } catch (NumberFormatException nfe) {
        log.info(
            "Failed to create MeasurementRangeNumericCacheElement with parameters: "
                + AlertConditionCacheUtils.getCacheElementErrorString(
                    alertConditionId, alertConditionOperator, hiValueStr, loValue, nfe));
      }

      if (cacheElement != null) {
        addTo(
            "measurementDataCache",
            measurementDataCache,
            rangeComposite.getScheduleId(),
            cacheElement,
            alertConditionId,
            stats);
      }
    }
  }