Search in sources :

Example 1 with WSManConfigDao

use of org.opennms.netmgt.dao.WSManConfigDao 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 2 with WSManConfigDao

use of org.opennms.netmgt.dao.WSManConfigDao in project opennms by OpenNMS.

the class WSManCollectorComplianceTest method getRequiredBeans.

public Map<String, Object> getRequiredBeans() {
    OnmsNode node = mock(OnmsNode.class, RETURNS_DEEP_STUBS);
    NodeDao nodeDao = mock(NodeDao.class);
    when(nodeDao.get(anyInt())).thenReturn(node);
    Definition agentConfig = new Definition();
    WSManConfigDao wsManConfigDao = mock(WSManConfigDao.class);
    when(wsManConfigDao.getAgentConfig(InetAddrUtils.getLocalHostAddress())).thenReturn(agentConfig);
    WsmanDatacollectionConfig config = new WsmanDatacollectionConfig();
    config.setRrdRepository("target");
    Collection collection = new Collection();
    collection.setRrd(new Rrd());
    WSManDataCollectionConfigDao wsManDataCollectionConfigDao = mock(WSManDataCollectionConfigDao.class);
    when(wsManDataCollectionConfigDao.getCollectionByName(COLLECTION)).thenReturn(collection);
    when(wsManDataCollectionConfigDao.getConfig()).thenReturn(config);
    return new ImmutableMap.Builder<String, Object>().put("nodeDao", nodeDao).put("wsManConfigDao", wsManConfigDao).put("wsManDataCollectionConfigDao", wsManDataCollectionConfigDao).build();
}
Also used : NodeDao(org.opennms.netmgt.dao.api.NodeDao) OnmsNode(org.opennms.netmgt.model.OnmsNode) WsmanDatacollectionConfig(org.opennms.netmgt.config.wsman.WsmanDatacollectionConfig) Rrd(org.opennms.netmgt.config.wsman.Rrd) Definition(org.opennms.netmgt.config.wsman.Definition) Collection(org.opennms.netmgt.config.wsman.Collection) WSManConfigDao(org.opennms.netmgt.dao.WSManConfigDao) WSManDataCollectionConfigDao(org.opennms.netmgt.dao.WSManDataCollectionConfigDao)

Example 3 with WSManConfigDao

use of org.opennms.netmgt.dao.WSManConfigDao in project opennms by OpenNMS.

the class WSManCollectorTest method canSuccesfullyCollectFromGroupWithNoAttributes.

@Test
public void canSuccesfullyCollectFromGroupWithNoAttributes() throws CollectionInitializationException, CollectionException {
    OnmsNode node = mock(OnmsNode.class);
    NodeDao nodeDao = mock(NodeDao.class);
    when(nodeDao.get(0)).thenReturn(node);
    WSManConfigDao configDao = mock(WSManConfigDao.class);
    when(configDao.getAgentConfig(any())).thenReturn(new Definition());
    Collection collection = new Collection();
    WSManDataCollectionConfigDao dataCollectionConfigDao = mock(WSManDataCollectionConfigDao.class);
    when(dataCollectionConfigDao.getCollectionByName("default")).thenReturn(collection);
    WsManCollector collector = new WsManCollector();
    collector.setWSManClientFactory(mock(WSManClientFactory.class));
    collector.setWSManConfigDao(configDao);
    collector.setWSManDataCollectionConfigDao(dataCollectionConfigDao);
    collector.setNodeDao(nodeDao);
    CollectionAgent agent = mock(CollectionAgent.class);
    when(agent.getAddress()).thenReturn(InetAddrUtils.getLocalHostAddress());
    when(agent.getStorageResourcePath()).thenReturn(ResourcePath.get());
    Map<String, Object> collectionParams = Maps.newHashMap();
    collectionParams.put("collection", "default");
    collectionParams.putAll(collector.getRuntimeAttributes(agent, collectionParams));
    CollectionSet collectionSet = collector.collect(agent, collectionParams);
    assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus());
    assertEquals(0, CollectionSetUtils.getAttributesByName(collectionSet).size());
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) Definition(org.opennms.netmgt.config.wsman.Definition) WSManConfigDao(org.opennms.netmgt.dao.WSManConfigDao) WSManClientFactory(org.opennms.core.wsman.WSManClientFactory) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) NodeDao(org.opennms.netmgt.dao.api.NodeDao) Collection(org.opennms.netmgt.config.wsman.Collection) WSManDataCollectionConfigDao(org.opennms.netmgt.dao.WSManDataCollectionConfigDao) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) Test(org.junit.Test)

Example 4 with WSManConfigDao

use of org.opennms.netmgt.dao.WSManConfigDao in project opennms by OpenNMS.

the class WSManMonitorTest method poll.

private static PollStatus poll(String rule, Node response) {
    String resourceUri = "mock-resource-uri";
    Map<String, String> selectors = Maps.newHashMap();
    selectors.put("mock-selector-a", "a1");
    selectors.put("mock-selector-b", "b1");
    Definition agentConfig = new Definition();
    WSManConfigDao configDao = mock(WSManConfigDao.class);
    when(configDao.getAgentConfig(anyObject())).thenReturn(agentConfig);
    WSManClient client = mock(WSManClient.class);
    when(client.get(resourceUri, selectors)).thenReturn(response);
    WSManClientFactory clientFactory = mock(WSManClientFactory.class);
    when(clientFactory.getClient(anyObject())).thenReturn(client);
    WsManMonitor monitor = new WsManMonitor();
    monitor.setWSManConfigDao(configDao);
    monitor.setWSManClientFactory(clientFactory);
    Map<String, Object> parameters = Maps.newHashMap();
    parameters.put(WsManMonitor.RESOURCE_URI_PARAM, resourceUri);
    selectors.entrySet().stream().forEach(e -> parameters.put(WsManMonitor.SELECTOR_PARAM_PREFIX + e.getKey(), e.getValue()));
    parameters.put(WsManMonitor.RULE_PARAM, rule);
    InetAddress localhost;
    try {
        localhost = InetAddress.getLocalHost();
    } catch (UnknownHostException e) {
        throw new RuntimeException(e);
    }
    MonitoredService svc = mock(MonitoredService.class);
    when(svc.getAddress()).thenReturn(localhost);
    return monitor.poll(svc, parameters);
}
Also used : UnknownHostException(java.net.UnknownHostException) MonitoredService(org.opennms.netmgt.poller.MonitoredService) Definition(org.opennms.netmgt.config.wsman.Definition) WSManConfigDao(org.opennms.netmgt.dao.WSManConfigDao) WSManClientFactory(org.opennms.core.wsman.WSManClientFactory) Matchers.anyObject(org.mockito.Matchers.anyObject) WSManClient(org.opennms.core.wsman.WSManClient) InetAddress(java.net.InetAddress)

Aggregations

WSManConfigDao (org.opennms.netmgt.dao.WSManConfigDao)4 Definition (org.opennms.netmgt.config.wsman.Definition)3 WSManClient (org.opennms.core.wsman.WSManClient)2 WSManClientFactory (org.opennms.core.wsman.WSManClientFactory)2 Collection (org.opennms.netmgt.config.wsman.Collection)2 WSManDataCollectionConfigDao (org.opennms.netmgt.dao.WSManDataCollectionConfigDao)2 NodeDao (org.opennms.netmgt.dao.api.NodeDao)2 OnmsNode (org.opennms.netmgt.model.OnmsNode)2 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 NoSuchElementException (java.util.NoSuchElementException)1 Test (org.junit.Test)1 Matchers.anyObject (org.mockito.Matchers.anyObject)1 WSManEndpoint (org.opennms.core.wsman.WSManEndpoint)1 WSManException (org.opennms.core.wsman.exceptions.WSManException)1 RetryNTimesLoop (org.opennms.core.wsman.utils.RetryNTimesLoop)1 CollectionAgent (org.opennms.netmgt.collection.api.CollectionAgent)1 CollectionSet (org.opennms.netmgt.collection.api.CollectionSet)1 Rrd (org.opennms.netmgt.config.wsman.Rrd)1 WsmanAgentConfig (org.opennms.netmgt.config.wsman.WsmanAgentConfig)1