@Test public void testPoll() throws Exception { MockMonitoredService dual = new MockMonitoredService(1, "wipv6day.opennms.org", InetAddress.getLocalHost(), "RESOLVE"); MockMonitoredService v4only = new MockMonitoredService(1, "choopa-ipv4.opennms.org", InetAddress.getLocalHost(), "RESOLVE"); MockMonitoredService v6only = new MockMonitoredService(1, "choopa-ipv6.opennms.org", InetAddress.getLocalHost(), "RESOLVE"); MockMonitoredService neither = new MockMonitoredService(1, "no-such-name.example.com", InetAddress.getLocalHost(), "RESOLVE"); DNSResolutionMonitor monitor = new DNSResolutionMonitor(); monitor.initialize(Collections.<String, Object>emptyMap()); monitor.initialize(dual); monitor.initialize(v4only); monitor.initialize(v6only); monitor.initialize(neither); Map<String, Object> v4Parms = Collections.<String, Object>singletonMap(RESOLUTION_TYPE_PARM, RT_V4); Map<String, Object> v6Parms = Collections.<String, Object>singletonMap(RESOLUTION_TYPE_PARM, RT_V6); Map<String, Object> bothParms = Collections.<String, Object>singletonMap(RESOLUTION_TYPE_PARM, RT_BOTH); Map<String, Object> eitherParms = Collections.<String, Object>singletonMap(RESOLUTION_TYPE_PARM, RT_EITHER); assertEquals(PollStatus.available(), monitor.poll(dual, v4Parms)); assertEquals(PollStatus.available(), monitor.poll(dual, v6Parms)); assertEquals(PollStatus.available(), monitor.poll(dual, bothParms)); assertEquals(PollStatus.available(), monitor.poll(dual, eitherParms)); assertEquals(PollStatus.available(), monitor.poll(v4only, v4Parms)); assertEquals(PollStatus.unavailable(), monitor.poll(v4only, v6Parms)); assertEquals(PollStatus.unavailable(), monitor.poll(v4only, bothParms)); assertEquals(PollStatus.available(), monitor.poll(v4only, eitherParms)); assertEquals(PollStatus.unavailable(), monitor.poll(v6only, v4Parms)); assertEquals(PollStatus.available(), monitor.poll(v6only, v6Parms)); assertEquals(PollStatus.unavailable(), monitor.poll(v6only, bothParms)); assertEquals(PollStatus.available(), monitor.poll(v6only, eitherParms)); assertEquals(PollStatus.unavailable(), monitor.poll(neither, v4Parms)); assertEquals(PollStatus.unavailable(), monitor.poll(neither, v6Parms)); assertEquals(PollStatus.unavailable(), monitor.poll(neither, bothParms)); assertEquals(PollStatus.unavailable(), monitor.poll(neither, eitherParms)); monitor.release(dual); monitor.release(v4only); monitor.release(v6only); monitor.release(neither); monitor.release(); }
@Override public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { String nodeLabel = svc.getNodeLabel(); String resolutionType = ParameterMap.getKeyedString(parameters, RESOLUTION_TYPE_PARM, RESOLUTION_TYPE_DEFAULT); boolean requireV4 = RT_V4.equalsIgnoreCase(resolutionType) || RT_BOTH.equals(resolutionType); boolean requireV6 = RT_V6.equalsIgnoreCase(resolutionType) || RT_BOTH.equals(resolutionType); try { long start = System.currentTimeMillis(); InetAddress[] addrs = resolve(nodeLabel); long end = System.currentTimeMillis(); boolean v4found = false; boolean v6found = false; for(InetAddress addr : addrs) { log().debug("Resolved " + nodeLabel + " to " + addr); if (addr instanceof Inet4Address) { v4found = true; } else if(addr instanceof Inet6Address) { v6found = true; } } if (!v4found && !v6found) { return logDown(Level.INFO, "Unable to resolve " + nodeLabel); } if (requireV4 && !v4found) { return logDown(Level.INFO, nodeLabel + " could only be resolved to an IPv6 address"); } if (requireV6 && !v6found) { return logDown(Level.INFO, nodeLabel + " could only be resolved to an IPv4 address"); } return logUp(Level.INFO, (double)(end - start), "Resolved " + nodeLabel + " correctly!"); } catch (TextParseException e) { return logDown(Level.INFO,"Unable to resolve "+nodeLabel+": "+e.getMessage()); } }
DNSResolutionMonitor extends AbstractServiceMonitor { @Override public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { String nodeLabel = svc.getNodeLabel(); String resolutionType = ParameterMap.getKeyedString(parameters, RESOLUTION_TYPE_PARM, RESOLUTION_TYPE_DEFAULT); boolean requireV4 = RT_V4.equalsIgnoreCase(resolutionType) || RT_BOTH.equals(resolutionType); boolean requireV6 = RT_V6.equalsIgnoreCase(resolutionType) || RT_BOTH.equals(resolutionType); try { long start = System.currentTimeMillis(); InetAddress[] addrs = resolve(nodeLabel); long end = System.currentTimeMillis(); boolean v4found = false; boolean v6found = false; for(InetAddress addr : addrs) { log().debug("Resolved " + nodeLabel + " to " + addr); if (addr instanceof Inet4Address) { v4found = true; } else if(addr instanceof Inet6Address) { v6found = true; } } if (!v4found && !v6found) { return logDown(Level.INFO, "Unable to resolve " + nodeLabel); } if (requireV4 && !v4found) { return logDown(Level.INFO, nodeLabel + " could only be resolved to an IPv6 address"); } if (requireV6 && !v6found) { return logDown(Level.INFO, nodeLabel + " could only be resolved to an IPv4 address"); } return logUp(Level.INFO, (double)(end - start), "Resolved " + nodeLabel + " correctly!"); } catch (TextParseException e) { return logDown(Level.INFO,"Unable to resolve "+nodeLabel+": "+e.getMessage()); } } }
DNSResolutionMonitor extends AbstractServiceMonitor { @Override public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { String nodeLabel = svc.getNodeLabel(); String resolutionType = ParameterMap.getKeyedString(parameters, RESOLUTION_TYPE_PARM, RESOLUTION_TYPE_DEFAULT); boolean requireV4 = RT_V4.equalsIgnoreCase(resolutionType) || RT_BOTH.equals(resolutionType); boolean requireV6 = RT_V6.equalsIgnoreCase(resolutionType) || RT_BOTH.equals(resolutionType); try { long start = System.currentTimeMillis(); InetAddress[] addrs = resolve(nodeLabel); long end = System.currentTimeMillis(); boolean v4found = false; boolean v6found = false; for(InetAddress addr : addrs) { log().debug("Resolved " + nodeLabel + " to " + addr); if (addr instanceof Inet4Address) { v4found = true; } else if(addr instanceof Inet6Address) { v6found = true; } } if (!v4found && !v6found) { return logDown(Level.INFO, "Unable to resolve " + nodeLabel); } if (requireV4 && !v4found) { return logDown(Level.INFO, nodeLabel + " could only be resolved to an IPv6 address"); } if (requireV6 && !v6found) { return logDown(Level.INFO, nodeLabel + " could only be resolved to an IPv4 address"); } return logUp(Level.INFO, (double)(end - start), "Resolved " + nodeLabel + " correctly!"); } catch (TextParseException e) { return logDown(Level.INFO,"Unable to resolve "+nodeLabel+": "+e.getMessage()); } }  }
DNSResolutionMonitor extends AbstractServiceMonitor { @Override public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { String nodeLabel = svc.getNodeLabel(); String resolutionType = ParameterMap.getKeyedString(parameters, RESOLUTION_TYPE_PARM, RESOLUTION_TYPE_DEFAULT); boolean requireV4 = RT_V4.equalsIgnoreCase(resolutionType) || RT_BOTH.equals(resolutionType); boolean requireV6 = RT_V6.equalsIgnoreCase(resolutionType) || RT_BOTH.equals(resolutionType); try { long start = System.currentTimeMillis(); InetAddress[] addrs = resolve(nodeLabel); long end = System.currentTimeMillis(); boolean v4found = false; boolean v6found = false; for(InetAddress addr : addrs) { log().debug("Resolved " + nodeLabel + " to " + addr); if (addr instanceof Inet4Address) { v4found = true; } else if(addr instanceof Inet6Address) { v6found = true; } } if (!v4found && !v6found) { return logDown(Level.INFO, "Unable to resolve " + nodeLabel); } if (requireV4 && !v4found) { return logDown(Level.INFO, nodeLabel + " could only be resolved to an IPv6 address"); } if (requireV6 && !v6found) { return logDown(Level.INFO, nodeLabel + " could only be resolved to an IPv4 address"); } return logUp(Level.INFO, (double)(end - start), "Resolved " + nodeLabel + " correctly!"); } catch (TextParseException e) { return logDown(Level.INFO,"Unable to resolve "+nodeLabel+": "+e.getMessage()); } }  @Override PollStatus poll(MonitoredService svc, Map<String, Object> parameters); }
DNSResolutionMonitor extends AbstractServiceMonitor { @Override public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { String nodeLabel = svc.getNodeLabel(); String resolutionType = ParameterMap.getKeyedString(parameters, RESOLUTION_TYPE_PARM, RESOLUTION_TYPE_DEFAULT); boolean requireV4 = RT_V4.equalsIgnoreCase(resolutionType) || RT_BOTH.equals(resolutionType); boolean requireV6 = RT_V6.equalsIgnoreCase(resolutionType) || RT_BOTH.equals(resolutionType); try { long start = System.currentTimeMillis(); InetAddress[] addrs = resolve(nodeLabel); long end = System.currentTimeMillis(); boolean v4found = false; boolean v6found = false; for(InetAddress addr : addrs) { log().debug("Resolved " + nodeLabel + " to " + addr); if (addr instanceof Inet4Address) { v4found = true; } else if(addr instanceof Inet6Address) { v6found = true; } } if (!v4found && !v6found) { return logDown(Level.INFO, "Unable to resolve " + nodeLabel); } if (requireV4 && !v4found) { return logDown(Level.INFO, nodeLabel + " could only be resolved to an IPv6 address"); } if (requireV6 && !v6found) { return logDown(Level.INFO, nodeLabel + " could only be resolved to an IPv4 address"); } return logUp(Level.INFO, (double)(end - start), "Resolved " + nodeLabel + " correctly!"); } catch (TextParseException e) { return logDown(Level.INFO,"Unable to resolve "+nodeLabel+": "+e.getMessage()); } }  @Override PollStatus poll(MonitoredService svc, Map<String, Object> parameters); static final String RESOLUTION_TYPE_PARM; static final String RT_V4; static final String RT_V6; static final String RT_BOTH; static final String RT_EITHER; static final String RESOLUTION_TYPE_DEFAULT; }