@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); }
private CollectionResourceWrapper createWrapper(SnmpCollectionResource resource, Map<String, CollectionAttribute> attributes, Date timestamp) { CollectionResourceWrapper wrapper = new CollectionResourceWrapper(timestamp, 1, "127.0.0.1", "SNMP", getRepository(), resource, attributes, getResourceStorageDao()); return wrapper; }
@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()); }
private SnmpAttribute addAttributeToCollectionResource(SnmpCollectionResource resource, String attributeName, AttributeType attributeType, String attributeInstance, BigInteger value) { MibObject object = createMibObject(attributeType, attributeName, attributeInstance); SnmpAttributeType objectType = new NumericAttributeType(resource.getResourceType(), "default", object, new AttributeGroupType("mibGroup", AttributeGroupType.IF_TYPE_IGNORE)); SnmpValue snmpValue = SnmpUtils.getValueFactory().getCounter64(value); resource.setAttributeValue(objectType, snmpValue); return new SnmpAttribute(resource, objectType, snmpValue); }
private CollectionResourceWrapper createWrapper(SnmpCollectionResource resource, Map<String, CollectionAttribute> attributes) { return this.createWrapper(resource, attributes, new Date()); }
@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); 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(label.getName(), label); CollectionResourceWrapper wrapper = createWrapper(resource, attributes); Assert.assertEquals("opt", wrapper.getInstanceLabel()); Assert.assertEquals(new Double("10000.0"), wrapper.getAttributeValue(total.getName()));
private SnmpAttribute addAttributeToCollectionResource(SnmpCollectionResource resource, String attributeName, AttributeType attributeType, String attributeInstance, String value) { MibObject object = createMibObject(attributeType, attributeName, attributeInstance); SnmpAttributeType objectType = null; SnmpValue snmpValue = null; if (AttributeType.STRING.equals(attributeType)) { objectType = new SnmpAttributeType(resource.getResourceType(), "default", object, new AttributeGroupType("mibGroup", AttributeGroupType.IF_TYPE_IGNORE)) { @Override public AttributeType getType() { return AttributeType.STRING; } @Override public void storeAttribute(CollectionAttribute attribute, Persister persister) { persister.persistStringAttribute(attribute); } }; snmpValue = SnmpUtils.getValueFactory().getOctetString(value.getBytes()); } else { objectType = new NumericAttributeType(resource.getResourceType(), "default", object, new AttributeGroupType("mibGroup", AttributeGroupType.IF_TYPE_IGNORE)); long v = Long.parseLong(value); snmpValue = AttributeType.COUNTER.equals(attributeType) ? SnmpUtils.getValueFactory().getCounter32(v) : SnmpUtils.getValueFactory().getGauge32(v); } resource.setAttributeValue(objectType, snmpValue); return new SnmpAttribute(resource, objectType, snmpValue); }
SnmpCollectionAgent agent = createCollectionAgent(); SnmpCollectionResource resource = createNodeResource(agent); Date baseDate = new Date(); 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); CollectionResourceWrapper wrapper = createWrapper(resource, attributes, baseDate); Assert.assertFalse(CollectionResourceWrapper.s_cache.containsKey(attributeId)); 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()); 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()); 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());
SnmpCollectionAgent agent = createCollectionAgent(); SnmpAttribute attribute = addAttributeToCollectionResource(resource, "ifInOctets", AttributeType.COUNTER, "ifIndex", "5000"); Map<String, CollectionAttribute> attributes = new HashMap<String, CollectionAttribute>(); attributes.put(attribute.getName(), attribute); CollectionResourceWrapper wrapper = createWrapper(resource, attributes);
@Test public void testGetCounterValueWithWrap() throws Exception { SnmpCollectionAgent agent = createCollectionAgent(); SnmpCollectionResource resource = createNodeResource(agent); 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); CollectionResourceWrapper wrapper = createWrapper(resource, attributes, baseDate); Assert.assertFalse(CollectionResourceWrapper.s_cache.containsKey(attributeId)); attribute = addAttributeToCollectionResource(resource, attributeName, AttributeType.COUNTER, "0", new BigInteger("40000")); attributes.put(attribute.getName(), attribute); wrapper = createWrapper(resource, attributes, new Date(baseDate.getTime() + 300000));
@Test public void testGetCounterValue() throws Exception { SnmpCollectionAgent agent = createCollectionAgent(); SnmpCollectionResource resource = createNodeResource(agent); 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); CollectionResourceWrapper wrapper = createWrapper(resource, attributes, baseDate); attribute = addAttributeToCollectionResource(resource, attributeName, AttributeType.COUNTER, "0", "2500"); attributes.put(attribute.getName(), attribute); wrapper = createWrapper(resource, attributes, new Date(baseDate.getTime()+300000)); attribute = addAttributeToCollectionResource(resource, attributeName, AttributeType.COUNTER, "0", "5500"); attributes.put(attribute.getName(), attribute); wrapper = createWrapper(resource, attributes, new Date(baseDate.getTime()+600000));
SnmpCollectionAgent agent = createCollectionAgent(); SnmpCollectionResource resource = createNodeResource(agent); 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); CollectionResourceWrapper wrapper = createWrapper(resource, attributes, baseDate); attribute = addAttributeToCollectionResource(resource, attributeName, AttributeType.COUNTER, "0", "2500"); attributes.put(attribute.getName(), attribute); wrapper = createWrapper(resource, attributes, new Date(baseDate.getTime()+300000)); attribute = addAttributeToCollectionResource(resource, "notMyCounter", AttributeType.COUNTER, "0", "1000"); // We want a value, just not one called "myCounter" attributes.put(attribute.getName(), attribute); wrapper = createWrapper(resource, attributes, new Date(baseDate.getTime() + 600000)); attribute = addAttributeToCollectionResource(resource, attributeName, AttributeType.COUNTER, "0", "7300"); attributes.put(attribute.getName(), attribute); wrapper = createWrapper(resource, attributes, new Date(baseDate.getTime() + 900000));