Search in sources :

Example 1 with PollerResponse

use of org.opennms.netmgt.poller.PollerResponse in project opennms by OpenNMS.

the class PollerRequestBuilderImpl method execute.

@Override
public CompletableFuture<PollerResponse> execute() {
    if (serviceMonitor == null) {
        throw new IllegalArgumentException("Monitor or monitor class name is required.");
    } else if (service == null) {
        throw new IllegalArgumentException("Monitored service is required.");
    }
    final RpcTarget target = client.getRpcTargetHelper().target().withNodeId(service.getNodeId()).withLocation(service.getNodeLocation()).withSystemId(systemId).withServiceAttributes(attributes).withLocationOverride((s) -> serviceMonitor.getEffectiveLocation(s)).build();
    final PollerRequestDTO request = new PollerRequestDTO();
    request.setLocation(target.getLocation());
    request.setSystemId(target.getSystemId());
    request.setClassName(serviceMonitor.getClass().getCanonicalName());
    request.setServiceName(service.getSvcName());
    request.setAddress(service.getAddress());
    request.setNodeId(service.getNodeId());
    request.setNodeLabel(service.getNodeLabel());
    request.setNodeLocation(service.getNodeLocation());
    request.setTimeToLiveMs(ttlInMs);
    request.addAttributes(attributes);
    // Retrieve the runtime attributes, which may include attributes
    // such as the agent details and other state related attributes
    // which should be included in the request
    final Map<String, Object> parameters = request.getMonitorParameters();
    request.addAttributes(serviceMonitor.getRuntimeAttributes(request, parameters));
    // Execute the request
    return client.getDelegate().execute(request).thenApply(results -> {
        PollStatus pollStatus = results.getPollStatus();
        // Invoke the adapters in the same order as which they were added
        for (ServiceMonitorAdaptor adaptor : adaptors) {
            // The adapters may update the status
            pollStatus = adaptor.handlePollResult(service, attributes, pollStatus);
        }
        results.setPollStatus(pollStatus);
        return results;
    });
}
Also used : ServiceMonitor(org.opennms.netmgt.poller.ServiceMonitor) ServiceMonitorAdaptor(org.opennms.netmgt.poller.ServiceMonitorAdaptor) List(java.util.List) PollerResponse(org.opennms.netmgt.poller.PollerResponse) Map(java.util.Map) MonitoredService(org.opennms.netmgt.poller.MonitoredService) RpcTarget(org.opennms.core.rpc.api.RpcTarget) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) LinkedList(java.util.LinkedList) PollerRequestBuilder(org.opennms.netmgt.poller.PollerRequestBuilder) PollStatus(org.opennms.netmgt.poller.PollStatus) PollStatus(org.opennms.netmgt.poller.PollStatus) ServiceMonitorAdaptor(org.opennms.netmgt.poller.ServiceMonitorAdaptor) RpcTarget(org.opennms.core.rpc.api.RpcTarget)

Example 2 with PollerResponse

use of org.opennms.netmgt.poller.PollerResponse in project opennms by OpenNMS.

the class Poll method doExecute.

@Override
protected Object doExecute() throws Exception {
    final CompletableFuture<PollerResponse> future = locationAwarePollerClient.poll().withService(new SimpleMonitoredService(InetAddress.getByName(host), "SVC", location)).withMonitorClassName(className).withTimeToLive(ttlInMs).withAttributes(parse(attributes)).execute();
    while (true) {
        try {
            try {
                PollStatus pollStatus = future.get(1, TimeUnit.SECONDS).getPollStatus();
                if (pollStatus.getStatusCode() == PollStatus.SERVICE_AVAILABLE) {
                    System.out.printf("\nService is %s on %s using %s:\n", pollStatus.getStatusName(), host, className);
                    final Map<String, Number> properties = pollStatus.getProperties();
                    if (properties.size() > 0) {
                        properties.entrySet().stream().forEach(e -> {
                            System.out.printf("\t%s: %.4f\n", e.getKey(), e.getValue());
                        });
                    } else {
                        System.out.printf("(No properties were returned by the monitor.\n");
                    }
                } else {
                    System.out.printf("\nService is %s on %s using %s\n", pollStatus.getStatusName(), host, className);
                    System.out.printf("\tReason: %s\n", pollStatus.getReason());
                }
            } catch (InterruptedException e) {
                System.out.println("\nInterrupted.");
            } catch (ExecutionException e) {
                System.out.printf("\nPoll failed with: %s\n", e);
            }
            break;
        } catch (TimeoutException e) {
        // pass
        }
        System.out.print(".");
        System.out.flush();
    }
    return null;
}
Also used : PollStatus(org.opennms.netmgt.poller.PollStatus) SimpleMonitoredService(org.opennms.netmgt.poller.support.SimpleMonitoredService) ExecutionException(java.util.concurrent.ExecutionException) PollerResponse(org.opennms.netmgt.poller.PollerResponse) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with PollerResponse

use of org.opennms.netmgt.poller.PollerResponse in project opennms by OpenNMS.

the class PollableServiceConfigTest method returnsUnknownOnRequestTimedOutException.

/**
     * Verifies that <b>PollStatus.unknown()</b> is returned when the
     * {@link LocationAwarePollerClient} fails with a {@link RequestTimedOutException}.
     *
     * This can happen when no Minions at the given location are available to process
     * the request, or the request was not completed in time, in which case we cannot
     * ascertain that the service is UP or DOWN.
     */
@Test
public void returnsUnknownOnRequestTimedOutException() throws Exception {
    // Create a future that fails with a RequestTimedOutException
    CompletableFuture<PollerResponse> future = new CompletableFuture<>();
    future.completeExceptionally(new RequestTimedOutException(new Exception("Test")));
    // Now mock the client to always return the future we created above
    LocationAwarePollerClient client = mock(LocationAwarePollerClient.class, Mockito.RETURNS_DEEP_STUBS);
    Mockito.when(client.poll().withService(any()).withMonitor(any()).withTimeToLive(any()).withAttributes(any()).withAdaptor(any()).withAdaptor(any()).execute()).thenReturn(future);
    // Mock all of the required objects required to successfully initialize the PollableServiceConfig
    PollableService pollableSvc = mock(PollableService.class);
    when(pollableSvc.getSvcName()).thenReturn("SVC");
    Service configuredSvc = new Service();
    configuredSvc.setName("SVC");
    Package pkg = mock(Package.class);
    when(pkg.getServices()).thenReturn(Lists.newArrayList(configuredSvc));
    PollerConfig pollerConfig = mock(PollerConfig.class);
    PollOutagesConfig pollOutagesConfig = mock(PollOutagesConfig.class);
    Timer timer = mock(Timer.class);
    PersisterFactory persisterFactory = mock(PersisterFactory.class);
    ResourceStorageDao resourceStorageDao = mock(ResourceStorageDao.class);
    final PollableServiceConfig psc = new PollableServiceConfig(pollableSvc, pollerConfig, pollOutagesConfig, pkg, timer, persisterFactory, resourceStorageDao, client);
    // Trigger the poll
    PollStatus pollStatus = psc.poll();
    // Verify
    assertThat(pollStatus.isUnknown(), is(true));
}
Also used : RequestTimedOutException(org.opennms.core.rpc.api.RequestTimedOutException) LocationAwarePollerClient(org.opennms.netmgt.poller.LocationAwarePollerClient) FilesystemResourceStorageDao(org.opennms.netmgt.dao.support.FilesystemResourceStorageDao) ResourceStorageDao(org.opennms.netmgt.dao.api.ResourceStorageDao) PollStatus(org.opennms.netmgt.poller.PollStatus) MockPersisterFactory(org.opennms.netmgt.mock.MockPersisterFactory) PersisterFactory(org.opennms.netmgt.collection.api.PersisterFactory) PollerConfig(org.opennms.netmgt.config.PollerConfig) Service(org.opennms.netmgt.config.poller.Service) RequestTimedOutException(org.opennms.core.rpc.api.RequestTimedOutException) CompletableFuture(java.util.concurrent.CompletableFuture) Timer(org.opennms.netmgt.scheduler.Timer) Package(org.opennms.netmgt.config.poller.Package) PollerResponse(org.opennms.netmgt.poller.PollerResponse) PollOutagesConfig(org.opennms.netmgt.config.PollOutagesConfig) Test(org.junit.Test)

Example 4 with PollerResponse

use of org.opennms.netmgt.poller.PollerResponse in project opennms by OpenNMS.

the class Poll method execute.

@Override
public Object execute() throws Exception {
    final CompletableFuture<PollerResponse> future = locationAwarePollerClient.poll().withService(new SimpleMonitoredService(InetAddress.getByName(host), "SVC", location)).withSystemId(systemId).withMonitorClassName(className).withTimeToLive(ttlInMs).withAttributes(parse(attributes)).execute();
    while (true) {
        try {
            try {
                PollStatus pollStatus = future.get(1, TimeUnit.SECONDS).getPollStatus();
                if (pollStatus.getStatusCode() == PollStatus.SERVICE_AVAILABLE) {
                    System.out.printf("\nService is %s on %s using %s:\n", pollStatus.getStatusName(), host, className);
                    final Map<String, Number> properties = pollStatus.getProperties();
                    if (properties.size() > 0) {
                        properties.entrySet().stream().forEach(e -> {
                            System.out.printf("\t%s: %.4f\n", e.getKey(), e.getValue() != null ? e.getValue().doubleValue() : null);
                        });
                    } else {
                        System.out.printf("(No properties were returned by the monitor.\n");
                    }
                } else {
                    System.out.printf("\nService is %s on %s using %s\n", pollStatus.getStatusName(), host, className);
                    System.out.printf("\tReason: %s\n", pollStatus.getReason());
                }
            } catch (InterruptedException e) {
                System.out.println("\nInterrupted.");
            } catch (ExecutionException e) {
                System.out.printf("\nPoll failed with: %s\n", e);
            }
            break;
        } catch (TimeoutException e) {
        // pass
        }
        System.out.print(".");
        System.out.flush();
    }
    return null;
}
Also used : PollStatus(org.opennms.netmgt.poller.PollStatus) SimpleMonitoredService(org.opennms.netmgt.poller.support.SimpleMonitoredService) ExecutionException(java.util.concurrent.ExecutionException) PollerResponse(org.opennms.netmgt.poller.PollerResponse) TimeoutException(java.util.concurrent.TimeoutException)

Example 5 with PollerResponse

use of org.opennms.netmgt.poller.PollerResponse in project opennms by OpenNMS.

the class PollableServiceConfigIT method returnsUnknownOnRequestTimedOutException.

/**
 * Verifies that <b>PollStatus.unknown()</b> is returned when the
 * {@link LocationAwarePollerClient} fails with a {@link RequestTimedOutException}.
 *
 * This can happen when no Minions at the given location are available to process
 * the request, or the request was not completed in time, in which case we cannot
 * ascertain that the service is UP or DOWN.
 */
@Test
public void returnsUnknownOnRequestTimedOutException() throws Exception {
    // Create a future that fails with a RequestTimedOutException
    CompletableFuture<PollerResponse> future = new CompletableFuture<>();
    future.completeExceptionally(new RequestTimedOutException(new Exception("Test")));
    // Now mock the client to always return the future we created above
    LocationAwarePollerClient client = mock(LocationAwarePollerClient.class, Mockito.RETURNS_DEEP_STUBS);
    Mockito.when(client.poll().withService(any()).withMonitor(any()).withTimeToLive(any()).withAttributes(any()).withAdaptor(any()).withAdaptor(any()).execute()).thenReturn(future);
    // Mock all of the required objects required to successfully initialize the PollableServiceConfig
    PollableService pollableSvc = mock(PollableService.class);
    when(pollableSvc.getSvcName()).thenReturn("SVC");
    Service configuredSvc = new Service();
    configuredSvc.setName("SVC");
    Package pkg = mock(Package.class);
    when(pkg.getServices()).thenReturn(Lists.newArrayList(configuredSvc));
    PollerConfig pollerConfig = mock(PollerConfig.class);
    PollOutagesConfig pollOutagesConfig = mock(PollOutagesConfig.class);
    Timer timer = mock(Timer.class);
    PersisterFactory persisterFactory = mock(PersisterFactory.class);
    ResourceStorageDao resourceStorageDao = mock(ResourceStorageDao.class);
    final PollableServiceConfig psc = new PollableServiceConfig(pollableSvc, pollerConfig, pollOutagesConfig, pkg, timer, persisterFactory, resourceStorageDao, client);
    // Trigger the poll
    PollStatus pollStatus = psc.poll();
    // Verify
    assertThat(pollStatus.isUnknown(), is(true));
}
Also used : RequestTimedOutException(org.opennms.core.rpc.api.RequestTimedOutException) LocationAwarePollerClient(org.opennms.netmgt.poller.LocationAwarePollerClient) FilesystemResourceStorageDao(org.opennms.netmgt.dao.support.FilesystemResourceStorageDao) ResourceStorageDao(org.opennms.netmgt.dao.api.ResourceStorageDao) PollStatus(org.opennms.netmgt.poller.PollStatus) MockPersisterFactory(org.opennms.netmgt.mock.MockPersisterFactory) PersisterFactory(org.opennms.netmgt.collection.api.PersisterFactory) PollerConfig(org.opennms.netmgt.config.PollerConfig) Service(org.opennms.netmgt.config.poller.Service) RequestTimedOutException(org.opennms.core.rpc.api.RequestTimedOutException) CompletableFuture(java.util.concurrent.CompletableFuture) Timer(org.opennms.netmgt.scheduler.Timer) Package(org.opennms.netmgt.config.poller.Package) PollerResponse(org.opennms.netmgt.poller.PollerResponse) PollOutagesConfig(org.opennms.netmgt.config.PollOutagesConfig) Test(org.junit.Test)

Aggregations

PollStatus (org.opennms.netmgt.poller.PollStatus)5 PollerResponse (org.opennms.netmgt.poller.PollerResponse)5 CompletableFuture (java.util.concurrent.CompletableFuture)3 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 Test (org.junit.Test)2 RequestTimedOutException (org.opennms.core.rpc.api.RequestTimedOutException)2 PersisterFactory (org.opennms.netmgt.collection.api.PersisterFactory)2 PollOutagesConfig (org.opennms.netmgt.config.PollOutagesConfig)2 PollerConfig (org.opennms.netmgt.config.PollerConfig)2 Package (org.opennms.netmgt.config.poller.Package)2 Service (org.opennms.netmgt.config.poller.Service)2 ResourceStorageDao (org.opennms.netmgt.dao.api.ResourceStorageDao)2 FilesystemResourceStorageDao (org.opennms.netmgt.dao.support.FilesystemResourceStorageDao)2 MockPersisterFactory (org.opennms.netmgt.mock.MockPersisterFactory)2 LocationAwarePollerClient (org.opennms.netmgt.poller.LocationAwarePollerClient)2 SimpleMonitoredService (org.opennms.netmgt.poller.support.SimpleMonitoredService)2 Timer (org.opennms.netmgt.scheduler.Timer)2 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1