use of org.opennms.netmgt.collection.api.CollectionAttribute in project opennms by OpenNMS.
the class CollectionResourceWrapperIT method testInterfaceResource.
@Test
public void testInterfaceResource() throws Exception {
// Set Defaults
String ipAddress = "10.0.0.1";
String ifName = "eth0";
int ifIndex = 2;
// Initialize Database
MockNetwork network = new MockNetwork();
network.setCriticalService("ICMP");
network.addNode(1, "testNode");
network.addInterface(ipAddress);
network.setIfAlias(ifName);
network.setIfIndex(ifIndex);
network.addService("ICMP");
network.addService("SNMP");
network.addService("HTTP");
MockDatabase db = new MockDatabase();
db.populate(network);
DataSourceFactory.setInstance(db);
// Create Mock Collection Agent
SnmpCollectionAgent agent = createCollectionAgent();
// Create SnmpIfData
OnmsNode node = new OnmsNode();
node.setId(agent.getNodeId());
node.setLabel("testNode");
node.setForeignSource(agent.getForeignSource());
node.setForeignId(agent.getForeignId());
OnmsSnmpInterface snmpIface = new OnmsSnmpInterface(node, ifIndex);
snmpIface.setIfDescr(ifName);
snmpIface.setIfName(ifName);
snmpIface.setIfAlias(ifName);
snmpIface.setIfSpeed(10000000l);
snmpIface.setPhysAddr("001122334455");
SnmpIfData ifData = new SnmpIfData(snmpIface);
// Creating IfResourceType
MockDataCollectionConfig dataCollectionConfig = new MockDataCollectionConfig();
OnmsSnmpCollection collection = new OnmsSnmpCollection(agent, new ServiceParameters(new HashMap<String, Object>()), dataCollectionConfig, m_locationAwareSnmpClient);
IfResourceType resourceType = new IfResourceType(agent, collection);
// Creating Resource
SnmpCollectionResource resource = new IfInfo(resourceType, agent, ifData);
SnmpAttribute attribute = addAttributeToCollectionResource(resource, "ifInOctets", AttributeType.COUNTER, "ifIndex", "5000");
Map<String, CollectionAttribute> attributes = new HashMap<String, CollectionAttribute>();
attributes.put(attribute.getName(), attribute);
// Create Wrapper
CollectionResourceWrapper wrapper = createWrapper(resource, attributes);
// Validations
Assert.assertEquals(node.getId().intValue(), wrapper.getNodeId());
// Should be the address of the SNMP Agent (Bug 3808)
Assert.assertEquals("127.0.0.1", wrapper.getHostAddress());
Assert.assertEquals("eth0-001122334455", wrapper.getIfLabel());
Assert.assertEquals("if", wrapper.getResourceTypeName());
Assert.assertEquals("SNMP", wrapper.getServiceName());
Assert.assertEquals(true, wrapper.isAnInterfaceResource());
Assert.assertEquals(Integer.toString(ifIndex), wrapper.getInstance());
Assert.assertEquals(Integer.toString(ifIndex), wrapper.getIfIndex());
// IfLabel is called only once
Assert.assertEquals(Integer.toString(ifIndex), wrapper.getIfIndex());
// IfLabel is called only once
Assert.assertEquals(Integer.toString(ifIndex), wrapper.getIfIndex());
// IfLabel is called only once
Assert.assertEquals("eth0", wrapper.getIfInfoValue("snmpifname"));
Assert.assertEquals("eth0-001122334455", wrapper.getInstanceLabel());
Assert.assertEquals("nodeSource[JUnit:T001].interfaceSnmp[eth0-001122334455]", wrapper.getResourceId().toString());
}
use of org.opennms.netmgt.collection.api.CollectionAttribute in project opennms by OpenNMS.
the class CollectionResourceWrapperIT method testGetCounterValueWithWrap.
@Test
public void testGetCounterValueWithWrap() throws Exception {
// Create Resource
SnmpCollectionAgent agent = createCollectionAgent();
SnmpCollectionResource resource = createNodeResource(agent);
// We manipulate the Date objects passed to the
// CollectionResourceWrapper to simulate various collection intervals
Date baseDate = new Date();
// Add Counter Attribute
String attributeName = "myCounter";
String attributeId = "node[1].resourceType[node].instance[null].metric[" + attributeName + "]";
Map<String, CollectionAttribute> attributes = new HashMap<String, CollectionAttribute>();
BigInteger initialValue = new BigDecimal(Math.pow(2, 32) - 20000).toBigInteger();
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());
// Increase counter
attribute = addAttributeToCollectionResource(resource, attributeName, AttributeType.COUNTER, "0", new BigInteger("40000"));
attributes.put(attribute.getName(), attribute);
wrapper = createWrapper(resource, attributes, new Date(baseDate.getTime() + 300000));
// Get counter value - second time (wrap)
// last = MAX - 20000, new = 40000; then last - new = 60000, rate: 60000/300 = 200
Assert.assertEquals(Double.valueOf(initialValue.doubleValue()), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
Assert.assertEquals(Double.valueOf(200.0), wrapper.getAttributeValue(attributeName));
Assert.assertEquals(Double.valueOf(40000.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
Assert.assertEquals(Double.valueOf(200.0), wrapper.getAttributeValue(attributeName));
Assert.assertEquals(Double.valueOf(40000.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
Assert.assertEquals(Double.valueOf(200.0), wrapper.getAttributeValue(attributeName));
Assert.assertEquals(Double.valueOf(40000.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue());
EasyMock.verify(agent);
}
use of org.opennms.netmgt.collection.api.CollectionAttribute in project opennms by OpenNMS.
the class CollectionResourceWrapperIT method testGenericResource.
@Test
public void testGenericResource() 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("hrStorageIndex");
rt.setLabel("host-resources storage");
StorageStrategy strategy = new StorageStrategy();
strategy.setClazz("org.opennms.netmgt.dao.support.SiblingColumnStorageStrategy");
strategy.addParameter(new Parameter("sibling-column-name", "hrStorageLabel"));
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 used = addAttributeToCollectionResource(resource, "hrStorageUsed", AttributeType.GAUGE, "hrStorageIndex", "5000");
SnmpAttribute label = addAttributeToCollectionResource(resource, "hrStorageLabel", AttributeType.STRING, "hrStorageIndex", "/opt");
Map<String, CollectionAttribute> attributes = new HashMap<String, CollectionAttribute>();
attributes.put(used.getName(), used);
attributes.put(label.getName(), label);
CollectionResourceWrapper wrapper = createWrapper(resource, attributes);
Assert.assertEquals("opt", wrapper.getInstanceLabel());
}
use of org.opennms.netmgt.collection.api.CollectionAttribute in project opennms by OpenNMS.
the class CollectionSetUtils method getAttributesByName.
public static Map<String, CollectionAttribute> getAttributesByName(CollectionSet collectionSet) {
final Map<String, CollectionAttribute> attributesByName = Maps.newHashMap();
collectionSet.visit(new AbstractCollectionSetVisitor() {
@Override
public void visitAttribute(CollectionAttribute attribute) {
attributesByName.put(attribute.getName(), attribute);
}
});
return attributesByName;
}
use of org.opennms.netmgt.collection.api.CollectionAttribute in project opennms by OpenNMS.
the class CollectionSetUtils method getAttributesByNameByGroup.
public static Map<String, Map<String, CollectionAttribute>> getAttributesByNameByGroup(CollectionSet collectionSet) {
final Map<String, Map<String, CollectionAttribute>> attributesByNameByGroup = Maps.newHashMap();
collectionSet.visit(new AbstractCollectionSetVisitor() {
private String groupName = null;
private Map<String, CollectionAttribute> attributesByName = Maps.newHashMap();
@Override
public void visitGroup(AttributeGroup group) {
groupName = group.getName();
}
@Override
public void visitAttribute(CollectionAttribute attribute) {
attributesByName.put(attribute.getName(), attribute);
}
@Override
public void completeGroup(AttributeGroup group) {
attributesByNameByGroup.put(groupName, attributesByName);
attributesByName = Maps.newHashMap();
}
});
return attributesByNameByGroup;
}
Aggregations