Exemple #1
0
  private boolean isGroupAvailable(final WmiAgentState agentState, final Wpm wpm) {
    LOG.debug(
        "Checking availability of group {} via object {} of class {} in namespace {}",
        wpm.getName(),
        wpm.getKeyvalue(),
        wpm.getWmiClass(),
        wpm.getWmiNamespace());
    WmiManager manager = null;

    /*
     * We provide a bogus comparison value and use an operator of "NOOP"
     * to ensure that, regardless of results, we receive a result and perform
     * no logic. We're only validating that the agent is reachable and gathering
     * the result objects.
     */
    try {
      // Get and initialize the WmiManager
      manager = agentState.getManager();
      manager.setNamespace(wpm.getWmiNamespace());
      manager.init();

      final WmiParams params =
          new WmiParams(
              WmiParams.WMI_OPERATION_INSTANCEOF,
              "not-applicable",
              "NOOP",
              wpm.getWmiClass(),
              wpm.getKeyvalue());
      final WmiResult result = manager.performOp(params);

      final boolean isAvailable = (result.getResultCode() == WmiResult.RES_STATE_OK);

      agentState.setGroupIsAvailable(wpm.getName(), isAvailable);
      LOG.debug("Group {} is {}{}.", wpm.getName(), (isAvailable ? "" : "not "), "available");
    } catch (final WmiException e) {
      // Log a warning signifying that this group is unavailable.
      LOG.warn("Error checking group ({}) availability.", wpm.getName(), e);
      // Set the group as unavailable.
      agentState.setGroupIsAvailable(wpm.getName(), false);

      // And then continue on to check the next wpm entry.
      return false;
    } finally {
      if (manager != null) {
        try {
          manager.close();
        } catch (WmiException e) {
          LOG.warn("An error occurred closing the WMI Manager", e);
        }
      }
    }
    return true;
  }
  /**
   * performInstanceOf
   *
   * @param params a {@link org.opennms.protocols.wmi.WmiParams} object.
   * @return a {@link org.opennms.protocols.wmi.WmiResult} object.
   * @throws org.opennms.protocols.wmi.WmiException if any.
   */
  public WmiResult performInstanceOf(final WmiParams params) throws WmiException {
    final ArrayList<Object> wmiObjects = new ArrayList<Object>();
    final OnmsWbemObjectSet wos = m_WmiClient.performInstanceOf(params.getWmiClass());

    for (int i = 0; i < wos.count(); i++) {
      wmiObjects.add(wos.get(i).getWmiProperties().getByName(params.getWmiObject()).getWmiValue());
    }

    final WmiResult result = new WmiResult(wmiObjects);

    if (params.getCompareOperation().equals("NOOP")) {
      result.setResultCode(WmiResult.RES_STATE_OK);
    } else if (params.getCompareOperation().equals("EQ")
        || params.getCompareOperation().equals("NEQ")
        || params.getCompareOperation().equals("GT")
        || params.getCompareOperation().equals("LT")) {
      performResultCheck(result, params);
    } else {
      result.setResultCode(WmiResult.RES_STATE_UNKNOWN);
    }

    return result;
  }
  private void performResultCheck(final WmiResult wmiResult, final WmiParams params)
      throws WmiException {
    final ArrayList<Object> wmiObjects = wmiResult.getResponse();

    int matches = 0;
    final int total = wmiObjects.size();
    for (int i = 0; i < total; i++) {
      final Object wmiObj = wmiObjects.get(i);
      final WmiMgrOperation op = WmiMgrOperation.valueOf(params.getCompareOperation());

      if (op.compareString(wmiObj, (String) params.getCompareValue())) {
        matches++;
      }
    }

    /*
     * Check that we meet the match requirements:
     * - all: all objects must match, one or more.
     * - none: no objects must match
     * - one: only one object must match
     * - some: one or more objects but not all objects must match.
     */
    if (m_MatchType.equals("all") && matches == total && matches > 0) {
      wmiResult.setResultCode(WmiResult.RES_STATE_OK);
    } else if (m_MatchType.equals("none") && matches == 0) {
      wmiResult.setResultCode(WmiResult.RES_STATE_OK);
    } else if (m_MatchType.equals("one") && matches == 1) {
      wmiResult.setResultCode(WmiResult.RES_STATE_OK);
    } else if (m_MatchType.equals("some") && matches >= 1) {
      // we want to match more than one but not all.
      if (matches != total) {
        wmiResult.setResultCode(WmiResult.RES_STATE_OK);
      } else {
        wmiResult.setResultCode(WmiResult.RES_STATE_CRIT);
      }
    } else {
      wmiResult.setResultCode(WmiResult.RES_STATE_CRIT);
    }
  }