use of org.opennms.netmgt.snmp.AggregateTracker in project opennms by OpenNMS.
the class AggregateTrackerProxyTest method setUp.
@Before
public void setUp() {
// Create a hierarchy of aggregated trackers
columnTrackers = new ColumnTracker[] { new ColumnTracker(baseOids[0]), new ColumnTracker(baseOids[1]), new ColumnTracker(baseOids[2]) };
singleInstanceTracker = new SingleInstanceTracker(baseOids[3], SnmpInstId.INST_ZERO);
childAggregateTracker = new AggregateTracker(columnTrackers);
parentAggregateTracker = new AggregateTracker(Lists.newArrayList(childAggregateTracker, singleInstanceTracker), gatherer);
// Verify the generated requests
List<WalkRequest> expectedRequests = new ArrayList<>();
// Column tracker requests
WalkRequest expectedRequest = new WalkRequest(baseOids[0]);
expectedRequest.setCorrelationId("0-0");
expectedRequest.setMaxRepetitions(2);
expectedRequests.add(expectedRequest);
expectedRequest = new WalkRequest(baseOids[1]);
expectedRequest.setCorrelationId("0-1");
expectedRequest.setMaxRepetitions(2);
expectedRequests.add(expectedRequest);
expectedRequest = new WalkRequest(baseOids[2]);
expectedRequest.setCorrelationId("0-2");
expectedRequest.setMaxRepetitions(2);
expectedRequests.add(expectedRequest);
// Single instance tracker request
expectedRequest = new WalkRequest(baseOids[3]);
expectedRequest.setCorrelationId("1");
expectedRequest.setInstance(SnmpInstId.INST_ZERO);
expectedRequest.setMaxRepetitions(1);
expectedRequests.add(expectedRequest);
assertThat(parentAggregateTracker.getWalkRequests(), hasSize(4));
assertThat(parentAggregateTracker.getWalkRequests(), contains(expectedRequests.toArray()));
// We shouldn't be finished yet
assertThat(parentAggregateTracker.isFinished(), equalTo(false));
assertThat(gatherer.getResults(), hasSize(0));
}
use of org.opennms.netmgt.snmp.AggregateTracker in project opennms by OpenNMS.
the class SnmpProxyRpcModule method walk.
private CompletableFuture<Collection<SnmpResponseDTO>> walk(SnmpRequestDTO request, List<SnmpWalkRequestDTO> walks) {
final CompletableFuture<Collection<SnmpResponseDTO>> future = new CompletableFuture<>();
final Map<String, SnmpResponseDTO> responsesByCorrelationId = new LinkedHashMap<>();
final List<Collectable> trackers = new ArrayList<>(walks.size());
for (final SnmpWalkRequestDTO walk : walks) {
CollectionTracker tracker;
if (walk.getInstance() != null) {
if (walk.getOids().size() != 1) {
future.completeExceptionally(new IllegalArgumentException("Single instance requests must have a single OID."));
return future;
}
final SnmpObjId oid = walk.getOids().get(0);
tracker = new SingleInstanceTracker(oid, new SnmpInstId(walk.getInstance())) {
@Override
protected void storeResult(SnmpResult res) {
addResult(res, walk.getCorrelationId(), responsesByCorrelationId);
}
};
} else {
final Collection<Collectable> columnTrackers = walk.getOids().stream().map(oid -> SnmpObjId.get(oid)).map(objId -> new ColumnTracker(objId)).collect(Collectors.toList());
tracker = new AggregateTracker(columnTrackers) {
@Override
protected void storeResult(SnmpResult res) {
addResult(res, walk.getCorrelationId(), responsesByCorrelationId);
}
};
}
if (walk.getMaxRepetitions() != null) {
tracker.setMaxRepetitions(walk.getMaxRepetitions());
}
trackers.add(tracker);
}
AggregateTracker aggregate = new AggregateTracker(trackers);
final SnmpWalker walker = SnmpUtils.createWalker(request.getAgent(), request.getDescription(), aggregate);
walker.setCallback(new SnmpWalkCallback() {
@Override
public void complete(SnmpWalker tracker, Throwable t) {
try {
if (t != null) {
future.completeExceptionally(t);
} else {
future.complete(responsesByCorrelationId.values());
}
} finally {
// Close the tracker using a separate thread
// This allows the SnmpWalker to clean up properly instead
// of interrupting execution as it's executing the callback
REAPER_EXECUTOR.submit(new Runnable() {
@Override
public void run() {
tracker.close();
}
});
}
}
});
walker.start();
return future;
}
use of org.opennms.netmgt.snmp.AggregateTracker in project opennms by OpenNMS.
the class SnmpProtocolCollector method collect.
@Override
public CollectionJob collect(final CollectionJob collectionJob) {
LOG.info("SnmpProtocolCollector is collecting collectionJob '{}'", collectionJob.getId());
SnmpAgentConfig snmpAgentConfig = SnmpAgentConfig.parseProtocolConfigurationString(collectionJob.getProtocolConfiguration());
List<Collectable> trackers = new ArrayList<Collectable>();
for (final String metricObjId : collectionJob.getAllMetrics()) {
SnmpObjId requestOid = SnmpObjId.get(metricObjId);
SnmpObjId base = requestOid.getPrefix(requestOid.length() - 1);
int lastId = requestOid.getLastSubId();
SingleInstanceTracker instanceTracker = new SingleInstanceTracker(base, new SnmpInstId(lastId)) {
@Override
protected void storeResult(SnmpResult result) {
LOG.trace("Collected SnmpValue '{}'", result);
SnmpValue value = result.getValue();
String metricType = value == null ? "unknown" : typeToString(value.getType());
collectionJob.setMetricValue(metricObjId, metricType, value == null ? null : value.toDisplayString());
}
@Override
public void setFailed(boolean failed) {
super.setFailed(failed);
LOG.trace("Collection Failed for metricObjId '{}'", metricObjId);
collectionJob.setMetricValue(metricObjId, "unknown", null);
}
@Override
public void setTimedOut(boolean timedOut) {
super.setTimedOut(timedOut);
LOG.trace("Collection timedOut for metricObjId '{}'", metricObjId);
collectionJob.setMetricValue(metricObjId, "unknown", null);
}
};
trackers.add(instanceTracker);
}
// Attempt to determine the location name
String locationName = null;
OnmsNode node = m_nodeDao.get(collectionJob.getNodeId());
if (node != null) {
OnmsMonitoringLocation monitoringLocation = node.getLocation();
if (monitoringLocation != null) {
locationName = monitoringLocation.getLocationName();
}
}
AggregateTracker tracker = new AggregateTracker(trackers);
CompletableFuture<AggregateTracker> future = m_locationAwareSnmpClient.walk(snmpAgentConfig, tracker).withDescription("NRTG").withLocation(locationName).execute();
try {
future.get();
} catch (ExecutionException e) {
LOG.warn("Failed to collect SNMP metrics for {}.", snmpAgentConfig.getAddress(), e);
} catch (InterruptedException e) {
LOG.warn("Interupted while collectiong SNMP metrics for {}.", snmpAgentConfig.getAddress());
Thread.interrupted();
}
return collectionJob;
}
use of org.opennms.netmgt.snmp.AggregateTracker in project opennms by OpenNMS.
the class ScanManager method updateSnmpData.
void updateSnmpData(final OnmsNode node) {
try {
m_systemGroup = new SystemGroup(m_address);
final Set<SnmpInstId> ipAddrs = new TreeSet<SnmpInstId>();
final Set<InetAddress> ipAddresses = new HashSet<InetAddress>();
for (final OnmsIpInterface iface : node.getIpInterfaces()) {
final InetAddress addr = iface.getIpAddress();
if (addr != null && addr instanceof Inet4Address) {
ipAddrs.add(new SnmpInstId(InetAddressUtils.toOid(addr)));
}
ipAddresses.add(addr);
}
m_ipAddrTable = new IpAddrTable(m_address, ipAddrs);
m_ipAddressTable = IpAddressTable.createTable(m_address, ipAddresses);
AggregateTracker tracker = new AggregateTracker(Lists.newArrayList(m_systemGroup, m_ipAddrTable, m_ipAddressTable));
final SnmpAgentConfig agentConfig = SnmpPeerFactory.getInstance().getAgentConfig(m_address, MonitoringLocationUtils.getLocationNameOrNullIfDefault(node));
try {
m_locationAwareSnmpClient.walk(agentConfig, tracker).withDescription("system/ipAddrTable/ipAddressTable").withLocation(node.getLocation() == null ? null : node.getLocation().getLocationName()).execute().get();
} catch (ExecutionException e) {
// pass
}
final Set<SnmpInstId> ifIndices = new TreeSet<SnmpInstId>();
for (final Integer ifIndex : m_ipAddrTable.getIfIndices()) {
ifIndices.add(new SnmpInstId(ifIndex));
}
m_ifTable = new IfTable(m_address, ifIndices);
m_ifXTable = new IfXTable(m_address, ifIndices);
tracker = new AggregateTracker(Lists.newArrayList(m_systemGroup, m_ifTable, m_ifXTable));
try {
m_locationAwareSnmpClient.walk(agentConfig, tracker).withDescription("ifTable/ifXTable").withLocation(node.getLocation() == null ? null : node.getLocation().getLocationName()).execute().get();
} catch (ExecutionException e) {
// pass
}
m_systemGroup.updateSnmpDataForNode(node);
for (final SnmpInstId ifIndex : ifIndices) {
m_ifTable.updateSnmpInterfaceData(node, ifIndex.toInt());
}
for (final SnmpInstId ifIndex : ifIndices) {
m_ifXTable.updateSnmpInterfaceData(node, ifIndex.toInt());
}
for (final SnmpInstId ipAddr : ipAddrs) {
m_ipAddrTable.updateIpInterfaceData(node, ipAddr.toString());
}
for (final InetAddress addr : ipAddresses) {
m_ipAddressTable.updateIpInterfaceData(node, InetAddressUtils.str(addr));
}
} catch (final InterruptedException e) {
LOG.info("thread interrupted while updating SNMP data", e);
Thread.currentThread().interrupt();
}
}
use of org.opennms.netmgt.snmp.AggregateTracker in project opennms by OpenNMS.
the class TcaProtocolCollector method collect.
@Override
public CollectionJob collect(final CollectionJob collectionJob) {
logger.info("TcaProtocolCollector is collecting collectionJob '{}'", collectionJob);
SnmpAgentConfig snmpAgentConfig = SnmpAgentConfig.parseProtocolConfigurationString(collectionJob.getProtocolConfiguration());
List<Collectable> trackers = new ArrayList<Collectable>();
for (final String metricObjId : collectionJob.getAllMetrics()) {
final String keyword = metricObjId.substring(metricObjId.lastIndexOf("_") + 1);
final SnmpObjId requestOid = SnmpObjId.get(metricObjId.substring(0, metricObjId.lastIndexOf("_")));
SnmpObjId base = requestOid.getPrefix(requestOid.length() - 1);
int lastId = requestOid.getLastSubId();
SingleInstanceTracker instanceTracker = new SingleInstanceTracker(base, new SnmpInstId(lastId)) {
@Override
protected void storeResult(SnmpResult result) {
logger.trace("Collected SnmpValue '{}'", result);
SnmpValue value = result.getValue();
String compositeResult = getCompositeValue(keyword, value.toDisplayString());
collectionJob.setMetricValue(metricObjId, "int32", compositeResult);
}
@Override
public void setFailed(boolean failed) {
super.setFailed(failed);
logger.trace("Collection Failed for metricObjId '{}'", metricObjId);
collectionJob.setMetricValue(metricObjId, "unknown", null);
}
@Override
public void setTimedOut(boolean timedOut) {
super.setTimedOut(timedOut);
logger.trace("Collection timedOut for metricObjId '{}'", metricObjId);
collectionJob.setMetricValue(metricObjId, "unknown", null);
}
};
trackers.add(instanceTracker);
}
CollectionTracker tracker = new AggregateTracker(trackers);
try (SnmpWalker walker = m_snmpStrategy.createWalker(snmpAgentConfig, "SnmpProtocolCollector for " + snmpAgentConfig.getAddress(), tracker)) {
walker.start();
try {
walker.waitFor();
} catch (InterruptedException e) {
logger.error("Interuppted while waiting for collector. Results may be incomplete.", e);
}
}
return collectionJob;
}
Aggregations