use of org.opennms.netmgt.collection.support.builder.NodeLevelResource 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.NodeLevelResource 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.NodeLevelResource 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.NodeLevelResource 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();
}
use of org.opennms.netmgt.collection.support.builder.NodeLevelResource in project opennms by OpenNMS.
the class HttpCollector method processResponse.
private static void processResponse(final Locale responseLocale, final String responseBodyAsString, final HttpCollectorAgent collectorAgent, final CollectionSetBuilder collectionSetBuilder) {
LOG.debug("processResponse:");
LOG.debug("responseBody = {}", responseBodyAsString);
LOG.debug("getmatches = {}", collectorAgent.getUriDef().getUrl().getMatches());
int numberOfButes = 0;
int flags = 0;
if (collectorAgent.getUriDef().getUrl().isCanonicalEquivalence()) {
flags |= Pattern.CANON_EQ;
}
if (collectorAgent.getUriDef().getUrl().isCaseInsensitive()) {
flags |= Pattern.CASE_INSENSITIVE;
}
if (collectorAgent.getUriDef().getUrl().isComments()) {
flags |= Pattern.COMMENTS;
}
if (collectorAgent.getUriDef().getUrl().isDotall()) {
flags |= Pattern.DOTALL;
}
if (collectorAgent.getUriDef().getUrl().isLiteral()) {
flags |= Pattern.LITERAL;
}
if (collectorAgent.getUriDef().getUrl().isMultiline()) {
flags |= Pattern.MULTILINE;
}
if (collectorAgent.getUriDef().getUrl().isUnicodeCase()) {
flags |= Pattern.UNICODE_CASE;
}
if (collectorAgent.getUriDef().getUrl().isUnixLines()) {
flags |= Pattern.UNIX_LINES;
}
LOG.debug("flags = {}", flags);
Pattern p = Pattern.compile(collectorAgent.getUriDef().getUrl().getMatches(), flags);
Matcher m = p.matcher(responseBodyAsString);
final boolean matches = m.matches();
if (matches) {
LOG.debug("processResponse: found matching attributes: {}", matches);
final List<Attrib> attribDefs = collectorAgent.getUriDef().getAttributes();
final List<Locale> locales = new ArrayList<>();
if (responseLocale != null) {
locales.add(responseLocale);
}
locales.add(Locale.getDefault());
if (Locale.getDefault() != Locale.ENGLISH) {
locales.add(Locale.ENGLISH);
}
// All node resources for HTTP; nothing of interface or "indexed resource" type
final NodeLevelResource resource = new NodeLevelResource(collectorAgent.getAgent().getNodeId());
for (final Attrib attribDef : attribDefs) {
final AttributeType type = attribDef.getType();
String value = null;
try {
value = m.group(attribDef.getMatchGroup());
} catch (final IndexOutOfBoundsException e) {
LOG.error("IndexOutOfBoundsException thrown while trying to find regex group, your regex does not contain the following group index: {}", attribDef.getMatchGroup());
LOG.error("Regex statement: {}", collectorAgent.getUriDef().getUrl().getMatches());
continue;
}
if (type.isNumeric()) {
Number num = null;
for (final Locale locale : locales) {
try {
num = NumberFormat.getNumberInstance(locale).parse(value);
LOG.debug("processResponse: found a parsable number with locale \"{}\".", locale);
break;
} catch (final ParseException e) {
LOG.warn("attribute {} failed to match a parsable number with locale \"{}\"! Matched \"{}\" instead.", attribDef.getAlias(), locale, value);
}
}
if (num == null) {
LOG.warn("processResponse: gave up attempting to parse numeric value, skipping group {}", attribDef.getMatchGroup());
continue;
}
LOG.debug("processResponse: adding numeric attribute {}", num);
collectionSetBuilder.withNumericAttribute(resource, collectorAgent.getUriDef().getName(), attribDef.getAlias(), num, type);
numberOfButes++;
} else {
LOG.debug("processResponse: adding string attribute {}", value);
collectionSetBuilder.withStringAttribute(resource, collectorAgent.getUriDef().getName(), attribDef.getAlias(), value);
numberOfButes++;
}
}
} else {
LOG.debug("processResponse: found matching attributes: {}", matches);
}
if (numberOfButes < 1) {
LOG.warn("doCollection: no attributes defined by the response: {}", responseBodyAsString.trim());
throw new HttpCollectorException("No attributes specified were found.");
}
}
Aggregations