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