Search in sources :

Example 6 with CollectionSet

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

the class NsclientCollectorTest method testCollector.

@Test
public void testCollector() throws Exception {
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("port", getServer().getLocalPort());
    NSClientCollector collector = getCollector(parameters);
    parameters.putAll(collector.getRuntimeAttributes(m_collectionAgent, parameters));
    CollectionSet collectionSet = collector.collect(m_collectionAgent, parameters);
    Assert.assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus());
    CountResourcesVisitor visitor = new CountResourcesVisitor();
    collectionSet.visit(visitor);
    Assert.assertEquals(42, visitor.getCount());
}
Also used : HashMap(java.util.HashMap) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) Test(org.junit.Test) AbstractNsclientTest(org.opennms.protocols.nsclient.AbstractNsclientTest)

Example 7 with CollectionSet

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

the class CollectionSetDTOTest method data.

@Parameters
public static Collection<Object[]> data() throws ParseException {
    CollectionAgent collectionAgent = mock(CollectionAgent.class);
    NodeLevelResource nodeLevelResource = new NodeLevelResource(1);
    InterfaceLevelResource interfaceLevelResource = new InterfaceLevelResource(nodeLevelResource, "eth0");
    ResourceType rt = mock(ResourceType.class, RETURNS_DEEP_STUBS);
    when(rt.getName()).thenReturn("Charles");
    when(rt.getStorageStrategy().getClazz()).thenReturn(MockStorageStrategy.class.getCanonicalName());
    when(rt.getPersistenceSelectorStrategy().getClazz()).thenReturn(MockPersistenceSelectorStrategy.class.getCanonicalName());
    DeferredGenericTypeResource deferredGenericTypeResource = new DeferredGenericTypeResource(nodeLevelResource, "Charles", "id");
    GenericTypeResource genericTypeResource = new GenericTypeResource(nodeLevelResource, rt, "idx");
    genericTypeResource.setTimestamp(new Date(0));
    ResourceTypeMapper.getInstance().setResourceTypeMapper((name) -> rt);
    // For complete coverage make sure that there is at least one attribute
    // for every different resource type, and that every different type
    // of attribute is represented at least once
    CollectionSet collectionSet = new CollectionSetBuilder(collectionAgent).withTimestamp(new Date(0)).withNumericAttribute(nodeLevelResource, "ucd-sysstat", "CpuRawIdle", 99, AttributeType.GAUGE).withNumericAttribute(interfaceLevelResource, "mib2-X-interfaces", "ifHCInOctets", 1001, AttributeType.COUNTER).withStringAttribute(interfaceLevelResource, "mib2-X-interfaces", "ifDescr", "LAN").withIdentifiedNumericAttribute(deferredGenericTypeResource, "net-snmp-disk", "ns-dsk1", 1024, AttributeType.GAUGE, "some-oid").withIdentifiedNumericAttribute(genericTypeResource, "net-snmp-disk", "ns-dskTotal", 1024, AttributeType.GAUGE, "some-oid").build();
    return Arrays.asList(new Object[][] { { collectionSet, "<collection-set status=\"SUCCEEDED\" timestamp=\"" + StringUtils.iso8601OffsetString(new Date(0), ZoneId.systemDefault(), ChronoUnit.SECONDS) + "\">\n" + "   <agent type=\"0\" store-by-fs=\"false\" node-id=\"0\" sys-up-time=\"0\"/>\n" + "   <collection-resource>\n" + "      <node-level-resource node-id=\"1\"/>\n" + "      <numeric-attribute group=\"ucd-sysstat\" name=\"CpuRawIdle\" type=\"gauge\" value=\"99\"/>\n" + "   </collection-resource>\n" + "   <collection-resource>\n" + "      <interface-level-resource if-name=\"eth0\">\n" + "         <node-level-resource node-id=\"1\"/>\n" + "      </interface-level-resource>\n" + "      <numeric-attribute group=\"mib2-X-interfaces\" name=\"ifHCInOctets\" type=\"counter\" value=\"1001\"/>\n" + "      <string-attribute group=\"mib2-X-interfaces\" name=\"ifDescr\" type=\"string\" value=\"LAN\"/>\n" + "   </collection-resource>\n" + "   <collection-resource>\n" + "      <generic-type-resource name=\"Charles\" instance=\"id\">\n" + "         <node-level-resource node-id=\"1\"/>\n" + "      </generic-type-resource>\n" + "      <numeric-attribute group=\"net-snmp-disk\" name=\"ns-dsk1\" type=\"gauge\" identifier=\"some-oid\" value=\"1024\"/>\n" + "   </collection-resource>\n" + "   <collection-resource>\n" + "      <generic-type-resource name=\"Charles\" instance=\"idx\" timestamp=\"" + StringUtils.iso8601OffsetString(new Date(0), ZoneId.systemDefault(), ChronoUnit.SECONDS) + "\">\n" + "         <node-level-resource node-id=\"1\"/>\n" + "      </generic-type-resource>\n" + "      <numeric-attribute group=\"net-snmp-disk\" name=\"ns-dskTotal\" type=\"gauge\" identifier=\"some-oid\" value=\"1024\"/>\n" + "   </collection-resource>\n" + "</collection-set>" } });
}
Also used : DeferredGenericTypeResource(org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource) CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) DeferredGenericTypeResource(org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource) GenericTypeResource(org.opennms.netmgt.collection.support.builder.GenericTypeResource) InterfaceLevelResource(org.opennms.netmgt.collection.support.builder.InterfaceLevelResource) ResourceType(org.opennms.netmgt.collection.api.ResourceType) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) Date(java.util.Date) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) Parameters(org.junit.runners.Parameterized.Parameters)

Example 8 with CollectionSet

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

the class CollectCommand method doExecute.

@Override
protected Void doExecute() {
    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).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 9 with CollectionSet

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

the class TcpOutputStrategyTest method peristAndReceiveProtobufMessages.

@Test
public void peristAndReceiveProtobufMessages() {
    Date start = new Date();
    // Build a collection set with both numeric and string attributes
    String owner = "192.168.1.1";
    MockCollectionAgent agent = new MockCollectionAgent(1, "n1", InetAddressUtils.addr(owner));
    CollectionSetBuilder builder = new CollectionSetBuilder(agent);
    NodeLevelResource node = new NodeLevelResource(agent.getNodeId());
    InterfaceLevelResource eth0 = new InterfaceLevelResource(node, "eth0");
    builder.withNumericAttribute(eth0, "mib2-interfaces", "ifInErrors", 0.0, AttributeType.COUNTER);
    builder.withStringAttribute(eth0, "mib2-interfaces", "ifSpeed", "10000000");
    builder.withStringAttribute(eth0, "mib2-interfaces", "ifHighSpeed", "10");
    CollectionSet collectionSet = builder.build();
    // Persist without storeByGroup
    persist(collectionSet, false);
    // Wait for the server to receive the readings
    await().until(() -> allReadings.size() == 1);
    PerformanceDataReadings readings = allReadings.get(0);
    // The reading should contain three messages
    assertEquals(3, readings.getMessageCount());
    PerformanceDataReading reading = readings.getMessage(0);
    assertEquals(PerformanceDataReading.newBuilder().setPath(Paths.get(tempFolder.getRoot().getAbsolutePath(), "1", "eth0", "ifInErrors").toString()).setOwner(owner).setTimestamp(reading.getTimestamp()).addAllDblValue(Arrays.asList(Double.valueOf(0.0))).addAllStrValue(Collections.emptyList()).build(), reading);
    reading = readings.getMessage(1);
    assertEquals(PerformanceDataReading.newBuilder().setPath(Paths.get(tempFolder.getRoot().getAbsolutePath(), "1", "eth0", "ifSpeed").toString()).setOwner(owner).setTimestamp(reading.getTimestamp()).addAllDblValue(Collections.emptyList()).addAllStrValue(Arrays.asList("10000000")).build(), reading);
    reading = readings.getMessage(2);
    assertEquals(PerformanceDataReading.newBuilder().setPath(Paths.get(tempFolder.getRoot().getAbsolutePath(), "1", "eth0", "ifHighSpeed").toString()).setOwner(owner).setTimestamp(reading.getTimestamp()).addAllDblValue(Collections.emptyList()).addAllStrValue(Arrays.asList("10")).build(), reading);
    // Persist with storeByGroup
    persist(collectionSet, true);
    // Wait for the server to receive the readings
    await().until(() -> allReadings.size() == 2);
    readings = allReadings.get(1);
    // The reading should contain 1 message
    assertEquals(1, readings.getMessageCount());
    reading = readings.getMessage(0);
    assertEquals(PerformanceDataReading.newBuilder().setPath(Paths.get(tempFolder.getRoot().getAbsolutePath(), "1", "eth0", "mib2-interfaces").toString()).setOwner(owner).setTimestamp(reading.getTimestamp()).addAllDblValue(Arrays.asList(Double.valueOf(0.0))).addAllStrValue(Arrays.asList("10", "10000000")).build(), reading);
    // The reading should be a timestamp in milliseconds
    Date dateFromReading = new Date(reading.getTimestamp());
    assertTrue(String.format("%s <= %s", start, dateFromReading), start.compareTo(dateFromReading) <= 0);
}
Also used : PerformanceDataReading(org.opennms.netmgt.rrd.tcp.PerformanceDataProtos.PerformanceDataReading) CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) PerformanceDataReadings(org.opennms.netmgt.rrd.tcp.PerformanceDataProtos.PerformanceDataReadings) MockCollectionAgent(org.opennms.core.collection.test.MockCollectionAgent) InterfaceLevelResource(org.opennms.netmgt.collection.support.builder.InterfaceLevelResource) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) Date(java.util.Date) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) Test(org.junit.Test)

Example 10 with CollectionSet

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

the class CollectorComplianceTest method canCollectUsingMinionWorkflow.

@Test
public void canCollectUsingMinionWorkflow() throws CollectionInitializationException, CollectionException {
    Assume.assumeTrue(runsOnMinion);
    // create the agent
    OnmsNode node = mock(OnmsNode.class);
    when(node.getId()).thenReturn(1);
    OnmsIpInterface iface = mock(OnmsIpInterface.class);
    when(iface.getNode()).thenReturn(node);
    when(iface.getIpAddress()).thenReturn(InetAddrUtils.getLocalHostAddress());
    IpInterfaceDao ifaceDao = mock(IpInterfaceDao.class);
    when(ifaceDao.load(1)).thenReturn(iface);
    PlatformTransactionManager transMgr = mock(PlatformTransactionManager.class);
    final CollectionAgent agent = DefaultCollectionAgent.create(1, ifaceDao, transMgr);
    // init() should execute without any exceptions
    final ServiceCollector opennmsCollector = getCollector();
    initialize(opennmsCollector);
    // getEffectiveLocation() should return the original location
    final String targetLocation = "!" + MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID;
    assertEquals("Location cannot be altered.", targetLocation, opennmsCollector.getEffectiveLocation(targetLocation));
    // getRuntimeAttributes() should return a valid map
    final Map<String, Object> requiredParams = getRequiredParameters();
    final Map<String, Object> runtimeAttrs = opennmsCollector.getRuntimeAttributes(agent, Collections.unmodifiableMap(requiredParams));
    // marshalParameters() should marshal all parameters to strings
    final Map<String, Object> allParms = new HashMap<>();
    allParms.putAll(requiredParams);
    allParms.putAll(runtimeAttrs);
    final Map<String, String> marshaledParms = opennmsCollector.marshalParameters(Collections.unmodifiableMap(allParms));
    beforeMinion();
    // create a separate instance of the collector
    final ServiceCollector minionCollector = getNewCollector();
    // unmarshalParameters() should unmarshal all parameters from strings
    final Map<String, Object> unmarshaledParms = minionCollector.unmarshalParameters(Collections.unmodifiableMap(marshaledParms));
    // collect() should return a valid collection set
    final CollectionAgentDTO agentDTO = new CollectionAgentDTO(agent);
    final CollectionSet collectionSet = minionCollector.collect(agentDTO, Collections.unmodifiableMap(unmarshaledParms));
    assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus());
    afterMinion();
    // the collection set should be marshalable
    JaxbUtils.marshal(collectionSet);
    // getRrdRepository() should return a valid repository
    assertNotNull(opennmsCollector.getRrdRepository(getCollectionName()));
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) HashMap(java.util.HashMap) CollectionAgentDTO(org.opennms.netmgt.collection.dto.CollectionAgentDTO) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) IpInterfaceDao(org.opennms.netmgt.dao.api.IpInterfaceDao) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) ServiceCollector(org.opennms.netmgt.collection.api.ServiceCollector) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) DefaultCollectionAgent(org.opennms.netmgt.collection.core.DefaultCollectionAgent) 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