Search in sources :

Example 1 with SimpleMonitoredService

use of org.opennms.netmgt.poller.support.SimpleMonitoredService 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 SimpleMonitoredService

use of org.opennms.netmgt.poller.support.SimpleMonitoredService in project opennms by OpenNMS.

the class Test method execute.

@Override
public Object execute() 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 3 with SimpleMonitoredService

use of org.opennms.netmgt.poller.support.SimpleMonitoredService in project opennms by OpenNMS.

the class Poll method doExecute.

@Override
protected Object doExecute() throws Exception {
    final CompletableFuture<PollerResponse> future = locationAwarePollerClient.poll().withService(new SimpleMonitoredService(InetAddress.getByName(host), "SVC", location)).withMonitorClassName(className).withTimeToLive(ttlInMs).withAttributes(parse(attributes)).execute();
    while (true) {
        try {
            try {
                PollStatus pollStatus = future.get(1, TimeUnit.SECONDS).getPollStatus();
                if (pollStatus.getStatusCode() == PollStatus.SERVICE_AVAILABLE) {
                    System.out.printf("\nService is %s on %s using %s:\n", pollStatus.getStatusName(), host, className);
                    final Map<String, Number> properties = pollStatus.getProperties();
                    if (properties.size() > 0) {
                        properties.entrySet().stream().forEach(e -> {
                            System.out.printf("\t%s: %.4f\n", e.getKey(), e.getValue());
                        });
                    } else {
                        System.out.printf("(No properties were returned by the monitor.\n");
                    }
                } else {
                    System.out.printf("\nService is %s on %s using %s\n", pollStatus.getStatusName(), host, className);
                    System.out.printf("\tReason: %s\n", pollStatus.getReason());
                }
            } catch (InterruptedException e) {
                System.out.println("\nInterrupted.");
            } catch (ExecutionException e) {
                System.out.printf("\nPoll failed with: %s\n", e);
            }
            break;
        } catch (TimeoutException e) {
        // pass
        }
        System.out.print(".");
        System.out.flush();
    }
    return null;
}
Also used : PollStatus(org.opennms.netmgt.poller.PollStatus) SimpleMonitoredService(org.opennms.netmgt.poller.support.SimpleMonitoredService) ExecutionException(java.util.concurrent.ExecutionException) PollerResponse(org.opennms.netmgt.poller.PollerResponse) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with SimpleMonitoredService

use of org.opennms.netmgt.poller.support.SimpleMonitoredService in project opennms by OpenNMS.

the class Poll method execute.

@Override
public Object execute() throws Exception {
    final CompletableFuture<PollerResponse> future = locationAwarePollerClient.poll().withService(new SimpleMonitoredService(InetAddress.getByName(host), "SVC", location)).withSystemId(systemId).withMonitorClassName(className).withTimeToLive(ttlInMs).withAttributes(parse(attributes)).execute();
    while (true) {
        try {
            try {
                PollStatus pollStatus = future.get(1, TimeUnit.SECONDS).getPollStatus();
                if (pollStatus.getStatusCode() == PollStatus.SERVICE_AVAILABLE) {
                    System.out.printf("\nService is %s on %s using %s:\n", pollStatus.getStatusName(), host, className);
                    final Map<String, Number> properties = pollStatus.getProperties();
                    if (properties.size() > 0) {
                        properties.entrySet().stream().forEach(e -> {
                            System.out.printf("\t%s: %.4f\n", e.getKey(), e.getValue() != null ? e.getValue().doubleValue() : null);
                        });
                    } else {
                        System.out.printf("(No properties were returned by the monitor.\n");
                    }
                } else {
                    System.out.printf("\nService is %s on %s using %s\n", pollStatus.getStatusName(), host, className);
                    System.out.printf("\tReason: %s\n", pollStatus.getReason());
                }
            } catch (InterruptedException e) {
                System.out.println("\nInterrupted.");
            } catch (ExecutionException e) {
                System.out.printf("\nPoll failed with: %s\n", e);
            }
            break;
        } catch (TimeoutException e) {
        // pass
        }
        System.out.print(".");
        System.out.flush();
    }
    return null;
}
Also used : PollStatus(org.opennms.netmgt.poller.PollStatus) SimpleMonitoredService(org.opennms.netmgt.poller.support.SimpleMonitoredService) ExecutionException(java.util.concurrent.ExecutionException) PollerResponse(org.opennms.netmgt.poller.PollerResponse) TimeoutException(java.util.concurrent.TimeoutException)

Example 5 with SimpleMonitoredService

use of org.opennms.netmgt.poller.support.SimpleMonitoredService in project opennms by OpenNMS.

the class AvailabilityMonitorTest method testPoll.

/**
 * Test method for {@link org.opennms.netmgt.poller.support.AvailabilityMonitor#poll(org.opennms.netmgt.poller.MonitoredService, Map)}.
 */
@Test
public final void testPoll() {
    ServiceMonitor sm = new AvailabilityMonitor();
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("timeout", "3000");
    MonitoredService svc = new SimpleMonitoredService(InetAddressUtils.addr("127.0.0.1"), "ICMP");
    PollStatus status = sm.poll(svc, parameters);
    assertEquals(PollStatus.SERVICE_AVAILABLE, status.getStatusCode());
}
Also used : ServiceMonitor(org.opennms.netmgt.poller.ServiceMonitor) PollStatus(org.opennms.netmgt.poller.PollStatus) HashMap(java.util.HashMap) SimpleMonitoredService(org.opennms.netmgt.poller.support.SimpleMonitoredService) MonitoredService(org.opennms.netmgt.poller.MonitoredService) SimpleMonitoredService(org.opennms.netmgt.poller.support.SimpleMonitoredService) Test(org.junit.Test)

Aggregations

PollStatus (org.opennms.netmgt.poller.PollStatus)5 SimpleMonitoredService (org.opennms.netmgt.poller.support.SimpleMonitoredService)5 MonitoredService (org.opennms.netmgt.poller.MonitoredService)3 ServiceMonitor (org.opennms.netmgt.poller.ServiceMonitor)3 InetAddress (java.net.InetAddress)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 PollerConfig (org.opennms.netmgt.config.PollerConfig)2 Parameter (org.opennms.netmgt.config.poller.Parameter)2 Service (org.opennms.netmgt.config.poller.Service)2 OnmsNode (org.opennms.netmgt.model.OnmsNode)2 PollerResponse (org.opennms.netmgt.poller.PollerResponse)2 TransactionStatus (org.springframework.transaction.TransactionStatus)2 HashMap (java.util.HashMap)1 Test (org.junit.Test)1