Exemplo n.º 1
0
  /**
   * replaceNotification
   *
   * @param oldName a {@link java.lang.String} object.
   * @param newNotice a {@link org.opennms.netmgt.config.notifications.Notification} object.
   * @throws org.exolab.castor.xml.MarshalException if any.
   * @throws org.exolab.castor.xml.ValidationException if any.
   * @throws java.io.IOException if any.
   * @throws java.lang.ClassNotFoundException if any.
   */
  public synchronized void replaceNotification(final String oldName, final Notification newNotice)
      throws MarshalException, ValidationException, IOException, ClassNotFoundException {
    //   In order to preserve the order of the notices, we have to replace "in place".

    Notification notice = getNotification(oldName);
    if (notice != null) {
      notice.setWriteable(newNotice.getWriteable());
      notice.setName(newNotice.getName());
      notice.setDescription(newNotice.getDescription());
      notice.setUei(newNotice.getUei());
      notice.setRule(newNotice.getRule());
      notice.setDestinationPath(newNotice.getDestinationPath());
      notice.setNoticeQueue(newNotice.getNoticeQueue());
      notice.setTextMessage(newNotice.getTextMessage());
      notice.setSubject(newNotice.getSubject());
      notice.setNumericMessage(newNotice.getNumericMessage());
      notice.setStatus(newNotice.getStatus());
      notice.setVarbind(newNotice.getVarbind());
      notice.getParameterCollection().clear(); // Required to avoid NMS-5948
      for (Parameter parameter : newNotice.getParameterCollection()) {
        Parameter newParam = new Parameter();
        newParam.setName(parameter.getName());
        newParam.setValue(parameter.getValue());
        notice.addParameter(newParam);
      }
      saveCurrent();
    } else addNotification(newNotice);
  }
Exemplo n.º 2
0
  /**
   * hasUei
   *
   * @param uei a {@link java.lang.String} object.
   * @return a boolean.
   * @throws java.io.IOException if any.
   * @throws org.exolab.castor.xml.MarshalException if any.
   * @throws org.exolab.castor.xml.ValidationException if any.
   */
  public boolean hasUei(final String uei)
      throws IOException, MarshalException, ValidationException {
    update();

    for (Notification notif : m_notifications.getNotificationCollection()) {
      if (uei.equals(notif.getUei()) || "MATCH-ANY-UEI".equals(notif.getUei())) {
        return true;
      } else if (notif.getUei().charAt(0) == '~') {
        if (uei.matches(notif.getUei().substring(1))) {
          return true;
        }
      }
    }

    return false;
  }
Exemplo n.º 3
0
  /**
   * nodeInterfaceServiceValid
   *
   * @param notif a {@link org.opennms.netmgt.config.notifications.Notification} object.
   * @param event a {@link org.opennms.netmgt.xml.event.Event} object.
   * @return a boolean.
   */
  protected boolean nodeInterfaceServiceValid(final Notification notif, final Event event) {
    Assert.notNull(notif, "notif argument must not be null");
    Assert.notNull(event, "event argument must not be null");
    Assert.notNull(notif.getRule(), "getRule() on notif argument must not return null");

    /*
     *  If the event doesn't have a nodeId, interface, or service,
     *  return true since there is nothing on which to filter.
     */
    if (event.getNodeid() == 0 && event.getInterface() == null && event.getService() == null) {
      if ("MATCH-ANY-UEI".equals(notif.getUei())) {
        // TODO: Trim parentheses from the filter and trim whitespace from inside the
        // filter statement. This comparison is very brittle as it is.
        if ("ipaddr != '0.0.0.0'".equals(notif.getRule().toLowerCase())
            || "ipaddr iplike *.*.*.*".equals(notif.getRule().toLowerCase())) {
          return true;
        } else {
          return false;
        }
      }
      return true;
    }

    StringBuffer constraints = new StringBuffer();
    if (event.getNodeid() != 0) {
      constraints.append(" & (nodeId == " + event.getNodeid() + ")");
    }

    if (event.getInterface() != null && !"0.0.0.0".equals(event.getInterface())) {
      constraints.append(" & (ipAddr == '" + event.getInterface() + "')");
      if (event.getService() != null) {
        constraints.append(" & (serviceName == '" + event.getService() + "')");
      }
    }

    String rule = "((" + notif.getRule() + ")" + constraints + ")";

    return isRuleMatchingFilter(notif, rule);
  }
Exemplo n.º 4
0
  /**
   * getNotifForEvent
   *
   * @param event a {@link org.opennms.netmgt.xml.event.Event} object.
   * @return an array of {@link org.opennms.netmgt.config.notifications.Notification} objects.
   * @throws java.io.IOException if any.
   * @throws org.exolab.castor.xml.MarshalException if any.
   * @throws org.exolab.castor.xml.ValidationException if any.
   */
  public Notification[] getNotifForEvent(final Event event)
      throws IOException, MarshalException, ValidationException {
    update();
    List<Notification> notifList = new ArrayList<Notification>();
    boolean matchAll = getConfigManager().getNotificationMatch();
    ThreadCategory log = this.log();

    // This if statement will check to see if notification should be suppressed for this event.

    if (event == null) {
      log.warn("unable to get notification for null event!");
      return null;
    } else if (event.getLogmsg() != null && !(event.getLogmsg().getNotify())) {
      if (log.isDebugEnabled())
        log.debug("Event " + event.getUei() + " is configured to suppress notifications.");
      return null;
    }

    for (Notification curNotif : m_notifications.getNotificationCollection()) {
      if (log.isDebugEnabled())
        log.debug("Checking " + event.getUei() + " against " + curNotif.getUei());

      if (event.getUei().equals(curNotif.getUei()) || "MATCH-ANY-UEI".equals(curNotif.getUei())) {
        // Match!
      } else if (curNotif.getUei().charAt(0) == '~') {
        if (event.getUei().matches(curNotif.getUei().substring(1))) {
          // Match!
        } else {
          if (log.isDebugEnabled())
            log.debug(
                "Notification regex "
                    + curNotif.getUei()
                    + " failed to match event UEI: "
                    + event.getUei());
          continue;
        }
      } else {
        if (log.isDebugEnabled())
          log.debug("Event UEI " + event.getUei() + " did not match " + curNotif.getUei());
        continue;
      }

      /** Check if event severity matches pattern in notification */
      if (log.isDebugEnabled())
        log.debug(
            "Checking event severity: "
                + event.getSeverity()
                + " against notification severity: "
                + curNotif.getEventSeverity());
      // parameter is optional, return true if not set
      if (curNotif.getEventSeverity() == null) {
        // Skip matching on severity
      } else if (event
          .getSeverity()
          .toLowerCase()
          .matches(curNotif.getEventSeverity().toLowerCase())) {
        // Severities match
      } else {
        if (log.isDebugEnabled())
          log.debug(
              "Event severity: "
                  + event.getSeverity()
                  + " did not match notification severity: "
                  + curNotif.getEventSeverity());
        continue;
      }

      // The notice has to be "on"
      // The notice has to match a severity if configured - currHasSeverity should be true if there
      // is no severity rule
      // The notice has to match the UEI of the event or MATCH-ANY-UEI
      // If all those things are true:
      // Then the service has to match if configured, the interface if configured, and the node if
      // configured.

      if (curNotif.getStatus().equals("on")) {
        if (nodeInterfaceServiceValid(curNotif, event)) {
          boolean parmsmatched = getConfigManager().matchNotificationParameters(event, curNotif);

          if (!parmsmatched) {
            if (log.isDebugEnabled())
              log.debug(
                  "Event "
                      + event.getUei()
                      + " did not match parameters for notice "
                      + curNotif.getName());
            continue;
          }
          // Add this notification to the return value
          notifList.add(curNotif);

          if (log.isDebugEnabled())
            log.debug("Event " + event.getUei() + " matched notice " + curNotif.getName());

          if (!matchAll) break;
        } else {
          if (log.isDebugEnabled())
            log.debug("Node/interface/service combination in the event was invalid");
        }
      } else {
        if (log.isDebugEnabled()) log.debug("Current notification is turned off.");
      }
    }

    if (!notifList.isEmpty()) {
      return notifList.toArray(new Notification[0]);
    } else {
      return null;
    }
  }