Search in sources :

Example 96 with PollStatus

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

the class DnsMonitor method pollDNS.

private PollStatus pollDNS(final TimeoutTracker timeoutTracker, final int port, final InetAddress address, final String lookup, final List<Integer> fatalCodes, int minAnswers, int maxAnswers) {
    final String addr = InetAddressUtils.str(address);
    for (timeoutTracker.reset(); timeoutTracker.shouldRetry(); timeoutTracker.nextAttempt()) {
        try {
            final Name name = Name.fromString(lookup, Name.root);
            final SimpleResolver resolver = new SimpleResolver();
            resolver.setAddress(new InetSocketAddress(addr, port));
            resolver.setLocalAddress((InetSocketAddress) null);
            double timeout = timeoutTracker.getSoTimeout() / 1000;
            resolver.setTimeout((timeout < 1 ? 1 : (int) timeout));
            final Record question = Record.newRecord(name, Type.A, DClass.IN);
            final Message query = Message.newQuery(question);
            PollStatus status;
            timeoutTracker.startAttempt();
            final Message response = resolver.send(query);
            double responseTime = timeoutTracker.elapsedTimeInMillis();
            final Integer rcode = response.getHeader().getRcode();
            LOG.debug("received response code: {}", rcode);
            if (fatalCodes.contains(rcode)) {
                status = PollStatus.unavailable("Received an invalid DNS response for address: " + addr);
                LOG.debug(status.getReason());
                return status;
            } else if (minAnswers != DEFAULT_MIN_ANSWERS || maxAnswers != DEFAULT_MAX_ANSWERS) {
                int numAnswers = response.getSectionArray(Section.ANSWER).length;
                boolean tooFewAnswers = numAnswers < minAnswers;
                boolean tooManyAnswers = numAnswers > maxAnswers;
                if (tooFewAnswers) {
                    status = PollStatus.unavailable("Response contained only " + numAnswers + " answer(s), but at least " + minAnswers + " answers(s) are needed.");
                    LOG.warn(status.getReason());
                    return status;
                }
                if (tooManyAnswers) {
                    status = PollStatus.unavailable("Response contained " + numAnswers + " answer(s), but " + maxAnswers + " or fewer answers(s) are needed.");
                    LOG.warn(status.getReason());
                    return status;
                }
                status = PollStatus.up(responseTime);
                LOG.debug("valid DNS response received with {} answer(s), responseTime = {}ms", numAnswers, responseTime);
                return status;
            } else {
                status = PollStatus.up(responseTime);
                LOG.debug("valid DNS response received, responseTime = {}ms", responseTime);
                return status;
            }
        } catch (final InterruptedIOException e) {
        // No response received, retry without marking the poll failed. If we get this condition over and over until 
        // the retries are exhausted, it will leave serviceStatus null and we'll get the log message at the bottom 
        } catch (final NoRouteToHostException e) {
            String reason1 = "No route to host exception for address: " + addr;
            LOG.debug(reason1, e);
            return PollStatus.unavailable(reason1);
        } catch (final ConnectException e) {
            String reason1 = "Connection exception for address: " + addr;
            LOG.debug(reason1, e);
            return PollStatus.unavailable(reason1);
        } catch (final IOException e) {
            String reason1 = "IOException while polling address: " + addr + " " + e.getMessage();
            LOG.debug(reason1, e);
            return PollStatus.unavailable(reason1);
        }
    }
    String reason = "Never received valid DNS response for address: " + addr;
    LOG.debug(reason);
    return PollStatus.unavailable(reason);
}
Also used : InterruptedIOException(java.io.InterruptedIOException) PollStatus(org.opennms.netmgt.poller.PollStatus) Message(org.xbill.DNS.Message) InetSocketAddress(java.net.InetSocketAddress) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) SimpleResolver(org.xbill.DNS.SimpleResolver) NoRouteToHostException(java.net.NoRouteToHostException) Name(org.xbill.DNS.Name) Record(org.xbill.DNS.Record) ConnectException(java.net.ConnectException)

Example 97 with PollStatus

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

the class PollerClientRpcModule method execute.

@Override
public CompletableFuture<PollerResponseDTO> execute(PollerRequestDTO request) {
    final String className = request.getClassName();
    final ServiceMonitor monitor = serviceMonitorRegistry.getMonitorByClassName(className);
    if (monitor == null) {
        return CompletableFuture.completedFuture(new PollerResponseDTO(PollStatus.unknown("No monitor found with class name '" + className + "'.")));
    }
    return CompletableFuture.supplyAsync(new Supplier<PollerResponseDTO>() {

        @Override
        public PollerResponseDTO get() {
            PollStatus pollStatus;
            try {
                final Map<String, Object> parameters = request.getMonitorParameters();
                pollStatus = monitor.poll(request, parameters);
            } catch (RuntimeException e) {
                pollStatus = PollStatus.unknown(e.getMessage());
            }
            return new PollerResponseDTO(pollStatus);
        }
    }, executor);
}
Also used : ServiceMonitor(org.opennms.netmgt.poller.ServiceMonitor) PollStatus(org.opennms.netmgt.poller.PollStatus) Map(java.util.Map)

Example 98 with PollStatus

use of org.opennms.netmgt.poller.PollStatus 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 PollerRequestDTO request = new PollerRequestDTO();
    request.setLocation(serviceMonitor.getEffectiveLocation(service.getNodeLocation()));
    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();
        for (ServiceMonitorAdaptor adaptor : adaptors) {
            pollStatus = adaptor.handlePollResult(service, attributes, pollStatus);
        }
        results.setPollStatus(pollStatus);
        return results;
    });
}
Also used : PollStatus(org.opennms.netmgt.poller.PollStatus) ServiceMonitorAdaptor(org.opennms.netmgt.poller.ServiceMonitorAdaptor)

Example 99 with PollStatus

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

the class JCifsMonitorTest method testPoll.

@Test
public void testPoll() throws UnknownHostException {
    MonitoredService svc = MonitorTestUtils.getMonitoredService(99, InetAddress.getByName("10.123.123.123"), "JCIFS");
    Map<String, Object> m = Collections.synchronizedMap(new TreeMap<String, Object>());
    replay(mockSmbFolderEmpty, mockSmbFolderNotEmpty, mockSmbFileValidPath, mockSmbFileInvalidPath, SmbFile.class);
    JCifsMonitor jCifsMonitor = new JCifsMonitor();
    PollStatus pollStatus;
    /*
         * checking path does exist and mode is PATH_EXIST => up
         */
    m.put("username", "user");
    m.put("password", "pass");
    m.put("domain", "dom");
    m.put("mode", "PATH_EXIST");
    m.put("path", "/validPath");
    pollStatus = jCifsMonitor.poll(svc, m);
    assertEquals(PollStatus.up(), pollStatus);
    /*
         * checking path does not exist and mode is PATH_EXIST => down
         */
    m.put("username", "user");
    m.put("password", "pass");
    m.put("domain", "dom");
    m.put("mode", "PATH_EXIST");
    m.put("path", "/invalidPath");
    pollStatus = jCifsMonitor.poll(svc, m);
    assertEquals(PollStatus.down(), pollStatus);
    /*
         * checking path does exist and mode is PATH_NOT_EXIST => down
         */
    m.put("username", "user");
    m.put("password", "pass");
    m.put("domain", "dom");
    m.put("mode", "PATH_NOT_EXIST");
    m.put("path", "/validPath");
    pollStatus = jCifsMonitor.poll(svc, m);
    assertEquals(PollStatus.down(), pollStatus);
    /*
         * checking path does not exist and mode is PATH_NOT_EXIST => up
         */
    m.put("username", "user");
    m.put("password", "pass");
    m.put("domain", "dom");
    m.put("mode", "PATH_NOT_EXIST");
    m.put("path", "/invalidPath");
    pollStatus = jCifsMonitor.poll(svc, m);
    assertEquals(PollStatus.up(), pollStatus);
    /*
         * checking folder not empty and mode is FOLDER_EMPTY => down
         */
    m.put("username", "user");
    m.put("password", "pass");
    m.put("domain", "dom");
    m.put("mode", "FOLDER_EMPTY");
    m.put("path", "/folderNotEmpty");
    pollStatus = jCifsMonitor.poll(svc, m);
    assertEquals(PollStatus.down(), pollStatus);
    /*
         * checking folder empty and mode is FOLDER_EMPTY => up
         */
    m.put("username", "user");
    m.put("password", "pass");
    m.put("domain", "dom");
    m.put("mode", "FOLDER_EMPTY");
    m.put("path", "/folderEmpty");
    pollStatus = jCifsMonitor.poll(svc, m);
    assertEquals(PollStatus.up(), pollStatus);
    /*
         * checking folder not empty and mode is FOLDER_NOT_EMPTY => up
         */
    m.put("username", "user");
    m.put("password", "pass");
    m.put("domain", "dom");
    m.put("mode", "FOLDER_NOT_EMPTY");
    m.put("path", "/folderNotEmpty");
    pollStatus = jCifsMonitor.poll(svc, m);
    assertEquals(PollStatus.up(), pollStatus);
    /*
         * checking folder empty and mode is FOLDER_NOT_EMPTY => down
         */
    m.put("username", "user");
    m.put("password", "pass");
    m.put("domain", "dom");
    m.put("mode", "FOLDER_NOT_EMPTY");
    m.put("path", "/folderEmpty");
    pollStatus = jCifsMonitor.poll(svc, m);
    assertEquals(PollStatus.down(), pollStatus);
    /*
         * checking for invalid mode => down
         */
    m.put("username", "user");
    m.put("password", "pass");
    m.put("domain", "dom");
    m.put("mode", "ABC");
    m.put("path", "/folderEmpty");
    pollStatus = jCifsMonitor.poll(svc, m);
    assertEquals(PollStatus.unknown(), pollStatus);
    /*
         * checking for SmbException => down
         */
    m.put("username", "user");
    m.put("password", "pass");
    m.put("domain", "dom");
    m.put("mode", "PATH_EXIST");
    m.put("path", "/smbException");
    pollStatus = jCifsMonitor.poll(svc, m);
    assertEquals(PollStatus.down(), pollStatus);
    /*
         * checking for MalformedUrlException => down
         */
    m.put("username", "user");
    m.put("password", "pass");
    m.put("domain", "dom");
    m.put("mode", "PATH_EXIST");
    m.put("path", "/malformedUrlException");
    pollStatus = jCifsMonitor.poll(svc, m);
    assertEquals(PollStatus.down(), pollStatus);
    /*
         * checking for overriding Ip address via empty string => up
         */
    m.put("username", "user");
    m.put("password", "pass");
    m.put("domain", "dom");
    m.put("mode", "PATH_EXIST");
    m.put("smbHost", "");
    m.put("path", "/validPath");
    pollStatus = jCifsMonitor.poll(svc, m);
    assertEquals(PollStatus.up(), pollStatus);
    /*
         * checking for overriding Ip address via smbHost => down
         */
    m.put("username", "user");
    m.put("password", "pass");
    m.put("domain", "dom");
    m.put("mode", "PATH_EXIST");
    m.put("smbHost", "192.168.0.123");
    m.put("path", "/smbException");
    pollStatus = jCifsMonitor.poll(svc, m);
    assertEquals(PollStatus.down(), pollStatus);
    assertTrue(pollStatus.getReason().matches(".*192\\.168\\.0\\.123.*"));
}
Also used : PollStatus(org.opennms.netmgt.poller.PollStatus) MonitoredService(org.opennms.netmgt.poller.MonitoredService) EasyMock.anyObject(org.easymock.EasyMock.anyObject) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 100 with PollStatus

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

the class RadiusAuthMonitorTest method testResponse.

@Test
@Ignore("have to have a radius server set up")
public void testResponse() throws Exception {
    final Map<String, Object> m = new ConcurrentSkipListMap<String, Object>();
    final ServiceMonitor monitor = new RadiusAuthMonitor();
    final MonitoredService svc = MonitorTestUtils.getMonitoredService(99, InetAddressUtils.addr("192.168.211.11"), "RADIUS");
    m.put("user", "testing");
    m.put("password", "password");
    m.put("retry", "1");
    m.put("secret", "testing123");
    m.put("authtype", "chap");
    final PollStatus status = monitor.poll(svc, m);
    MockUtil.println("Reason: " + status.getReason());
    assertEquals(PollStatus.SERVICE_AVAILABLE, status.getStatusCode());
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) ServiceMonitor(org.opennms.netmgt.poller.ServiceMonitor) PollStatus(org.opennms.netmgt.poller.PollStatus) MonitoredService(org.opennms.netmgt.poller.MonitoredService) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

PollStatus (org.opennms.netmgt.poller.PollStatus)200 Test (org.junit.Test)92 MonitoredService (org.opennms.netmgt.poller.MonitoredService)51 ServiceMonitor (org.opennms.netmgt.poller.ServiceMonitor)47 InetAddress (java.net.InetAddress)39 MockMonitoredService (org.opennms.netmgt.poller.mock.MockMonitoredService)36 HashMap (java.util.HashMap)35 TimeoutTracker (org.opennms.core.utils.TimeoutTracker)29 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)28 JUnitHttpServer (org.opennms.core.test.http.annotations.JUnitHttpServer)18 IOException (java.io.IOException)17 Socket (java.net.Socket)17 InputStreamReader (java.io.InputStreamReader)16 SnmpAgentConfig (org.opennms.netmgt.snmp.SnmpAgentConfig)14 SnmpValue (org.opennms.netmgt.snmp.SnmpValue)14 BufferedReader (java.io.BufferedReader)13 SnmpObjId (org.opennms.netmgt.snmp.SnmpObjId)12 InterruptedIOException (java.io.InterruptedIOException)11 ConnectException (java.net.ConnectException)11 NoRouteToHostException (java.net.NoRouteToHostException)11