use of org.opennms.netmgt.snmp.SnmpInstId in project opennms by OpenNMS.
the class RrdPersistOperationBuilderTest method testCommitWithDeclaredAttributeAndValue.
@Test
public void testCommitWithDeclaredAttributeAndValue() throws Exception {
File nodeDir = m_fileAnticipator.expecting(getSnmpRrdDirectory(), m_node.getId().toString());
m_fileAnticipator.expecting(nodeDir, "rrdName" + m_rrdStrategy.getDefaultFileExtension());
m_fileAnticipator.expecting(nodeDir, "rrdName" + ".meta");
RrdRepository repository = createRrdRepository();
SnmpCollectionAgent agent = getCollectionAgent();
MockDataCollectionConfig dataCollectionConfig = new MockDataCollectionConfig();
OnmsSnmpCollection collection = new OnmsSnmpCollection(agent, new ServiceParameters(new HashMap<String, Object>()), dataCollectionConfig, m_locationAwareSnmpClient);
NodeResourceType resourceType = new NodeResourceType(agent, collection);
CollectionResource resource = new NodeInfo(resourceType, agent);
MibObject mibObject = new MibObject();
mibObject.setOid(".1.1.1.1");
mibObject.setAlias("mibObjectAlias");
mibObject.setType("counter");
mibObject.setInstance("0");
mibObject.setMaxval(null);
mibObject.setMinval(null);
SnmpCollectionSet collectionSet = new SnmpCollectionSet(agent, collection, m_locationAwareSnmpClient);
SnmpAttributeType attributeType = new NumericAttributeType(resourceType, "some-collection", mibObject, new AttributeGroupType("mibGroup", AttributeGroupType.IF_TYPE_IGNORE));
attributeType.storeResult(collectionSet, null, new SnmpResult(mibObject.getSnmpObjId(), new SnmpInstId(mibObject.getInstance()), SnmpUtils.getValueFactory().getOctetString("hello".getBytes())));
RrdPersistOperationBuilder builder = new RrdPersistOperationBuilder(m_rrdStrategy, repository, resource, "rrdName", false);
builder.declareAttribute(attributeType);
builder.setAttributeValue(attributeType, 6.022E23d);
builder.commit();
}
use of org.opennms.netmgt.snmp.SnmpInstId in project opennms by OpenNMS.
the class SnmpStoreTest method testStoreResultWithValueThenEndOfMibView.
public void testStoreResultWithValueThenEndOfMibView() {
String baseOid = ".1.3.6.1.2.1.31.1.1.1.18";
String ifAliasName = "ifAlias";
String ifAliasValue = "Foo!";
SnmpStore store = new SnmpStore(new NamedSnmpVar[] { new NamedSnmpVar(NamedSnmpVar.SNMPOCTETSTRING, ifAliasName, baseOid, 18) });
SnmpObjId base = SnmpObjId.get(baseOid);
SnmpInstId inst = new SnmpInstId("1");
store.storeResult(new SnmpResult(base, inst, SnmpUtils.getValueFactory().getOctetString(ifAliasValue.getBytes())));
store.storeResult(new SnmpResult(base, inst, SnmpUtils.getValueFactory().getValue(SnmpValue.SNMP_END_OF_MIB, null)));
assertEquals("ifAlias value", ifAliasValue, store.getDisplayString(ifAliasName));
}
use of org.opennms.netmgt.snmp.SnmpInstId in project opennms by OpenNMS.
the class CollectionResourceWrapperIT method testNumericFields.
@Test
public void testNumericFields() throws Exception {
SnmpCollectionAgent agent = createCollectionAgent();
MockDataCollectionConfig dataCollectionConfig = new MockDataCollectionConfig();
OnmsSnmpCollection collection = new OnmsSnmpCollection(agent, new ServiceParameters(new HashMap<String, Object>()), dataCollectionConfig, m_locationAwareSnmpClient);
ResourceType rt = new ResourceType();
rt.setName("dskIndex");
rt.setLabel("Disk Table Index (UCD-SNMP MIB)");
StorageStrategy strategy = new StorageStrategy();
strategy.setClazz("org.opennms.netmgt.dao.support.SiblingColumnStorageStrategy");
strategy.addParameter(new Parameter("sibling-column-name", "ns-dskPath"));
strategy.addParameter(new Parameter("replace-first", "s/^-$/_root_fs/"));
strategy.addParameter(new Parameter("replace-all", "s/^-//"));
strategy.addParameter(new Parameter("replace-all", "s/\\s//"));
strategy.addParameter(new Parameter("replace-all", "s/:\\\\.*//"));
rt.setStorageStrategy(strategy);
PersistenceSelectorStrategy pstrategy = new PersistenceSelectorStrategy();
pstrategy.setClazz("org.opennms.netmgt.collection.support.PersistAllSelectorStrategy");
rt.setPersistenceSelectorStrategy(pstrategy);
GenericIndexResourceType resourceType = new GenericIndexResourceType(agent, collection, rt);
SnmpCollectionResource resource = new GenericIndexResource(resourceType, resourceType.getName(), new SnmpInstId(100));
SnmpAttribute total = addAttributeToCollectionResource(resource, "ns-dskTotal", AttributeType.GAUGE, "dskIndex", "10000");
SnmpAttribute used = addAttributeToCollectionResource(resource, "ns-dskUsed", AttributeType.GAUGE, "dskIndex", "5000");
SnmpAttribute label = addAttributeToCollectionResource(resource, "ns-dskPath", AttributeType.STRING, "dskIndex", "/opt");
Map<String, CollectionAttribute> attributes = new HashMap<String, CollectionAttribute>();
attributes.put(used.getName(), used);
attributes.put(total.getName(), total);
attributes.put(label.getName(), label);
CollectionResourceWrapper wrapper = createWrapper(resource, attributes);
Assert.assertEquals("opt", wrapper.getInstanceLabel());
Assert.assertEquals(new Double("10000.0"), wrapper.getAttributeValue(total.getName()));
Assert.assertEquals("10000.0", wrapper.getFieldValue(total.getName()));
}
use of org.opennms.netmgt.snmp.SnmpInstId in project opennms by OpenNMS.
the class TrapdIT method testSnmpV2cTrapWithAddressFromVarbind.
/**
* Verifies that we can pull the agent address from the snmpTrapAddress
* varbind in a SNMPv2 trap.
*/
@Test
public void testSnmpV2cTrapWithAddressFromVarbind() throws Exception {
// Enable the feature (disabled by default)
m_trapdConfig.getConfig().setUseAddressFromVarbind(true);
String localhost = "127.0.0.1";
InetAddress remoteAddr = InetAddress.getByName("10.255.1.1");
SnmpObjId enterpriseId = SnmpObjId.get(".1.3.6.1.4.1.5813");
SnmpObjId trapOID = SnmpObjId.get(enterpriseId, new SnmpInstId(1));
SnmpTrapBuilder pdu = SnmpUtils.getV2TrapBuilder();
pdu.addVarBind(SnmpObjId.get(".1.3.6.1.2.1.1.3.0"), SnmpUtils.getValueFactory().getTimeTicks(0));
pdu.addVarBind(SnmpObjId.get(".1.3.6.1.6.3.1.1.4.1.0"), SnmpUtils.getValueFactory().getObjectId(trapOID));
pdu.addVarBind(SnmpObjId.get(".1.3.6.1.6.3.1.1.4.3.0"), SnmpUtils.getValueFactory().getObjectId(enterpriseId));
// The varbind with the address
pdu.addVarBind(TrapUtils.SNMP_TRAP_ADDRESS_OID, SnmpUtils.getValueFactory().getIpAddress(InetAddress.getByName("10.255.1.1")));
EventBuilder defaultTrapBuilder = new EventBuilder("uei.opennms.org/default/trap", "trapd");
defaultTrapBuilder.setInterface(remoteAddr);
defaultTrapBuilder.setSnmpVersion("v2c");
m_mockEventIpcManager.getEventAnticipator().anticipateEvent(defaultTrapBuilder.getEvent());
EventBuilder newSuspectBuilder = new EventBuilder(EventConstants.NEW_SUSPECT_INTERFACE_EVENT_UEI, "trapd");
// The address in the newSuspect event should match the one specified in the varbind
newSuspectBuilder.setInterface(remoteAddr);
m_mockEventIpcManager.getEventAnticipator().anticipateEvent(newSuspectBuilder.getEvent());
pdu.send(localhost, m_trapdConfig.getSnmpTrapPort(), "public");
// Allow time for Trapd and Eventd to do their magic
Thread.sleep(5000);
}
use of org.opennms.netmgt.snmp.SnmpInstId 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(SnmpObjId::get).map(ColumnTracker::new).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;
}
Aggregations