Search in sources :

Example 26 with MockMonitoredService

use of org.opennms.netmgt.poller.mock.MockMonitoredService in project opennms by OpenNMS.

the class MockNetworkTest method testInvalidPoll.

public void testInvalidPoll() throws UnknownHostException {
    m_network.resetInvalidPollCount();
    MonitoredService svc = new MockMonitoredService(99, "InvalidNode", InetAddressUtils.addr("1.1.1.1"), "ICMP");
    ServiceMonitor monitor = m_pollerConfig.getServiceMonitor("ICMP");
    monitor.poll(svc, Collections.emptyMap());
    assertEquals(1, m_network.getInvalidPollCount());
}
Also used : ServiceMonitor(org.opennms.netmgt.poller.ServiceMonitor) MockMonitoredService(org.opennms.netmgt.poller.mock.MockMonitoredService) MonitoredService(org.opennms.netmgt.poller.MonitoredService) MockMonitoredService(org.opennms.netmgt.poller.mock.MockMonitoredService)

Example 27 with MockMonitoredService

use of org.opennms.netmgt.poller.mock.MockMonitoredService in project opennms by OpenNMS.

the class LatencyStoringServiceMonitorAdaptorPersistenceTest method persistAndVerifyLatencySamples.

private void persistAndVerifyLatencySamples(String locationName, Path pathToResourceInResponseTime) throws Exception {
    PollStatus pollStatus = PollStatus.get(PollStatus.SERVICE_AVAILABLE, 42.1);
    // For the purposes of this test, it's important the attributes are not added in lexicographical order
    Map<String, Number> props = new LinkedHashMap<String, Number>(pollStatus.getProperties());
    props.put("ping1", Integer.valueOf(1));
    props.put("loss", Integer.valueOf(2));
    props.put("median", Integer.valueOf(3));
    pollStatus.setProperties(props);
    ServiceMonitor serviceMonitor = new FixedServiceMonitor(pollStatus);
    Package pkg = new Package();
    MockNetwork mockNetwork = new MockNetwork();
    mockNetwork.createStandardNetwork();
    MockPollerConfig pollerConfig = new MockPollerConfig(mockNetwork);
    pollerConfig.setRRAList(pkg, Lists.newArrayList("RRA:AVERAGE:0.5:1:2016"));
    LatencyStoringServiceMonitorAdaptor lssma = new LatencyStoringServiceMonitorAdaptor(pollerConfig, pkg, m_persisterFactory, m_resourceStorageDao);
    MonitoredService monitoredService = new MockMonitoredService(3, "Firewall", locationName, InetAddress.getByName("192.168.1.5"), "SMTP");
    Map<String, Object> params = Maps.newHashMap();
    params.put("rrd-repository", getResponseTimeRoot().getAbsolutePath());
    params.put("rrd-base-name", "smtp-base");
    EasyMock.expect(m_rrdStrategy.getDefaultFileExtension()).andReturn(".jrb").atLeastOnce();
    m_rrdStrategy.createDefinition(EasyMock.eq("192.168.1.5"), EasyMock.eq(getResponseTimeRoot().toPath().resolve(pathToResourceInResponseTime).toString()), EasyMock.eq("smtp-base"), EasyMock.anyInt(), EasyMock.anyObject(), EasyMock.anyObject());
    EasyMock.expectLastCall().andReturn(null).once();
    m_rrdStrategy.createFile(EasyMock.anyObject());
    EasyMock.expectLastCall().once();
    m_rrdStrategy.openFile(EasyMock.eq(getResponseTimeRoot().toPath().resolve(pathToResourceInResponseTime.resolve("smtp-base.jrb")).toString()));
    EasyMock.expectLastCall().andReturn(null).once();
    // This is the important bit, the order of the values should match the order there were inserted above
    m_rrdStrategy.updateFile(EasyMock.isNull(), EasyMock.eq("192.168.1.5"), EasyMock.endsWith(":42.1:1:2:3"));
    EasyMock.expectLastCall().once();
    EasyMock.replay(m_rrdStrategy);
    // Trigger the poll
    lssma.handlePollResult(monitoredService, params, serviceMonitor.poll(monitoredService, params));
    // Verify
    EasyMock.verify(m_rrdStrategy);
    // Reset
    EasyMock.reset(m_rrdStrategy);
}
Also used : PollStatus(org.opennms.netmgt.poller.PollStatus) MockMonitoredService(org.opennms.netmgt.poller.mock.MockMonitoredService) MonitoredService(org.opennms.netmgt.poller.MonitoredService) MockPollerConfig(org.opennms.netmgt.mock.MockPollerConfig) LinkedHashMap(java.util.LinkedHashMap) ServiceMonitor(org.opennms.netmgt.poller.ServiceMonitor) AbstractServiceMonitor(org.opennms.netmgt.poller.support.AbstractServiceMonitor) MockNetwork(org.opennms.netmgt.mock.MockNetwork) MockMonitoredService(org.opennms.netmgt.poller.mock.MockMonitoredService) Package(org.opennms.netmgt.config.poller.Package)

Example 28 with MockMonitoredService

use of org.opennms.netmgt.poller.mock.MockMonitoredService in project opennms by OpenNMS.

the class SmtpMonitorIT method testPollSvrStatus554.

@Test
public void testPollSvrStatus554() throws UnknownHostException, InterruptedException {
    m_serverThread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                m_serverSocket.setSoTimeout(1000);
                Socket s = m_serverSocket.accept();
                System.out.println("S: 554 localhost.localdomain ESMTP bogon");
                s.getOutputStream().write("554 localhost.localdomain ESMTP bogon\r\n".getBytes());
                BufferedReader r = new BufferedReader(new InputStreamReader(s.getInputStream()));
                String command = r.readLine();
                System.out.println("C: " + command);
                if (command.equals("QUIT")) {
                    System.out.println("S: 221-Goodbye, friend.");
                    s.getOutputStream().write("221-Goodbye, friend.\r\n".getBytes());
                    System.out.println("S: 221 See ya");
                    s.getOutputStream().write("221 See ya\r\n".getBytes());
                }
            } catch (Throwable e) {
                throw new UndeclaredThrowableException(e);
            }
        }
    });
    m_serverThread.start();
    ServiceMonitor sm = new SmtpMonitor();
    MonitoredService svc = new MockMonitoredService(1, "Node One", InetAddressUtils.addr("127.0.0.1"), "SMTP");
    Map<String, Object> parms = new HashMap<String, Object>();
    parms.put("port", m_serverSocket.getLocalPort());
    PollStatus ps = sm.poll(svc, parms);
    assertTrue(ps.isUnavailable());
    assertFalse(ps.isUp());
}
Also used : PollStatus(org.opennms.netmgt.poller.PollStatus) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) MonitoredService(org.opennms.netmgt.poller.MonitoredService) MockMonitoredService(org.opennms.netmgt.poller.mock.MockMonitoredService) ServiceMonitor(org.opennms.netmgt.poller.ServiceMonitor) MockMonitoredService(org.opennms.netmgt.poller.mock.MockMonitoredService) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) BufferedReader(java.io.BufferedReader) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 29 with MockMonitoredService

use of org.opennms.netmgt.poller.mock.MockMonitoredService in project opennms by OpenNMS.

the class SmtpMonitorIT method testPollCase1.

@Test
public void testPollCase1() throws UnknownHostException, InterruptedException {
    m_serverThread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                m_serverSocket.setSoTimeout(1000);
                Socket s = m_serverSocket.accept();
                System.out.println("S: 220-localhost.localdomain ESMTP bogon");
                s.getOutputStream().write("220-localhost.localdomain ESMTP bogon\r\n".getBytes());
                System.out.println("S: 220-send me mail soon!");
                s.getOutputStream().write("220-send me mail soon!\r\n".getBytes());
                System.out.println("S: 220 send me mail now!");
                s.getOutputStream().write("220 send me mail now!\r\n".getBytes());
                BufferedReader r = new BufferedReader(new InputStreamReader(s.getInputStream()));
                String command = r.readLine();
                System.out.println("C: " + command);
                if (command.startsWith("HELO ")) {
                    System.out.println("S: 250 Hello");
                    s.getOutputStream().write("250 Hello\r\n".getBytes());
                }
                command = r.readLine();
                System.out.println("C: " + command);
                if (command.equals("QUIT")) {
                    System.out.println("S: 250 Hello");
                    s.getOutputStream().write("221 See ya\r\n".getBytes());
                }
            } catch (Throwable e) {
                throw new UndeclaredThrowableException(e);
            }
        }
    });
    m_serverThread.start();
    ServiceMonitor sm = new SmtpMonitor();
    MonitoredService svc = new MockMonitoredService(1, "Node One", InetAddressUtils.addr("127.0.0.1"), "SMTP");
    Map<String, Object> parms = new HashMap<String, Object>();
    parms.put("port", m_serverSocket.getLocalPort());
    PollStatus ps = sm.poll(svc, parms);
    assertTrue(ps.isUp());
    assertFalse(ps.isDown());
}
Also used : PollStatus(org.opennms.netmgt.poller.PollStatus) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) MonitoredService(org.opennms.netmgt.poller.MonitoredService) MockMonitoredService(org.opennms.netmgt.poller.mock.MockMonitoredService) ServiceMonitor(org.opennms.netmgt.poller.ServiceMonitor) MockMonitoredService(org.opennms.netmgt.poller.mock.MockMonitoredService) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) BufferedReader(java.io.BufferedReader) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 30 with MockMonitoredService

use of org.opennms.netmgt.poller.mock.MockMonitoredService in project opennms by OpenNMS.

the class HttpMonitorIT method callTestWithInvalidNodelabelHostName.

public void callTestWithInvalidNodelabelHostName(boolean preferIPv6) throws UnknownHostException {
    if (m_runTests == false)
        return;
    Map<String, Object> m = new ConcurrentSkipListMap<String, Object>();
    PollStatus status = null;
    ServiceMonitor monitor = new HttpMonitor();
    MockMonitoredService svc = MonitorTestUtils.getMonitoredService(3, "localhost", DnsUtils.resolveHostname("localhost", preferIPv6), "HTTP");
    svc.setNodeLabel("bad.virtual.host.example.com");
    final int port = JUnitHttpServerExecutionListener.getPort();
    if (port > 0) {
        m.put("port", String.valueOf(port));
    } else {
        throw new IllegalStateException("Unable to determine what port the HTTP server started on!");
    }
    m.put("retry", "0");
    m.put("timeout", "500");
    // Ensure that we get a 404 for this GET since we're using an inappropriate virtual host
    m.put("response", "404");
    m.put("verbose", "true");
    m.put("nodelabel-host-name", "true");
    status = monitor.poll(svc, m);
    MockUtil.println("Reason: " + status.getReason());
    assertEquals(PollStatus.SERVICE_AVAILABLE, status.getStatusCode());
    assertNull(status.getReason());
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) PollStatus(org.opennms.netmgt.poller.PollStatus) ServiceMonitor(org.opennms.netmgt.poller.ServiceMonitor) MockMonitoredService(org.opennms.netmgt.poller.mock.MockMonitoredService)

Aggregations

MockMonitoredService (org.opennms.netmgt.poller.mock.MockMonitoredService)35 HashMap (java.util.HashMap)27 Test (org.junit.Test)27 PollStatus (org.opennms.netmgt.poller.PollStatus)26 ServiceMonitor (org.opennms.netmgt.poller.ServiceMonitor)23 MonitoredService (org.opennms.netmgt.poller.MonitoredService)21 BufferedReader (java.io.BufferedReader)6 InputStreamReader (java.io.InputStreamReader)6 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)6 ServerSocket (java.net.ServerSocket)6 Socket (java.net.Socket)6 Package (org.opennms.netmgt.config.poller.Package)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 LinkedHashMap (java.util.LinkedHashMap)1 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)1 Before (org.junit.Before)1 JUnitHttpServer (org.opennms.core.test.http.annotations.JUnitHttpServer)1 Parameter (org.opennms.netmgt.config.poller.Parameter)1