Search in sources :

Example 1 with NodeLevelResource

use of org.opennms.netmgt.collection.support.builder.NodeLevelResource in project opennms by OpenNMS.

the class CollectionResourceDTOTest method data.

@Parameters
public static Collection<Object[]> data() throws ParseException {
    NodeLevelResource nodeLevelResource = new NodeLevelResource(1);
    NumericAttribute attribute = new NumericAttribute("group-x", "cores", 1, AttributeType.GAUGE, "some-oid");
    CollectionResourceDTO dto = new CollectionResourceDTO();
    dto.setResource(nodeLevelResource);
    dto.getAttributes().add(attribute);
    return Arrays.asList(new Object[][] { { dto, "<collection-resource>\n" + "   <node-level-resource node-id=\"1\"/>\n" + "   <numeric-attribute group=\"group-x\" name=\"cores\" type=\"gauge\" identifier=\"some-oid\" value=\"1\"/>\n" + "</collection-resource>" } });
}
Also used : CollectionResourceDTO(org.opennms.netmgt.collection.dto.CollectionResourceDTO) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) NumericAttribute(org.opennms.netmgt.collection.support.builder.NumericAttribute) Parameters(org.junit.runners.Parameterized.Parameters)

Example 2 with NodeLevelResource

use of org.opennms.netmgt.collection.support.builder.NodeLevelResource 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>" } });
}
Also used : DeferredGenericTypeResource(org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource) CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) DeferredGenericTypeResource(org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource) GenericTypeResource(org.opennms.netmgt.collection.support.builder.GenericTypeResource) InterfaceLevelResource(org.opennms.netmgt.collection.support.builder.InterfaceLevelResource) ResourceType(org.opennms.netmgt.collection.api.ResourceType) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) Date(java.util.Date) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) Parameters(org.junit.runners.Parameterized.Parameters)

Example 3 with NodeLevelResource

use of org.opennms.netmgt.collection.support.builder.NodeLevelResource in project opennms by OpenNMS.

the class GenericTypeResourceTest method getInstanceInResourcePath.

private String getInstanceInResourcePath(String instance) {
    // Mock the ResourceType
    ResourceType rt = mock(ResourceType.class, RETURNS_DEEP_STUBS);
    when(rt.getName()).thenReturn("type");
    when(rt.getStorageStrategy().getClazz()).thenReturn(IndexStorageStrategy.class.getCanonicalName());
    when(rt.getStorageStrategy().getParameters()).thenReturn(Collections.emptyList());
    when(rt.getPersistenceSelectorStrategy().getClazz()).thenReturn(PersistAllSelectorStrategy.class.getCanonicalName());
    when(rt.getPersistenceSelectorStrategy().getParameters()).thenReturn(Collections.emptyList());
    // Create the GenericTypeResource
    NodeLevelResource nlr = new NodeLevelResource(1);
    GenericTypeResource gtr = new GenericTypeResource(nlr, rt, instance);
    // Mock the CollectionResource
    CollectionResource resource = mock(CollectionResource.class);
    when(resource.getInstance()).thenReturn(gtr.getInstance());
    // Build the resource path, and extract the instance (the last element of the path)
    ResourcePath path = gtr.getPath(resource);
    String[] elements = path.elements();
    return elements[elements.length - 1];
}
Also used : CollectionResource(org.opennms.netmgt.collection.api.CollectionResource) ResourcePath(org.opennms.netmgt.model.ResourcePath) GenericTypeResource(org.opennms.netmgt.collection.support.builder.GenericTypeResource) ResourceType(org.opennms.netmgt.collection.api.ResourceType) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource)

Example 4 with NodeLevelResource

use of org.opennms.netmgt.collection.support.builder.NodeLevelResource in project opennms by OpenNMS.

the class TcpOutputStrategyTest method peristAndReceiveProtobufMessages.

@Test
public void peristAndReceiveProtobufMessages() {
    // 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);
}
Also used : PerformanceDataReading(org.opennms.netmgt.rrd.tcp.PerformanceDataProtos.PerformanceDataReading) CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) PerformanceDataReadings(org.opennms.netmgt.rrd.tcp.PerformanceDataProtos.PerformanceDataReadings) MockCollectionAgent(org.opennms.core.collection.test.MockCollectionAgent) InterfaceLevelResource(org.opennms.netmgt.collection.support.builder.InterfaceLevelResource) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) Test(org.junit.Test)

Example 5 with NodeLevelResource

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<Locale>();
        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.");
    }
}
Also used : Locale(java.util.Locale) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) Attrib(org.opennms.netmgt.config.httpdatacollection.Attrib) AttributeType(org.opennms.netmgt.collection.api.AttributeType) ParseException(java.text.ParseException)

Aggregations

NodeLevelResource (org.opennms.netmgt.collection.support.builder.NodeLevelResource)25 CollectionSetBuilder (org.opennms.netmgt.collection.support.builder.CollectionSetBuilder)15 CollectionSet (org.opennms.netmgt.collection.api.CollectionSet)8 GenericTypeResource (org.opennms.netmgt.collection.support.builder.GenericTypeResource)8 Test (org.junit.Test)7 AttributeType (org.opennms.netmgt.collection.api.AttributeType)6 InterfaceLevelResource (org.opennms.netmgt.collection.support.builder.InterfaceLevelResource)6 Resource (org.opennms.netmgt.collection.support.builder.Resource)6 CollectionAgent (org.opennms.netmgt.collection.api.CollectionAgent)5 DeferredGenericTypeResource (org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource)5 ResourcePath (org.opennms.netmgt.model.ResourcePath)4 HashMap (java.util.HashMap)3 List (java.util.List)3 CollectionResource (org.opennms.netmgt.collection.api.CollectionResource)3 File (java.io.File)2 MalformedURLException (java.net.MalformedURLException)2 RemoteException (java.rmi.RemoteException)2 Date (java.util.Date)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Parameters (org.junit.runners.Parameterized.Parameters)2