Search in sources :

Example 16 with CollectionAttribute

use of org.opennms.netmgt.collection.api.CollectionAttribute in project opennms by OpenNMS.

the class WSManCollectorTest method canCollectFromMultivaluedKeyUsingIndexOf.

@Test
public void canCollectFromMultivaluedKeyUsingIndexOf() {
    /* The iDrac provides the following keys in the DCIM_ComputerSystem entry:
         *  <n1:IdentifyingDescriptions>CIM:GUID</n1:IdentifyingDescriptions>
         *  <n1:IdentifyingDescriptions>CIM:Tag</n1:IdentifyingDescriptions>
         *  <n1:IdentifyingDescriptions>DCIM:ServiceTag</n1:IdentifyingDescriptions>
         *  <n1:OtherIdentifyingInfo>44454C4C-3700-104A-8052-C3C04BB25031</n1:OtherIdentifyingInfo>
         *  <n1:OtherIdentifyingInfo>mainsystemchassis</n1:OtherIdentifyingInfo>
         *  <n1:OtherIdentifyingInfo>C7BBBP1</n1:OtherIdentifyingInfo>
         *
         * We want to be able to collect the value of 'OtherIdentifyingInfo' at the same
         * index where 'IdentifyingDescriptions' has the value of 'DCIM:ServiceTag'.
         */
    Group group = new Group();
    group.setName("DCIM_ComputerSystem");
    Attrib attr = new Attrib();
    attr.setName("OtherIdentifyingInfo");
    attr.setAlias("ServiceTag");
    attr.setIndexOf("#IdentifyingDescriptions matches '.*ServiceTag'");
    attr.setType(AttributeType.STRING);
    group.addAttrib(attr);
    CollectionAgent agent = mock(CollectionAgent.class);
    when(agent.getStorageResourcePath()).thenReturn(ResourcePath.get());
    CollectionSetBuilder builder = new CollectionSetBuilder(agent);
    Supplier<Resource> resourceSupplier = () -> mock(NodeLevelResource.class);
    XMLTag xmlTag = XMLDoc.newDocument(true).addRoot("body").addTag("DCIM_ComputerSystem").addTag("IdentifyingDescriptions").setText("CIM:GUID").addTag("IdentifyingDescriptions").setText("DCIM:ServiceTag").addTag("IdentifyingDescriptions").setText("CIM:Tag").addTag("OtherIdentifyingInfo").setText("44454C4C-3700-104A-8052-C3C04BB25031").addTag("OtherIdentifyingInfo").setText("C7BBBP1").addTag("OtherIdentifyingInfo").setText("mainsystemchassis");
    List<Node> nodes = xmlTag.gotoRoot().getChildElement().stream().map(el -> (Node) el).collect(Collectors.toList());
    WsManCollector.processEnumerationResults(group, builder, resourceSupplier, nodes);
    // Verify
    Map<String, CollectionAttribute> attributesByName = CollectionSetUtils.getAttributesByName(builder.build());
    assertEquals("C7BBBP1", attributesByName.get("ServiceTag").getStringValue());
}
Also used : StorageStrategy(org.opennms.netmgt.config.datacollection.StorageStrategy) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) WSManDataCollectionConfigDao(org.opennms.netmgt.dao.WSManDataCollectionConfigDao) Arrays(java.util.Arrays) CollectionStatus(org.opennms.netmgt.collection.api.CollectionStatus) ResourceType(org.opennms.netmgt.config.datacollection.ResourceType) WSManConfigDao(org.opennms.netmgt.dao.WSManConfigDao) Supplier(java.util.function.Supplier) CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) XMLDoc(com.mycila.xmltool.XMLDoc) SiblingColumnStorageStrategy(org.opennms.netmgt.dao.support.SiblingColumnStorageStrategy) CollectionException(org.opennms.netmgt.collection.api.CollectionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Node(org.w3c.dom.Node) AttributeType(org.opennms.netmgt.collection.api.AttributeType) GenericTypeResource(org.opennms.netmgt.collection.support.builder.GenericTypeResource) Collection(org.opennms.netmgt.config.wsman.Collection) Attrib(org.opennms.netmgt.config.wsman.Attrib) OnmsNode(org.opennms.netmgt.model.OnmsNode) Resource(org.opennms.netmgt.collection.support.builder.Resource) NodeDao(org.opennms.netmgt.dao.api.NodeDao) InetAddressUtils(org.opennms.core.utils.InetAddressUtils) CollectionAttribute(org.opennms.netmgt.collection.api.CollectionAttribute) WSManClientFactory(org.opennms.core.wsman.WSManClientFactory) XMLTag(com.mycila.xmltool.XMLTag) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Group(org.opennms.netmgt.config.wsman.Group) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) Sets(com.google.common.collect.Sets) Definition(org.opennms.netmgt.config.wsman.Definition) Matchers.any(org.mockito.Matchers.any) PersistenceSelectorStrategy(org.opennms.netmgt.config.datacollection.PersistenceSelectorStrategy) CollectionInitializationException(org.opennms.netmgt.collection.api.CollectionInitializationException) List(java.util.List) PersistAllSelectorStrategy(org.opennms.netmgt.collection.support.PersistAllSelectorStrategy) Assert.assertFalse(org.junit.Assert.assertFalse) CollectionSetUtils(org.opennms.core.collection.test.CollectionSetUtils) ResourcePath(org.opennms.netmgt.model.ResourcePath) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) Parameter(org.opennms.netmgt.config.datacollection.Parameter) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Group(org.opennms.netmgt.config.wsman.Group) CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) Node(org.w3c.dom.Node) OnmsNode(org.opennms.netmgt.model.OnmsNode) GenericTypeResource(org.opennms.netmgt.collection.support.builder.GenericTypeResource) Resource(org.opennms.netmgt.collection.support.builder.Resource) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) XMLTag(com.mycila.xmltool.XMLTag) Attrib(org.opennms.netmgt.config.wsman.Attrib) CollectionAttribute(org.opennms.netmgt.collection.api.CollectionAttribute) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) Test(org.junit.Test)

Example 17 with CollectionAttribute

use of org.opennms.netmgt.collection.api.CollectionAttribute in project opennms by OpenNMS.

the class AliasedGroup method visit.

/**
 * {@inheritDoc}
 */
@Override
public void visit(CollectionSetVisitor visitor) {
    visitor.visitGroup(this);
    for (CollectionAttribute attr : getAttributes()) {
        AliasedAttribute aliased = new AliasedAttribute(getResource(), (SnmpAttribute) attr);
        LOG.debug("visiting at aliased  = {}", aliased);
        aliased.visit(visitor);
    }
    visitor.completeGroup(this);
}
Also used : CollectionAttribute(org.opennms.netmgt.collection.api.CollectionAttribute)

Example 18 with CollectionAttribute

use of org.opennms.netmgt.collection.api.CollectionAttribute in project opennms by OpenNMS.

the class SnmpAttributeTest method testPersisting.

@Ignore
private void testPersisting(String matchValue, SnmpValue snmpValue) throws Exception {
    OnmsNode node = new OnmsNode();
    node.setId(3);
    OnmsIpInterface ipInterface = new OnmsIpInterface();
    ipInterface.setId(1);
    ipInterface.setNode(node);
    ipInterface.setIpAddress(InetAddressUtils.addr("192.168.1.1"));
    // It used to be 3, but I think it is more correct to use getStoreDir from DefaultCollectionAgentService on DefaultCollectionAgent (NMS-7516)
    expect(m_ipInterfaceDao.load(1)).andReturn(ipInterface).times(5);
    expect(m_rrdStrategy.getDefaultFileExtension()).andReturn(".myLittleEasyMockedStrategyAndMe").anyTimes();
    expect(m_rrdStrategy.createDefinition(isA(String.class), isA(String.class), isA(String.class), anyInt(), isAList(RrdDataSource.class), isAList(String.class))).andReturn(new Object());
    m_rrdStrategy.createFile(isA(Object.class));
    expect(m_rrdStrategy.openFile(isA(String.class))).andReturn(new Object());
    m_rrdStrategy.updateFile(isA(Object.class), isA(String.class), matches(".*:" + matchValue));
    m_rrdStrategy.closeFile(isA(Object.class));
    m_mocks.replayAll();
    SnmpCollectionAgent agent = DefaultSnmpCollectionAgent.create(ipInterface.getId(), m_ipInterfaceDao, new MockPlatformTransactionManager());
    OnmsSnmpCollection snmpCollection = new OnmsSnmpCollection(agent, new ServiceParameters(new HashMap<String, Object>()), new MockDataCollectionConfig(), m_locationAwareSnmpClient);
    NodeResourceType resourceType = new NodeResourceType(agent, snmpCollection);
    NodeInfo nodeInfo = resourceType.getNodeInfo();
    MibObject mibObject = new MibObject();
    mibObject.setOid(".1.3.6.1.4.1.12238.55.9997.4.1.2.9.116.101.109.112.95.117.108.107.111");
    mibObject.setInstance("1");
    mibObject.setAlias("temp_ulko");
    mibObject.setType("gauge");
    NumericAttributeType attributeType = new NumericAttributeType(resourceType, snmpCollection.getName(), mibObject, new AttributeGroupType("foo", AttributeGroupType.IF_TYPE_IGNORE));
    attributeType.storeResult(new SnmpCollectionSet(agent, snmpCollection, m_locationAwareSnmpClient), null, new SnmpResult(mibObject.getSnmpObjId(), new SnmpInstId(mibObject.getInstance()), snmpValue));
    RrdRepository repository = createRrdRepository();
    repository.setRraList(Collections.singletonList("RRA:AVERAGE:0.5:1:2016"));
    RrdPersisterFactory persisterFactory = new RrdPersisterFactory();
    persisterFactory.setRrdStrategy(m_rrdStrategy);
    persisterFactory.setResourceStorageDao(m_resourceStorageDao);
    CollectionSetVisitor persister = persisterFactory.createPersister(new ServiceParameters(Collections.emptyMap()), repository);
    final AtomicInteger count = new AtomicInteger(0);
    nodeInfo.visit(new CollectionSetVisitorWrapper(persister) {

        @Override
        public void visitAttribute(CollectionAttribute attribute) {
            super.visitAttribute(attribute);
            count.incrementAndGet();
        }
    });
    assertEquals(1, count.get());
}
Also used : HashMap(java.util.HashMap) CollectionSetVisitor(org.opennms.netmgt.collection.api.CollectionSetVisitor) MockDataCollectionConfig(org.opennms.netmgt.mock.MockDataCollectionConfig) SnmpInstId(org.opennms.netmgt.snmp.SnmpInstId) RrdPersisterFactory(org.opennms.netmgt.collection.persistence.rrd.RrdPersisterFactory) RrdDataSource(org.opennms.netmgt.rrd.RrdDataSource) MockPlatformTransactionManager(org.opennms.core.test.MockPlatformTransactionManager) CollectionSetVisitorWrapper(org.opennms.netmgt.collection.support.CollectionSetVisitorWrapper) SnmpResult(org.opennms.netmgt.snmp.SnmpResult) OnmsNode(org.opennms.netmgt.model.OnmsNode) RrdRepository(org.opennms.netmgt.rrd.RrdRepository) CollectionAttribute(org.opennms.netmgt.collection.api.CollectionAttribute) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AttributeGroupType(org.opennms.netmgt.collection.api.AttributeGroupType) MibObject(org.opennms.netmgt.config.datacollection.MibObject) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) MibObject(org.opennms.netmgt.config.datacollection.MibObject) Ignore(org.junit.Ignore)

Example 19 with CollectionAttribute

use of org.opennms.netmgt.collection.api.CollectionAttribute in project opennms by OpenNMS.

the class BasePersisterTest method testPersistStringAttributeUsingBuilder.

/**
 * Test for bug #1817 where a string attribute will get persisted to
 * both strings.properties and an RRD file if it is a numeric value.
 */
@Test
public void testPersistStringAttributeUsingBuilder() throws Exception {
    initPersister();
    File nodeDir = m_fileAnticipator.expecting(getSnmpRrdDirectory(), m_node.getId().toString());
    m_fileAnticipator.expecting(nodeDir, RrdResourceAttributeUtils.STRINGS_PROPERTIES_FILE_NAME);
    CollectionAttribute attribute = buildStringAttribute();
    m_persister.pushShouldPersist(attribute.getResource());
    m_persister.pushShouldPersist(attribute);
    m_persister.setBuilder(m_persister.createBuilder(attribute.getResource(), attribute.getName(), Sets.newHashSet(attribute.getAttributeType())));
    // This will end up calling m_persister.persistStringAttribute(attribute);
    m_persister.storeAttribute(attribute);
    m_persister.commitBuilder();
    m_persister.popShouldPersist();
}
Also used : CollectionAttribute(org.opennms.netmgt.collection.api.CollectionAttribute) File(java.io.File) Test(org.junit.Test)

Example 20 with CollectionAttribute

use of org.opennms.netmgt.collection.api.CollectionAttribute in project opennms by OpenNMS.

the class CollectionResourceWrapperIT method addAttributeToCollectionResource.

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);
}
Also used : CollectionAttribute(org.opennms.netmgt.collection.api.CollectionAttribute) NumericAttributeType(org.opennms.netmgt.collectd.NumericAttributeType) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SnmpAttribute(org.opennms.netmgt.collectd.SnmpAttribute) AttributeType(org.opennms.netmgt.collection.api.AttributeType) SnmpAttributeType(org.opennms.netmgt.collectd.SnmpAttributeType) NumericAttributeType(org.opennms.netmgt.collectd.NumericAttributeType) AttributeGroupType(org.opennms.netmgt.collection.api.AttributeGroupType) Persister(org.opennms.netmgt.collection.api.Persister) MibObject(org.opennms.netmgt.config.datacollection.MibObject) SnmpAttributeType(org.opennms.netmgt.collectd.SnmpAttributeType)

Aggregations

CollectionAttribute (org.opennms.netmgt.collection.api.CollectionAttribute)30 Test (org.junit.Test)16 HashMap (java.util.HashMap)11 SnmpAttribute (org.opennms.netmgt.collectd.SnmpAttribute)9 SnmpCollectionAgent (org.opennms.netmgt.collectd.SnmpCollectionAgent)8 SnmpCollectionResource (org.opennms.netmgt.collectd.SnmpCollectionResource)8 ServiceParameters (org.opennms.netmgt.collection.api.ServiceParameters)6 Map (java.util.Map)5 CollectionResource (org.opennms.netmgt.collection.api.CollectionResource)5 Parameter (org.opennms.netmgt.config.datacollection.Parameter)5 PersistenceSelectorStrategy (org.opennms.netmgt.config.datacollection.PersistenceSelectorStrategy)5 ResourceType (org.opennms.netmgt.config.datacollection.ResourceType)5 StorageStrategy (org.opennms.netmgt.config.datacollection.StorageStrategy)5 OnmsNode (org.opennms.netmgt.model.OnmsNode)5 Date (java.util.Date)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 AttributeGroupType (org.opennms.netmgt.collection.api.AttributeGroupType)4 AttributeType (org.opennms.netmgt.collection.api.AttributeType)4 CollectionSet (org.opennms.netmgt.collection.api.CollectionSet)4 Resource (org.opennms.netmgt.collection.support.builder.Resource)4