Search in sources :

Example 1 with SnmpCollectionAgent

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()));
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) SnmpCollectionAgent(org.opennms.netmgt.collectd.SnmpCollectionAgent) HashMap(java.util.HashMap) CollectionAgentDTO(org.opennms.netmgt.collection.dto.CollectionAgentDTO) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) IpInterfaceDao(org.opennms.netmgt.dao.api.IpInterfaceDao) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) ServiceCollector(org.opennms.netmgt.collection.api.ServiceCollector) Test(org.junit.Test)

Example 2 with SnmpCollectionAgent

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()));
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) SnmpCollectionAgent(org.opennms.netmgt.collectd.SnmpCollectionAgent) HashMap(java.util.HashMap) ServiceCollector(org.opennms.netmgt.collection.api.ServiceCollector) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) IpInterfaceDao(org.opennms.netmgt.dao.api.IpInterfaceDao) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) Test(org.junit.Test)

Example 3 with SnmpCollectionAgent

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);
}
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 4 with SnmpCollectionAgent

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()));
}
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 5 with SnmpCollectionAgent

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;
}
Also used : IfResourceType(org.opennms.netmgt.collectd.IfResourceType) SnmpCollectionAgent(org.opennms.netmgt.collectd.SnmpCollectionAgent) HashSet(java.util.HashSet)

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