@Test public void testGetCounterValue() throws Exception { CollectionAgent agent = createCollectionAgent(); SnmpCollectionResource resource = createNodeResource(agent); 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, "counter", "0", "1000"); attributes.put(attribute.getName(), attribute); Date baseDate = new Date(); CollectionResourceWrapper wrapper = createWrapper(resource, attributes, baseDate); Assert.assertFalse(CollectionResourceWrapper.s_cache.containsKey(attributeId)); Assert.assertEquals(Double.NaN, wrapper.getAttributeValue(attributeName)); Assert.assertEquals(Double.NaN, wrapper.getAttributeValue(attributeName)); Assert.assertEquals(1000.0, CollectionResourceWrapper.s_cache.get(attributeId).value); attribute = addAttributeToCollectionResource(resource, attributeName, "counter", "0", "2500"); attributes.put(attribute.getName(), attribute); wrapper = createWrapper(resource, attributes, new Date(baseDate.getTime()+300000)); Assert.assertEquals(1000.0, CollectionResourceWrapper.s_cache.get(attributeId).value); Assert.assertEquals(5.0, wrapper.getAttributeValue(attributeName)); Assert.assertEquals(2500.0, CollectionResourceWrapper.s_cache.get(attributeId).value); Assert.assertEquals(5.0, wrapper.getAttributeValue(attributeName)); Assert.assertEquals(2500.0, CollectionResourceWrapper.s_cache.get(attributeId).value); Assert.assertEquals(5.0, wrapper.getAttributeValue(attributeName)); Assert.assertEquals(2500.0, CollectionResourceWrapper.s_cache.get(attributeId).value); attribute = addAttributeToCollectionResource(resource, attributeName, "counter", "0", "5500"); attributes.put(attribute.getName(), attribute); wrapper = createWrapper(resource, attributes, new Date(baseDate.getTime()+600000)); Assert.assertEquals(2500.0, CollectionResourceWrapper.s_cache.get(attributeId).value); Assert.assertEquals(10.0, wrapper.getAttributeValue(attributeName)); Assert.assertEquals(5500.0, CollectionResourceWrapper.s_cache.get(attributeId).value); Assert.assertEquals(10.0, wrapper.getAttributeValue(attributeName)); Assert.assertEquals(5500.0, CollectionResourceWrapper.s_cache.get(attributeId).value); Assert.assertEquals(10.0, wrapper.getAttributeValue(attributeName)); Assert.assertEquals(5500.0, CollectionResourceWrapper.s_cache.get(attributeId).value); }
private Double getCounterValue(String id, Double current) { synchronized (m_localCache) { if (m_localCache.containsKey(id) == false) { CacheEntry last = s_cache.put(id, new CacheEntry(m_collectionTimestamp, current)); if (log().isDebugEnabled()) { log().debug("getCounterValue: id=" + id + ", last=" + (last==null ? last : last.value +"@"+last.timestamp) + ", current=" + current); } if (last == null) { m_localCache.put(id, Double.NaN); log().info("getCounterValue: unknown last value for " + id + ", ignoring current"); } else { Double delta = current.doubleValue() - last.value.doubleValue(); if (delta < 0) { double newDelta = delta.doubleValue(); newDelta += Math.pow(2, 32); if (newDelta < 0) { newDelta += Math.pow(2, 64) - Math.pow(2, 32); } log().info("getCounterValue: " + id + "(counter) wrapped counter adjusted last=" + last.value +"@"+last.timestamp + ", current=" + current + ", olddelta=" + delta + ", newdelta=" + newDelta); delta = newDelta; } long interval = ( m_collectionTimestamp.getTime() - last.timestamp.getTime() ) / 1000; if (interval > 0) { log().debug("getCounterValue: id=" + id + ", value=" + delta/interval + ", delta=" + delta + ", interval=" + interval); m_localCache.put(id, delta / interval); } else { log().info("getCounterValue: invalid zero-length rate interval for " + id + ", returning rate of zero"); m_localCache.put(id, 0.0); s_cache.put(id, last); } } } Double value = m_localCache.get(id); if (value == null) { log().error("getCounterValue: value was not calculated correctly for " + id + ", using NaN"); m_localCache.put(id, Double.NaN); return Double.NaN; } else { return value; } } }
CollectionResourceWrapper { private Double getCounterValue(String id, Double current) { synchronized (m_localCache) { if (m_localCache.containsKey(id) == false) { CacheEntry last = s_cache.put(id, new CacheEntry(m_collectionTimestamp, current)); if (log().isDebugEnabled()) { log().debug("getCounterValue: id=" + id + ", last=" + (last==null ? last : last.value +"@"+last.timestamp) + ", current=" + current); } if (last == null) { m_localCache.put(id, Double.NaN); log().info("getCounterValue: unknown last value for " + id + ", ignoring current"); } else { Double delta = current.doubleValue() - last.value.doubleValue(); if (delta < 0) { double newDelta = delta.doubleValue(); newDelta += Math.pow(2, 32); if (newDelta < 0) { newDelta += Math.pow(2, 64) - Math.pow(2, 32); } log().info("getCounterValue: " + id + "(counter) wrapped counter adjusted last=" + last.value +"@"+last.timestamp + ", current=" + current + ", olddelta=" + delta + ", newdelta=" + newDelta); delta = newDelta; } long interval = ( m_collectionTimestamp.getTime() - last.timestamp.getTime() ) / 1000; if (interval > 0) { log().debug("getCounterValue: id=" + id + ", value=" + delta/interval + ", delta=" + delta + ", interval=" + interval); m_localCache.put(id, delta / interval); } else { log().info("getCounterValue: invalid zero-length rate interval for " + id + ", returning rate of zero"); m_localCache.put(id, 0.0); s_cache.put(id, last); } } } Double value = m_localCache.get(id); if (value == null) { log().error("getCounterValue: value was not calculated correctly for " + id + ", using NaN"); m_localCache.put(id, Double.NaN); return Double.NaN; } else { return value; } } } }
CollectionResourceWrapper { private Double getCounterValue(String id, Double current) { synchronized (m_localCache) { if (m_localCache.containsKey(id) == false) { CacheEntry last = s_cache.put(id, new CacheEntry(m_collectionTimestamp, current)); if (log().isDebugEnabled()) { log().debug("getCounterValue: id=" + id + ", last=" + (last==null ? last : last.value +"@"+last.timestamp) + ", current=" + current); } if (last == null) { m_localCache.put(id, Double.NaN); log().info("getCounterValue: unknown last value for " + id + ", ignoring current"); } else { Double delta = current.doubleValue() - last.value.doubleValue(); if (delta < 0) { double newDelta = delta.doubleValue(); newDelta += Math.pow(2, 32); if (newDelta < 0) { newDelta += Math.pow(2, 64) - Math.pow(2, 32); } log().info("getCounterValue: " + id + "(counter) wrapped counter adjusted last=" + last.value +"@"+last.timestamp + ", current=" + current + ", olddelta=" + delta + ", newdelta=" + newDelta); delta = newDelta; } long interval = ( m_collectionTimestamp.getTime() - last.timestamp.getTime() ) / 1000; if (interval > 0) { log().debug("getCounterValue: id=" + id + ", value=" + delta/interval + ", delta=" + delta + ", interval=" + interval); m_localCache.put(id, delta / interval); } else { log().info("getCounterValue: invalid zero-length rate interval for " + id + ", returning rate of zero"); m_localCache.put(id, 0.0); s_cache.put(id, last); } } } Double value = m_localCache.get(id); if (value == null) { log().error("getCounterValue: value was not calculated correctly for " + id + ", using NaN"); m_localCache.put(id, Double.NaN); return Double.NaN; } else { return value; } } } CollectionResourceWrapper(Date collectionTimestamp, int nodeId, String hostAddress, String serviceName, RrdRepository repository, CollectionResource resource, Map<String, CollectionAttribute> attributes); }
CollectionResourceWrapper { private Double getCounterValue(String id, Double current) { synchronized (m_localCache) { if (m_localCache.containsKey(id) == false) { CacheEntry last = s_cache.put(id, new CacheEntry(m_collectionTimestamp, current)); if (log().isDebugEnabled()) { log().debug("getCounterValue: id=" + id + ", last=" + (last==null ? last : last.value +"@"+last.timestamp) + ", current=" + current); } if (last == null) { m_localCache.put(id, Double.NaN); log().info("getCounterValue: unknown last value for " + id + ", ignoring current"); } else { Double delta = current.doubleValue() - last.value.doubleValue(); if (delta < 0) { double newDelta = delta.doubleValue(); newDelta += Math.pow(2, 32); if (newDelta < 0) { newDelta += Math.pow(2, 64) - Math.pow(2, 32); } log().info("getCounterValue: " + id + "(counter) wrapped counter adjusted last=" + last.value +"@"+last.timestamp + ", current=" + current + ", olddelta=" + delta + ", newdelta=" + newDelta); delta = newDelta; } long interval = ( m_collectionTimestamp.getTime() - last.timestamp.getTime() ) / 1000; if (interval > 0) { log().debug("getCounterValue: id=" + id + ", value=" + delta/interval + ", delta=" + delta + ", interval=" + interval); m_localCache.put(id, delta / interval); } else { log().info("getCounterValue: invalid zero-length rate interval for " + id + ", returning rate of zero"); m_localCache.put(id, 0.0); s_cache.put(id, last); } } } Double value = m_localCache.get(id); if (value == null) { log().error("getCounterValue: value was not calculated correctly for " + id + ", using NaN"); m_localCache.put(id, Double.NaN); return Double.NaN; } else { return value; } } } CollectionResourceWrapper(Date collectionTimestamp, int nodeId, String hostAddress, String serviceName, RrdRepository repository, CollectionResource resource, Map<String, CollectionAttribute> attributes); int getNodeId(); String getHostAddress(); String getServiceName(); RrdRepository getRepository(); String getDsLabel(); void setDsLabel(String dsLabel); String getInstance(); String getInstanceLabel(); String getResourceTypeName(); String getResourceId(); String getIfLabel(); String getIfIndex(); boolean isAnInterfaceResource(); boolean isValidInterfaceResource(); Double getAttributeValue(String ds); String getFieldValue(String ds); @Override String toString(); }
CollectionResourceWrapper { private Double getCounterValue(String id, Double current) { synchronized (m_localCache) { if (m_localCache.containsKey(id) == false) { CacheEntry last = s_cache.put(id, new CacheEntry(m_collectionTimestamp, current)); if (log().isDebugEnabled()) { log().debug("getCounterValue: id=" + id + ", last=" + (last==null ? last : last.value +"@"+last.timestamp) + ", current=" + current); } if (last == null) { m_localCache.put(id, Double.NaN); log().info("getCounterValue: unknown last value for " + id + ", ignoring current"); } else { Double delta = current.doubleValue() - last.value.doubleValue(); if (delta < 0) { double newDelta = delta.doubleValue(); newDelta += Math.pow(2, 32); if (newDelta < 0) { newDelta += Math.pow(2, 64) - Math.pow(2, 32); } log().info("getCounterValue: " + id + "(counter) wrapped counter adjusted last=" + last.value +"@"+last.timestamp + ", current=" + current + ", olddelta=" + delta + ", newdelta=" + newDelta); delta = newDelta; } long interval = ( m_collectionTimestamp.getTime() - last.timestamp.getTime() ) / 1000; if (interval > 0) { log().debug("getCounterValue: id=" + id + ", value=" + delta/interval + ", delta=" + delta + ", interval=" + interval); m_localCache.put(id, delta / interval); } else { log().info("getCounterValue: invalid zero-length rate interval for " + id + ", returning rate of zero"); m_localCache.put(id, 0.0); s_cache.put(id, last); } } } Double value = m_localCache.get(id); if (value == null) { log().error("getCounterValue: value was not calculated correctly for " + id + ", using NaN"); m_localCache.put(id, Double.NaN); return Double.NaN; } else { return value; } } } CollectionResourceWrapper(Date collectionTimestamp, int nodeId, String hostAddress, String serviceName, RrdRepository repository, CollectionResource resource, Map<String, CollectionAttribute> attributes); int getNodeId(); String getHostAddress(); String getServiceName(); RrdRepository getRepository(); String getDsLabel(); void setDsLabel(String dsLabel); String getInstance(); String getInstanceLabel(); String getResourceTypeName(); String getResourceId(); String getIfLabel(); String getIfIndex(); boolean isAnInterfaceResource(); boolean isValidInterfaceResource(); Double getAttributeValue(String ds); String getFieldValue(String ds); @Override String toString();  }