Beispiel #1
0
  /**
   * Return the number of events for this node and the given acknowledgment type.
   *
   * @param nodeId a int.
   * @param ackType a {@link org.opennms.web.event.AcknowledgeType} object.
   * @return a int.
   * @throws java.sql.SQLException if any.
   */
  public static int getEventCountForNode(int nodeId, AcknowledgeType ackType) throws SQLException {
    if (ackType == null) {
      throw new IllegalArgumentException("Cannot take null parameters.");
    }

    int eventCount = 0;
    Connection conn = Vault.getDbConnection();

    try {
      StringBuffer select =
          new StringBuffer("SELECT COUNT(EVENTID) AS EVENTCOUNT FROM EVENTS WHERE ");
      select.append(getAcknowledgeTypeClause(ackType));

      select.append(" AND NODEID=?");
      select.append(" AND EVENTDISPLAY='Y' ");

      PreparedStatement stmt = conn.prepareStatement(select.toString());
      stmt.setInt(1, nodeId);
      ResultSet rs = stmt.executeQuery();

      if (rs.next()) {
        eventCount = rs.getInt("EVENTCOUNT");
      }

      rs.close();
      stmt.close();
    } finally {
      Vault.releaseDbConnection(conn);
    }

    return eventCount;
  }
Beispiel #2
0
  /**
   * Return all events (optionally only unacknowledged events) sorted by time that have the given
   * distributed poller.
   *
   * @param poller a {@link java.lang.String} object.
   * @param includeAcknowledged a boolean.
   * @return an array of {@link org.opennms.web.event.Event} objects.
   * @throws java.sql.SQLException if any.
   */
  public static Event[] getEventsForPoller(String poller, boolean includeAcknowledged)
      throws SQLException {
    if (poller == null) {
      throw new IllegalArgumentException("Cannot take null parameters.");
    }

    Event[] events = null;
    Connection conn = Vault.getDbConnection();

    try {
      StringBuffer select = new StringBuffer("SELECT * FROM EVENTS WHERE EVENTDPNAME=?");

      if (!includeAcknowledged) {
        select.append(" AND EVENTACKUSER IS NULL");
      }

      select.append(" AND EVENTDISPLAY='Y' ");
      select.append(" ORDER BY EVENTID DESC");

      PreparedStatement stmt = conn.prepareStatement(select.toString());
      stmt.setString(1, poller);
      ResultSet rs = stmt.executeQuery();

      events = rs2Events(rs);

      rs.close();
      stmt.close();
    } finally {
      Vault.releaseDbConnection(conn);
    }

    return events;
  }
Beispiel #3
0
  /**
   * Return a specific event.
   *
   * @param eventId a int.
   * @return a {@link org.opennms.web.event.Event} object.
   * @throws java.sql.SQLException if any.
   */
  public static Event getEvent(int eventId) throws SQLException {
    Event event = null;
    Connection conn = Vault.getDbConnection();

    try {
      PreparedStatement stmt =
          conn.prepareStatement(
              "SELECT EVENTS.*, NODE.NODELABEL, SERVICE.SERVICENAME FROM EVENTS LEFT OUTER JOIN NODE USING (NODEID) LEFT OUTER JOIN SERVICE USING (SERVICEID) WHERE EVENTID=? ");
      stmt.setInt(1, eventId);
      ResultSet rs = stmt.executeQuery();

      Event[] events = rs2Events(rs);

      // what do I do if this actually returns more than one service?
      if (events.length > 0) {
        event = events[0];
      }

      rs.close();
      stmt.close();
    } finally {
      Vault.releaseDbConnection(conn);
    }

    return event;
  }
Beispiel #4
0
  /**
   * Unacknowledge events that match the given filter criteria.
   *
   * @param filters an array of org$opennms$web$filter$Filter objects.
   * @throws java.sql.SQLException if any.
   */
  public static void unacknowledge(Filter[] filters) throws SQLException {
    if (filters == null) {
      throw new IllegalArgumentException("Cannot take null parameters.");
    }

    StringBuffer update =
        new StringBuffer("UPDATE EVENTS SET EVENTACKUSER=NULL, EVENTACKTIME=NULL WHERE");
    update.append(getAcknowledgeTypeClause(AcknowledgeType.ACKNOWLEDGED));

    for (Filter filter : filters) {
      update.append(" AND");
      update.append(filter.getParamSql());
    }

    Connection conn = Vault.getDbConnection();

    try {
      PreparedStatement stmt = conn.prepareStatement(update.toString());

      int parameterIndex = 1;
      for (Filter filter : filters) {
        parameterIndex += filter.bindParam(stmt, parameterIndex);
      }

      stmt.executeUpdate();
      stmt.close();
    } finally {
      Vault.releaseDbConnection(conn);
    }
  }
Beispiel #5
0
  /**
   * Unacknowledge a list of events.
   *
   * @param eventIds an array of int.
   * @throws java.sql.SQLException if any.
   */
  public static void unacknowledge(int[] eventIds) throws SQLException {
    if (eventIds == null) {
      throw new IllegalArgumentException("Cannot take null parameters.");
    }

    if (eventIds.length > 0) {
      StringBuffer update =
          new StringBuffer("UPDATE EVENTS SET EVENTACKUSER=NULL, EVENTACKTIME=NULL");
      update.append(" WHERE EVENTID IN (");
      update.append(eventIds[0]);

      for (int i = 1; i < eventIds.length; i++) {
        update.append(",");
        update.append(eventIds[i]);
      }

      update.append(")");

      Connection conn = Vault.getDbConnection();

      try {
        PreparedStatement stmt = conn.prepareStatement(update.toString());
        stmt.executeUpdate();
        stmt.close();
      } finally {
        Vault.releaseDbConnection(conn);
      }
    }
  }
Beispiel #6
0
  /**
   * Unacknowledge all acknowledged events.
   *
   * @throws java.sql.SQLException if any.
   */
  public static void unacknowledgeAll() throws SQLException {
    Connection conn = Vault.getDbConnection();

    try {
      PreparedStatement stmt =
          conn.prepareStatement(
              "UPDATE EVENTS SET EVENTACKUSER=NULL, EVENTACKTIME=NULL WHERE EVENTACKUSER IS NOT NULL");
      stmt.executeUpdate();
      stmt.close();
    } finally {
      Vault.releaseDbConnection(conn);
    }
  }
Beispiel #7
0
  /**
   * Count the number of events for a given acknowledgement type.
   *
   * @return An array of event counts. Each index of the array corresponds to the event severity for
   *     the counts (indeterminate is 1, critical is 7, etc).
   * @param ackType a {@link org.opennms.web.event.AcknowledgeType} object.
   * @param filters an array of org$opennms$web$filter$Filter objects.
   * @throws java.sql.SQLException if any.
   */
  public static int[] getEventCountBySeverity(AcknowledgeType ackType, Filter[] filters)
      throws SQLException {
    if (ackType == null || filters == null) {
      throw new IllegalArgumentException("Cannot take null parameters.");
    }

    int[] eventCounts = new int[8];
    Connection conn = Vault.getDbConnection();

    try {
      StringBuffer select =
          new StringBuffer(
              "SELECT EVENTSEVERITY, COUNT(*) AS EVENTCOUNT FROM EVENTS LEFT OUTER JOIN NODE USING (NODEID) LEFT OUTER JOIN SERVICE USING (SERVICEID) WHERE ");
      select.append(getAcknowledgeTypeClause(ackType));

      for (Filter filter : filters) {
        select.append(" AND");
        select.append(filter.getParamSql());
      }

      select.append(" AND EVENTDISPLAY='Y'");
      select.append(" GROUP BY EVENTSEVERITY");

      PreparedStatement stmt = conn.prepareStatement(select.toString());

      int parameterIndex = 1;
      for (Filter filter : filters) {
        parameterIndex += filter.bindParam(stmt, parameterIndex);
      }

      ResultSet rs = stmt.executeQuery();

      while (rs.next()) {
        int severity = rs.getInt("EVENTSEVERITY");
        int eventCount = rs.getInt("EVENTCOUNT");

        eventCounts[severity] = eventCount;
      }

      rs.close();
      stmt.close();
    } finally {
      Vault.releaseDbConnection(conn);
    }

    return eventCounts;
  }
Beispiel #8
0
  /**
   * Acknowledge all unacknowledged events with the given username and the given time.
   *
   * @param user a {@link java.lang.String} object.
   * @param time a java$util$Date object.
   * @throws java.sql.SQLException if any.
   */
  public static void acknowledgeAll(String user, Date time) throws SQLException {
    if (user == null || time == null) {
      throw new IllegalArgumentException("Cannot take null parameters.");
    }

    Connection conn = Vault.getDbConnection();

    try {
      PreparedStatement stmt =
          conn.prepareStatement(
              "UPDATE EVENTS SET EVENTACKUSER=?, EVENTACKTIME=? WHERE EVENTACKUSER IS NULL");
      stmt.setString(1, user);
      stmt.setTimestamp(2, new Timestamp(time.getTime()));

      stmt.executeUpdate();
      stmt.close();
    } finally {
      Vault.releaseDbConnection(conn);
    }
  }
Beispiel #9
0
  /**
   * Return all events (optionally only unacknowledged events) sorted by the given sort style.
   *
   * <p><strong>Note: </strong> This limit/offset code is <em>Postgres specific!</em> Per <a
   * href="mailto:[email protected]">Shane </a>, this is okay for now until we can come up with an
   * Oracle alternative too.
   *
   * @param limit if -1 or zero, no limit or offset is used
   * @param offset if -1, no limit or offset if used
   * @param sortStyle a {@link org.opennms.web.event.SortStyle} object.
   * @param ackType a {@link org.opennms.web.event.AcknowledgeType} object.
   * @param filters an array of org$opennms$web$filter$Filter objects.
   * @return an array of {@link org.opennms.web.event.Event} objects.
   * @throws java.sql.SQLException if any.
   */
  public static Event[] getEvents(
      SortStyle sortStyle, AcknowledgeType ackType, Filter[] filters, int limit, int offset)
      throws SQLException {
    if (sortStyle == null || ackType == null || filters == null) {
      throw new IllegalArgumentException("Cannot take null parameters.");
    }

    boolean useLimits = false;
    if (limit > 0 && offset > -1) {
      useLimits = true;
    }

    Event[] events = null;
    Connection conn = Vault.getDbConnection();

    try {
      /*
      StringBuffer select = new StringBuffer("" +
      		"  SELECT EVENTS.*, NODE.NODELABEL, SERVICE.SERVICENAME " +
      		"    FROM EVENTS " +
      		"LEFT OUTER JOIN NODE USING(NODEID) " +
      		"LEFT OUTER JOIN SERVICE USING(SERVICEID) WHERE");
      */
      StringBuffer select =
          new StringBuffer(
              ""
                  + "          SELECT events.*, node.nodelabel, service.servicename "
                  + "            FROM node "
                  + "RIGHT OUTER JOIN events "
                  + "              ON (events.nodeid = node.nodeid) "
                  + " LEFT OUTER JOIN service "
                  + "              ON (service.serviceid = events.serviceid) "
                  + "           WHERE ");

      select.append(getAcknowledgeTypeClause(ackType));

      for (Filter filter : filters) {
        select.append(" AND");
        select.append(filter.getParamSql());
      }

      select.append(" AND EVENTDISPLAY='Y' ");
      select.append(getOrderByClause(sortStyle));

      if (useLimits) {
        select.append(" LIMIT ");
        select.append(limit);
        select.append(" OFFSET ");
        select.append(offset);
      }

      PreparedStatement stmt = conn.prepareStatement(select.toString());

      int parameterIndex = 1;
      for (Filter filter : filters) {
        parameterIndex += filter.bindParam(stmt, parameterIndex);
      }

      ResultSet rs = stmt.executeQuery();

      events = rs2Events(rs);

      rs.close();
      stmt.close();
    } finally {
      Vault.releaseDbConnection(conn);
    }

    return events;
  }