Search in sources :

Example 1 with ServiceCollector

use of org.opennms.netmgt.collection.api.ServiceCollector 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 2 with ServiceCollector

use of org.opennms.netmgt.collection.api.ServiceCollector 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 SnmpCollectionAgent 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) SnmpCollectionAgent(org.opennms.netmgt.collectd.SnmpCollectionAgent) 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) Test(org.junit.Test)

Example 3 with ServiceCollector

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

the class CollectorComplianceTest method canCollectUsingOpenNMSWorkflow.

@Test
public void canCollectUsingOpenNMSWorkflow() throws CollectionInitializationException, CollectionException {
    // create the agent
    OnmsNode node = mock(OnmsNode.class);
    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 SnmpCollectionAgent agent = DefaultCollectionAgent.create(1, ifaceDao, transMgr);
    // init() should execute without any exceptions
    final ServiceCollector opennmsCollector = getCollector();
    initialize(opennmsCollector);
    // getEffectiveLocation() should execute without any exceptions
    // in this context there are no requirements on its return value
    final String targetLocation = "!" + MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID;
    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));
    // collect() should return a valid collection set
    final Map<String, Object> allParms = new HashMap<>();
    allParms.putAll(requiredParams);
    allParms.putAll(runtimeAttrs);
    final CollectionSet collectionSet = opennmsCollector.collect(agent, Collections.unmodifiableMap(allParms));
    assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus());
    // getRrdRepository() should return a valid repository
    assertNotNull(opennmsCollector.getRrdRepository(getCollectionName()));
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) SnmpCollectionAgent(org.opennms.netmgt.collectd.SnmpCollectionAgent) HashMap(java.util.HashMap) ServiceCollector(org.opennms.netmgt.collection.api.ServiceCollector) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) IpInterfaceDao(org.opennms.netmgt.dao.api.IpInterfaceDao) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) Test(org.junit.Test)

Example 4 with ServiceCollector

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

the class CollectorComplianceTest method canInitializeManyTimes.

@Test
public void canInitializeManyTimes() throws CollectionInitializationException {
    final ServiceCollector collector = getCollector();
    initialize(collector);
    initialize(collector);
    initialize(collector);
}
Also used : ServiceCollector(org.opennms.netmgt.collection.api.ServiceCollector) Test(org.junit.Test)

Example 5 with ServiceCollector

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

the class Collectd method instantiateCollectors.

private void instantiateCollectors() {
    LOG.debug("instantiateCollectors: Loading collectors");
    /*
         * Load up an instance of each collector from the config
         * so that the event processor will have them for
         * new incoming events to create collectable service objects.
         */
    Collection<Collector> collectors = m_collectdConfigFactory.getCollectdConfig().getCollectors();
    for (Collector collector : collectors) {
        String svcName = collector.getService();
        try {
            LOG.debug("instantiateCollectors: Loading collector {}, classname {}", svcName, collector.getClassName());
            final ServiceCollector sc = m_serviceCollectorRegistry.getCollectorByClassName(collector.getClassName());
            if (sc == null) {
                throw new IllegalArgumentException(String.format("No collector found with class name '%s'. Available collectors include: %s", collector.getClassName(), m_serviceCollectorRegistry.getCollectorClassNames()));
            }
            sc.initialize();
            setServiceCollector(svcName, sc);
        } catch (Throwable t) {
            LOG.warn("instantiateCollectors: Failed to load collector {} for service {}", collector.getClassName(), svcName, t);
        }
    }
}
Also used : ServiceCollector(org.opennms.netmgt.collection.api.ServiceCollector) Collector(org.opennms.netmgt.config.collectd.Collector) ServiceCollector(org.opennms.netmgt.collection.api.ServiceCollector)

Aggregations

ServiceCollector (org.opennms.netmgt.collection.api.ServiceCollector)8 Test (org.junit.Test)3 CollectionSet (org.opennms.netmgt.collection.api.CollectionSet)3 Collector (org.opennms.netmgt.config.collectd.Collector)3 HashMap (java.util.HashMap)2 SnmpCollectionAgent (org.opennms.netmgt.collectd.SnmpCollectionAgent)2 CollectionAgent (org.opennms.netmgt.collection.api.CollectionAgent)2 IpInterfaceDao (org.opennms.netmgt.dao.api.IpInterfaceDao)2 OnmsIpInterface (org.opennms.netmgt.model.OnmsIpInterface)2 OnmsNode (org.opennms.netmgt.model.OnmsNode)2 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 CollectionInitializationException (org.opennms.netmgt.collection.api.CollectionInitializationException)1 CollectionAgentDTO (org.opennms.netmgt.collection.dto.CollectionAgentDTO)1 CollectdConfigFactory (org.opennms.netmgt.config.CollectdConfigFactory)1 CollectdConfiguration (org.opennms.netmgt.config.collectd.CollectdConfiguration)1