use of org.opennms.netmgt.collection.api.CollectionAgent in project opennms by OpenNMS.
the class JdbcCollectorTest method collect.
public CollectionSet collect(JdbcDataCollection collection, ResultSet resultSet, ResourceType... resourceTypes) throws Exception {
final int nodeId = 1;
JdbcDataCollectionConfig config = new JdbcDataCollectionConfig();
config.setRrdRepository(tempFolder.getRoot().getAbsolutePath());
JdbcDataCollectionConfigDao jdbcCollectionDao = mock(JdbcDataCollectionConfigDao.class);
when(jdbcCollectionDao.getConfig()).thenReturn(config);
when(jdbcCollectionDao.getDataCollectionByName(null)).thenReturn(collection);
ResourceTypeMapper.getInstance().setResourceTypeMapper((name) -> {
for (ResourceType resourceType : resourceTypes) {
if (resourceType.getName().equals(name)) {
return resourceType;
}
}
return null;
});
MyJdbcCollector jdbcCollector = new MyJdbcCollector();
jdbcCollector.setJdbcCollectionDao(jdbcCollectionDao);
jdbcCollector.initialize();
CollectionAgent agent = mock(CollectionAgent.class);
when(agent.getNodeId()).thenReturn(nodeId);
when(agent.getAddress()).thenReturn(InetAddressUtils.ONE_TWENTY_SEVEN);
when(agent.getStorageResourcePath()).thenReturn(ResourcePath.get("snmp", Integer.toString(nodeId)));
JdbcAgentState jdbcAgentState = mock(JdbcAgentState.class);
when(jdbcAgentState.groupIsAvailable(any(String.class))).thenReturn(true);
when(jdbcAgentState.executeJdbcQuery(anyObject(), anyObject())).thenReturn(resultSet);
jdbcCollector.setJdbcAgentState(jdbcAgentState);
Map<String, Object> params = new HashMap<>();
params.putAll(jdbcCollector.getRuntimeAttributes(agent, params));
CollectionSet collectionSet = jdbcCollector.collect(agent, params);
return collectionSet;
}
use of org.opennms.netmgt.collection.api.CollectionAgent in project opennms by OpenNMS.
the class NewtsPersisterIT method canPersist.
@Test
public void canPersist() throws InterruptedException {
ServiceParameters params = new ServiceParameters(Collections.emptyMap());
RrdRepository repo = new RrdRepository();
// Only the last element of the path matters here
repo.setRrdBaseDir(Paths.get("a", "path", "that", "ends", "with", "snmp").toFile());
Persister persister = m_persisterFactory.createPersister(params, repo);
int nodeId = 1;
CollectionAgent agent = mock(CollectionAgent.class);
when(agent.getStorageResourcePath()).thenReturn(ResourcePath.get(Integer.toString(nodeId)));
NodeLevelResource nodeLevelResource = new NodeLevelResource(nodeId);
// Build a collection set with a single sample
Timestamp now = Timestamp.now();
CollectionSet collectionSet = new CollectionSetBuilder(agent).withNumericAttribute(nodeLevelResource, "metrics", "metric", 900, AttributeType.GAUGE).withTimestamp(now.asDate()).build();
// Persist
collectionSet.visit(persister);
// Wait for the sample(s) to be flushed
Thread.sleep(5 * 1000);
// Fetch the (persisted) sample
Resource resource = new Resource("snmp:1:metrics");
Timestamp end = Timestamp.now();
Results<Sample> samples = m_sampleRepository.select(Context.DEFAULT_CONTEXT, resource, Optional.of(now), Optional.of(end));
assertEquals(1, samples.getRows().size());
Row<Sample> row = samples.getRows().iterator().next();
assertEquals(900, row.getElement("metric").getValue().doubleValue(), 0.00001);
}
use of org.opennms.netmgt.collection.api.CollectionAgent in project opennms by OpenNMS.
the class WSManDataCollectionConfigDaoJaxbTest method canEvaluteSystemDefinitionRules.
@Test
public void canEvaluteSystemDefinitionRules() throws UnknownHostException {
OnmsNode node = mock(OnmsNode.class);
CollectionAgent agent = mock(CollectionAgent.class);
Definition config = new Definition();
config.setProductVendor("Dell Inc.");
config.setProductVersion(" iDRAC 6");
SystemDefinition sysDef = new SystemDefinition();
sysDef.addRule("#productVendor matches 'Dell.*' and #productVersion matches '.*DRAC.*'");
assertTrue("agent should be matched", WSManDataCollectionConfigDaoJaxb.isAgentSupportedBySystemDefinition(sysDef, agent, config, node));
}
use of org.opennms.netmgt.collection.api.CollectionAgent 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>" } });
}
use of org.opennms.netmgt.collection.api.CollectionAgent 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;
}
Aggregations