/**
   * 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;
  }
Esempio n. 2
0
  /** {@inheritDoc} */
  @Override
  public CollectionSet collect(
      final CollectionAgent agent, final EventProxy eproxy, final Map<String, Object> parameters) {

    String collectionName =
        ParameterMap.getKeyedString(
            parameters,
            "collection",
            ParameterMap.getKeyedString(parameters, "wmi-collection", null));
    // Find attributes to collect - check groups in configuration. For each,
    // check scheduled nodes to see if that group should be collected
    final WmiCollection collection =
        WmiDataCollectionConfigFactory.getInstance().getWmiCollection(collectionName);
    final WmiAgentState agentState = m_scheduledNodes.get(agent.getNodeId());

    // Load the attribute group types.
    loadAttributeGroupList(collection);

    // Load the attribute types.
    loadAttributeTypeList(collection);

    // Create a new collection set.
    final WmiCollectionSet collectionSet = new WmiCollectionSet();
    collectionSet.setCollectionTimestamp(new Date());
    final WmiSingleInstanceCollectionResource nodeResource =
        new WmiSingleInstanceCollectionResource(agent);

    // Iterate through the WMI collection groups.
    for (final Wpm wpm : collection.getWpms().getWpm()) {
      // A wpm consists of a list of attributes, identified by name
      if (agentState.shouldCheckAvailability(wpm.getName(), wpm.getRecheckInterval())) {
        if (!isGroupAvailable(agentState, wpm)) {
          continue;
        }
      }

      if (agentState.groupIsAvailable(wpm.getName())) {
        WmiClient client = null;
        // Collect the data
        try {
          // Tell the agent to connect
          agentState.connect(wpm.getWmiNamespace());

          // And retrieve the client object for working.
          client = (WmiClient) agentState.getWmiClient();

          // Retrieve the WbemObjectSet from the class defined on the group.
          final OnmsWbemObjectSet wOS = client.performInstanceOf(wpm.getWmiClass());

          // If we received a WbemObjectSet result, lets go through it and collect it.
          if (wOS != null) {
            //  Go through each object (class instance) in the object set.
            for (int i = 0; i < wOS.count(); i++) {
              // Create a new collection resource.
              WmiCollectionResource resource = null;

              // Fetch our WBEM Object
              final OnmsWbemObject obj = wOS.get(i);

              // If this is multi-instance, fetch the instance name and store it.
              if (wOS.count() > 1) {
                // Fetch the value of the key value. e.g. Name.
                final OnmsWbemProperty prop = obj.getWmiProperties().getByName(wpm.getKeyvalue());
                final Object propVal = prop.getWmiValue();
                String instance = null;
                if (propVal instanceof String) {
                  instance = (String) propVal;
                } else {
                  instance = propVal.toString();
                }
                resource =
                    new WmiMultiInstanceCollectionResource(agent, instance, wpm.getResourceType());
              } else {
                resource = nodeResource;
              }

              for (final Attrib attrib : wpm.getAttrib()) {
                final OnmsWbemProperty prop =
                    obj.getWmiProperties().getByName(attrib.getWmiObject());
                final WmiCollectionAttributeType attribType =
                    m_attribTypeList.get(attrib.getName());
                resource.setAttributeValue(attribType, prop.getWmiValue().toString());
              }
              collectionSet.getCollectionResources().add(resource);
            }
          }
        } catch (final WmiException e) {
          LOG.info("unable to collect params for wpm '{}'", wpm.getName(), e);
        } finally {
          if (client != null) {
            try {
              client.disconnect();
            } catch (final WmiException e) {
              LOG.warn("An error occurred disconnecting while collecting from WMI.", e);
            }
          }
        }
      }
    }
    collectionSet.setStatus(ServiceCollector.COLLECTION_SUCCEEDED);
    return collectionSet;
  }