use of org.opennms.netmgt.config.poller.IncludeRange in project opennms by OpenNMS.
the class PollerConfigFactoryIT method testPollerConfigFactory.
public void testPollerConfigFactory() throws Exception {
TestPollerConfigManager factory = new TestPollerConfigManager(POLLER_CONFIG, "localhost", false);
assertNull(factory.getPackage("TestPkg"));
Package pkg = new Package();
pkg.setName("TestPkg");
Filter filter = new Filter();
filter.setContent("IPADDR IPLIKE *.*.*.*");
pkg.setFilter(filter);
Rrd rrd = new Rrd();
rrd.setStep(300);
rrd.addRra("RRA:AVERAGE:0.5:1:2016");
pkg.setRrd(rrd);
Service svc = new Service();
svc.setName("TestService");
svc.setInterval(300000l);
pkg.addService(svc);
Downtime dt = new Downtime();
dt.setBegin(0l);
pkg.addDowntime(dt);
IncludeRange inclde = new IncludeRange();
inclde.setBegin("192.169.0.0");
inclde.setEnd("192.169.255.255");
pkg.addIncludeRange(inclde);
factory.addPackage(pkg);
factory.save();
assertNotNull(factory.getPackage("TestPkg"));
TestPollerConfigManager newFactory = new TestPollerConfigManager(factory.getXml(), "localhost", false);
Package p = newFactory.getPackage("TestPkg");
assertNotNull("package for 'TestPkg'", p);
assertTrue("Expected 192.169.1.5 to be in the package", newFactory.isInterfaceInPackage("192.169.1.5", p));
assertFalse("Expected 192.168.1.5 to *not* be in the package", newFactory.isInterfaceInPackage("192.168.1.5", p));
}
use of org.opennms.netmgt.config.poller.IncludeRange in project opennms by OpenNMS.
the class PollerConfigManager method isInterfaceInPackage.
/**
* {@inheritDoc}
*
* This method is used to determine if the named interface is included in
* the passed package definition. If the interface belongs to the package
* then a value of true is returned. If the interface does not belong to the
* package a false value is returned.
*
* <strong>Note: </strong>Evaluation of the interface against a package
* filter will only work if the IP is already in the database.
*/
@Override
public boolean isInterfaceInPackage(final String iface, final Package pkg) {
boolean filterPassed = false;
final InetAddress ifaceAddr = addr(iface);
// get list of IPs in this package
final List<InetAddress> ipList = m_pkgIpMap.get().get(pkg);
if (ipList != null && ipList.size() > 0) {
filterPassed = ipList.contains(ifaceAddr);
}
LOG.debug("interfaceInPackage: Interface {} passed filter for package {}?: {}", iface, pkg.getName(), Boolean.valueOf(filterPassed));
if (!filterPassed)
return false;
//
// Ensure that the interface is in the specific list or
// that it is in the include range and is not excluded
//
boolean has_specific = false;
boolean has_range_include = false;
boolean has_range_exclude = false;
// if there are NO include ranges then treat act as if the user include
// the range of all valid addresses (0.0.0.0 - 255.255.255.255, ::1 - ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff)
has_range_include = pkg.getIncludeRanges().size() == 0 && pkg.getSpecifics().size() == 0 && pkg.getIncludeUrls().size() == 0;
final byte[] addr = toIpAddrBytes(iface);
for (final IncludeRange rng : pkg.getIncludeRanges()) {
int comparison = new ByteArrayComparator().compare(addr, toIpAddrBytes(rng.getBegin()));
if (comparison > 0) {
int endComparison = new ByteArrayComparator().compare(addr, toIpAddrBytes(rng.getEnd()));
if (endComparison <= 0) {
has_range_include = true;
break;
}
} else if (comparison == 0) {
has_range_include = true;
break;
}
}
for (final String spec : pkg.getSpecifics()) {
if (new ByteArrayComparator().compare(addr, toIpAddrBytes(spec)) == 0) {
has_specific = true;
LOG.debug("interfaceInPackage: Interface {} defined as 'specific'", iface);
break;
}
}
for (final String includeUrl : pkg.getIncludeUrls()) {
if (interfaceInUrl(iface, includeUrl)) {
has_specific = true;
LOG.debug("interfaceInPackage: Interface {} exist on {}", iface, includeUrl);
break;
}
}
if (!has_specific) {
for (final ExcludeRange rng : pkg.getExcludeRanges()) {
int comparison = new ByteArrayComparator().compare(addr, toIpAddrBytes(rng.getBegin()));
if (comparison > 0) {
int endComparison = new ByteArrayComparator().compare(addr, toIpAddrBytes(rng.getEnd()));
if (endComparison <= 0) {
LOG.debug("interfaceInPackage: Interface {} matches an exclude range", iface);
has_range_exclude = true;
break;
}
} else if (comparison == 0) {
LOG.debug("interfaceInPackage: Interface {} matches an exclude range", iface);
has_range_exclude = true;
break;
}
}
}
return has_specific || (has_range_include && !has_range_exclude);
}
Aggregations