Search in sources :

Example 36 with CollectionSet

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

the class TcaCollectorIT method testCollector.

/**
 * Test collector.
 *
 * @throws Exception the exception
 */
@Test
public void testCollector() throws Exception {
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("collection", "default");
    // Create Collection Set
    TcaCollector collector = new TcaCollector();
    collector.setConfigDao(m_configDao);
    collector.setResourceStorageDao(m_resourceStorageDao);
    collector.setResourceTypesDao(m_resourceTypesDao);
    collector.setLocationAwareSnmpClient(m_client);
    CollectionSetVisitor persister = m_persisterFactory.createOneToOnePersister(new ServiceParameters(parameters), collector.getRrdRepository("default"), false, false);
    // Setup SNMP Value Handling
    SnmpValueFactory valFac = SnmpUtils.getValueFactory();
    SnmpObjId peer1 = SnmpObjId.get(".1.3.6.1.4.1.27091.3.1.6.1.2.171.19.37.60");
    SnmpObjId peer2 = SnmpObjId.get(".1.3.6.1.4.1.27091.3.1.6.1.2.171.19.38.70");
    // Collect and Persist Data - Step 1
    CollectionSet collectionSet = collector.collect(m_collectionAgent, parameters);
    validateCollectionSet(collectionSet);
    collectionSet.visit(persister);
    // Generate new SNMP Data
    final StringBuilder sb = new StringBuilder("|25|");
    long ts = 1327451787l;
    for (int i = 0; i < 25; i++) {
        sb.append(ts++);
        sb.append(",12,-1,12,-2,1|");
    }
    // Get Current Values
    SnmpValue v1a = SnmpUtils.get(m_collectionAgent.getAgentConfig(), peer1);
    SnmpValue v2a = SnmpUtils.get(m_collectionAgent.getAgentConfig(), peer2);
    // Set New Values
    SnmpUtils.set(m_collectionAgent.getAgentConfig(), peer1, valFac.getOctetString(sb.toString().getBytes()));
    SnmpUtils.set(m_collectionAgent.getAgentConfig(), peer2, valFac.getOctetString(sb.toString().getBytes()));
    // Validate New Values
    SnmpValue v1b = SnmpUtils.get(m_collectionAgent.getAgentConfig(), peer1);
    SnmpValue v2b = SnmpUtils.get(m_collectionAgent.getAgentConfig(), peer2);
    Assert.assertFalse(v1a.toDisplayString().equals(v1b.toDisplayString()));
    Assert.assertFalse(v2a.toDisplayString().equals(v2b.toDisplayString()));
    // Collect and Persist Data - Step 2
    collectionSet = collector.collect(m_collectionAgent, parameters);
    validateCollectionSet(collectionSet);
    collectionSet.visit(persister);
    // Validate Persisted Data
    Path pathToJrbFile = getSnmpRoot().toPath().resolve(Paths.get("1", TcaCollectionHandler.RESOURCE_TYPE_NAME, "171.19.37.60", TcaCollectionHandler.INBOUND_DELAY + m_rrdStrategy.getDefaultFileExtension()));
    RrdDb jrb = new RrdDb(pathToJrbFile.toString());
    // According with the Fixed Step
    Assert.assertEquals(1, jrb.getArchive(0).getArcStep());
    // According with the Sample Data
    Assert.assertEquals(ts - 1, jrb.getArchive(0).getEndTime());
    Robin inboundDelay = jrb.getArchive(0).getRobin(0);
    for (int i = inboundDelay.getSize() - 49; i < inboundDelay.getSize() - 25; i++) {
        Assert.assertEquals(new Double(11), Double.valueOf(inboundDelay.getValue(i)));
    }
    for (int i = inboundDelay.getSize() - 24; i < inboundDelay.getSize(); i++) {
        Assert.assertEquals(new Double(12), Double.valueOf(inboundDelay.getValue(i)));
    }
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) CollectionSetVisitor(org.opennms.netmgt.collection.api.CollectionSetVisitor) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) SnmpValueFactory(org.opennms.netmgt.snmp.SnmpValueFactory) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) RrdDb(org.jrobin.core.RrdDb) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) Robin(org.jrobin.core.Robin) Test(org.junit.Test)

Example 37 with CollectionSet

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

the class CollectCommand method execute.

@Override
public Void execute() {
    final ServiceCollector collector = serviceCollectorRegistry.getCollectorByClassName(className);
    if (collector == null) {
        System.out.printf("No collector found with class name '%s'. Aborting.\n", className);
        return null;
    }
    try {
        // The collector may not have been initialized - initialize it
        collector.initialize();
    } catch (CollectionInitializationException e) {
        System.out.println("Failed to initialize the collector. Aborting.");
        e.printStackTrace();
        return null;
    }
    final CollectionAgent agent = getCollectionAgent();
    final CompletableFuture<CollectionSet> future = locationAwareCollectorClient.collect().withAgent(agent).withSystemId(systemId).withCollector(collector).withTimeToLive(ttlInMs).withAttributes(parse(attributes)).execute();
    while (true) {
        try {
            try {
                CollectionSet collectionSet = future.get(1, TimeUnit.SECONDS);
                if (CollectionStatus.SUCCEEDED.equals(collectionSet.getStatus())) {
                    printCollectionSet(collectionSet);
                } else {
                    System.out.printf("\nThe collector returned a collection set with status: %s\n", collectionSet.getStatus());
                }
            } catch (InterruptedException e) {
                System.out.println("\nInterrupted.");
            } catch (ExecutionException e) {
                System.out.printf("\nCollect failed with:", e);
                e.printStackTrace();
                System.out.println();
            }
            break;
        } catch (TimeoutException e) {
        // pass
        }
        System.out.print(".");
        System.out.flush();
    }
    return null;
}
Also used : CollectionInitializationException(org.opennms.netmgt.collection.api.CollectionInitializationException) ServiceCollector(org.opennms.netmgt.collection.api.ServiceCollector) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) ExecutionException(java.util.concurrent.ExecutionException) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) TimeoutException(java.util.concurrent.TimeoutException)

Example 38 with CollectionSet

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

the class CollectorTestUtils method collectNTimes.

public static void collectNTimes(RrdStrategy<?, ?> rrdStrategy, ResourceStorageDao resourceStorageDao, CollectionSpecification spec, CollectionAgent agent, int numUpdates) throws InterruptedException, CollectionException {
    for (int i = 0; i < numUpdates; i++) {
        // now do the actual collection
        CollectionSet collectionSet = spec.collect(agent);
        assertEquals("collection status", CollectionStatus.SUCCEEDED, collectionSet.getStatus());
        persistCollectionSet(rrdStrategy, resourceStorageDao, spec, collectionSet);
        System.err.println("COLLECTION " + i + " FINISHED");
        // need a one second time elapse to update the RRD
        Thread.sleep(1010);
    }
}
Also used : CollectionSet(org.opennms.netmgt.collection.api.CollectionSet)

Example 39 with CollectionSet

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

the class CollectorTestUtils method failToCollectNTimes.

public static void failToCollectNTimes(RrdStrategy<?, ?> rrdStrategy, ResourceStorageDao resourceStorageDao, CollectionSpecification spec, CollectionAgent agent, int numUpdates) throws InterruptedException, CollectionException {
    for (int i = 0; i < numUpdates; i++) {
        // now do the actual collection
        CollectionSet collectionSet = spec.collect(agent);
        assertEquals("collection status", CollectionStatus.FAILED, collectionSet.getStatus());
        persistCollectionSet(rrdStrategy, resourceStorageDao, spec, collectionSet);
        System.err.println("COLLECTION " + i + " FINISHED");
        // need a one second time elapse to update the RRD
        Thread.sleep(1010);
    }
}
Also used : CollectionSet(org.opennms.netmgt.collection.api.CollectionSet)

Example 40 with CollectionSet

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

the class WSManCollectorTest method canProcessEnumerationResults.

@Test
public void canProcessEnumerationResults() {
    Group group = new Group();
    group.setName("ComputerSystem");
    addAttribute(group, "PrimaryStatus", "GaugeWithValue", AttributeType.GAUGE);
    addAttribute(group, "!PrimaryStatus!", "GaugeWithoutValue", AttributeType.GAUGE);
    addAttribute(group, "ElementName", "StringWithValue", AttributeType.STRING);
    addAttribute(group, "!ElementName!", "StringWithoutValue", AttributeType.STRING);
    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("ElementName").setText("Computer System").addTag("PrimaryStatus").setText("42.1").addTag("OtherIdentifyingInfo").setText("ANONYMIZED01").addTag("OtherIdentifyingInfo").setText("mainsystemchassis").addTag("OtherIdentifyingInfo").setText("ANONYMIZED02");
    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());
    assertFalse("The CollectionSet should not contain attributes for missing values.", attributesByName.containsKey("GaugeWithoutValue"));
    assertFalse("The CollectionSet should not contain attributes for missing values.", attributesByName.containsKey("StringWithoutValue"));
    assertEquals(42.1, attributesByName.get("GaugeWithValue").getNumericValue().doubleValue(), 2);
    assertEquals("Computer System", attributesByName.get("StringWithValue").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) CollectionAttribute(org.opennms.netmgt.collection.api.CollectionAttribute) 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) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) Test(org.junit.Test)

Aggregations

CollectionSet (org.opennms.netmgt.collection.api.CollectionSet)52 Test (org.junit.Test)30 HashMap (java.util.HashMap)17 CollectionAgent (org.opennms.netmgt.collection.api.CollectionAgent)17 CollectionSetBuilder (org.opennms.netmgt.collection.support.builder.CollectionSetBuilder)11 NodeLevelResource (org.opennms.netmgt.collection.support.builder.NodeLevelResource)11 CollectionSetVisitor (org.opennms.netmgt.collection.api.CollectionSetVisitor)10 ServiceParameters (org.opennms.netmgt.collection.api.ServiceParameters)10 File (java.io.File)9 RrdRepository (org.opennms.netmgt.rrd.RrdRepository)8 RrdDb (org.jrobin.core.RrdDb)7 JUnitCollector (org.opennms.core.collection.test.JUnitCollector)7 OnmsNode (org.opennms.netmgt.model.OnmsNode)7 JUnitHttpServer (org.opennms.core.test.http.annotations.JUnitHttpServer)6 JUnitSnmpAgent (org.opennms.core.test.snmp.annotations.JUnitSnmpAgent)6 Map (java.util.Map)5 Datasource (org.jrobin.core.Datasource)5 Date (java.util.Date)4 GenericTypeResource (org.opennms.netmgt.collection.support.builder.GenericTypeResource)4 ResourcePath (org.opennms.netmgt.model.ResourcePath)4