Пример #1
0
  /**
   * Create an SNMP V2 trap, based on the content of the specified event, and forward the trap to
   * the specified address and port. It is assumed that the specified event represents an SNMP V1 or
   * V2 trap that was received by OpenNMS (TrapD).
   *
   * @param event The event upon which the trap content should be based
   * @param destAddr The address to which the trap should be forwarded
   * @param destPort The port to which the trap should be forwarded
   * @exception Throws SnmpTrapHelperException if the variable binding cannot be added to the trap
   *     for any reason.
   * @throws org.opennms.netmgt.scriptd.helper.SnmpTrapHelperException if any.
   */
  public void forwardV2Trap(Event event, String destAddr, int destPort)
      throws SnmpTrapHelperException {

    // the event must correspond to an SNMP trap

    Snmp snmpInfo = event.getSnmp();

    if (snmpInfo == null) {
      throw new SnmpTrapHelperException(
          "Cannot forward an event with no SNMP info: " + event.getUei());
    }

    // check the version of the original trap

    String version = snmpInfo.getVersion();

    SnmpTrapBuilder packet = SnmpUtils.getV2TrapBuilder();

    if ("v1".equals(version)) {

      // converting V1 trap to V2 (see RFC2576)

      addVarBinding(
          packet,
          SNMP_SYSUPTIME_OID,
          EventConstants.TYPE_SNMP_TIMETICKS,
          Long.toString(snmpInfo.getTimeStamp()));

      String oid;

      if (snmpInfo.getGeneric() == ENTERPRISE_SPECIFIC && snmpInfo.hasSpecific()) {
        oid = snmpInfo.getId() + ".0." + snmpInfo.getSpecific();
      } else {
        oid = SNMP_TRAPS + '.' + (snmpInfo.getGeneric() + 1);
      }

      addVarBinding(packet, SNMP_TRAP_OID, EventConstants.TYPE_SNMP_OBJECT_IDENTIFIER, oid);

      // add the V1 var bindings

      boolean addrPresent = false;
      boolean communityPresent = false;
      boolean enterprisePresent = false;

      int i = 0;
      for (Parm parm : event.getParmCollection()) {
        Value value = parm.getValue();

        try {
          addVarBinding(
              packet, parm.getParmName(), value.getType(), value.getEncoding(), value.getContent());
        } catch (SnmpTrapHelperException e) {
          throw new SnmpTrapHelperException(e.getMessage() + " in event parm[" + i + "]");
        }

        if (SNMP_TRAP_ADDRESS_OID.equals(parm.getParmName())) {
          addrPresent = true;
        } else if (SNMP_TRAP_COMMUNITY_OID.equals(parm.getParmName())) {
          communityPresent = true;
        } else if (SNMP_TRAP_ENTERPRISE_OID.equals(parm.getParmName())) {
          enterprisePresent = true;
        }
        i++;
      }

      if (!addrPresent) {
        addVarBinding(
            packet, SNMP_TRAP_ADDRESS_OID, EventConstants.TYPE_SNMP_IPADDRESS, event.getSnmphost());
      }

      if (!communityPresent) {
        addVarBinding(
            packet,
            SNMP_TRAP_COMMUNITY_OID,
            EventConstants.TYPE_SNMP_OCTET_STRING,
            snmpInfo.getCommunity());
      }

      if (!enterprisePresent) {
        addVarBinding(
            packet,
            SNMP_TRAP_ENTERPRISE_OID,
            EventConstants.TYPE_SNMP_OBJECT_IDENTIFIER,
            snmpInfo.getId());
      }
    } else if ("v2".equals(version)) {

      addVarBinding(
          packet,
          SNMP_SYSUPTIME_OID,
          EventConstants.TYPE_SNMP_TIMETICKS,
          Long.toString(snmpInfo.getTimeStamp()));

      String oid;

      if (snmpInfo.getGeneric() == ENTERPRISE_SPECIFIC) {
        oid = snmpInfo.getId() + "." + snmpInfo.getSpecific();
      } else {
        oid = SNMP_TRAPS + '.' + (snmpInfo.getGeneric() + 1);
      }

      addVarBinding(packet, SNMP_TRAP_OID, EventConstants.TYPE_SNMP_OBJECT_IDENTIFIER, oid);

      int i = 0;
      for (Parm parm : event.getParmCollection()) {
        Value value = parm.getValue();

        try {
          addVarBinding(
              packet, parm.getParmName(), value.getType(), value.getEncoding(), value.getContent());
        } catch (SnmpTrapHelperException e) {
          throw new SnmpTrapHelperException(e.getMessage() + " in event parm[" + i + "]");
        }

        i++;
      }
    } else {
      throw new SnmpTrapHelperException("Invalid SNMP version: " + version);
    }

    // send the trap

    sendTrap(destAddr, destPort, snmpInfo.getCommunity(), packet);
  }
Пример #2
0
  @Override
  public void process(final Header eventHeader, final Event event) throws EventProcessorException {
    LOG.debug("Writing event: {}", event);
    final OnmsEvent oe = new OnmsEvent();
    oe.setEventAutoAction(
        (event.getAutoactionCount() > 0)
            ? AutoAction.format(event.getAutoaction(), EVENT_AUTOACTION_FIELD_SIZE)
            : null);
    oe.setEventCorrelation(
        (event.getCorrelation() != null)
            ? org.opennms.netmgt.dao.util.Correlation.format(
                event.getCorrelation(), EVENT_CORRELATION_FIELD_SIZE)
            : null);
    oe.setEventCreateTime(event.getCreationTime());
    oe.setId(event.getDbid());
    oe.setEventDescr(event.getDescr());
    try {
      oe.setDistPoller(m_distPollerDao.get(event.getDistPoller()));
    } catch (final DataAccessException e) {
      throw new EventProcessorException(e);
    }
    oe.setEventHost(event.getHost());
    oe.setEventForward(
        (event.getForwardCount() > 0)
            ? org.opennms.netmgt.dao.util.Forward.format(
                event.getForward(), EVENT_FORWARD_FIELD_SIZE)
            : null);
    oe.setIfIndex(event.getIfIndex());
    oe.setIpAddr(event.getInterfaceAddress());

    if (event.getLogmsg() != null) {
      // set log message
      oe.setEventLogMsg(EventDatabaseConstants.format(event.getLogmsg().getContent(), 0));
      final String logdest = event.getLogmsg().getDest();
      if (logdest.equals("logndisplay")) {
        // if 'logndisplay' set both log and display column to yes
        oe.setEventLog("Y");
        oe.setEventDisplay("Y");
      } else if (logdest.equals("logonly")) {
        // if 'logonly' set log column to true
        oe.setEventLog("Y");
        oe.setEventDisplay("N");
      } else if (logdest.equals("displayonly")) {
        // if 'displayonly' set display column to true
        oe.setEventLog("N");
        oe.setEventDisplay("Y");
      } else if (logdest.equals("suppress")) {
        // if 'suppress' set both log and display to false
        oe.setEventLog("N");
        oe.setEventDisplay("N");
      }
    }

    oe.setEventMouseOverText(event.getMouseovertext());
    try {
      oe.setNode(m_nodeDao.get(event.getNodeid().intValue()));
    } catch (final DataAccessException e) {
      throw new EventProcessorException(e);
    }

    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());
      }

      oe.setEventOperAction(OperatorAction.format(a, EVENT_OPERACTION_FIELD_SIZE));
      oe.setEventOperActionMenuText(
          EventDatabaseConstants.format(b, EVENT_OPERACTION_MENU_FIELD_SIZE));
    }
    oe.setEventOperInstruct(event.getOperinstruct());
    oe.setEventParms(EventParameterUtils.format(event));
    oe.setEventPathOutage(event.getPathoutage());
    try {
      oe.setServiceType(m_serviceTypeDao.findByName(event.getService()));
    } catch (final DataAccessException e) {
      throw new EventProcessorException(e);
    }
    oe.setSeverityLabel(event.getSeverity());
    oe.setEventSnmp(SnmpInfo.format(event.getSnmp(), EVENT_SNMP_FIELD_SIZE));
    oe.setEventSnmpHost(
        EventDatabaseConstants.format(event.getSnmphost(), EVENT_SNMPHOST_FIELD_SIZE));
    oe.setEventSource(event.getSource());
    oe.setEventTime(event.getTime());

    if (event.getTticket() != null) {
      oe.setEventTTicket(
          EventDatabaseConstants.format(event.getTticket().getContent(), EVENT_TTICKET_FIELD_SIZE));
      oe.setEventTTicketState(event.getTticket().getState().equals("on") ? 1 : 0);
    }
    oe.setEventUei(event.getUei());

    m_eventDao.saveOrUpdate(oe);
  }
Пример #3
0
  /**
   * Create an SNMP V1 trap, based on the content of the specified event, and forward the trap to
   * the specified address and port. It is assumed that the specified event represents an SNMP V1 or
   * V2 trap that was received by OpenNMS (TrapD).
   *
   * @param event The event upon which the trap content should be based
   * @param destAddr The address to which the trap should be forwarded
   * @param destPort The port to which the trap should be forwarded
   * @exception Throws SnmpTrapHelperException if the variable binding cannot be added to the trap
   *     for any reason.
   * @throws org.opennms.netmgt.scriptd.helper.SnmpTrapHelperException if any.
   */
  public void forwardV1Trap(Event event, String destAddr, int destPort)
      throws SnmpTrapHelperException {
    // the event must correspond to an SNMP trap

    Snmp snmpInfo = event.getSnmp();

    if (snmpInfo == null) {
      throw new SnmpTrapHelperException(
          "Cannot forward an event with no SNMP info: " + event.getUei());
    }

    // check the version of the original trap

    String version = snmpInfo.getVersion();

    SnmpV1TrapBuilder trap = SnmpUtils.getV1TrapBuilder();

    if ("v1".equals(version)) {

      trap.setEnterprise(SnmpObjId.get(snmpInfo.getId()));

      InetAddress agentAddress;
      agentAddress = InetAddressUtils.addr(event.getSnmphost());
      if (agentAddress == null) {
        throw new SnmpTrapHelperException("Invalid ip address.");
      }

      trap.setAgentAddress(agentAddress);

      if (snmpInfo.hasGeneric()) {
        trap.setGeneric(snmpInfo.getGeneric());
      }

      if (snmpInfo.hasSpecific()) {
        trap.setSpecific(snmpInfo.getSpecific());
      }

      trap.setTimeStamp(snmpInfo.getTimeStamp());

      // varbinds

      int i = 0;
      for (Parm parm : event.getParmCollection()) {
        try {
          Value value = parm.getValue();
          addVarBinding(
              trap, parm.getParmName(), value.getType(), value.getEncoding(), value.getContent());
        } catch (SnmpTrapHelperException e) {
          throw new SnmpTrapHelperException(e.getMessage() + " in event parm[" + i + "]");
        } finally {
          i++;
        }
      }
    } else if ("v2".equals(version)) {

      // converting V2 trap to V1 (see RFC2576)

      trap.setEnterprise(SnmpObjId.get(snmpInfo.getId()));

      String addr = null;

      for (Parm parm : event.getParmCollection()) {
        if (SNMP_TRAP_ADDRESS_OID.equals(parm.getParmName())) {
          addr = parm.getValue().getContent();
          break;
        }
      }

      if (addr == null) {
        addr = "0.0.0.0";
      }

      InetAddress agentAddress;
      agentAddress = InetAddressUtils.addr(addr);
      if (agentAddress == null) {
        throw new SnmpTrapHelperException("Invalid ip address.");
      }

      trap.setAgentAddress(agentAddress);

      trap.setGeneric(snmpInfo.getGeneric());

      trap.setSpecific(snmpInfo.getSpecific());

      trap.setTimeStamp(snmpInfo.getTimeStamp());

      // varbinds

      int i = 0;
      for (Parm parm : event.getParmCollection()) {
        Value value = parm.getValue();

        // omit any parms with type=Counter64

        if (!(EventConstants.TYPE_SNMP_COUNTER64.equals(value.getType()))) {

          try {
            addVarBinding(
                trap, parm.getParmName(), value.getType(), value.getEncoding(), value.getContent());
          } catch (SnmpTrapHelperException e) {
            throw new SnmpTrapHelperException(e.getMessage() + " in event parm[" + i + "]");
          }
        }

        i++;
      }

    } else {
      throw new SnmpTrapHelperException("Invalid SNMP version: " + version);
    }

    // send the trap

    sendTrap(destAddr, destPort, snmpInfo.getCommunity(), trap);
  }
Пример #4
0
  /**
   * 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());
  }