resource = new CollectionResourceWrapper(date, 0, null, null, null, null, null, null); String dsLabelValue = resource.getFieldValue(resource.getDsLabel()); if (dsLabelValue == null) dsLabelValue = UNKNOWN; bldr.setNodeid(resource.getNodeId()); bldr.setService(resource.getServiceName()); bldr.setInterface(addr(resource.getHostAddress())); if (resource.isAnInterfaceResource()) { dsLabelValue = resource.getIfLabel(); bldr.addParam("ifLabel", resource.getIfLabel()); bldr.addParam("ifIndex", resource.getIfIndex()); String ipaddr = resource.getIfInfoValue("ipaddr"); if (ipaddr != null && !"0.0.0.0".equals(ipaddr)) { bldr.addParam("ifIpAddress", ipaddr); if (resource.isNodeResource() && UNKNOWN.equals(dsLabelValue)) { dsLabelValue = CollectionResource.RESOURCE_TYPE_NODE; String defaultInstance = resource.isNodeResource() ? CollectionResource.RESOURCE_TYPE_NODE : UNKNOWN; bldr.addParam("instance", resource.getInstance() == null ? defaultInstance : resource.getInstance()); bldr.addParam("instanceLabel", resource.getInstanceLabel() == null ? defaultInstance : resource.getInstanceLabel());
return m_hostAddress; } else if ("iflabel".equalsIgnoreCase(ds)) { return getIfLabel(); } else if ("id".equalsIgnoreCase(ds)) { return m_resource.getPath().getName().toString(); if (isAnInterfaceResource()) { retval = getIfInfoValue(ds); if (retval != null) { return retval; Double d = getAttributeValue(ds); if (d != null) { return d.toString();
/** {@inheritDoc} */ @Override protected boolean passedThresholdFilters(CollectionResourceWrapper resource, ThresholdEntity thresholdEntity) { if (resource.isAnInterfaceResource() && !resource.isValidInterfaceResource()) { LOG.info("passedThresholdFilters: Could not get data interface information for '{}' or this interface has an invalid ifIndex. Not evaluating threshold.", resource.getIfLabel()); return false; } return super.passedThresholdFilters(resource, thresholdEntity); }
/** {@inheritDoc} */ public List<Event> applyThresholds(CollectionResource resource, Map<String, CollectionAttribute> attributesMap, Date collectionTimestamp) { if (!isCollectionEnabled(resource)) { LOG.debug("applyThresholds: Ignoring resource {} because data collection is disabled for this resource.", resource); return new LinkedList<>(); } CollectionResourceWrapper resourceWrapper = new CollectionResourceWrapper( collectionTimestamp, m_nodeId, m_hostAddress, m_serviceName, m_repository, resource, attributesMap, m_resourceStorageDao); resourceWrapper.setCounterReset(counterReset); return applyThresholds(resourceWrapper, attributesMap); }
if (isAnInterfaceResource() && ("snmpifspeed".equalsIgnoreCase(ds) || "snmpiftype".equalsIgnoreCase(ds))) { // Get Value from ifInfo only for Interface Resource String value = getIfInfoValue(ds); if (value != null) { try { return current; } else { return getCounterValue(id, current);
Assert.assertEquals(node.getId().intValue(), wrapper.getNodeId()); Assert.assertEquals("127.0.0.1", wrapper.getHostAddress()); // Should be the address of the SNMP Agent (Bug 3808) 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()); 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")); // IfLabel is called only once Assert.assertEquals("eth0-001122334455", wrapper.getInstanceLabel()); Assert.assertEquals("nodeSource[JUnit:T001].interfaceSnmp[eth0-001122334455]", wrapper.getResourceId().toString());
String resourceType = getResourceTypeName(); String resourceLabel = getInstanceLabel(); if (CollectionResource.RESOURCE_TYPE_NODE.equals(resourceType)) { resourceType = "nodeSnmp"; String parentResourceName = Integer.toString(getNodeId());
Assert.assertEquals("opt", wrapper.getInstanceLabel()); Assert.assertEquals(new Double("10000.0"), wrapper.getAttributeValue(total.getName())); Assert.assertEquals("10000.0", wrapper.getFieldValue(total.getName()));
synchronized(m_thresholdGroups) { for (ThresholdGroup group : m_thresholdGroups) { Map<String,Set<ThresholdEntity>> entityMap = getEntityMap(group, resourceWrapper.getResourceTypeName()); if (entityMap != null) { for (final Entry<String, Set<ThresholdEntity>> entry : entityMap.entrySet()) { boolean relaxed = thresholdEntity.getThresholdConfig().getBasethresholddef().getRelaxed(); for(final String ds : requiredDatasources) { final Double dsValue = resourceWrapper.getAttributeValue(ds); if(dsValue == null) { LOG.info("applyThresholds: Could not get data source value for '{}', {}", ds, (relaxed ? "but the expression will be evaluated (relaxed mode enabled)" : "not evaluating threshold")); resourceWrapper.setDsLabel(thresholdEntity.getDatasourceLabel()); try { List<Event> thresholdEvents = thresholdEntity.evaluateAndCreateEvents(resourceWrapper, values, date);
m_resourceStorageDao = resourceStorageDao; if (isAnInterfaceResource()) { if (resource instanceof AliasedResource) { // TODO What about AliasedResource's custom attributes? m_iflabel = ((AliasedResource) resource).getInterfaceLabel(); } else if (resource instanceof LatencyCollectionResource) { String ipAddress = ((LatencyCollectionResource) resource).getIpAddress(); m_iflabel = IfLabelDaoImpl.getInstance().getIfLabel(getNodeId(), addr(ipAddress)); if (m_iflabel != null) { // See Bug 3488 m_ifInfo.putAll(IfLabelDaoImpl.getInstance().getInterfaceInfoFromIfLabel(getNodeId(), m_iflabel)); } else { LOG.info("Can't find ifLabel for latency resource {} on node {}", resource.getInstance(), getNodeId());
Assert.assertEquals(Double.valueOf(Double.NaN), wrapper.getAttributeValue(attributeName)); // Last value is null Assert.assertEquals(Double.valueOf(Double.NaN), wrapper.getAttributeValue(attributeName)); // Last value is null Assert.assertEquals(Double.valueOf(initialValue.doubleValue()), CollectionResourceWrapper.s_cache.get(attributeId).getValue()); Assert.assertTrue(wrapper.getAttributeValue(attributeName).isNaN()); Assert.assertFalse(CollectionResourceWrapper.s_cache.get(attributeId).getValue().isNaN()); Assert.assertEquals(Double.valueOf(300.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue()); Assert.assertEquals(Double.valueOf(1.0), wrapper.getAttributeValue(attributeName)); // 600 - 300 / 300 = 1.0 Assert.assertFalse(CollectionResourceWrapper.s_cache.get(attributeId).getValue().isNaN()); Assert.assertEquals(Double.valueOf(600.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue()); Assert.assertEquals(Double.valueOf(1.0), wrapper.getAttributeValue(attributeName)); // 900 - 600 / 300 = 1.0 attributes.put(attribute.getName(), attribute); wrapper = createWrapper(resource, attributes, new Date(baseDate.getTime() + 900000)); wrapper.setCounterReset(true); Assert.assertTrue(wrapper.getAttributeValue(attributeName).isNaN()); attributes.put(attribute.getName(), attribute); wrapper = createWrapper(resource, attributes, new Date(baseDate.getTime() + 1200000)); Assert.assertEquals(Double.valueOf(0.2), wrapper.getAttributeValue(attributeName)); // 120 - 60 / 300 = 0.2
/** {@inheritDoc} */ public List<Event> applyThresholds(String svcName, Map<String, Double> attributes) { LatencyCollectionResource latencyResource = new LatencyCollectionResource(svcName, m_hostAddress, m_location); LatencyCollectionAttributeType latencyType = new LatencyCollectionAttributeType(); Map<String, CollectionAttribute> attributesMap = new HashMap<String, CollectionAttribute>(); for (final Entry<String, Double> entry : attributes.entrySet()) { final String ds = entry.getKey(); attributesMap.put(ds, new LatencyCollectionAttribute(latencyResource, latencyType, ds, entry.getValue())); } //The timestamp is irrelevant; latency is never a COUNTER (which is the only reason the date is used). //Yes, we have to know a little too much about the implementation details of CollectionResourceWrapper to say that, but // we have little choice CollectionResourceWrapper resourceWrapper = new CollectionResourceWrapper(new Date(), m_nodeId, m_hostAddress, m_serviceName, m_repository, latencyResource, attributesMap, m_resourceStorageDao); return Collections.unmodifiableList(applyThresholds(resourceWrapper, attributesMap)); }
@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); }
instance = resource.getInstanceLabel();
count++; String attr = resource.getFieldValue(f.getField()); if (attr != null) { try {
@Test(expected=IllegalArgumentException.class) public void testBadderConstructorCall() throws Throwable { try { new CollectionResourceWrapper(null, -1, null, null, null, null, null, null); } catch (Throwable e) { e.printStackTrace(); throw e; } }
Assert.assertEquals(Double.valueOf(Double.NaN), wrapper.getAttributeValue(attributeName)); // Last value is null Assert.assertEquals(Double.valueOf(Double.NaN), wrapper.getAttributeValue(attributeName)); // Last value is null 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());
@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()); }
@Test(expected=IllegalArgumentException.class) public void testBadConstructorCall() throws Throwable { try { new CollectionResourceWrapper(null, 1, "127.0.0.1", "HTTP", null, null, null, null); } catch (Throwable e) { //e.printStackTrace(); throw e; } }
Assert.assertEquals(Double.valueOf(Double.NaN), wrapper.getAttributeValue(attributeName)); // Last value is null Assert.assertEquals(Double.valueOf(Double.NaN), wrapper.getAttributeValue(attributeName)); // Last value is null Assert.assertEquals(Double.valueOf(1000.0), CollectionResourceWrapper.s_cache.get(attributeId).getValue()); Assert.assertEquals(Double.valueOf(5.0), wrapper.getAttributeValue(attributeName)); 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()); 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());