Search in sources :

Example 6 with ServiceCollector

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

the class CollectdTest method setupCollector.

@SuppressWarnings("unchecked")
private void setupCollector(String svcName) throws CollectionInitializationException {
    ServiceCollector svcCollector = m_easyMockUtils.createMock(ServiceCollector.class);
    svcCollector.initialize();
    svcCollector.validateAgent(isA(CollectionAgent.class), isA(Map.class));
    expectLastCall().anyTimes();
    setupCollector(svcName, svcCollector);
}
Also used : ServiceCollector(org.opennms.netmgt.collection.api.ServiceCollector) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with ServiceCollector

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

the class CollectdTest method testInterfaceIsNotScheduledWhenValidationFails.

/**
 * NMS-9413: Verifies that collectd does not schedule interfaces when the
 * {@link ServiceCollector} throws a {@link CollectionInitializationException}
 * while validating the agent.
 */
@SuppressWarnings("unchecked")
@Test
public void testInterfaceIsNotScheduledWhenValidationFails() throws Exception {
    ServiceCollector svcCollector = m_easyMockUtils.createMock(ServiceCollector.class);
    svcCollector.initialize();
    svcCollector.validateAgent(isA(CollectionAgent.class), isA(Map.class));
    expectLastCall().andThrow(new CollectionInitializationException("No!")).once();
    setupCollector("SNMP", svcCollector);
    OnmsIpInterface iface = getInterface();
    setupInterface(iface);
    setupTransactionManager();
    expect(m_collectdConfig.getPackages()).andReturn(Collections.singletonList(getCollectionPackageThatMatchesSNMP()));
    expect(m_collectdConfigFactory.interfaceInPackage(iface, getCollectionPackageThatMatchesSNMP())).andReturn(true);
    m_easyMockUtils.replayAll();
    assertEquals("scheduler entry count", 0, m_scheduler.getEntryCount());
    m_collectd.afterPropertiesSet();
    m_collectd.start();
    m_scheduler.next();
    assertEquals("scheduler entry count", 0, m_scheduler.getEntryCount());
    m_collectd.stop();
    m_easyMockUtils.verifyAll();
}
Also used : OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) CollectionInitializationException(org.opennms.netmgt.collection.api.CollectionInitializationException) ServiceCollector(org.opennms.netmgt.collection.api.ServiceCollector) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 8 with ServiceCollector

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

the class Collectd method rebuildScheduler.

private void rebuildScheduler() {
    // Register new collectors if necessary
    Set<String> configuredCollectors = new HashSet<>();
    for (Collector collector : m_collectdConfigFactory.getCollectdConfig().getCollectors()) {
        String svcName = collector.getService();
        configuredCollectors.add(svcName);
        if (getServiceCollector(svcName) == null) {
            try {
                LOG.debug("rebuildScheduler: Loading collector {}, classname {}", svcName, collector.getClassName());
                Class<?> cc = Class.forName(collector.getClassName());
                ServiceCollector sc = (ServiceCollector) cc.newInstance();
                sc.initialize();
                setServiceCollector(svcName, sc);
            } catch (Throwable t) {
                LOG.warn("rebuildScheduler: Failed to load collector {} for service {}", collector.getClassName(), svcName, t);
            }
        }
    }
    // Removing unused collectors if necessary
    List<String> blackList = new ArrayList<>();
    for (String collectorName : getCollectorNames()) {
        if (!configuredCollectors.contains(collectorName)) {
            blackList.add(collectorName);
        }
    }
    for (String collectorName : blackList) {
        LOG.info("rebuildScheduler: removing collector for {}, it is no longer required", collectorName);
        m_collectors.remove(collectorName);
    }
    // Recreating all Collectable Services (using the nodeID list populated at the beginning)
    Collection<Integer> nodeIds = m_nodeDao.getNodeIds();
    m_filterDao.flushActiveIpAddressListCache();
    for (Integer nodeId : nodeIds) {
        unscheduleNodeAndMarkForDeletion(new Long(nodeId));
        scheduleNode(nodeId, true);
    }
}
Also used : ServiceCollector(org.opennms.netmgt.collection.api.ServiceCollector) Collector(org.opennms.netmgt.config.collectd.Collector) ArrayList(java.util.ArrayList) ServiceCollector(org.opennms.netmgt.collection.api.ServiceCollector) HashSet(java.util.HashSet)

Example 9 with ServiceCollector

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

the class CollectorRequestBuilderImpl method execute.

@Override
public CompletableFuture<CollectionSet> execute() {
    if (serviceCollector == null) {
        throw new IllegalArgumentException("Collector or collector class name is required.");
    } else if (agent == null) {
        throw new IllegalArgumentException("Agent is required.");
    }
    final RpcTarget target = client.getRpcTargetHelper().target().withNodeId(agent.getNodeId()).withLocation(agent.getLocationName()).withSystemId(systemId).withServiceAttributes(attributes).withLocationOverride((s) -> serviceCollector.getEffectiveLocation(s)).build();
    CollectorRequestDTO request = new CollectorRequestDTO();
    request.setLocation(target.getLocation());
    request.setSystemId(target.getSystemId());
    request.setClassName(serviceCollector.getClass().getCanonicalName());
    request.setTimeToLiveMs(ttlInMs);
    // Retrieve the runtime attributes, which may include attributes
    // such as the agent details and other state related attributes
    // which should be included in the request
    final Map<String, Object> runtimeAttributes = serviceCollector.getRuntimeAttributes(agent, attributes);
    final Map<String, Object> allAttributes = new HashMap<>();
    allAttributes.putAll(attributes);
    allAttributes.putAll(runtimeAttributes);
    // Only marshal these if the request is being executed at another location.
    if (MonitoringLocationUtils.isDefaultLocationName(request.getLocation())) {
        // As-is
        request.setAgent(agent);
        request.addAttributes(allAttributes);
    } else {
        // Marshal
        request.setAgent(new CollectionAgentDTO(agent));
        final Map<String, String> marshaledParms = serviceCollector.marshalParameters(allAttributes);
        marshaledParms.forEach(request::addAttribute);
        request.setAttributesNeedUnmarshaling(true);
    }
    // Execute the request
    return client.getDelegate().execute(request).thenApply(CollectorResponseDTO::getCollectionSet);
}
Also used : ServiceCollector(org.opennms.netmgt.collection.api.ServiceCollector) Objects(java.util.Objects) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) CollectorRequestBuilder(org.opennms.netmgt.collection.api.CollectorRequestBuilder) MonitoringLocationUtils(org.opennms.netmgt.dao.api.MonitoringLocationUtils) CollectionAgentDTO(org.opennms.netmgt.collection.dto.CollectionAgentDTO) Map(java.util.Map) RpcTarget(org.opennms.core.rpc.api.RpcTarget) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) HashMap(java.util.HashMap) CollectionAgentDTO(org.opennms.netmgt.collection.dto.CollectionAgentDTO) RpcTarget(org.opennms.core.rpc.api.RpcTarget)

Example 10 with ServiceCollector

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

Aggregations

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