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;
}
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;
}
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;
}
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;
}
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());
}
Aggregations