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