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);
}
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();
}
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);
}
}
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);
}
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;
}
Aggregations