use of org.opennms.netmgt.collection.support.builder.CollectionSetBuilder in project opennms by OpenNMS.
the class StressCommand method generateCollectionSet.
private CollectionSet generateCollectionSet(CollectionAgent agent, int nodeId, int interfaceId, Resource resource) {
CollectionSetBuilder builder = new CollectionSetBuilder(agent);
for (int groupId = 0; groupId < numberOfGroupsPerInterface; groupId++) {
String groupName = "group" + groupId;
// Number attributes
for (int attributeId = 0; attributeId < numberOfNumericAttributesPerGroup; attributeId++) {
// Generate a predictable, non-constant number
int value = groupId * attributeId + seed.incrementAndGet() % 100;
builder.withNumericAttribute(resource, groupName, "metric_" + groupId + "_" + attributeId, value, AttributeType.GAUGE);
numericAttributesGenerated.mark();
}
String stringAttributeValueSuffix = "";
// Add 1 to each of these to make sure they are > 0
int groupInstance = (nodeId + 1) * (interfaceId + 1) * (groupId + 1);
if (stringVariationFactor > 0 && groupInstance % stringVariationFactor == 0) {
stringAttributeValueSuffix = String.format("-%d-varied", groupInstance);
stringAttributesVaried.mark(numberOfStringAttributesPerGroup);
}
// String attributes
for (int stringAttributeId = 0; stringAttributeId < numberOfStringAttributesPerGroup; stringAttributeId++) {
// String attributes are stored at the resource level, and not at the group level, so we prefix
// the keys with the group name to make sure these don't collide
String key = String.format("%s-key-%d", groupName, stringAttributeId);
String value = String.format("%s-value-%d%s", groupName, stringAttributeId, stringAttributeValueSuffix);
builder.withStringAttribute(resource, groupName, key, value);
stringAttributesGenerated.mark();
}
}
return builder.build();
}
use of org.opennms.netmgt.collection.support.builder.CollectionSetBuilder in project opennms by OpenNMS.
the class TcpOutputStrategyTest method peristAndReceiveProtobufMessages.
@Test
public void peristAndReceiveProtobufMessages() {
Date start = new Date();
// Build a collection set with both numeric and string attributes
String owner = "192.168.1.1";
MockCollectionAgent agent = new MockCollectionAgent(1, "n1", InetAddressUtils.addr(owner));
CollectionSetBuilder builder = new CollectionSetBuilder(agent);
NodeLevelResource node = new NodeLevelResource(agent.getNodeId());
InterfaceLevelResource eth0 = new InterfaceLevelResource(node, "eth0");
builder.withNumericAttribute(eth0, "mib2-interfaces", "ifInErrors", 0.0, AttributeType.COUNTER);
builder.withStringAttribute(eth0, "mib2-interfaces", "ifSpeed", "10000000");
builder.withStringAttribute(eth0, "mib2-interfaces", "ifHighSpeed", "10");
CollectionSet collectionSet = builder.build();
// Persist without storeByGroup
persist(collectionSet, false);
// Wait for the server to receive the readings
await().until(() -> allReadings.size() == 1);
PerformanceDataReadings readings = allReadings.get(0);
// The reading should contain three messages
assertEquals(3, readings.getMessageCount());
PerformanceDataReading reading = readings.getMessage(0);
assertEquals(PerformanceDataReading.newBuilder().setPath(Paths.get(tempFolder.getRoot().getAbsolutePath(), "1", "eth0", "ifInErrors").toString()).setOwner(owner).setTimestamp(reading.getTimestamp()).addAllDblValue(Arrays.asList(Double.valueOf(0.0))).addAllStrValue(Collections.emptyList()).build(), reading);
reading = readings.getMessage(1);
assertEquals(PerformanceDataReading.newBuilder().setPath(Paths.get(tempFolder.getRoot().getAbsolutePath(), "1", "eth0", "ifSpeed").toString()).setOwner(owner).setTimestamp(reading.getTimestamp()).addAllDblValue(Collections.emptyList()).addAllStrValue(Arrays.asList("10000000")).build(), reading);
reading = readings.getMessage(2);
assertEquals(PerformanceDataReading.newBuilder().setPath(Paths.get(tempFolder.getRoot().getAbsolutePath(), "1", "eth0", "ifHighSpeed").toString()).setOwner(owner).setTimestamp(reading.getTimestamp()).addAllDblValue(Collections.emptyList()).addAllStrValue(Arrays.asList("10")).build(), reading);
// Persist with storeByGroup
persist(collectionSet, true);
// Wait for the server to receive the readings
await().until(() -> allReadings.size() == 2);
readings = allReadings.get(1);
// The reading should contain 1 message
assertEquals(1, readings.getMessageCount());
reading = readings.getMessage(0);
assertEquals(PerformanceDataReading.newBuilder().setPath(Paths.get(tempFolder.getRoot().getAbsolutePath(), "1", "eth0", "mib2-interfaces").toString()).setOwner(owner).setTimestamp(reading.getTimestamp()).addAllDblValue(Arrays.asList(Double.valueOf(0.0))).addAllStrValue(Arrays.asList("10", "10000000")).build(), reading);
// The reading should be a timestamp in milliseconds
Date dateFromReading = new Date(reading.getTimestamp());
assertTrue(String.format("%s <= %s", start, dateFromReading), start.compareTo(dateFromReading) <= 0);
}
use of org.opennms.netmgt.collection.support.builder.CollectionSetBuilder in project opennms by OpenNMS.
the class JdbcCollector method collect.
@Override
public CollectionSet collect(CollectionAgent agent, Map<String, Object> parameters) throws CollectionException {
final JdbcDataCollection collection = (JdbcDataCollection) parameters.get(JDBC_COLLECTION_KEY);
JdbcAgentState agentState = null;
Connection con = null;
ResultSet results = null;
Statement stmt = null;
try {
agentState = createAgentState(agent.getAddress(), parameters);
agentState.setupDatabaseConnections(parameters);
// Create a new collection set.
CollectionSetBuilder builder = new CollectionSetBuilder(agent);
// Creating a single resource object, because all node-level metric must belong to the exact same resource.
final NodeLevelResource nodeResource = new NodeLevelResource(agent.getNodeId());
// Cycle through all of the queries for this collection
for (JdbcQuery query : collection.getQueries()) {
// Verify if we should check for availability of a query.
if (agentState.shouldCheckAvailability(query.getQueryName(), query.getRecheckInterval())) {
// Check to see if the query is available.
if (!isGroupAvailable(agentState, query)) {
LOG.warn("Group is not available.");
continue;
}
}
try {
// If the query is available, lets collect it.
if (agentState.groupIsAvailable(query.getQueryName())) {
if (agentState.getUseDataSourceName()) {
initDatabaseConnectionFactory(agentState.getDataSourceName());
con = DataSourceFactory.getInstance(agentState.getDataSourceName()).getConnection();
} else {
con = agentState.getJdbcConnection();
}
stmt = agentState.createStatement(con);
results = agentState.executeJdbcQuery(stmt, query);
// Determine if there were any results for this query to
if (results.isBeforeFirst() && results.isAfterLast()) {
LOG.warn("Query '{}' returned no results.", query.getQueryName());
// Close the statement, but retain the connection.
agentState.closeResultSet(results);
agentState.closeStmt(stmt);
continue;
}
// Determine if there are results and how many.
results.last();
boolean singleInstance = (results.getRow() == 1) ? true : false;
results.beforeFirst();
// Iterate through each row.
while (results.next()) {
Resource resource = null;
// Create the appropriate resource container.
if (singleInstance) {
resource = nodeResource;
} else {
// Retrieve the name of the column to use as the instance key for multi-row queries.
String instance = results.getString(query.getInstanceColumn());
resource = new DeferredGenericTypeResource(nodeResource, query.getResourceType(), instance);
}
for (JdbcColumn curColumn : query.getJdbcColumns()) {
final AttributeType type = curColumn.getDataType();
String columnName = null;
if (curColumn.getDataSourceName() != null && curColumn.getDataSourceName().length() != 0) {
columnName = curColumn.getDataSourceName();
} else {
columnName = curColumn.getColumnName();
}
String columnValue = results.getString(columnName);
if (columnValue == null) {
LOG.debug("Skipping column named '{}' with null value.", curColumn.getColumnName());
continue;
}
if (type.isNumeric()) {
Double numericValue = Double.NaN;
try {
numericValue = Double.parseDouble(columnValue);
} catch (NumberFormatException e) {
LOG.warn("Value '{}' for column named '{}' cannot be converted to a number. Skipping.", columnValue, curColumn.getColumnName());
continue;
}
builder.withNumericAttribute(resource, query.getQueryName(), curColumn.getAlias(), numericValue, type);
} else {
builder.withStringAttribute(resource, query.getQueryName(), curColumn.getAlias(), columnValue);
}
}
}
}
} catch (SQLException e) {
// Close the statement but retain the connection, log the exception and continue to the next query.
LOG.warn("There was a problem executing query '{}' Please review the query or configuration. Reason: {}", query.getQueryName(), e.getMessage());
continue;
} finally {
agentState.closeResultSet(results);
agentState.closeStmt(stmt);
agentState.closeConnection(con);
}
}
builder.withStatus(CollectionStatus.SUCCEEDED);
return builder.build();
} finally {
if (agentState != null) {
// Make sure that when we're done we close all results, statements and connections.
agentState.closeResultSet(results);
agentState.closeStmt(stmt);
agentState.closeConnection(con);
// agentState.closeAgentConnection();
}
}
}
use of org.opennms.netmgt.collection.support.builder.CollectionSetBuilder in project opennms by OpenNMS.
the class CollectionSetDTOTest method getCollectionSetWithAllResourceTypes.
private static CollectionSet getCollectionSetWithAllResourceTypes() {
CollectionAgent collectionAgent = mock(CollectionAgent.class);
NodeLevelResource nodeLevelResource = new NodeLevelResource(1);
NodeLevelResource jmxNodeLevelResource = new NodeLevelResource(1, "opennns-jvm");
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);
// of attribute is represented at least once
return new CollectionSetBuilder(collectionAgent).withTimestamp(new Date(0)).withNumericAttribute(nodeLevelResource, "ucd-sysstat", "CpuRawIdle", 99, AttributeType.GAUGE).withNumericAttribute(jmxNodeLevelResource, "opennms-jvm", "heap", 2048, 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();
}
use of org.opennms.netmgt.collection.support.builder.CollectionSetBuilder in project opennms by OpenNMS.
the class CollectionSetDTOTest method getCollectionSetWithAllNumberTypes.
private static CollectionSet getCollectionSetWithAllNumberTypes() {
CollectionAgent collectionAgent = mock(CollectionAgent.class);
NodeLevelResource nodeLevelResource = new NodeLevelResource(1);
return new CollectionSetBuilder(collectionAgent).withTimestamp(new Date(0)).withNumericAttribute(nodeLevelResource, "double", "zero", 0d, AttributeType.GAUGE).withNumericAttribute(nodeLevelResource, "double", "pi", Math.PI, AttributeType.GAUGE).withNumericAttribute(nodeLevelResource, "double", "max", Double.MAX_VALUE, AttributeType.GAUGE).withNumericAttribute(nodeLevelResource, "double", "min", Double.MIN_VALUE, AttributeType.GAUGE).withNumericAttribute(nodeLevelResource, "double", "+inf", Double.POSITIVE_INFINITY, AttributeType.GAUGE).withNumericAttribute(nodeLevelResource, "double", "-inf", Double.NEGATIVE_INFINITY, AttributeType.GAUGE).withNumericAttribute(nodeLevelResource, "double", "-inf", Double.NEGATIVE_INFINITY, AttributeType.GAUGE).withNumericAttribute(nodeLevelResource, "double", "NaN", Double.NaN, AttributeType.GAUGE).withNumericAttribute(nodeLevelResource, "long", "max long", Long.MAX_VALUE, AttributeType.GAUGE).withNumericAttribute(nodeLevelResource, "long", "min long", Long.MIN_VALUE, AttributeType.GAUGE).build();
}
Aggregations