Search in sources :

Example 1 with Parameter

use of org.opennms.netmgt.config.poller.Parameter in project opennms by OpenNMS.

the class TcpMonitorIT method testLocalhostIPv6Connection.

@Test
@JUnitHttpServer(port = 10342)
public void testLocalhostIPv6Connection() throws UnknownHostException {
    if (Boolean.getBoolean("skipIpv6Tests"))
        return;
    Map<String, Object> m = new ConcurrentSkipListMap<String, Object>();
    Parameter p = new Parameter();
    ServiceMonitor monitor = new TcpMonitor();
    MonitoredService svc = MonitorTestUtils.getMonitoredService(3, "::1", addr("::1"), "TCP");
    p.setKey("port");
    p.setValue("10342");
    m.put(p.getKey(), p.getValue());
    p.setKey("retry");
    p.setValue("1");
    m.put(p.getKey(), p.getValue());
    p.setKey("timeout");
    p.setValue("500");
    m.put(p.getKey(), p.getValue());
    PollStatus 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) ServiceMonitor(org.opennms.netmgt.poller.ServiceMonitor) PollStatus(org.opennms.netmgt.poller.PollStatus) MonitoredService(org.opennms.netmgt.poller.MonitoredService) Parameter(org.opennms.netmgt.config.poller.Parameter) Test(org.junit.Test) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Example 2 with Parameter

use of org.opennms.netmgt.config.poller.Parameter in project opennms by OpenNMS.

the class TcpMonitorIT method testExternalServerConnection.

/*
     * Test method for 'org.opennms.netmgt.poller.monitors.TcpMonitor.poll(NetworkInterface, Map, Package)'
     */
@Test
public void testExternalServerConnection() throws UnknownHostException {
    Map<String, Object> m = new ConcurrentSkipListMap<String, Object>();
    Parameter p = new Parameter();
    ServiceMonitor monitor = new TcpMonitor();
    MonitoredService svc = MonitorTestUtils.getMonitoredService(99, "www.opennms.org", DnsUtils.resolveHostname("www.opennms.org"), "TCP");
    p.setKey("port");
    p.setValue("3020");
    m.put(p.getKey(), p.getValue());
    p.setKey("retry");
    p.setValue("1");
    m.put(p.getKey(), p.getValue());
    p.setKey("timeout");
    p.setValue("500");
    m.put(p.getKey(), p.getValue());
    PollStatus status = monitor.poll(svc, m);
    MockUtil.println("Reason: " + status.getReason());
    assertEquals(PollStatus.SERVICE_UNAVAILABLE, status.getStatusCode());
    assertNotNull(status.getReason());
}
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) Parameter(org.opennms.netmgt.config.poller.Parameter) Test(org.junit.Test)

Example 3 with Parameter

use of org.opennms.netmgt.config.poller.Parameter in project opennms by OpenNMS.

the class TcpMonitorIT method testLocalhostConnection.

@Test
@JUnitHttpServer(port = 10342)
public void testLocalhostConnection() throws UnknownHostException {
    Map<String, Object> m = new ConcurrentSkipListMap<String, Object>();
    Parameter p = new Parameter();
    ServiceMonitor monitor = new TcpMonitor();
    MonitoredService svc = MonitorTestUtils.getMonitoredService(3, "localhost", DnsUtils.resolveHostname("localhost"), "TCP");
    p.setKey("port");
    p.setValue("10342");
    m.put(p.getKey(), p.getValue());
    p.setKey("retry");
    p.setValue("1");
    m.put(p.getKey(), p.getValue());
    p.setKey("timeout");
    p.setValue("500");
    m.put(p.getKey(), p.getValue());
    PollStatus 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) ServiceMonitor(org.opennms.netmgt.poller.ServiceMonitor) PollStatus(org.opennms.netmgt.poller.PollStatus) MonitoredService(org.opennms.netmgt.poller.MonitoredService) Parameter(org.opennms.netmgt.config.poller.Parameter) Test(org.junit.Test) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Example 4 with Parameter

use of org.opennms.netmgt.config.poller.Parameter 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 5 with Parameter

use of org.opennms.netmgt.config.poller.Parameter in project opennms by OpenNMS.

the class HttpMonitorIT method testPollStatusReason.

/*
     * Test method for 'org.opennms.netmgt.poller.monitors.HttpMonitor.poll(NetworkInterface, Map, Package)'
     */
@Test
public void testPollStatusReason() throws UnknownHostException {
    if (m_runTests == false)
        return;
    Map<String, Object> m = new ConcurrentSkipListMap<String, Object>();
    Parameter p = new Parameter();
    ServiceMonitor monitor = new HttpMonitor();
    InetAddress address = DnsUtils.resolveHostname("www.opennms.org");
    assertNotNull("Failed to resolved address: www.opennms.org", address);
    MonitoredService svc = MonitorTestUtils.getMonitoredService(99, "www.opennms.org", address, "HTTP");
    p.setKey("port");
    p.setValue("3020");
    m.put(p.getKey(), p.getValue());
    p.setKey("retry");
    p.setValue("1");
    m.put(p.getKey(), p.getValue());
    p.setKey("timeout");
    p.setValue("500");
    m.put(p.getKey(), p.getValue());
    PollStatus status = monitor.poll(svc, m);
    MockUtil.println("Reason: " + status.getReason());
    assertEquals(PollStatus.SERVICE_UNAVAILABLE, status.getStatusCode());
    assertNotNull(status.getReason());
/*
        // TODO: Enable this portion of the test as soon as there is a IPv6 www.opennms.org
        // Try with IPv6
        svc = MonitorTestUtils.getMonitoredService(99, "www.opennms.org", "HTTP", true);
        status = monitor.poll(svc, m);
        MockUtil.println("Reason: "+status.getReason());
        assertEquals(PollStatus.SERVICE_UNAVAILABLE, status.getStatusCode());
        assertNotNull(status.getReason());
         */
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) ServiceMonitor(org.opennms.netmgt.poller.ServiceMonitor) PollStatus(org.opennms.netmgt.poller.PollStatus) MockMonitoredService(org.opennms.netmgt.poller.mock.MockMonitoredService) MonitoredService(org.opennms.netmgt.poller.MonitoredService) Parameter(org.opennms.netmgt.config.poller.Parameter) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Aggregations

Parameter (org.opennms.netmgt.config.poller.Parameter)13 Test (org.junit.Test)7 PollStatus (org.opennms.netmgt.poller.PollStatus)6 ServiceMonitor (org.opennms.netmgt.poller.ServiceMonitor)6 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)5 MonitoredService (org.opennms.netmgt.poller.MonitoredService)5 Service (org.opennms.netmgt.config.poller.Service)4 InetAddress (java.net.InetAddress)2 HashMap (java.util.HashMap)2 JUnitHttpServer (org.opennms.core.test.http.annotations.JUnitHttpServer)2 PageSequence (org.opennms.netmgt.config.pagesequence.PageSequence)2 MockMonitoredService (org.opennms.netmgt.poller.mock.MockMonitoredService)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 PollerConfig (org.opennms.netmgt.config.PollerConfig)1 Page (org.opennms.netmgt.config.pagesequence.Page)1 Package (org.opennms.netmgt.config.poller.Package)1