use of org.opennms.netmgt.collectd.SnmpAttribute 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.SnmpAttribute 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.SnmpAttribute in project opennms by OpenNMS.
the class CollectionResourceWrapperIT method testGetCounterValueWithGap.
/**
* Per bug report NMS-4244, multiple calls to the getCounter functionality on CollectionResourceWrapper used to pay
* no mind of the actual time when samples were collected, instead assuming that it was the collection interval ago.
* When a collection cycle fails (entirely or partly) and thresholded values weren't available, the counter value
* was incorrectly calculated on the next succeeding cycle (taking the difference from the last successful
* collection and dividing by just a single collection interval.
*/
@Test
public void testGetCounterValueWithGap() throws Exception {
// we get a warning on the first getAttributeValue()
m_ignoreWarnings = true;
SnmpCollectionAgent agent = createCollectionAgent();
SnmpCollectionResource resource = createNodeResource(agent);
// Add Counter Attribute
String attributeName = "myCounter";
String attributeId = "node[1].resourceType[node].instance[null].metric[" + attributeName + "]";
Map<String, CollectionAttribute> attributes = new HashMap<String, CollectionAttribute>();
SnmpAttribute attribute = addAttributeToCollectionResource(resource, attributeName, AttributeType.COUNTER, "0", "1000");
attributes.put(attribute.getName(), attribute);
// We manipulate the Date objects passed to the
// CollectionResourceWrapper to simulate various collection intervals
Date baseDate = new Date();
// Get counter value - first time
CollectionResourceWrapper wrapper = createWrapper(resource, attributes, baseDate);
Assert.assertFalse(CollectionResourceWrapper.s_cache.containsKey(attributeId));
Assert.assertEquals(Double.valueOf(Double.NaN), // Last value is null
wrapper.getAttributeValue(attributeName));
Assert.assertEquals(Double.valueOf(Double.NaN), // Last value is null
wrapper.getAttributeValue(attributeName));
Assert.assertEquals(Double.valueOf(1000.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
// Increase counter
attribute = addAttributeToCollectionResource(resource, attributeName, AttributeType.COUNTER, "0", "2500");
attributes.put(attribute.getName(), attribute);
//Next wrapper is told the data was collected 5 minutes in the future (300 seconds)
wrapper = createWrapper(resource, attributes, new Date(baseDate.getTime() + 300000));
// Get counter value - second time
// Last value is 1000.0, so 2500-1000/300 = 1500/300 = 5.
Assert.assertEquals(Double.valueOf(1000.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
Assert.assertEquals(Double.valueOf(5.0), wrapper.getAttributeValue(attributeName));
//Validate that the cached counter value has been updated
Assert.assertEquals(Double.valueOf(2500.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
//but that calling getAttributeValue doesn't re-calculate the rate inappropriately or update the static cache
Assert.assertEquals(Double.valueOf(5.0), wrapper.getAttributeValue(attributeName));
Assert.assertEquals(Double.valueOf(2500.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
// Now create a collection that is missing the counter value; we're
// expecting null result and no cache updates
attributes = new HashMap<String, CollectionAttribute>();
attribute = addAttributeToCollectionResource(resource, "notMyCounter", AttributeType.COUNTER, "0", // We want a value, just not one called "myCounter"
"1000");
attributes.put(attribute.getName(), attribute);
// Next collection is 10 minutes (600 seconds) after the first.
wrapper = createWrapper(resource, attributes, new Date(baseDate.getTime() + 600000));
// No change, so we expect the cache to have (and continue to) remain
// the same, and to get no attribute value out
Assert.assertEquals(Double.valueOf(2500.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
Assert.assertNull(wrapper.getAttributeValue(attributeName));
Assert.assertEquals(Double.valueOf(2500.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
// Now if we collect successfully again, we expect the counter to be the
// change divided by two collection cycles
attributes = new HashMap<String, CollectionAttribute>();
attribute = addAttributeToCollectionResource(resource, attributeName, AttributeType.COUNTER, "0", "7300");
attributes.put(attribute.getName(), attribute);
// Next collection is 15 minutes (900 seconds) after the first.
wrapper = createWrapper(resource, attributes, new Date(baseDate.getTime() + 900000));
// Get counter value - fourth time
// Last value is 5500, but we've had two collection cycles, so
// 7300-2500/600 = 4800/600 = 8
Assert.assertEquals(Double.valueOf(2500.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
Assert.assertEquals(Double.valueOf(8.0), wrapper.getAttributeValue(attributeName));
Assert.assertEquals(Double.valueOf(7300.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
Assert.assertEquals(Double.valueOf(8.0), wrapper.getAttributeValue(attributeName));
Assert.assertEquals(Double.valueOf(7300.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
Assert.assertEquals(Double.valueOf(8.0), wrapper.getAttributeValue(attributeName));
Assert.assertEquals(Double.valueOf(7300.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
EasyMock.verify(agent);
}
use of org.opennms.netmgt.collectd.SnmpAttribute in project opennms by OpenNMS.
the class CollectionResourceWrapperIT method testGetCounterValue.
@Test
public void testGetCounterValue() throws Exception {
// Create Resource
SnmpCollectionAgent agent = createCollectionAgent();
SnmpCollectionResource resource = createNodeResource(agent);
// Add Counter Attribute
String attributeName = "myCounter";
String attributeId = "node[1].resourceType[node].instance[null].metric[" + attributeName + "]";
Map<String, CollectionAttribute> attributes = new HashMap<String, CollectionAttribute>();
SnmpAttribute attribute = addAttributeToCollectionResource(resource, attributeName, AttributeType.COUNTER, "0", "1000");
attributes.put(attribute.getName(), attribute);
//We manipulate the Date objects passed to the CollectionResourceWrapper to simulate various collection intervals
Date baseDate = new Date();
// 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(1000.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
// Increase counter
attribute = addAttributeToCollectionResource(resource, attributeName, AttributeType.COUNTER, "0", "2500");
attributes.put(attribute.getName(), attribute);
//Next wrapper is told the data was collected 5 minutes in the future (300 seconds)
wrapper = createWrapper(resource, attributes, new Date(baseDate.getTime() + 300000));
// Get counter value - second time
// Last value is 1000.0, so 2500-1000/300 = 1500/300 = 5.
Assert.assertEquals(Double.valueOf(1000.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
Assert.assertEquals(Double.valueOf(5.0), wrapper.getAttributeValue(attributeName));
//Validate that the cached counter value has been updated
Assert.assertEquals(Double.valueOf(2500.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
//but that calling getAttributeValue doesn't re-calculate the rate inappropriately
Assert.assertEquals(Double.valueOf(5.0), wrapper.getAttributeValue(attributeName));
Assert.assertEquals(Double.valueOf(2500.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
Assert.assertEquals(Double.valueOf(5.0), wrapper.getAttributeValue(attributeName));
Assert.assertEquals(Double.valueOf(2500.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
// Increase counter
attribute = addAttributeToCollectionResource(resource, attributeName, AttributeType.COUNTER, "0", "5500");
attributes.put(attribute.getName(), attribute);
//Next wrapper is told the data was collected 10 minutes in the future (600 seconds), or after the first collection
wrapper = createWrapper(resource, attributes, new Date(baseDate.getTime() + 600000));
// Get counter value - third time
// Last value is 2500.0, so 5500-2500/300 = 3000/300 = 10;
Assert.assertEquals(Double.valueOf(2500.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
Assert.assertEquals(Double.valueOf(10.0), wrapper.getAttributeValue(attributeName));
Assert.assertEquals(Double.valueOf(5500.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
Assert.assertEquals(Double.valueOf(10.0), wrapper.getAttributeValue(attributeName));
Assert.assertEquals(Double.valueOf(5500.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
Assert.assertEquals(Double.valueOf(10.0), wrapper.getAttributeValue(attributeName));
Assert.assertEquals(Double.valueOf(5500.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
}
use of org.opennms.netmgt.collectd.SnmpAttribute in project opennms by OpenNMS.
the class CollectionResourceWrapperIT method testGetGaugeValue.
@Test
public void testGetGaugeValue() throws Exception {
// Create Resource
SnmpCollectionAgent agent = createCollectionAgent();
SnmpCollectionResource resource = createNodeResource(agent);
// Add Gauge Attribute
Map<String, CollectionAttribute> attributes = new HashMap<String, CollectionAttribute>();
SnmpAttribute attribute = addAttributeToCollectionResource(resource, "myGauge", AttributeType.GAUGE, "0", "100");
attributes.put(attribute.getName(), attribute);
// Create Wrapper
CollectionResourceWrapper wrapper = createWrapper(resource, attributes);
// Get gauge value 3 times
Assert.assertEquals(Double.valueOf(100.0), wrapper.getAttributeValue("myGauge"));
Assert.assertEquals(Double.valueOf(100.0), wrapper.getAttributeValue("myGauge"));
Assert.assertEquals(Double.valueOf(100.0), wrapper.getAttributeValue("myGauge"));
EasyMock.verify(agent);
}
Aggregations