Search in sources :

Example 1 with PollerConfig

use of org.opennms.netmgt.config.PollerConfig in project opennms by OpenNMS.

the class Test method doExecute.

@Override
protected Object doExecute() throws Exception {
    // Parse/validate the IP address
    final InetAddress addr = InetAddressUtils.addr(ipAddress);
    if (addr == null) {
        throw new IllegalStateException("Error getting InetAddress object for " + ipAddress);
    }
    final Map<String, Object> parameters = Poll.parse(serviceParameters);
    final MonitoredService monSvc = transactionTemplate.execute(new TransactionCallback<MonitoredService>() {

        @Override
        public MonitoredService doInTransaction(TransactionStatus status) {
            final List<OnmsIpInterface> ips = ipInterfaceDao.findByIpAddress(ipAddress);
            if (ips == null || ips.size() == 0) {
                System.err.printf("Error: Can't find the IP address %s on the database\n", ipAddress);
                return null;
            }
            if (ips.size() > 1) {
                System.out.printf("Warning: there are several IP interface objects associated with the IP address %s (picking the first one)\n", ipAddress);
            }
            OnmsNode n = ips.get(0).getNode();
            return new SimpleMonitoredService(addr, n.getId(), n.getLabel(), serviceName);
        }
    });
    if (monSvc == null) {
        // in which case we already printed an error message above
        return null;
    }
    // Read a fresh copy of poller-configuration.xml
    final PollerConfig pollerConfig = ReadOnlyPollerConfigManager.create();
    System.out.printf("Checking service %s on IP %s%n", serviceName, ipAddress);
    final org.opennms.netmgt.config.poller.Package pkg = packageName == null ? pollerConfig.getFirstLocalPackageMatch(ipAddress) : pollerConfig.getPackage(packageName);
    if (pkg == null) {
        System.err.printf("Error: Package %s doesn't exist%n", packageName);
        return null;
    }
    System.out.printf("Package: %s%n", pkg.getName());
    final Service svc = pollerConfig.getServiceInPackage(serviceName, pkg);
    if (svc == null) {
        System.err.printf("Error: Service %s not defined on package %s%n", serviceName, packageName);
        return null;
    }
    ServiceMonitor monitor = null;
    if (monitorClass == null) {
        monitor = pollerConfig.getServiceMonitor(serviceName);
        if (monitor == null) {
            System.err.printf("Error: Service %s doesn't have a monitor class defined%n", serviceName);
            return null;
        }
    } else {
        monitor = registry.getMonitorByClassName(monitorClass);
        System.err.printf("Error: No monitor found with class name %s\n", monitorClass);
        if (monitor == null) {
            return null;
        }
    }
    System.out.printf("Monitor: %s%n", monitor.getClass().getName());
    if (pollerConfig.isPolledLocally(ipAddress, serviceName)) {
        for (Parameter p : svc.getParameters()) {
            if (!parameters.containsKey(p.getKey())) {
                String value = p.getValue();
                if (value == null) {
                    try {
                        value = JaxbUtils.marshal(p.getAnyObject());
                    } catch (Exception e) {
                    }
                }
                parameters.put(p.getKey(), value);
            }
        }
        for (Entry<String, Object> e : parameters.entrySet()) {
            System.out.printf("Parameter %s : %s%n", e.getKey(), e.getValue());
        }
        try {
            PollStatus status = monitor.poll(monSvc, parameters);
            System.out.printf("Available ? %s (status %s[%s])%n", status.isAvailable(), status.getStatusName(), status.getStatusCode());
            if (status.isAvailable()) {
                System.out.printf("Response time: %s%n", status.getResponseTime());
            } else {
                if (status.getReason() != null) {
                    System.out.printf("Reason: %s%n", status.getReason());
                }
            }
        } catch (Exception e) {
            System.err.println("Error: Can't execute the monitor. " + e.getMessage());
            return null;
        }
    } else {
        System.err.printf("Error: Polling is not enabled for service %s using IP %s%n", serviceName, ipAddress);
    }
    return null;
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) PollStatus(org.opennms.netmgt.poller.PollStatus) SimpleMonitoredService(org.opennms.netmgt.poller.support.SimpleMonitoredService) MonitoredService(org.opennms.netmgt.poller.MonitoredService) PollerConfig(org.opennms.netmgt.config.PollerConfig) TransactionStatus(org.springframework.transaction.TransactionStatus) SimpleMonitoredService(org.opennms.netmgt.poller.support.SimpleMonitoredService) MonitoredService(org.opennms.netmgt.poller.MonitoredService) Service(org.opennms.netmgt.config.poller.Service) ServiceMonitor(org.opennms.netmgt.poller.ServiceMonitor) SimpleMonitoredService(org.opennms.netmgt.poller.support.SimpleMonitoredService) Parameter(org.opennms.netmgt.config.poller.Parameter) List(java.util.List) InetAddress(java.net.InetAddress)

Example 2 with PollerConfig

use of org.opennms.netmgt.config.PollerConfig in project opennms by OpenNMS.

the class MockNetworkTest method testPollerConfig.

public void testPollerConfig() {
    m_pollerConfig.setNodeOutageProcessingEnabled(true);
    m_pollerConfig.setPollInterval("HTTP", 750L);
    m_pollerConfig.setPollerThreads(5);
    m_pollerConfig.setCriticalService("YAHOO");
    PollerConfig pollerConfig = m_pollerConfig;
    // test the nodeOutageProcessing setting works
    assertTrue(pollerConfig.isNodeOutageProcessingEnabled());
    // test to ensure that the poller has packages
    Enumeration<Package> pkgs = pollerConfig.enumeratePackage();
    assertNotNull(pkgs);
    int pkgCount = 0;
    Package pkg = null;
    while (pkgs.hasMoreElements()) {
        pkg = (Package) pkgs.nextElement();
        pkgCount++;
    }
    assertTrue(pkgCount > 0);
    // ensure a sample interface is in the package
    assertTrue(pollerConfig.isInterfaceInPackage("192.168.1.1", pkg));
    for (final Service svc : pkg.getServices()) {
        if ("ICMP".equals(svc.getName()))
            assertEquals(Long.valueOf(500L), svc.getInterval());
        else if ("HTTP".equals(svc.getName()))
            assertEquals(Long.valueOf(750L), svc.getInterval());
        else
            assertEquals(Long.valueOf(1000L), svc.getInterval());
    }
    // ensure that setting the thread worked
    assertEquals(5, pollerConfig.getThreads());
    // ensure that setting the critical service worked
    assertEquals("YAHOO", pollerConfig.getCriticalService());
    // ensure that we have service monitors to the sevices
    assertNotNull(pollerConfig.getServiceMonitor("SMTP"));
}
Also used : PollerConfig(org.opennms.netmgt.config.PollerConfig) MonitoredService(org.opennms.netmgt.poller.MonitoredService) MockMonitoredService(org.opennms.netmgt.poller.mock.MockMonitoredService) Service(org.opennms.netmgt.config.poller.Service) Package(org.opennms.netmgt.config.poller.Package)

Example 3 with PollerConfig

use of org.opennms.netmgt.config.PollerConfig 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)

Aggregations

PollerConfig (org.opennms.netmgt.config.PollerConfig)3 Service (org.opennms.netmgt.config.poller.Service)3 Package (org.opennms.netmgt.config.poller.Package)2 MonitoredService (org.opennms.netmgt.poller.MonitoredService)2 PollStatus (org.opennms.netmgt.poller.PollStatus)2 InetAddress (java.net.InetAddress)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Test (org.junit.Test)1 RequestTimedOutException (org.opennms.core.rpc.api.RequestTimedOutException)1 PersisterFactory (org.opennms.netmgt.collection.api.PersisterFactory)1 PollOutagesConfig (org.opennms.netmgt.config.PollOutagesConfig)1 Parameter (org.opennms.netmgt.config.poller.Parameter)1 ResourceStorageDao (org.opennms.netmgt.dao.api.ResourceStorageDao)1 FilesystemResourceStorageDao (org.opennms.netmgt.dao.support.FilesystemResourceStorageDao)1 MockPersisterFactory (org.opennms.netmgt.mock.MockPersisterFactory)1 OnmsNode (org.opennms.netmgt.model.OnmsNode)1 LocationAwarePollerClient (org.opennms.netmgt.poller.LocationAwarePollerClient)1 PollerResponse (org.opennms.netmgt.poller.PollerResponse)1 ServiceMonitor (org.opennms.netmgt.poller.ServiceMonitor)1