Search in sources :

Example 1 with CollectionAttribute

use of org.opennms.netmgt.collection.api.CollectionAttribute in project opennms by OpenNMS.

the class CollectionSetUtils method flatten.

public static List<String> flatten(CollectionSet collectionSet) {
    final List<String> strings = new ArrayList<>();
    collectionSet.visit(new AbstractCollectionSetVisitor() {

        CollectionResource resource;

        AttributeGroup group;

        @Override
        public void visitResource(CollectionResource resource) {
            this.resource = resource;
        }

        @Override
        public void visitGroup(AttributeGroup group) {
            this.group = group;
        }

        @Override
        public void visitAttribute(CollectionAttribute attribute) {
            strings.add(String.format("%s/%s/%s[%s,%s]", resource.getPath(), group.getName(), attribute.getName(), attribute.getStringValue(), attribute.getNumericValue()));
        }
    });
    return strings;
}
Also used : CollectionAttribute(org.opennms.netmgt.collection.api.CollectionAttribute) CollectionResource(org.opennms.netmgt.collection.api.CollectionResource) ArrayList(java.util.ArrayList) AttributeGroup(org.opennms.netmgt.collection.api.AttributeGroup) AbstractCollectionSetVisitor(org.opennms.netmgt.collection.support.AbstractCollectionSetVisitor)

Example 2 with CollectionAttribute

use of org.opennms.netmgt.collection.api.CollectionAttribute 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);
}
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) BigInteger(java.math.BigInteger) SnmpCollectionResource(org.opennms.netmgt.collectd.SnmpCollectionResource) Date(java.util.Date) Test(org.junit.Test)

Example 3 with CollectionAttribute

use of org.opennms.netmgt.collection.api.CollectionAttribute 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()));
}
Also used : StorageStrategy(org.opennms.netmgt.config.datacollection.StorageStrategy) SnmpCollectionAgent(org.opennms.netmgt.collectd.SnmpCollectionAgent) HashMap(java.util.HashMap) ResourceType(org.opennms.netmgt.config.datacollection.ResourceType) GenericIndexResourceType(org.opennms.netmgt.collectd.GenericIndexResourceType) NodeResourceType(org.opennms.netmgt.collectd.NodeResourceType) IfResourceType(org.opennms.netmgt.collectd.IfResourceType) MockDataCollectionConfig(org.opennms.netmgt.mock.MockDataCollectionConfig) GenericIndexResource(org.opennms.netmgt.collectd.GenericIndexResource) GenericIndexResourceType(org.opennms.netmgt.collectd.GenericIndexResourceType) CollectionAttribute(org.opennms.netmgt.collection.api.CollectionAttribute) SnmpAttribute(org.opennms.netmgt.collectd.SnmpAttribute) SnmpInstId(org.opennms.netmgt.snmp.SnmpInstId) Parameter(org.opennms.netmgt.config.datacollection.Parameter) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) SnmpCollectionResource(org.opennms.netmgt.collectd.SnmpCollectionResource) OnmsSnmpCollection(org.opennms.netmgt.collectd.OnmsSnmpCollection) PersistenceSelectorStrategy(org.opennms.netmgt.config.datacollection.PersistenceSelectorStrategy) Test(org.junit.Test)

Example 4 with CollectionAttribute

use of org.opennms.netmgt.collection.api.CollectionAttribute 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 5 with CollectionAttribute

use of org.opennms.netmgt.collection.api.CollectionAttribute 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)

Aggregations

CollectionAttribute (org.opennms.netmgt.collection.api.CollectionAttribute)29 Test (org.junit.Test)15 HashMap (java.util.HashMap)10 SnmpAttribute (org.opennms.netmgt.collectd.SnmpAttribute)9 SnmpCollectionAgent (org.opennms.netmgt.collectd.SnmpCollectionAgent)8 SnmpCollectionResource (org.opennms.netmgt.collectd.SnmpCollectionResource)8 ServiceParameters (org.opennms.netmgt.collection.api.ServiceParameters)6 CollectionResource (org.opennms.netmgt.collection.api.CollectionResource)5 Parameter (org.opennms.netmgt.config.datacollection.Parameter)5 PersistenceSelectorStrategy (org.opennms.netmgt.config.datacollection.PersistenceSelectorStrategy)5 ResourceType (org.opennms.netmgt.config.datacollection.ResourceType)5 StorageStrategy (org.opennms.netmgt.config.datacollection.StorageStrategy)5 OnmsNode (org.opennms.netmgt.model.OnmsNode)5 Date (java.util.Date)4 Map (java.util.Map)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 AttributeGroupType (org.opennms.netmgt.collection.api.AttributeGroupType)4 AttributeType (org.opennms.netmgt.collection.api.AttributeType)4 AbstractCollectionSetVisitor (org.opennms.netmgt.collection.support.AbstractCollectionSetVisitor)4 Resource (org.opennms.netmgt.collection.support.builder.Resource)4