Search in sources :

Example 1 with WSManEndpoint

use of org.opennms.core.wsman.WSManEndpoint in project opennms by OpenNMS.

the class WsmanEndpointUtilsTest method canConvertToAndFromMap.

@Test
public void canConvertToAndFromMap() throws MalformedURLException {
    WSManEndpoint expectedEndpoint = new WSManEndpoint.Builder("https://www.opennms.org/wsman").withConnectionTimeout(60).withBasicAuth("x", "y").build();
    WSManEndpoint actualEndpoint = WsmanEndpointUtils.fromMap(WsmanEndpointUtils.toMap(expectedEndpoint));
    assertEquals(expectedEndpoint.getUrl(), actualEndpoint.getUrl());
    assertEquals(expectedEndpoint.isBasicAuth(), actualEndpoint.isBasicAuth());
    assertEquals(expectedEndpoint.getUsername(), actualEndpoint.getUsername());
    assertEquals(expectedEndpoint.getPassword(), actualEndpoint.getPassword());
    assertEquals(expectedEndpoint.getConnectionTimeout(), actualEndpoint.getConnectionTimeout());
}
Also used : WSManEndpoint(org.opennms.core.wsman.WSManEndpoint) Test(org.junit.Test)

Example 2 with WSManEndpoint

use of org.opennms.core.wsman.WSManEndpoint in project opennms by OpenNMS.

the class WSManConfigDao method getEndpoint.

static WSManEndpoint getEndpoint(WsmanAgentConfig agentConfig, InetAddress agentInetAddress) {
    Objects.requireNonNull(agentConfig, "agentConfig argument");
    Objects.requireNonNull(agentInetAddress, "agentInetAddress argument");
    URL url;
    try {
        String protocol = DEFAULT_PROTOCOL;
        if (agentConfig.isSsl() != null) {
            protocol = agentConfig.isSsl() ? "https" : "http";
        }
        String port = "";
        if (agentConfig.getPort() != null) {
            port = String.format(":%d", agentConfig.getPort());
        }
        String path = DEFAULT_PATH;
        if (agentConfig.getPath() != null) {
            path = agentConfig.getPath();
        }
        // Prepend a forward slash if missing
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        String host = agentInetAddress.getHostAddress();
        if (agentConfig.isGssAuth() != null && agentConfig.isGssAuth()) {
            // Always use the canonical host name when using GSS authentication
            host = agentInetAddress.getCanonicalHostName();
        }
        url = new URL(String.format("%s://%s%s%s", protocol, host, port, path));
    } catch (MalformedURLException e) {
        throw new RuntimeException("Invalid endpoint URL: " + e.getMessage());
    }
    final WSManEndpoint.Builder builder = new WSManEndpoint.Builder(url).withServerVersion(WSManVersion.WSMAN_1_0);
    if (agentConfig.getUsername() != null && agentConfig.getPassword() != null) {
        builder.withBasicAuth(agentConfig.getUsername(), agentConfig.getPassword());
    }
    if (agentConfig.isGssAuth() != null && agentConfig.isGssAuth()) {
        builder.withGSSAuth();
    }
    if (agentConfig.getMaxElements() != null) {
        builder.withMaxElements(agentConfig.getMaxElements());
    }
    if (agentConfig.isStrictSsl() != null) {
        builder.withStrictSSL(false);
    }
    if (agentConfig.getTimeout() != null) {
        builder.withConnectionTimeout(agentConfig.getTimeout()).withReceiveTimeout(agentConfig.getTimeout());
    }
    return builder.build();
}
Also used : MalformedURLException(java.net.MalformedURLException) WSManEndpoint(org.opennms.core.wsman.WSManEndpoint) URL(java.net.URL)

Example 3 with WSManEndpoint

use of org.opennms.core.wsman.WSManEndpoint in project opennms by OpenNMS.

the class WSManConfigDaoJaxbTest method canBuildEndpointForSpecific.

@Test
public void canBuildEndpointForSpecific() throws UnknownHostException {
    WSManConfigDaoJaxb configDao = new WSManConfigDaoJaxb();
    configDao.setConfigResource(new FileSystemResource("src/test/resources/wsman-config.xml"));
    configDao.afterPropertiesSet();
    WSManEndpoint endpoint = configDao.getEndpoint(InetAddress.getByName("172.23.1.2"));
    assertEquals("http://172.23.1.2:5985/ws-man", endpoint.getUrl().toString());
}
Also used : WSManEndpoint(org.opennms.core.wsman.WSManEndpoint) FileSystemResource(org.springframework.core.io.FileSystemResource) Test(org.junit.Test)

Example 4 with WSManEndpoint

use of org.opennms.core.wsman.WSManEndpoint 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 5 with WSManEndpoint

use of org.opennms.core.wsman.WSManEndpoint 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(g -> g.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 : CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) WSManDataCollectionConfigDao(org.opennms.netmgt.dao.WSManDataCollectionConfigDao) ListMultimap(com.google.common.collect.ListMultimap) LoggerFactory(org.slf4j.LoggerFactory) WSManException(org.opennms.core.wsman.exceptions.WSManException) CollectionException(org.opennms.netmgt.collection.api.CollectionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ParameterMap(org.opennms.core.utils.ParameterMap) NodeDao(org.opennms.netmgt.dao.api.NodeDao) WsmanAgentConfig(org.opennms.netmgt.config.wsman.WsmanAgentConfig) RetryNTimesLoop(org.opennms.core.wsman.utils.RetryNTimesLoop) WSManEndpoint(org.opennms.core.wsman.WSManEndpoint) Group(org.opennms.netmgt.config.wsman.Group) Collectors(java.util.stream.Collectors) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) BeanUtils(org.opennms.core.spring.BeanUtils) Definition(org.opennms.netmgt.config.wsman.Definition) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) WSManClient(org.opennms.core.wsman.WSManClient) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) Groups(org.opennms.netmgt.config.wsman.Groups) Iterables(com.google.common.collect.Iterables) HashMap(java.util.HashMap) WSManConfigDao(org.opennms.netmgt.dao.WSManConfigDao) Supplier(java.util.function.Supplier) CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) DeferredGenericTypeResource(org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource) WsmanDatacollectionConfig(org.opennms.netmgt.config.wsman.WsmanDatacollectionConfig) Lists(com.google.common.collect.Lists) Node(org.w3c.dom.Node) Collection(org.opennms.netmgt.config.wsman.Collection) Attrib(org.opennms.netmgt.config.wsman.Attrib) NoSuchElementException(java.util.NoSuchElementException) InvalidResourceURI(org.opennms.core.wsman.exceptions.InvalidResourceURI) SimpleEntry(java.util.AbstractMap.SimpleEntry) OnmsNode(org.opennms.netmgt.model.OnmsNode) ResponseHandlingUtils(org.opennms.core.wsman.utils.ResponseHandlingUtils) Resource(org.opennms.netmgt.collection.support.builder.Resource) Logger(org.slf4j.Logger) WSManClientFactory(org.opennms.core.wsman.WSManClientFactory) AbstractRemoteServiceCollector(org.opennms.netmgt.collection.api.AbstractRemoteServiceCollector) CXFWSManClientFactory(org.opennms.core.wsman.cxf.CXFWSManClientFactory) File(java.io.File) CollectionInitializationException(org.opennms.netmgt.collection.api.CollectionInitializationException) Collections(java.util.Collections) RrdRepository(org.opennms.netmgt.rrd.RrdRepository) 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

WSManEndpoint (org.opennms.core.wsman.WSManEndpoint)5 NoSuchElementException (java.util.NoSuchElementException)2 Test (org.junit.Test)2 WSManClient (org.opennms.core.wsman.WSManClient)2 WSManException (org.opennms.core.wsman.exceptions.WSManException)2 RetryNTimesLoop (org.opennms.core.wsman.utils.RetryNTimesLoop)2 WsmanAgentConfig (org.opennms.netmgt.config.wsman.WsmanAgentConfig)2 WSManConfigDao (org.opennms.netmgt.dao.WSManConfigDao)2 Node (org.w3c.dom.Node)2 Iterables (com.google.common.collect.Iterables)1 ListMultimap (com.google.common.collect.ListMultimap)1 Lists (com.google.common.collect.Lists)1 File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 SimpleEntry (java.util.AbstractMap.SimpleEntry)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1