@Test
  @Transactional
  public void testActions() {
    OnmsEvent event = new OnmsEvent();
    event.setEventLog("Y");
    event.setEventDisplay("Y");
    event.setEventCreateTime(new Date());
    event.setDistPoller(m_distPollerDao.load("localhost"));
    event.setEventTime(new Date());
    event.setEventSeverity(Integer.valueOf(6));
    event.setEventUei("uei://org/opennms/test/EventDaoTest");
    event.setEventSource("test");
    m_eventDao.save(event);

    OnmsNode node = m_nodeDao.findAll().iterator().next();

    OnmsAlarm alarm = new OnmsAlarm();

    alarm.setNode(node);
    alarm.setUei(event.getEventUei());
    alarm.setSeverity(OnmsSeverity.get(event.getEventSeverity()));
    alarm.setSeverityId(event.getEventSeverity());
    alarm.setFirstEventTime(event.getEventTime());
    alarm.setLastEvent(event);
    alarm.setCounter(1);
    alarm.setDistPoller(m_distPollerDao.load("localhost"));

    m_alarmDao.save(alarm);

    OnmsAlarm newAlarm = m_alarmDao.load(alarm.getId());
    assertEquals("uei://org/opennms/test/EventDaoTest", newAlarm.getUei());
    assertEquals(alarm.getLastEvent().getId(), newAlarm.getLastEvent().getId());

    assertEquals(OnmsSeverity.MAJOR, newAlarm.getSeverity());

    newAlarm.escalate("admin");
    assertEquals(OnmsSeverity.CRITICAL, newAlarm.getSeverity());

    newAlarm.clear("admin");
    assertEquals(OnmsSeverity.CLEARED, newAlarm.getSeverity());

    newAlarm.unacknowledge("admin");
    assertNull(newAlarm.getAckUser());
    assertNull(newAlarm.getAlarmAckTime());
  }
Exemple #2
0
  // FIXME: Don't reuse the same "element" variable for multiple objects.
  protected static Event[] rs2Events(ResultSet rs) throws SQLException {
    Event[] events = null;
    Vector<Event> vector = new Vector<Event>();

    while (rs.next()) {
      Event event = new Event();

      Object element = new Integer(rs.getInt("eventID"));
      event.id = ((Integer) element).intValue();

      element = rs.getString("eventUei");
      event.uei = (String) element;

      element = rs.getString("eventSnmp");
      event.snmp = (String) element;

      element = rs.getTimestamp("eventTime");
      event.time = new Date(((Timestamp) element).getTime());

      element = rs.getString("eventHost");
      event.host = (String) element;

      element = rs.getString("eventSnmpHost");
      event.snmphost = (String) element;

      element = rs.getString("eventDpName");
      event.dpName = (String) element;

      element = rs.getString("eventParms");
      event.parms = (String) element;

      // node id can be null
      element = rs.getObject("nodeID");
      if (element == null) {
        event.nodeID = new Integer(0);
      } else {
        event.nodeID = (Integer) element;
      }

      element = rs.getString("ipAddr");
      event.ipAddr = (String) element;

      element = rs.getObject("serviceID");
      event.serviceID = (Integer) element;

      element = rs.getString("nodeLabel");
      event.nodeLabel = (String) element;

      element = rs.getString("serviceName");
      event.serviceName = (String) element;

      element = rs.getTimestamp("eventCreateTime");
      event.createTime = new Date(((Timestamp) element).getTime());

      element = rs.getString("eventDescr");
      event.description = (String) element;

      element = rs.getString("eventLoggroup");
      event.logGroup = (String) element;

      element = rs.getString("eventLogmsg");
      event.logMessage = (String) element;

      element = OnmsSeverity.get(rs.getInt("eventSeverity"));
      event.severity = ((OnmsSeverity) element);

      element = rs.getString("eventOperInstruct");
      event.operatorInstruction = (String) element;

      element = rs.getString("eventAutoAction");
      event.autoAction = (String) element;

      element = rs.getString("eventOperAction");
      event.operatorAction = (String) element;

      element = rs.getString("eventOperActionMenuText");
      event.operatorActionMenuText = (String) element;

      element = rs.getString("eventNotification");
      event.notification = (String) element;

      element = rs.getString("eventTticket");
      event.troubleTicket = (String) element;

      element = rs.getObject("eventTticketState");
      event.troubleTicketState = (Integer) element;

      element = rs.getString("eventForward");
      event.forward = (String) element;

      element = rs.getString("eventMouseOverText");
      event.mouseOverText = (String) element;

      element = rs.getString("eventAckUser");
      event.acknowledgeUser = (String) element;

      element = rs.getTimestamp("eventAckTime");
      if (element != null) {
        event.acknowledgeTime = new Date(((Timestamp) element).getTime());
      }

      element = rs.getObject("alarmid");
      event.alarmId = (Integer) element;

      vector.addElement(event);
    }

    events = new Event[vector.size()];

    for (int i = 0; i < events.length; i++) {
      events[i] = vector.elementAt(i);
    }

    return events;
  }
Exemple #3
0
  @Test
  @JUnitTemporaryDatabase(tempDbClass = MockDatabase.class)
  public void changeFields() throws InterruptedException, SQLException {
    assertEquals(
        0, m_jdbcTemplate.queryForObject("select count(*) from alarms", Integer.class).intValue());

    String reductionKey = "testUpdateField";

    int alarmCount =
        m_jdbcTemplate.queryForObject("select count(*) from alarms", Integer.class).intValue();

    assertEquals(0, alarmCount);

    MockNode node1 = m_mockNetwork.getNode(1);

    // Verify we have the default alarm
    sendNodeDownEvent(reductionKey, node1);
    int severity =
        m_jdbcTemplate
            .queryForObject(
                "select severity from alarms a where a.reductionKey = ?",
                new Object[] {reductionKey},
                Integer.class)
            .intValue();
    assertEquals(OnmsSeverity.MAJOR, OnmsSeverity.get(severity));

    // Store the original logmsg from the original alarm (we are about to test changing it with
    // subsequent alarm reduction)
    String defaultLogMsg =
        m_jdbcTemplate.query(
            "select logmsg from alarms",
            new ResultSetExtractor<String>() {

              @Override
              public String extractData(ResultSet results)
                  throws SQLException, DataAccessException {
                results.next();
                int row = results.getRow();
                boolean isLast = results.isLast();
                boolean isFirst = results.isFirst();

                if (row != 1 && !isLast && !isFirst) {
                  throw new SQLException(
                      "Row count is not = 1.  There should only be one row returned from the query: \n"
                          + results.getStatement());
                }

                return results.getString(1);
              }
            });

    assertTrue("The logmsg column should not be null", defaultLogMsg != null);

    // Duplicate the alarm but change the severity and verify the change
    sendNodeDownEventWithUpdateFieldSeverity(reductionKey, node1, OnmsSeverity.CRITICAL);
    severity =
        m_jdbcTemplate.queryForObject("select severity from alarms", Integer.class).intValue();
    assertEquals(
        "Severity should now be Critical", OnmsSeverity.CRITICAL, OnmsSeverity.get(severity));

    // Duplicate the alarm but don't force the change of severity
    sendNodeDownEvent(reductionKey, node1);
    severity =
        m_jdbcTemplate.queryForObject("select severity from alarms", Integer.class).intValue();
    assertEquals(
        "Severity should still be Critical", OnmsSeverity.CRITICAL, OnmsSeverity.get(severity));

    // Duplicate the alarm and change the logmsg
    sendNodeDownEventChangeLogMsg(reductionKey, node1, "new logMsg");
    String newLogMsg =
        m_jdbcTemplate.query(
            "select logmsg from alarms",
            new ResultSetExtractor<String>() {
              @Override
              public String extractData(ResultSet results)
                  throws SQLException, DataAccessException {
                results.next();
                return results.getString(1);
              }
            });
    assertEquals("new logMsg", newLogMsg);
    assertTrue(!newLogMsg.equals(defaultLogMsg));

    // Duplicate the alarm but force logmsg to not change (lggmsg field is updated by default)
    sendNodeDownEventDontChangeLogMsg(reductionKey, node1, "newer logMsg");
    newLogMsg =
        m_jdbcTemplate.query(
            "select logmsg from alarms",
            new ResultSetExtractor<String>() {
              @Override
              public String extractData(ResultSet results)
                  throws SQLException, DataAccessException {
                results.next();
                return results.getString(1);
              }
            });
    assertTrue("The logMsg should not have changed.", !"newer logMsg".equals(newLogMsg));
    assertEquals(
        "The logMsg should still be equal to the previous update.", "new logMsg", newLogMsg);

    // Duplicate the alarm with the default configuration and verify the logmsg has changed (as is
    // the default behavior
    // for this field)
    sendNodeDownEvent(reductionKey, node1);
    newLogMsg =
        m_jdbcTemplate.query(
            "select logmsg from alarms",
            new ResultSetExtractor<String>() {
              @Override
              public String extractData(ResultSet results)
                  throws SQLException, DataAccessException {
                results.next();
                return results.getString(1);
              }
            });
    assertTrue("The logMsg should have changed.", !"new logMsg".equals(newLogMsg));
    assertEquals("The logMsg should new be the default logMsg.", newLogMsg, defaultLogMsg);
  }
  /**
   * Insert values into the EVENTS table
   *
   * @exception java.sql.SQLException Thrown if there is an error adding the event to the database.
   * @exception java.lang.NullPointerException Thrown if a required resource cannot be found in the
   *     properties file.
   */
  private void insertEvent(final Header eventHeader, final Event event, final Connection connection)
      throws SQLException {
    // Execute the statement to get the next event id
    final int eventID = getNextId();

    LOG.debug("DBID: {}", eventID);

    synchronized (event) {
      event.setDbid(eventID);
    }
    final DBUtils d = new DBUtils(getClass());

    try {
      final PreparedStatement insStmt =
          connection.prepareStatement(EventdConstants.SQL_DB_INS_EVENT);
      d.watch(insStmt);

      // eventID
      insStmt.setInt(1, eventID);

      // eventUEI
      insStmt.setString(2, Constants.format(event.getUei(), EVENT_UEI_FIELD_SIZE));

      // nodeID
      final Long nodeid = event.getNodeid();
      set(insStmt, 3, event.hasNodeid() ? nodeid.intValue() : -1);

      // eventTime
      insStmt.setTimestamp(4, getEventTime(event));

      // Resolve the event host to a hostname using the ipInterface table
      String hostname = getEventHost(event);

      // eventHost
      set(insStmt, 5, Constants.format(hostname, EVENT_HOST_FIELD_SIZE));

      // ipAddr
      set(insStmt, 6, Constants.format(event.getInterface(), EVENT_INTERFACE_FIELD_SIZE));

      // eventDpName
      String dpName = "localhost";
      if (eventHeader != null && eventHeader.getDpName() != null) {
        dpName = Constants.format(eventHeader.getDpName(), EVENT_DPNAME_FIELD_SIZE);
      } else if (event.getDistPoller() != null) {
        dpName = Constants.format(event.getDistPoller(), EVENT_DPNAME_FIELD_SIZE);
      }
      insStmt.setString(7, dpName);

      // eventSnmpHost
      set(insStmt, 8, Constants.format(event.getSnmphost(), EVENT_SNMPHOST_FIELD_SIZE));

      // service identifier - convert the service name to a service id
      set(insStmt, 9, getEventServiceId(event));

      // eventSnmp
      if (event.getSnmp() != null) {
        insStmt.setString(10, SnmpInfo.format(event.getSnmp(), EVENT_SNMP_FIELD_SIZE));
      } else {
        insStmt.setNull(10, Types.VARCHAR);
      }

      // eventParms

      // Replace any null bytes with a space, otherwise postgres will complain about encoding in
      // UNICODE
      final String parametersString = Parameter.format(event);
      set(insStmt, 11, Constants.format(parametersString, 0));

      // eventCreateTime
      final Timestamp eventCreateTime = new Timestamp(System.currentTimeMillis());
      insStmt.setTimestamp(12, eventCreateTime);

      // eventDescr
      set(insStmt, 13, Constants.format(event.getDescr(), 0));

      // eventLoggroup
      set(
          insStmt,
          14,
          (event.getLoggroupCount() > 0)
              ? Constants.format(event.getLoggroup(), EVENT_LOGGRP_FIELD_SIZE)
              : null);

      // eventLogMsg
      // eventLog
      // eventDisplay
      if (event.getLogmsg() != null) {
        // set log message
        set(insStmt, 15, Constants.format(event.getLogmsg().getContent(), 0));
        String logdest = event.getLogmsg().getDest();
        if (logdest.equals("logndisplay")) {
          // if 'logndisplay' set both log and display column to yes
          set(insStmt, 16, MSG_YES);
          set(insStmt, 17, MSG_YES);
        } else if (logdest.equals("logonly")) {
          // if 'logonly' set log column to true
          set(insStmt, 16, MSG_YES);
          set(insStmt, 17, MSG_NO);
        } else if (logdest.equals("displayonly")) {
          // if 'displayonly' set display column to true
          set(insStmt, 16, MSG_NO);
          set(insStmt, 17, MSG_YES);
        } else if (logdest.equals("suppress")) {
          // if 'suppress' set both log and display to false
          set(insStmt, 16, MSG_NO);
          set(insStmt, 17, MSG_NO);
        }
      } else {
        insStmt.setNull(15, Types.VARCHAR);

        /*
         * If this is an event that had no match in the event conf
         * mark it as to be logged and displayed so that there
         * are no events that slip through the system
         * without the user knowing about them
         */
        set(insStmt, 17, MSG_YES);
      }

      // eventSeverity
      set(insStmt, 18, OnmsSeverity.get(event.getSeverity()).getId());

      // eventPathOutage
      set(
          insStmt,
          19,
          (event.getPathoutage() != null)
              ? Constants.format(event.getPathoutage(), EVENT_PATHOUTAGE_FIELD_SIZE)
              : null);

      // eventCorrelation
      set(
          insStmt,
          20,
          (event.getCorrelation() != null)
              ? org.opennms.netmgt.dao.util.Correlation.format(
                  event.getCorrelation(), EVENT_CORRELATION_FIELD_SIZE)
              : null);

      // eventSuppressedCount
      insStmt.setNull(21, Types.INTEGER);

      // eventOperInstruct
      set(insStmt, 22, Constants.format(event.getOperinstruct(), EVENT_OPERINSTRUCT_FIELD_SIZE));

      // eventAutoAction
      set(
          insStmt,
          23,
          (event.getAutoactionCount() > 0)
              ? AutoAction.format(event.getAutoaction(), EVENT_AUTOACTION_FIELD_SIZE)
              : null);

      // eventOperAction / eventOperActionMenuText
      if (event.getOperactionCount() > 0) {
        final List<Operaction> a = new ArrayList<Operaction>();
        final List<String> b = new ArrayList<String>();

        for (final Operaction eoa : event.getOperactionCollection()) {
          a.add(eoa);
          b.add(eoa.getMenutext());
        }

        set(insStmt, 24, OperatorAction.format(a, EVENT_OPERACTION_FIELD_SIZE));
        set(insStmt, 25, Constants.format(b, EVENT_OPERACTION_MENU_FIELD_SIZE));
      } else {
        insStmt.setNull(24, Types.VARCHAR);
        insStmt.setNull(25, Types.VARCHAR);
      }

      // eventNotification, this column no longer needed
      insStmt.setNull(26, Types.VARCHAR);

      // eventTroubleTicket / eventTroubleTicket state
      if (event.getTticket() != null) {
        set(
            insStmt,
            27,
            Constants.format(event.getTticket().getContent(), EVENT_TTICKET_FIELD_SIZE));
        set(insStmt, 28, event.getTticket().getState().equals("on") ? 1 : 0);
      } else {
        insStmt.setNull(27, Types.VARCHAR);
        insStmt.setNull(28, Types.INTEGER);
      }

      // eventForward
      set(
          insStmt,
          29,
          (event.getForwardCount() > 0)
              ? org.opennms.netmgt.dao.util.Forward.format(
                  event.getForward(), EVENT_FORWARD_FIELD_SIZE)
              : null);

      // event mouseOverText
      set(insStmt, 30, Constants.format(event.getMouseovertext(), EVENT_MOUSEOVERTEXT_FIELD_SIZE));

      // eventAckUser
      if (event.getAutoacknowledge() != null
          && event.getAutoacknowledge().getState().equals("on")) {
        set(
            insStmt,
            31,
            Constants.format(event.getAutoacknowledge().getContent(), EVENT_ACKUSER_FIELD_SIZE));

        // eventAckTime - if autoacknowledge is present,
        // set time to event create time
        set(insStmt, 32, eventCreateTime);
      } else {
        insStmt.setNull(31, Types.INTEGER);
        insStmt.setNull(32, Types.TIMESTAMP);
      }

      // eventSource
      set(insStmt, 33, Constants.format(event.getSource(), EVENT_SOURCE_FIELD_SIZE));

      // ifindex
      if (event.hasIfIndex()) {
        set(insStmt, 34, event.getIfIndex());
      } else {
        insStmt.setNull(34, Types.INTEGER);
      }

      // execute
      insStmt.executeUpdate();
    } finally {
      d.cleanUp();
    }

    LOG.debug("SUCCESSFULLY added {} related  data into the EVENTS table.", event.getUei());
  }
 /**
  * setSeverity
  *
  * @param severity a {@link java.lang.String} object.
  * @return a {@link org.opennms.netmgt.model.events.EventBuilder} object.
  */
 public EventBuilder setSeverity(final String severity) {
   m_event.setSeverity(OnmsSeverity.get(severity).getLabel());
   return this;
 }