Search in sources :

Example 11 with PollStatus

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

the class SmtpMonitorIT method testPollCase2.

@Test
public void testPollCase2() 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());
                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());
                    System.out.println("S: 250 send me mail now!");
                    s.getOutputStream().write("250 send me mail now!\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 12 with PollStatus

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

the class SmtpMonitorIT method testPollCaseForCRLFCheck.

@Test
public void testPollCaseForCRLFCheck() 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");
                // Having \r should fail in cheking for end of the line as the requirement is for CRLF
                s.getOutputStream().write("220 localhost.localdomain ESMTP bogon\r".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: 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 13 with PollStatus

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

the class SmtpMonitorIT method testPoll.

@Test
public void testPoll() 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());
                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 14 with PollStatus

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

the class PageSequenceMonitorIT method testLoginDynamicCredentialsTwice.

@Test
@JUnitHttpServer(port = 10342, webapps = @Webapp(context = "/opennms", path = "src/test/resources/loginTestWar"))
public void testLoginDynamicCredentialsTwice() throws Exception {
    m_params.put("page-sequence", "" + "<?xml version=\"1.0\"?>" + "<page-sequence>\n" + "  <page path=\"/opennms/\" port=\"10342\" virtual-host=\"localhost\" successMatch=\"(?s)&gt;Login (.)(.)(.)(.) Username and Password&lt;\">\n" + "    <session-variable name=\"ltr1\" match-group=\"1\" />\n" + "    <session-variable name=\"ltr2\" match-group=\"2\" />\n" + "    <session-variable name=\"ltr3\" match-group=\"3\" />\n" + "    <session-variable name=\"ltr4\" match-group=\"4\" />\n" + "  </page>\n" + "  <!-- Pick out the letters w-i-t-h to try and log in, this will fail -->\n" + "  <page virtual-host=\"localhost\" path=\"/opennms/j_spring_security_check\" response-range=\"300-399\" locationMatch=\"/opennms/spring_security_login\\?login_error\" port=\"10342\" method=\"POST\">\n" + "    <parameter key=\"j_username\" value=\"${ltr1}${ltr2}${ltr3}${ltr4}\"/>\n" + "    <parameter key=\"j_password\" value=\"${ltr1}${ltr2}${ltr3}${ltr4}\"/>\n" + "  </page>\n" + "  <page virtual-host=\"localhost\" path=\"/opennms/spring_security_login\" query=\"login_error\" port=\"10342\" failureMatch=\"(?s)Log out\" failureMessage=\"Login should have failed but did not\" successMatch=\"Your login attempt was not successful\"/>\n" + "  <!-- Pick out the letters d-e-m-o to try and log in, this will succeed -->\n" + "  <page path=\"/opennms/\" port=\"10342\" virtual-host=\"localhost\" successMatch=\"(?s)&lt;hea(.)&gt;&lt;titl(.)&gt;.*&lt;/for(.)&gt;&lt;/b(.)dy&gt;\">\n" + "    <session-variable name=\"ltr1\" match-group=\"1\" />\n" + "    <session-variable name=\"ltr2\" match-group=\"2\" />\n" + "    <session-variable name=\"ltr3\" match-group=\"3\" />\n" + "    <session-variable name=\"ltr4\" match-group=\"4\" />\n" + "  </page>\n" + "  <page virtual-host=\"localhost\" path=\"/opennms/j_spring_security_check\" response-range=\"300-399\" port=\"10342\" method=\"POST\">\n" + "    <parameter key=\"j_username\" value=\"${ltr1}${ltr2}${ltr3}${ltr4}\"/>\n" + "    <parameter key=\"j_password\" value=\"${ltr1}${ltr2}${ltr3}${ltr4}\"/>\n" + "  </page>\n" + "  <page virtual-host=\"localhost\" path=\"/opennms/events.html\" port=\"10342\" successMatch=\"Event Queries\" />\n" + "  <page virtual-host=\"localhost\" path=\"/opennms/j_spring_security_logout\" port=\"10342\" successMatch=\"Login with Username and Password\" />\n" + "</page-sequence>\n");
    try {
        PollStatus status = m_monitor.poll(getHttpService("localhost"), m_params);
        assertTrue("Expected available but was " + status + ": reason = " + status.getReason(), status.isAvailable());
        assertTrue("Expected a DS called 'response-time' but did not find one", status.getProperties().containsKey(PollStatus.PROPERTY_RESPONSE_TIME));
    } finally {
    // Print some debug output if necessary
    }
}
Also used : PollStatus(org.opennms.netmgt.poller.PollStatus) Test(org.junit.Test) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Example 15 with PollStatus

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

the class PageSequenceMonitorIT method testRedirectLocationDoesNotMatch.

@Test
@JUnitHttpServer(port = 10342, webapps = @Webapp(context = "/opennms", path = "src/test/resources/loginTestWar"))
public void testRedirectLocationDoesNotMatch() throws Exception {
    m_params.put("page-sequence", "" + "<?xml version=\"1.0\"?>" + "<page-sequence>\n" + "  <page path=\"/opennms/\" port=\"10342\" virtual-host=\"localhost\" successMatch=\"(?s)&lt;hea(.)&gt;&lt;titl(.)&gt;.*&lt;/for(.)&gt;&lt;/b(.)dy&gt;\">\n" + "    <session-variable name=\"ltr1\" match-group=\"1\" />\n" + "    <session-variable name=\"ltr2\" match-group=\"2\" />\n" + "    <session-variable name=\"ltr3\" match-group=\"3\" />\n" + "    <session-variable name=\"ltr4\" match-group=\"4\" />\n" + "  </page>\n" + "  <page virtual-host=\"localhost\" path=\"/opennms/j_spring_security_check\" port=\"10342\" method=\"POST\" response-range=\"300-399\" locationMatch=\"/opensadfnms/\">\n" + "    <parameter key=\"j_username\" value=\"${ltr1}${ltr2}${ltr3}${ltr4}\"/>\n" + "    <parameter key=\"j_password\" value=\"${ltr1}${ltr2}${ltr3}${ltr4}\"/>\n" + "  </page>\n" + "</page-sequence>\n");
    PollStatus status = m_monitor.poll(getHttpService("localhost"), m_params);
    assertTrue("Expected down but was " + status + ": reason = " + status.getReason(), status.isDown());
    assertEquals("Failed to find '/opensadfnms/' in Location: header at http://127.0.0.1:10342/opennms/j_spring_security_check", status.getReason());
    assertTrue("Expected a DS called 'response-time' but did not find one", status.getProperties().containsKey(PollStatus.PROPERTY_RESPONSE_TIME));
}
Also used : PollStatus(org.opennms.netmgt.poller.PollStatus) Test(org.junit.Test) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Aggregations

PollStatus (org.opennms.netmgt.poller.PollStatus)209 Test (org.junit.Test)98 MonitoredService (org.opennms.netmgt.poller.MonitoredService)57 ServiceMonitor (org.opennms.netmgt.poller.ServiceMonitor)53 InetAddress (java.net.InetAddress)40 HashMap (java.util.HashMap)36 MockMonitoredService (org.opennms.netmgt.poller.mock.MockMonitoredService)36 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)32 TimeoutTracker (org.opennms.core.utils.TimeoutTracker)29 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