Exemple #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 + ")";
  }
  /**
   * 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);
  }
 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();
       }
     }
   }
 }
  /**
   * 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;
  }
Exemple #7
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;
  }
  /**
   * 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);
  }
  /**
   * 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);
  }