Search in sources :

Example 6 with SnmpCollectionAgent

use of org.opennms.netmgt.collectd.SnmpCollectionAgent 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);
}
Also used : CollectionAttribute(org.opennms.netmgt.collection.api.CollectionAttribute) SnmpCollectionAgent(org.opennms.netmgt.collectd.SnmpCollectionAgent) HashMap(java.util.HashMap) SnmpAttribute(org.opennms.netmgt.collectd.SnmpAttribute) SnmpCollectionResource(org.opennms.netmgt.collectd.SnmpCollectionResource) Date(java.util.Date) Test(org.junit.Test)

Example 7 with SnmpCollectionAgent

use of org.opennms.netmgt.collectd.SnmpCollectionAgent 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());
}
Also used : CollectionAttribute(org.opennms.netmgt.collection.api.CollectionAttribute) SnmpCollectionAgent(org.opennms.netmgt.collectd.SnmpCollectionAgent) HashMap(java.util.HashMap) SnmpAttribute(org.opennms.netmgt.collectd.SnmpAttribute) SnmpCollectionResource(org.opennms.netmgt.collectd.SnmpCollectionResource) Date(java.util.Date) Test(org.junit.Test)

Example 8 with SnmpCollectionAgent

use of org.opennms.netmgt.collectd.SnmpCollectionAgent 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);
}
Also used : CollectionAttribute(org.opennms.netmgt.collection.api.CollectionAttribute) SnmpCollectionAgent(org.opennms.netmgt.collectd.SnmpCollectionAgent) HashMap(java.util.HashMap) SnmpAttribute(org.opennms.netmgt.collectd.SnmpAttribute) SnmpCollectionResource(org.opennms.netmgt.collectd.SnmpCollectionResource) Test(org.junit.Test)

Example 9 with SnmpCollectionAgent

use of org.opennms.netmgt.collectd.SnmpCollectionAgent in project opennms by OpenNMS.

the class BasePersisterTest method buildStringAttribute.

private SnmpAttribute buildStringAttribute() {
    EasyMock.expect(m_ifDao.load(m_intf.getId())).andReturn(m_intf).anyTimes();
    m_easyMockUtils.replayAll();
    SnmpCollectionAgent agent = DefaultCollectionAgent.create(m_intf.getId(), m_ifDao, m_transMgr);
    MockDataCollectionConfig dataCollectionConfig = new MockDataCollectionConfig();
    OnmsSnmpCollection collection = new OnmsSnmpCollection(agent, new ServiceParameters(new HashMap<String, Object>()), dataCollectionConfig, m_locationAwareSnmpClient);
    NodeResourceType resourceType = new NodeResourceType(agent, collection);
    SnmpCollectionResource resource = new NodeInfo(resourceType, agent);
    MibObject mibObject = new MibObject();
    mibObject.setOid(".1.1.1.1");
    mibObject.setAlias("mibObjectAlias");
    mibObject.setType("string");
    mibObject.setInstance("0");
    mibObject.setMaxval(null);
    mibObject.setMinval(null);
    SnmpAttributeType attributeType = new StringAttributeType(resourceType, "some-collection", mibObject, new AttributeGroupType("mibGroup", AttributeGroupType.IF_TYPE_IGNORE));
    return new SnmpAttribute(resource, attributeType, SnmpUtils.getValueFactory().getOctetString("foo".getBytes()));
}
Also used : NodeResourceType(org.opennms.netmgt.collectd.NodeResourceType) SnmpCollectionAgent(org.opennms.netmgt.collectd.SnmpCollectionAgent) HashMap(java.util.HashMap) MockDataCollectionConfig(org.opennms.netmgt.mock.MockDataCollectionConfig) SnmpAttributeType(org.opennms.netmgt.collectd.SnmpAttributeType) StringAttributeType(org.opennms.netmgt.collectd.StringAttributeType) SnmpAttribute(org.opennms.netmgt.collectd.SnmpAttribute) NodeInfo(org.opennms.netmgt.collectd.NodeInfo) AttributeGroupType(org.opennms.netmgt.collection.api.AttributeGroupType) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) SnmpCollectionResource(org.opennms.netmgt.collectd.SnmpCollectionResource) MibObject(org.opennms.netmgt.config.datacollection.MibObject) OnmsSnmpCollection(org.opennms.netmgt.collectd.OnmsSnmpCollection)

Example 10 with SnmpCollectionAgent

use of org.opennms.netmgt.collectd.SnmpCollectionAgent in project opennms by OpenNMS.

the class ThresholdingVisitorIT method testBug2746.

/*
     * This bug has not been replicated, but this code covers the apparent scenario, and can be adapted to match
     * any scenario which can actually replicate the reported issue
     * 
     * This test uses this files from src/test/resources:
     * - threshd-configuration.xml
     * - test-thresholds-bug2746.xml
     */
@Test
public void testBug2746() throws Exception {
    initFactories("/threshd-configuration.xml", "/test-thresholds-bug2746.xml");
    ThresholdingVisitor visitor = createVisitor();
    SnmpCollectionAgent agent = createCollectionAgent();
    NodeResourceType resourceType = createNodeResourceType(agent);
    MibObject mibObject = createMibObject("gauge", "bug2746", "0");
    SnmpAttributeType attributeType = new NumericAttributeType(resourceType, "default", mibObject, new AttributeGroupType("mibGroup", AttributeGroupType.IF_TYPE_IGNORE));
    // Add Events
    addHighThresholdEvent(1, 50, 40, 60, "node", "node", "bug2746", null, null);
    // Step 1 : Execute visitor
    SnmpCollectionResource resource = new NodeInfo(resourceType, agent);
    resource.setAttributeValue(attributeType, SnmpUtils.getValueFactory().getGauge32(20));
    resource.visit(visitor);
    // Step 2 : Repeat a couple of times with the same value, to replicate a steady state
    resource.visit(visitor);
    resource.visit(visitor);
    resource.visit(visitor);
    // Step 3 : Trigger
    resource = new NodeInfo(resourceType, agent);
    resource.setAttributeValue(attributeType, SnmpUtils.getValueFactory().getGauge32(60));
    resource.visit(visitor);
    // Step 4 : Don't rearm, but do drop
    resource = new NodeInfo(resourceType, agent);
    resource.setAttributeValue(attributeType, SnmpUtils.getValueFactory().getGauge32(45));
    resource.visit(visitor);
    // Step 5 : Shouldn't trigger again
    resource = new NodeInfo(resourceType, agent);
    resource.setAttributeValue(attributeType, SnmpUtils.getValueFactory().getGauge32(55));
    resource.visit(visitor);
    EasyMock.verify(agent);
    verifyEvents(0);
}
Also used : NodeResourceType(org.opennms.netmgt.collectd.NodeResourceType) NumericAttributeType(org.opennms.netmgt.collectd.NumericAttributeType) SnmpCollectionAgent(org.opennms.netmgt.collectd.SnmpCollectionAgent) NodeInfo(org.opennms.netmgt.collectd.NodeInfo) AttributeGroupType(org.opennms.netmgt.collection.api.AttributeGroupType) MibObject(org.opennms.netmgt.config.datacollection.MibObject) SnmpAttributeType(org.opennms.netmgt.collectd.SnmpAttributeType) SnmpCollectionResource(org.opennms.netmgt.collectd.SnmpCollectionResource) Test(org.junit.Test)

Aggregations

SnmpCollectionAgent (org.opennms.netmgt.collectd.SnmpCollectionAgent)30 Test (org.junit.Test)22 SnmpCollectionResource (org.opennms.netmgt.collectd.SnmpCollectionResource)22 HashMap (java.util.HashMap)13 Date (java.util.Date)10 IfResourceType (org.opennms.netmgt.collectd.IfResourceType)10 NodeResourceType (org.opennms.netmgt.collectd.NodeResourceType)10 SnmpAttribute (org.opennms.netmgt.collectd.SnmpAttribute)9 NodeInfo (org.opennms.netmgt.collectd.NodeInfo)8 CollectionAttribute (org.opennms.netmgt.collection.api.CollectionAttribute)8 MibObject (org.opennms.netmgt.config.datacollection.MibObject)7 BigInteger (java.math.BigInteger)6 IfInfo (org.opennms.netmgt.collectd.IfInfo)6 SnmpAttributeType (org.opennms.netmgt.collectd.SnmpAttributeType)6 SnmpIfData (org.opennms.netmgt.collectd.SnmpIfData)6 AttributeGroupType (org.opennms.netmgt.collection.api.AttributeGroupType)6 GenericIndexResource (org.opennms.netmgt.collectd.GenericIndexResource)5 GenericIndexResourceType (org.opennms.netmgt.collectd.GenericIndexResourceType)5 NumericAttributeType (org.opennms.netmgt.collectd.NumericAttributeType)5 OnmsSnmpCollection (org.opennms.netmgt.collectd.OnmsSnmpCollection)5