Example #1
0
  /**
   * Format each parameter
   *
   * @param parm the parameter
   * @return the formatted event parameter string
   */
  public static String format(Parm parm) {
    Value pValue = parm.getValue();

    String type = pValue.getType();
    String encoding = pValue.getEncoding();

    String tmp = Constants.escape(parm.getParmName(), Constants.NAME_VAL_DELIM);
    String name = Constants.escape(tmp, Constants.MULTIPLE_VAL_DELIM);
    tmp = Constants.escape(pValue.getContent(), Constants.NAME_VAL_DELIM);
    String value = Constants.escape(tmp, Constants.MULTIPLE_VAL_DELIM);

    String empty = "";
    name = (name != null ? name.trim() : empty);
    value = (value != null ? value.trim() : empty);
    type = (type != null ? type.trim() : empty);
    encoding = (encoding != null ? encoding.trim() : empty);

    StringBuffer buf = new StringBuffer();
    buf.append(name);
    buf.append(Constants.NAME_VAL_DELIM);
    buf.append(value);
    buf.append('(');
    buf.append(type);
    buf.append(Constants.DB_ATTRIB_DELIM);
    buf.append(encoding);
    buf.append(')');

    return buf.toString();
    // return name + Constants.NAME_VAL_DELIM + value + "(" + type +
    // Constants.DB_ATTRIB_DELIM + encoding + ")";
  }
    /**
     * Process the received events. For each event, use the EventExpander to look up matching
     * eventconf entry and load info from that match, expand event parms, add event to database and
     * send event to appropriate listeners.
     */
    @Override
    public void run() {
      Events events = m_eventLog.getEvents();
      if (events == null || events.getEventCount() <= 0) {
        // no events to process
        return;
      }

      for (final Event event : events.getEventCollection()) {
        final ThreadCategory log = log();
        if (log.isDebugEnabled()) {
          // Log the uei, source, and other important aspects
          final String uuid = event.getUuid();
          log.debug("Event {");
          log.debug("  uuid  = " + (uuid != null && uuid.length() > 0 ? uuid : "<not-set>"));
          log.debug("  uei   = " + event.getUei());
          log.debug("  src   = " + event.getSource());
          log.debug("  iface = " + event.getInterface());
          log.debug("  time  = " + event.getTime());
          if (event.getParmCollection().size() > 0) {
            log.debug("  parms {");
            for (final Parm parm : event.getParmCollection()) {
              if ((parm.getParmName() != null) && (parm.getValue().getContent() != null)) {
                log.debug(
                    "    ("
                        + parm.getParmName().trim()
                        + ", "
                        + parm.getValue().getContent().trim()
                        + ")");
              }
            }
            log.debug("  }");
          }
          log.debug("}");
        }

        for (final EventProcessor eventProcessor : m_eventProcessors) {
          try {
            eventProcessor.process(m_eventLog.getHeader(), event);
          } catch (SQLException e) {
            log.warn(
                "Unable to process event using processor "
                    + eventProcessor
                    + "; not processing with any later processors.  Exception: "
                    + e,
                e);
            break;
          } catch (Throwable t) {
            log.warn(
                "Unknown exception processing event with processor "
                    + eventProcessor
                    + "; not processing with any later processors.  Exception: "
                    + t,
                t);
            break;
          }
        }
      }
    }
  /**
   * addParam
   *
   * @param parmName a {@link java.lang.String} object.
   * @param val a {@link java.lang.String} object.
   * @return a {@link org.opennms.netmgt.model.events.EventBuilder} object.
   */
  public EventBuilder addParam(final String parmName, final boolean val) {
    if (parmName != null) {
      final Value value = new Value();
      value.setContent(val ? "true" : "false");

      final Parm parm = new Parm();
      parm.setParmName(parmName);
      parm.setValue(value);

      this.addParam(parm);
    }

    return this;
  }
  /**
   * setParam
   *
   * @param parmName a {@link java.lang.String} object.
   * @param val a {@link java.lang.String} object.
   * @return a {@link org.opennms.netmgt.model.events.EventBuilder} object.
   */
  public EventBuilder setParam(final String parmName, final String val) {
    if (m_event.getParmCollection().size() < 1) {
      return addParam(parmName, val);
    }

    for (final Parm parm : m_event.getParmCollection()) {
      if (parm.getParmName().equals(val)) {
        final Value value = new Value();
        value.setContent(val);
        parm.setValue(value);
        return this;
      }
    }

    return addParam(parmName, val);
  }
Example #5
0
 private void sendRearmForTriggeredStates() {
   for (String instance : m_thresholdEvaluatorStates.keySet()) {
     for (ThresholdEvaluatorState state : m_thresholdEvaluatorStates.get(instance)) {
       if (state.isTriggered()) {
         Event e = state.getEventForState(Status.RE_ARMED, new Date(), Double.NaN, null);
         Parm p = new Parm();
         p.setParmName("reason");
         Value v = new Value();
         v.setContent("Configuration has been changed");
         p.setValue(v);
         e.addParm(p);
         log().info("sendRearmForTriggeredStates: sending rearm for " + e);
         ThresholdingEventProxyFactory.getFactory().getProxy().add(e);
         state.clearState();
       }
     }
   }
 }
  private boolean isReloadConfigEventTarget(final Event event) {
    boolean isTarget = false;

    for (final Parm parm : event.getParmCollection()) {
      if (EventConstants.PARM_DAEMON_NAME.equals(parm.getParmName())
          && ("Provisiond." + NAME).equalsIgnoreCase(parm.getValue().getContent())) {
        isTarget = true;
        break;
      }
    }

    log()
        .debug(
            "isReloadConfigEventTarget: Provisiond."
                + NAME
                + " was target of reload event: "
                + isTarget);
    return isTarget;
  }
  /**
   * matchNotificationParameters
   *
   * @param event a {@link org.opennms.netmgt.xml.event.Event} object.
   * @param notification a {@link org.opennms.netmgt.config.notifications.Notification} object.
   * @return a boolean.
   */
  public boolean matchNotificationParameters(Event event, Notification notification) {
    ThreadCategory log = ThreadCategory.getInstance(getClass());

    boolean parmmatch = false;
    if (notification.getVarbind() != null && notification.getVarbind().getVbname() != null) {
      String notfValue = null;
      String notfName = notification.getVarbind().getVbname();

      if (notification.getVarbind().getVbvalue() != null) {
        notfValue = notification.getVarbind().getVbvalue();
      } else {
        if (log.isDebugEnabled()) {
          log.debug(
              "BroadcastEventProcessor:matchNotificationParameters:  Null value for varbind, assuming true.");
        }
        parmmatch = true;
      }

      for (final Parm parm : event.getParmCollection()) {
        final String parmName = parm.getParmName();
        final Value parmValue = parm.getValue();
        final String parmContent;
        if (parmValue == null) {
          continue;
        } else {
          parmContent = parmValue.getContent();
        }

        if (parmName.equals(notfName) && parmContent.startsWith(notfValue)) {
          parmmatch = true;
        }
      }
    } else if (notification.getVarbind() == null || notification.getVarbind().getVbname() == null) {
      parmmatch = true;
    }

    return parmmatch;
  }
  public EventBuilder addParam(
      final String parmName, final String val, final String type, final String encoding) {
    if (parmName != null) {
      Value value = new Value();
      value.setContent(val);

      if (type != null) {
        value.setType(type);
      }

      if (encoding != null) {
        value.setEncoding(encoding);
      }

      Parm parm = new Parm();
      parm.setParmName(parmName);
      parm.setValue(value);

      addParam(parm);
    }

    return this;
  }
Example #9
0
  /**
   * decode
   *
   * @param eventparms an event parm string
   * @return a list of parameters
   */
  public static List<Parm> decode(final String eventparms) {
    if (eventparms == null) return null;
    final List<Parm> parms = new ArrayList<Parm>();

    String[] paramslistString = eventparms.split(Character.toString(Constants.MULTIPLE_VAL_DELIM));
    if (paramslistString != null) {
      for (int i = 0; i < paramslistString.length; i++) {
        String[] paramEncoded =
            paramslistString[i].split(Character.toString(Constants.NAME_VAL_DELIM));
        if (paramEncoded != null && paramEncoded.length == 2) {
          Parm parm = new Parm();
          parm.setParmName(paramEncoded[0]);
          Value value = new Value();
          int startParamType = paramEncoded[1].lastIndexOf('(');
          if (startParamType == -1) {
            value.setContent(paramEncoded[1]);
            value.setType("string");
            value.setEncoding("text");
          } else {
            value.setContent(paramEncoded[1].substring(0, startParamType));
            String paramType = paramEncoded[1].substring(startParamType + 1);
            String[] typeAndEncode = paramType.split(Character.toString(Constants.DB_ATTRIB_DELIM));
            if (typeAndEncode != null && typeAndEncode.length == 2) {
              value.setType(typeAndEncode[0]);
              value.setEncoding(typeAndEncode[1].split("\\)")[0]);
            } else {
              value.setType("string");
              value.setEncoding("text");
            }
          }
          parm.setValue(value);
          parms.add(parm);
        }
      }
    }
    return parms;
  }
Example #10
0
  /**
   * Format the list of event parameters
   *
   * @param event TODO
   * @return the formatted event parameters string
   */
  public static String format(final Event event) {
    if (event == null
        || event.getParmCollection() == null
        || event.getParmCollection().size() == 0) {
      return null;
    }

    boolean first = true;
    StringBuffer parmbuf = new StringBuffer();

    for (final Parm parm : event.getParmCollection()) {
      if (parm.getParmName() != null
          && parm.getValue() != null
          && parm.getValue().getContent() != null) {
        if (!first) {
          parmbuf.append(Constants.MULTIPLE_VAL_DELIM);
        }
        parmbuf.append(format(parm));
        first = false;
      }
    }

    return parmbuf.toString();
  }
  private static String fetchSnmpAssetString(
      final SnmpAgentConfig agentConfig, final MibObjs mibObjs, final String formatString)
      throws MissingFormatArgumentException {

    final List<String> aliases = new ArrayList<String>();
    final List<SnmpObjId> objs = new ArrayList<SnmpObjId>();
    for (final MibObj mibobj : mibObjs.getMibObj()) {
      aliases.add(mibobj.getAlias());
      objs.add(SnmpObjId.get(mibobj.getOid()));
    }
    // Fetch the values from the SNMP agent
    final SnmpValue[] values = SnmpUtils.get(agentConfig, objs.toArray(new SnmpObjId[0]));
    if (values.length == aliases.size()) {
      final Properties substitutions = new Properties();
      boolean foundAValue = false;
      for (int i = 0; i < values.length; i++) {
        // If the value is a NO_SUCH_OBJECT or NO_SUCH_INSTANCE error, then skip it
        if (values[i] == null || values[i].isError()) {
          // No value for this OID
          continue;
        }
        foundAValue = true;
        // Use trapd's SyntaxToEvent parser so that we format base64
        // and MAC address values appropriately
        Parm parm = SyntaxToEvent.processSyntax(aliases.get(i), values[i]);
        substitutions.setProperty(aliases.get(i), parm.getValue().getContent());
      }

      if (!foundAValue) {
        if (log().isDebugEnabled()) {
          log()
              .debug(
                  "fetchSnmpAssetString: Failed to fetch any SNMP values for system "
                      + agentConfig.toString());
        }
        throw new MissingFormatArgumentException(
            "fetchSnmpAssetString: Failed to fetch any SNMP values for system "
                + agentConfig.toString());
      } else {
        log()
            .debug(
                "fetchSnmpAssetString: Fetched asset properties from SNMP agent:\n"
                    + formatPropertiesAsString(substitutions));
      }

      if (objs.size() != substitutions.size()) {
        log()
            .warn(
                "fetchSnmpAssetString: Unexpected number of properties returned from SNMP GET:\n"
                    + formatPropertiesAsString(substitutions));
      }

      return PropertiesUtils.substitute(formatString, substitutions);
    } else {
      log()
          .warn(
              "fetchSnmpAssetString: Invalid number of SNMP parameters returned: "
                  + values.length
                  + " != "
                  + aliases.size());
      throw new MissingFormatArgumentException(
          "fetchSnmpAssetString: Invalid number of SNMP parameters returned: "
              + values.length
              + " != "
              + aliases.size());
    }
  }
Example #12
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);
  }
Example #13
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);
  }