Search in sources :

Example 1 with WSManException

use of org.opennms.core.wsman.exceptions.WSManException in project opennms by OpenNMS.

the class WsManCollector method collectGroupUsing.

private void collectGroupUsing(Group group, CollectionAgent agent, WSManClient client, int retries, CollectionSetBuilder builder) throws CollectionException {
    // Determine the appropriate resource type
    final NodeLevelResource nodeResource = new NodeLevelResource(agent.getNodeId());
    final AtomicInteger instanceId = new AtomicInteger();
    Supplier<Resource> resourceSupplier = () -> nodeResource;
    if (!"node".equalsIgnoreCase(group.getResourceType())) {
        resourceSupplier = () -> {
            // Generate a unique instance for each node in each group to ensure
            // that the attributes are grouped together properly.
            // Since these instances have no real meaning, a storage strategy
            // similar to the SiblingColumnStorageStrategy should be used instead
            // of the IndexStorageStrategy.
            final String instance = String.format("%s%d", group.getName(), instanceId.getAndIncrement());
            return new DeferredGenericTypeResource(nodeResource, group.getResourceType(), instance);
        };
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Using resource {} for group named {}", resourceSupplier.get(), group.getName());
    }
    // Enumerate
    List<Node> nodes = Lists.newLinkedList();
    RetryNTimesLoop retryLoop = new RetryNTimesLoop(retries);
    while (retryLoop.shouldContinue()) {
        try {
            if (group.getFilter() == null) {
                LOG.debug("Enumerating and pulling {} on {}.", group.getResourceUri(), client);
                client.enumerateAndPull(group.getResourceUri(), nodes, true);
            } else {
                LOG.debug("Enumerating and pulling {} with dialect {} and filter {} on {}.", group.getResourceUri(), group.getDialect(), group.getFilter(), client);
                client.enumerateAndPullUsingFilter(group.getResourceUri(), group.getDialect(), group.getFilter(), nodes, true);
            }
            break;
        } catch (WSManException e) {
            retryLoop.takeException(e);
        }
    }
    LOG.debug("Found {} nodes.", nodes.size());
    // Process the results
    processEnumerationResults(group, builder, resourceSupplier, nodes);
}
Also used : WSManException(org.opennms.core.wsman.exceptions.WSManException) RetryNTimesLoop(org.opennms.core.wsman.utils.RetryNTimesLoop) DeferredGenericTypeResource(org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Node(org.w3c.dom.Node) OnmsNode(org.opennms.netmgt.model.OnmsNode) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) DeferredGenericTypeResource(org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource) Resource(org.opennms.netmgt.collection.support.builder.Resource) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource)

Example 2 with WSManException

use of org.opennms.core.wsman.exceptions.WSManException in project opennms by OpenNMS.

the class WsManDetector method isServiceDetected.

public DetectResults isServiceDetected(InetAddress address, WSManEndpoint endpoint) {
    // Issue the "Identify" request
    final WSManClient client = m_factory.getClient(endpoint);
    Identity identity = null;
    final Map<String, String> attributes = new HashMap<>();
    try {
        identity = client.identify();
        LOG.info("Identify succeeded for address {} with product vendor '{}' and product version '{}'.", address, identity.getProductVendor(), identity.getProductVersion());
        attributes.put(UPDATE_ASSETS, Boolean.toString(m_updateAssets));
        attributes.put(PRODUCT_VENDOR, identity.getProductVendor());
        attributes.put(PRODUCT_VERSION, identity.getProductVersion());
    } catch (WSManException e) {
        LOG.info("Identify failed for address {} with endpoint {}.", address, endpoint, e);
    }
    return new DetectResultsImpl(identity != null, attributes);
}
Also used : WSManException(org.opennms.core.wsman.exceptions.WSManException) HashMap(java.util.HashMap) WSManClient(org.opennms.core.wsman.WSManClient) Identity(org.opennms.core.wsman.Identity) DetectResultsImpl(org.opennms.netmgt.provision.support.DetectResultsImpl)

Example 3 with WSManException

use of org.opennms.core.wsman.exceptions.WSManException in project opennms by OpenNMS.

the class WsManMonitor method poll.

@Override
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
    // Fetch the monitor specific parameters
    final String resourceUri = getKeyedString(parameters, RESOURCE_URI_PARAM, null);
    if (resourceUri == null) {
        throw new IllegalArgumentException("'" + RESOURCE_URI_PARAM + "' parameter is required.");
    }
    final String rule = getKeyedString(parameters, RULE_PARAM, null);
    if (rule == null) {
        throw new IllegalArgumentException("'" + RULE_PARAM + "' parameter is required.");
    }
    final Map<String, String> selectors = Maps.newHashMap();
    for (Entry<String, Object> parameter : parameters.entrySet()) {
        if (parameter.getKey().startsWith(SELECTOR_PARAM_PREFIX)) {
            final String selectorKey = parameter.getKey().substring(SELECTOR_PARAM_PREFIX.length());
            final Object selectorValue = parameter.getValue();
            if (selectorValue == null) {
                continue;
            }
            selectors.put(selectorKey, selectorValue instanceof String ? (String) selectorValue : selectorValue.toString());
        }
    }
    // Setup the WS-Man Client
    if (m_wsManConfigDao == null) {
        m_wsManConfigDao = BeanUtils.getBean("daoContext", "wsManConfigDao", WSManConfigDao.class);
    }
    final WsmanAgentConfig config = m_wsManConfigDao.getAgentConfig(svc.getAddress());
    final WSManEndpoint endpoint = WSManConfigDao.getEndpoint(config, svc.getAddress());
    final WSManClient client = m_factory.getClient(endpoint);
    final RetryNTimesLoop retryLoop = new RetryNTimesLoop(config.getRetry() != null ? config.getRetry() : 0);
    // Issue a GET
    Node node = null;
    try {
        while (retryLoop.shouldContinue()) {
            try {
                node = client.get(resourceUri, selectors);
                break;
            } catch (WSManException e) {
                retryLoop.takeException(e);
            }
        }
    } catch (WSManException e) {
        return PollStatus.down(e.getMessage());
    }
    if (node == null) {
        return PollStatus.down(String.format("No resource was found at URI: '%s' with selectors: '%s'.", resourceUri, selectors));
    }
    // Verify the results
    final ListMultimap<String, String> elementValues = ResponseHandlingUtils.toMultiMap(node);
    try {
        ResponseHandlingUtils.getMatchingIndex(rule, elementValues);
        // We've successfully matched an index
        return PollStatus.up();
    } catch (NoSuchElementException e) {
        return PollStatus.down(String.format("No index was matched by rule: '%s' with values '%s'.", rule, elementValues));
    }
}
Also used : RetryNTimesLoop(org.opennms.core.wsman.utils.RetryNTimesLoop) Node(org.w3c.dom.Node) WsmanAgentConfig(org.opennms.netmgt.config.wsman.WsmanAgentConfig) WSManConfigDao(org.opennms.netmgt.dao.WSManConfigDao) WSManException(org.opennms.core.wsman.exceptions.WSManException) WSManEndpoint(org.opennms.core.wsman.WSManEndpoint) WSManClient(org.opennms.core.wsman.WSManClient) NoSuchElementException(java.util.NoSuchElementException)

Example 4 with WSManException

use of org.opennms.core.wsman.exceptions.WSManException in project opennms by OpenNMS.

the class WsManCollector method collect.

@Override
public CollectionSet collect(CollectionAgent agent, Map<String, Object> parameters) throws CollectionException {
    LOG.debug("collect({}, {}, {})", agent, parameters);
    final WsmanAgentConfig config = (WsmanAgentConfig) parameters.get(WSMAN_AGENT_CONFIG_KEY);
    final Groups groups = (Groups) parameters.get(WSMAN_GROUPS_KEY);
    final WSManEndpoint endpoint = WSManConfigDao.getEndpoint(config, agent.getAddress());
    final WSManClient client = m_factory.getClient(endpoint);
    final CollectionSetBuilder collectionSetBuilder = new CollectionSetBuilder(agent);
    if (LOG.isDebugEnabled()) {
        String groupNames = groups.getGroups().stream().map(Group::getName).collect(Collectors.joining(", "));
        LOG.debug("Collecting attributes on {} from groups: {}", agent, groupNames);
    }
    for (Group group : groups.getGroups()) {
        try {
            collectGroupUsing(group, agent, client, config.getRetry() != null ? config.getRetry() : 0, collectionSetBuilder);
        } catch (InvalidResourceURI e) {
            LOG.info("Resource URI {} in group named {} is not available on {}.", group.getResourceUri(), group.getName(), agent);
        } catch (WSManException e) {
            // failed, and abort trying to collect any other groups
            throw new CollectionException(String.format("Collecting group '%s' on %s failed with '%s'. See logs for details.", group.getName(), agent, e.getMessage()), e);
        }
    }
    return collectionSetBuilder.build();
}
Also used : WSManException(org.opennms.core.wsman.exceptions.WSManException) Group(org.opennms.netmgt.config.wsman.Group) CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) WSManEndpoint(org.opennms.core.wsman.WSManEndpoint) Groups(org.opennms.netmgt.config.wsman.Groups) CollectionException(org.opennms.netmgt.collection.api.CollectionException) WsmanAgentConfig(org.opennms.netmgt.config.wsman.WsmanAgentConfig) WSManClient(org.opennms.core.wsman.WSManClient) InvalidResourceURI(org.opennms.core.wsman.exceptions.InvalidResourceURI)

Aggregations

WSManException (org.opennms.core.wsman.exceptions.WSManException)4 WSManClient (org.opennms.core.wsman.WSManClient)3 WSManEndpoint (org.opennms.core.wsman.WSManEndpoint)2 RetryNTimesLoop (org.opennms.core.wsman.utils.RetryNTimesLoop)2 WsmanAgentConfig (org.opennms.netmgt.config.wsman.WsmanAgentConfig)2 Node (org.w3c.dom.Node)2 HashMap (java.util.HashMap)1 NoSuchElementException (java.util.NoSuchElementException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Identity (org.opennms.core.wsman.Identity)1 InvalidResourceURI (org.opennms.core.wsman.exceptions.InvalidResourceURI)1 CollectionException (org.opennms.netmgt.collection.api.CollectionException)1 CollectionSetBuilder (org.opennms.netmgt.collection.support.builder.CollectionSetBuilder)1 DeferredGenericTypeResource (org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource)1 NodeLevelResource (org.opennms.netmgt.collection.support.builder.NodeLevelResource)1 Resource (org.opennms.netmgt.collection.support.builder.Resource)1 Group (org.opennms.netmgt.config.wsman.Group)1 Groups (org.opennms.netmgt.config.wsman.Groups)1 WSManConfigDao (org.opennms.netmgt.dao.WSManConfigDao)1 OnmsNode (org.opennms.netmgt.model.OnmsNode)1