use of org.opennms.netmgt.config.PollerConfig 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.config.PollerConfig in project opennms by OpenNMS.
the class MockNetworkTest method testPollerConfig.
public void testPollerConfig() {
m_pollerConfig.setNodeOutageProcessingEnabled(true);
m_pollerConfig.setPollInterval("HTTP", 750L);
m_pollerConfig.setPollerThreads(5);
m_pollerConfig.setCriticalService("YAHOO");
PollerConfig pollerConfig = m_pollerConfig;
// test the nodeOutageProcessing setting works
assertTrue(pollerConfig.isNodeOutageProcessingEnabled());
// test to ensure that the poller has packages
Enumeration<Package> pkgs = pollerConfig.enumeratePackage();
assertNotNull(pkgs);
int pkgCount = 0;
Package pkg = null;
while (pkgs.hasMoreElements()) {
pkg = (Package) pkgs.nextElement();
pkgCount++;
}
assertTrue(pkgCount > 0);
// ensure a sample interface is in the package
assertTrue(pollerConfig.isInterfaceInPackage("192.168.1.1", pkg));
for (final Service svc : pkg.getServices()) {
if ("ICMP".equals(svc.getName()))
assertEquals(Long.valueOf(500L), svc.getInterval());
else if ("HTTP".equals(svc.getName()))
assertEquals(Long.valueOf(750L), svc.getInterval());
else
assertEquals(Long.valueOf(1000L), svc.getInterval());
}
// ensure that setting the thread worked
assertEquals(5, pollerConfig.getThreads());
// ensure that setting the critical service worked
assertEquals("YAHOO", pollerConfig.getCriticalService());
// ensure that we have service monitors to the sevices
assertNotNull(pollerConfig.getServiceMonitor("SMTP"));
}
use of org.opennms.netmgt.config.PollerConfig in project opennms by OpenNMS.
the class PollableServiceConfigTest method returnsUnknownOnRequestTimedOutException.
/**
* Verifies that <b>PollStatus.unknown()</b> is returned when the
* {@link LocationAwarePollerClient} fails with a {@link RequestTimedOutException}.
*
* This can happen when no Minions at the given location are available to process
* the request, or the request was not completed in time, in which case we cannot
* ascertain that the service is UP or DOWN.
*/
@Test
public void returnsUnknownOnRequestTimedOutException() throws Exception {
// Create a future that fails with a RequestTimedOutException
CompletableFuture<PollerResponse> future = new CompletableFuture<>();
future.completeExceptionally(new RequestTimedOutException(new Exception("Test")));
// Now mock the client to always return the future we created above
LocationAwarePollerClient client = mock(LocationAwarePollerClient.class, Mockito.RETURNS_DEEP_STUBS);
Mockito.when(client.poll().withService(any()).withMonitor(any()).withTimeToLive(any()).withAttributes(any()).withAdaptor(any()).withAdaptor(any()).execute()).thenReturn(future);
// Mock all of the required objects required to successfully initialize the PollableServiceConfig
PollableService pollableSvc = mock(PollableService.class);
when(pollableSvc.getSvcName()).thenReturn("SVC");
Service configuredSvc = new Service();
configuredSvc.setName("SVC");
Package pkg = mock(Package.class);
when(pkg.getServices()).thenReturn(Lists.newArrayList(configuredSvc));
PollerConfig pollerConfig = mock(PollerConfig.class);
PollOutagesConfig pollOutagesConfig = mock(PollOutagesConfig.class);
Timer timer = mock(Timer.class);
PersisterFactory persisterFactory = mock(PersisterFactory.class);
ResourceStorageDao resourceStorageDao = mock(ResourceStorageDao.class);
final PollableServiceConfig psc = new PollableServiceConfig(pollableSvc, pollerConfig, pollOutagesConfig, pkg, timer, persisterFactory, resourceStorageDao, client);
// Trigger the poll
PollStatus pollStatus = psc.poll();
// Verify
assertThat(pollStatus.isUnknown(), is(true));
}
Aggregations