Search in sources :

Example 1 with WmiException

use of org.opennms.protocols.wmi.WmiException in project opennms by OpenNMS.

the class WmiCollector method isGroupAvailable.

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;
}
Also used : WmiResult(org.opennms.protocols.wmi.WmiResult) WmiParams(org.opennms.protocols.wmi.WmiParams) WmiException(org.opennms.protocols.wmi.WmiException) WmiManager(org.opennms.protocols.wmi.WmiManager)

Example 2 with WmiException

use of org.opennms.protocols.wmi.WmiException in project opennms by OpenNMS.

the class WmiCollector method collect.

/**
 * {@inheritDoc}
 */
@Override
public CollectionSet collect(final CollectionAgent agent, final Map<String, Object> parameters) {
    // Find attributes to collect - check groups in configuration. For each,
    // check scheduled nodes to see if that group should be collected
    final WmiCollection collection = (WmiCollection) parameters.get(WMI_COLLECTION_KEY);
    final WmiAgentConfig agentConfig = (WmiAgentConfig) parameters.get(WMI_AGENT_CONFIG_KEY);
    final WmiAgentState agentState = new WmiAgentState(agent.getAddress(), agentConfig, parameters);
    // Create a new collection set.
    CollectionSetBuilder builder = new CollectionSetBuilder(agent).withStatus(CollectionStatus.FAILED);
    if (collection.getWpms().size() < 1) {
        LOG.info("No groups to collect.");
        return builder.withStatus(CollectionStatus.SUCCEEDED).build();
    }
    final NodeLevelResource nodeResource = new NodeLevelResource(agent.getNodeId());
    // Iterate through the WMI collection groups.
    for (final Wpm wpm : collection.getWpms()) {
        // 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.
                        Resource 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 = getWmiResource(agent, wpm.getResourceType(), nodeResource, instance);
                        } else {
                            resource = nodeResource;
                        }
                        for (final Attrib attrib : wpm.getAttribs()) {
                            final OnmsWbemProperty prop = obj.getWmiProperties().getByName(attrib.getWmiObject());
                            final AttributeType type = attrib.getType();
                            final String stringValue = prop.getWmiValue().toString();
                            if (type.isNumeric()) {
                                Double numericValue = Double.NaN;
                                try {
                                    numericValue = Double.parseDouble(stringValue);
                                } catch (NumberFormatException e) {
                                    LOG.warn("Value '{}' for attribute named '{}' cannot be converted to a number. Skipping.", prop.getWmiValue(), attrib.getName());
                                    continue;
                                }
                                builder.withNumericAttribute(resource, wpm.getName(), attrib.getAlias(), numericValue, type);
                            } else {
                                builder.withStringAttribute(resource, wpm.getName(), attrib.getAlias(), stringValue);
                            }
                        }
                    }
                }
                builder.withStatus(CollectionStatus.SUCCEEDED);
            } 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);
                    }
                }
            }
        }
    }
    return builder.build();
}
Also used : CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) WmiAgentConfig(org.opennms.netmgt.config.wmi.WmiAgentConfig) GenericTypeResource(org.opennms.netmgt.collection.support.builder.GenericTypeResource) Resource(org.opennms.netmgt.collection.support.builder.Resource) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) OnmsWbemProperty(org.opennms.protocols.wmi.wbem.OnmsWbemProperty) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) WmiCollection(org.opennms.netmgt.config.wmi.WmiCollection) Attrib(org.opennms.netmgt.config.wmi.Attrib) Wpm(org.opennms.netmgt.config.wmi.Wpm) WmiClient(org.opennms.protocols.wmi.WmiClient) AttributeType(org.opennms.netmgt.collection.api.AttributeType) OnmsWbemObject(org.opennms.protocols.wmi.wbem.OnmsWbemObject) WmiAgentState(org.opennms.netmgt.collectd.wmi.WmiAgentState) OnmsWbemObjectSet(org.opennms.protocols.wmi.wbem.OnmsWbemObjectSet) WmiException(org.opennms.protocols.wmi.WmiException) OnmsWbemObject(org.opennms.protocols.wmi.wbem.OnmsWbemObject)

Example 3 with WmiException

use of org.opennms.protocols.wmi.WmiException in project opennms by OpenNMS.

the class OnmsWbemMethodSetImpl method get.

/**
 * {@inheritDoc}
 */
@Override
public OnmsWbemMethod get(final Integer idx) throws WmiException {
    try {
        final IJIComObject enumComObject = wbemMethodSetDispatch.get("_NewEnum").getObjectAsComObject();
        final IJIEnumVariant enumVariant = (IJIEnumVariant) JIObjectFactory.narrowObject(enumComObject.queryInterface(IJIEnumVariant.IID));
        OnmsWbemMethod wbemMethod;
        IJIDispatch wbemMethod_dispatch = null;
        for (int i = 0; i < (idx + 1); i++) {
            final Object[] values = enumVariant.next(1);
            final JIArray array = (JIArray) values[0];
            final Object[] arrayObj = (Object[]) array.getArrayInstance();
            for (int j = 0; j < arrayObj.length; j++) {
                wbemMethod_dispatch = (IJIDispatch) JIObjectFactory.narrowObject(((JIVariant) arrayObj[j]).getObjectAsComObject());
            }
        }
        wbemMethod = new OnmsWbemMethodImpl(wbemMethod_dispatch);
        return wbemMethod;
    } catch (final JIException e) {
        throw new WmiException("Failed to enumerate WbemObject variant: " + e.getMessage(), e);
    }
}
Also used : JIArray(org.jinterop.dcom.core.JIArray) IJIComObject(org.jinterop.dcom.core.IJIComObject) OnmsWbemMethod(org.opennms.protocols.wmi.wbem.OnmsWbemMethod) IJIEnumVariant(org.jinterop.dcom.impls.automation.IJIEnumVariant) IJIDispatch(org.jinterop.dcom.impls.automation.IJIDispatch) IJIComObject(org.jinterop.dcom.core.IJIComObject) WmiException(org.opennms.protocols.wmi.WmiException) JIException(org.jinterop.dcom.common.JIException)

Example 4 with WmiException

use of org.opennms.protocols.wmi.WmiException in project opennms by OpenNMS.

the class OnmsWbemObjectImpl method getWmiPath.

/**
 * <p>getWmiPath</p>
 *
 * @return a {@link org.opennms.protocols.wmi.wbem.OnmsWbemObjectPath} object.
 * @throws org.opennms.protocols.wmi.WmiException if any.
 */
@Override
public OnmsWbemObjectPath getWmiPath() throws WmiException {
    try {
        // Get the WbemMethodSet dispatcher.
        final IJIComObject pathComObject = wbemObjectDispatch.get("Path_").getObjectAsComObject();
        final IJIDispatch path_dispatch = (IJIDispatch) JIObjectFactory.narrowObject(pathComObject);
        return new OnmsWbemObjectPathImpl(path_dispatch);
    } catch (final JIException e) {
        throw new WmiException("Failed to retrieve object path: " + e.getMessage(), e);
    }
}
Also used : IJIComObject(org.jinterop.dcom.core.IJIComObject) IJIDispatch(org.jinterop.dcom.impls.automation.IJIDispatch) WmiException(org.opennms.protocols.wmi.WmiException) JIException(org.jinterop.dcom.common.JIException)

Example 5 with WmiException

use of org.opennms.protocols.wmi.WmiException in project opennms by OpenNMS.

the class OnmsWbemObjectImpl method getWmiProperties.

/**
 * <p>getWmiProperties</p>
 *
 * @return a {@link org.opennms.protocols.wmi.wbem.OnmsWbemPropertySet} object.
 * @throws org.opennms.protocols.wmi.WmiException if any.
 */
@Override
public OnmsWbemPropertySet getWmiProperties() throws WmiException {
    try {
        // Get the WbemMethodSet dispatcher.
        final IJIComObject propsSetComObject = wbemObjectDispatch.get("Properties_").getObjectAsComObject();
        final IJIDispatch propSet_dispatch = (IJIDispatch) JIObjectFactory.narrowObject(propsSetComObject);
        return new OnmsWbemPropertySetImpl(propSet_dispatch);
    } catch (final JIException e) {
        throw new WmiException("Failed to retrieve object property set: " + e.getMessage(), e);
    }
}
Also used : IJIComObject(org.jinterop.dcom.core.IJIComObject) IJIDispatch(org.jinterop.dcom.impls.automation.IJIDispatch) WmiException(org.opennms.protocols.wmi.WmiException) JIException(org.jinterop.dcom.common.JIException)

Aggregations

WmiException (org.opennms.protocols.wmi.WmiException)17 WmiManager (org.opennms.protocols.wmi.WmiManager)9 JIException (org.jinterop.dcom.common.JIException)7 IJIComObject (org.jinterop.dcom.core.IJIComObject)7 IJIDispatch (org.jinterop.dcom.impls.automation.IJIDispatch)7 WmiParams (org.opennms.protocols.wmi.WmiParams)5 JIArray (org.jinterop.dcom.core.JIArray)4 IJIEnumVariant (org.jinterop.dcom.impls.automation.IJIEnumVariant)4 WmiResult (org.opennms.protocols.wmi.WmiResult)3 OnmsWbemObjectSet (org.opennms.protocols.wmi.wbem.OnmsWbemObjectSet)3 WmiAgentConfig (org.opennms.netmgt.config.wmi.WmiAgentConfig)2 OnmsWbemObjectBiosStub (org.opennms.protocols.wmi.test.stubs.OnmsWbemObjectBiosStub)2 OnmsWbemObjectSetBiosStub (org.opennms.protocols.wmi.test.stubs.OnmsWbemObjectSetBiosStub)2 OnmsWbemPropBiosStub (org.opennms.protocols.wmi.test.stubs.OnmsWbemPropBiosStub)2 OnmsWbemPropSetBiosStub (org.opennms.protocols.wmi.test.stubs.OnmsWbemPropSetBiosStub)2 OnmsWbemObject (org.opennms.protocols.wmi.wbem.OnmsWbemObject)2 OnmsWbemProperty (org.opennms.protocols.wmi.wbem.OnmsWbemProperty)2 InetAddress (java.net.InetAddress)1 JIVariant (org.jinterop.dcom.core.JIVariant)1 TimeoutTracker (org.opennms.core.utils.TimeoutTracker)1