use of org.opennms.core.wsman.WSManClient 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);
}
use of org.opennms.core.wsman.WSManClient 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));
}
}
use of org.opennms.core.wsman.WSManClient 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();
}
use of org.opennms.core.wsman.WSManClient 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);
}
Aggregations