Search in sources :

Example 1 with WmiCollection

use of org.opennms.netmgt.config.wmi.WmiCollection in project opennms by OpenNMS.

the class WmiCollector method getRuntimeAttributes.

@Override
public Map<String, Object> getRuntimeAttributes(CollectionAgent agent, Map<String, Object> parameters) {
    final Map<String, Object> runtimeAttributes = new HashMap<>();
    final String collectionName = ParameterMap.getKeyedString(parameters, "collection", ParameterMap.getKeyedString(parameters, "wmi-collection", null));
    final WmiCollection collection = WmiDataCollectionConfigFactory.getInstance().getWmiCollection(collectionName);
    runtimeAttributes.put(WMI_COLLECTION_KEY, collection);
    final WmiAgentConfig agentConfig = WmiPeerFactory.getInstance().getAgentConfig(agent.getAddress());
    runtimeAttributes.put(WMI_AGENT_CONFIG_KEY, agentConfig);
    return runtimeAttributes;
}
Also used : HashMap(java.util.HashMap) WmiAgentConfig(org.opennms.netmgt.config.wmi.WmiAgentConfig) OnmsWbemObject(org.opennms.protocols.wmi.wbem.OnmsWbemObject) WmiCollection(org.opennms.netmgt.config.wmi.WmiCollection)

Example 2 with WmiCollection

use of org.opennms.netmgt.config.wmi.WmiCollection 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)

Aggregations

WmiAgentConfig (org.opennms.netmgt.config.wmi.WmiAgentConfig)2 WmiCollection (org.opennms.netmgt.config.wmi.WmiCollection)2 OnmsWbemObject (org.opennms.protocols.wmi.wbem.OnmsWbemObject)2 HashMap (java.util.HashMap)1 WmiAgentState (org.opennms.netmgt.collectd.wmi.WmiAgentState)1 AttributeType (org.opennms.netmgt.collection.api.AttributeType)1 CollectionSetBuilder (org.opennms.netmgt.collection.support.builder.CollectionSetBuilder)1 GenericTypeResource (org.opennms.netmgt.collection.support.builder.GenericTypeResource)1 NodeLevelResource (org.opennms.netmgt.collection.support.builder.NodeLevelResource)1 Resource (org.opennms.netmgt.collection.support.builder.Resource)1 Attrib (org.opennms.netmgt.config.wmi.Attrib)1 Wpm (org.opennms.netmgt.config.wmi.Wpm)1 WmiClient (org.opennms.protocols.wmi.WmiClient)1 WmiException (org.opennms.protocols.wmi.WmiException)1 OnmsWbemObjectSet (org.opennms.protocols.wmi.wbem.OnmsWbemObjectSet)1 OnmsWbemProperty (org.opennms.protocols.wmi.wbem.OnmsWbemProperty)1