use of org.opennms.netmgt.collectd.SnmpCollectionAgent in project opennms by OpenNMS.
the class CollectorComplianceTest method canCollectUsingMinionWorkflow.
@Test
public void canCollectUsingMinionWorkflow() throws CollectionInitializationException, CollectionException {
Assume.assumeTrue(runsOnMinion);
// create the agent
OnmsNode node = mock(OnmsNode.class);
when(node.getId()).thenReturn(1);
OnmsIpInterface iface = mock(OnmsIpInterface.class);
when(iface.getNode()).thenReturn(node);
when(iface.getIpAddress()).thenReturn(InetAddrUtils.getLocalHostAddress());
IpInterfaceDao ifaceDao = mock(IpInterfaceDao.class);
when(ifaceDao.load(1)).thenReturn(iface);
PlatformTransactionManager transMgr = mock(PlatformTransactionManager.class);
final SnmpCollectionAgent agent = DefaultCollectionAgent.create(1, ifaceDao, transMgr);
// init() should execute without any exceptions
final ServiceCollector opennmsCollector = getCollector();
initialize(opennmsCollector);
// getEffectiveLocation() should return the original location
final String targetLocation = "!" + MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID;
assertEquals("Location cannot be altered.", targetLocation, opennmsCollector.getEffectiveLocation(targetLocation));
// getRuntimeAttributes() should return a valid map
final Map<String, Object> requiredParams = getRequiredParameters();
final Map<String, Object> runtimeAttrs = opennmsCollector.getRuntimeAttributes(agent, Collections.unmodifiableMap(requiredParams));
// marshalParameters() should marshal all parameters to strings
final Map<String, Object> allParms = new HashMap<>();
allParms.putAll(requiredParams);
allParms.putAll(runtimeAttrs);
final Map<String, String> marshaledParms = opennmsCollector.marshalParameters(Collections.unmodifiableMap(allParms));
beforeMinion();
// create a separate instance of the collector
final ServiceCollector minionCollector = getNewCollector();
// unmarshalParameters() should unmarshal all parameters from strings
final Map<String, Object> unmarshaledParms = minionCollector.unmarshalParameters(Collections.unmodifiableMap(marshaledParms));
// collect() should return a valid collection set
final CollectionAgentDTO agentDTO = new CollectionAgentDTO(agent);
final CollectionSet collectionSet = minionCollector.collect(agentDTO, Collections.unmodifiableMap(unmarshaledParms));
assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus());
afterMinion();
// the collection set should be marshalable
JaxbUtils.marshal(collectionSet);
// getRrdRepository() should return a valid repository
assertNotNull(opennmsCollector.getRrdRepository(getCollectionName()));
}
use of org.opennms.netmgt.collectd.SnmpCollectionAgent in project opennms by OpenNMS.
the class CollectorComplianceTest method canCollectUsingOpenNMSWorkflow.
@Test
public void canCollectUsingOpenNMSWorkflow() throws CollectionInitializationException, CollectionException {
// create the agent
OnmsNode node = mock(OnmsNode.class);
OnmsIpInterface iface = mock(OnmsIpInterface.class);
when(iface.getNode()).thenReturn(node);
when(iface.getIpAddress()).thenReturn(InetAddrUtils.getLocalHostAddress());
IpInterfaceDao ifaceDao = mock(IpInterfaceDao.class);
when(ifaceDao.load(1)).thenReturn(iface);
PlatformTransactionManager transMgr = mock(PlatformTransactionManager.class);
final SnmpCollectionAgent agent = DefaultCollectionAgent.create(1, ifaceDao, transMgr);
// init() should execute without any exceptions
final ServiceCollector opennmsCollector = getCollector();
initialize(opennmsCollector);
// getEffectiveLocation() should execute without any exceptions
// in this context there are no requirements on its return value
final String targetLocation = "!" + MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID;
opennmsCollector.getEffectiveLocation(targetLocation);
// getRuntimeAttributes() should return a valid map
final Map<String, Object> requiredParams = getRequiredParameters();
final Map<String, Object> runtimeAttrs = opennmsCollector.getRuntimeAttributes(agent, Collections.unmodifiableMap(requiredParams));
// collect() should return a valid collection set
final Map<String, Object> allParms = new HashMap<>();
allParms.putAll(requiredParams);
allParms.putAll(runtimeAttrs);
final CollectionSet collectionSet = opennmsCollector.collect(agent, Collections.unmodifiableMap(allParms));
assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus());
// getRrdRepository() should return a valid repository
assertNotNull(opennmsCollector.getRrdRepository(getCollectionName()));
}
use of org.opennms.netmgt.collectd.SnmpCollectionAgent in project opennms by OpenNMS.
the class CollectionResourceWrapperIT method testCounterReset_NMS7106.
@Test
public void testCounterReset_NMS7106() throws Exception {
// Create Resource
SnmpCollectionAgent agent = createCollectionAgent();
SnmpCollectionResource resource = createNodeResource(agent);
Date baseDate = new Date();
// Add Counter Attribute
System.err.println("------------------------");
String attributeName = "myCounter";
String attributeId = "node[1].resourceType[node].instance[null].metric[" + attributeName + "]";
Map<String, CollectionAttribute> attributes = new HashMap<String, CollectionAttribute>();
BigInteger initialValue = new BigInteger("300");
SnmpAttribute attribute = addAttributeToCollectionResource(resource, attributeName, AttributeType.COUNTER, "0", initialValue);
attributes.put(attribute.getName(), attribute);
// Get counter value - first time
CollectionResourceWrapper wrapper = createWrapper(resource, attributes, baseDate);
Assert.assertFalse(CollectionResourceWrapper.s_cache.containsKey(attributeId));
// Last value is null
Assert.assertEquals(Double.valueOf(Double.NaN), wrapper.getAttributeValue(attributeName));
// Last value is null
Assert.assertEquals(Double.valueOf(Double.NaN), wrapper.getAttributeValue(attributeName));
Assert.assertEquals(Double.valueOf(initialValue.doubleValue()), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
Assert.assertTrue(wrapper.getAttributeValue(attributeName).isNaN());
// Increase counter
attribute = addAttributeToCollectionResource(resource, attributeName, AttributeType.COUNTER, "0", new BigInteger("600"));
attributes.put(attribute.getName(), attribute);
wrapper = createWrapper(resource, attributes, new Date(baseDate.getTime() + 300000));
Assert.assertFalse(CollectionResourceWrapper.s_cache.get(attributeId).getValue().isNaN());
Assert.assertEquals(Double.valueOf(300.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
// 600 - 300 / 300 = 1.0
Assert.assertEquals(Double.valueOf(1.0), wrapper.getAttributeValue(attributeName));
// Increase counter again
attribute = addAttributeToCollectionResource(resource, attributeName, AttributeType.COUNTER, "0", new BigInteger("900"));
attributes.put(attribute.getName(), attribute);
wrapper = createWrapper(resource, attributes, new Date(baseDate.getTime() + 600000));
Assert.assertFalse(CollectionResourceWrapper.s_cache.get(attributeId).getValue().isNaN());
Assert.assertEquals(Double.valueOf(600.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
// 900 - 600 / 300 = 1.0
Assert.assertEquals(Double.valueOf(1.0), wrapper.getAttributeValue(attributeName));
// Emulate a sysUpTime restart
attribute = addAttributeToCollectionResource(resource, attributeName, AttributeType.COUNTER, "0", new BigInteger("60"));
attributes.put(attribute.getName(), attribute);
wrapper = createWrapper(resource, attributes, new Date(baseDate.getTime() + 900000));
wrapper.setCounterReset(true);
Assert.assertTrue(wrapper.getAttributeValue(attributeName).isNaN());
// Increase counter again
attribute = addAttributeToCollectionResource(resource, attributeName, AttributeType.COUNTER, "0", new BigInteger("120"));
attributes.put(attribute.getName(), attribute);
wrapper = createWrapper(resource, attributes, new Date(baseDate.getTime() + 1200000));
// 120 - 60 / 300 = 0.2
Assert.assertEquals(Double.valueOf(0.2), wrapper.getAttributeValue(attributeName));
EasyMock.verify(agent);
}
use of org.opennms.netmgt.collectd.SnmpCollectionAgent 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.collectd.SnmpCollectionAgent in project opennms by OpenNMS.
the class CollectionResourceWrapperIT method createCollectionAgent.
private SnmpCollectionAgent createCollectionAgent() {
SnmpCollectionAgent agent = EasyMock.createMock(SnmpCollectionAgent.class);
EasyMock.expect(agent.getNodeId()).andReturn(1).anyTimes();
EasyMock.expect(agent.getHostAddress()).andReturn("127.0.0.1").anyTimes();
EasyMock.expect(agent.getSnmpInterfaceInfo((IfResourceType) EasyMock.anyObject())).andReturn(new HashSet<IfInfo>()).anyTimes();
EasyMock.expect(agent.getForeignSource()).andReturn("JUnit").anyTimes();
EasyMock.expect(agent.getForeignId()).andReturn("T001").anyTimes();
EasyMock.expect(agent.getStorageResourcePath()).andReturn(ResourcePath.get(ResourceTypeUtils.FOREIGN_SOURCE_DIRECTORY, "JUnit", "T001")).anyTimes();
EasyMock.replay(agent);
return agent;
}
Aggregations