Search in sources :

Example 1 with ResourcePath

use of org.opennms.netmgt.model.ResourcePath 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 2 with ResourcePath

use of org.opennms.netmgt.model.ResourcePath in project opennms by OpenNMS.

the class ResourceTypeFilteringResourceVisitorTest method testVisitWithMatch.

public void testVisitWithMatch() throws Exception {
    ResourceTypeFilteringResourceVisitor filteringVisitor = new ResourceTypeFilteringResourceVisitor();
    filteringVisitor.setDelegatedVisitor(m_delegatedVisitor);
    filteringVisitor.setResourceTypeMatch("interfaceSnmp");
    filteringVisitor.afterPropertiesSet();
    MockResourceType resourceType = new MockResourceType();
    resourceType.setName("interfaceSnmp");
    OnmsResource resource = new OnmsResource("1", "Node One", resourceType, new HashSet<OnmsAttribute>(0), new ResourcePath("foo"));
    m_delegatedVisitor.visit(resource);
    m_mocks.replayAll();
    filteringVisitor.visit(resource);
    m_mocks.verifyAll();
}
Also used : OnmsResource(org.opennms.netmgt.model.OnmsResource) ResourcePath(org.opennms.netmgt.model.ResourcePath) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) MockResourceType(org.opennms.netmgt.mock.MockResourceType)

Example 3 with ResourcePath

use of org.opennms.netmgt.model.ResourcePath in project opennms by OpenNMS.

the class SiblingColumnStorageStrategyTest method testMatchIndex.

@Test
public void testMatchIndex() throws Exception {
    strategy.setResourceTypeName("macIndex");
    List<org.opennms.netmgt.collection.api.Parameter> params = new ArrayList<>();
    params.add(createParameter("sibling-column-name", "_index"));
    params.add(createParameter("replace-first", "s/^(([\\d]{1,3}\\.){8,8}).*$/$1/"));
    params.add(createParameter("replace-first", "s/\\.$//"));
    strategy.setParameters(params);
    ResourcePath parentResource = ResourcePath.get("1");
    MockCollectionResource resource = new MockCollectionResource(parentResource, "0.132.43.51.76.89.2.144.10.1.1.1", "macIndex");
    String resourceName = strategy.getResourceNameFromIndex(resource);
    Assert.assertEquals("0.132.43.51.76.89.2.144", resourceName);
}
Also used : ResourcePath(org.opennms.netmgt.model.ResourcePath) ArrayList(java.util.ArrayList) Parameter(org.opennms.netmgt.config.datacollection.Parameter) Test(org.junit.Test)

Example 4 with ResourcePath

use of org.opennms.netmgt.model.ResourcePath in project opennms by OpenNMS.

the class NewtsConverter method processStringsProperties.

private void processStringsProperties(final Path path) {
    try {
        // Find an process all 'strings.properties' files
        Files.walk(path).filter(p -> p.endsWith("strings.properties")).forEach(p -> {
            final Properties properties = new Properties();
            try (final BufferedReader r = Files.newBufferedReader(p)) {
                properties.load(r);
            } catch (final IOException e) {
                throw Throwables.propagate(e);
            }
            final ResourcePath resourcePath = buildResourcePath(p.getParent());
            if (resourcePath == null) {
                return;
            }
            this.injectStringPropertiesToNewts(resourcePath, Maps.fromProperties(properties));
        });
    } catch (Exception e) {
        LOG.error("Error while reading string.properties", e);
        return;
    }
}
Also used : Connection(java.sql.Connection) LoggerFactory(org.slf4j.LoggerFactory) NewtsUtils(org.opennms.netmgt.newts.support.NewtsUtils) SampleRepository(org.opennms.newts.api.SampleRepository) BigDecimal(java.math.BigDecimal) Optional(com.google.common.base.Optional) Counter(org.opennms.newts.api.Counter) ResultSet(java.sql.ResultSet) Map(java.util.Map) Path(java.nio.file.Path) Resource(org.opennms.newts.api.Resource) ValueType(org.opennms.newts.api.ValueType) AbstractDS(org.opennms.netmgt.rrd.model.AbstractDS) Sets(com.google.common.collect.Sets) Indexer(org.opennms.newts.api.search.Indexer) List(java.util.List) PeriodFormatterBuilder(org.joda.time.format.PeriodFormatterBuilder) ParseException(org.apache.commons.cli.ParseException) ResourcePath(org.opennms.netmgt.model.ResourcePath) RrdConvertUtils(org.opennms.netmgt.rrd.model.RrdConvertUtils) FilenameUtils(org.apache.commons.io.FilenameUtils) SortedMap(java.util.SortedMap) Iterables(com.google.common.collect.Iterables) UnsignedLong(com.google.common.primitives.UnsignedLong) Options(org.apache.commons.cli.Options) HelpFormatter(org.apache.commons.cli.HelpFormatter) PeriodFormatter(org.joda.time.format.PeriodFormatter) MetricType(org.opennms.newts.api.MetricType) AbstractRRD(org.opennms.netmgt.rrd.model.AbstractRRD) ArrayList(java.util.ArrayList) Interval(org.joda.time.Interval) Lists(com.google.common.collect.Lists) CommandLine(org.apache.commons.cli.CommandLine) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Sample(org.opennms.newts.api.Sample) PosixParser(org.apache.commons.cli.PosixParser) Option(org.apache.commons.cli.Option) ExecutorService(java.util.concurrent.ExecutorService) Period(org.joda.time.Period) Logger(org.slf4j.Logger) Properties(java.util.Properties) CommandLineParser(org.apache.commons.cli.CommandLineParser) Files(java.nio.file.Files) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) Maps(com.google.common.collect.Maps) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Paths(java.nio.file.Paths) ForkJoinPool(java.util.concurrent.ForkJoinPool) Timestamp(org.opennms.newts.api.Timestamp) Statement(java.sql.Statement) DataSourceFactory(org.opennms.core.db.DataSourceFactory) BufferedReader(java.io.BufferedReader) Gauge(org.opennms.newts.api.Gauge) ResourcePath(org.opennms.netmgt.model.ResourcePath) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Properties(java.util.Properties) ParseException(org.apache.commons.cli.ParseException) IOException(java.io.IOException)

Example 5 with ResourcePath

use of org.opennms.netmgt.model.ResourcePath in project opennms by OpenNMS.

the class NewtsConverter method processResource.

/**
     * Process metric.
     *
     * @param resourceDir the path where the resource file lives in
     * @param fileName the RRD file name without extension
     * @param group the group name
     */
private void processResource(final Path resourceDir, final String fileName, final String group) {
    LOG.info("Processing resource: dir={}, file={}, group={}", resourceDir, fileName, group);
    final ResourcePath resourcePath = buildResourcePath(resourceDir);
    if (resourcePath == null) {
        return;
    }
    // Load the RRD file
    final Path file;
    switch(this.storageTool) {
        case RRDTOOL:
            file = resourceDir.resolve(fileName + ".rrd");
            break;
        case JROBIN:
            file = resourceDir.resolve(fileName + ".jrb");
            break;
        default:
            file = null;
    }
    if (!Files.exists(file)) {
        LOG.error("File not found: {}", file);
        return;
    }
    final AbstractRRD rrd;
    try {
        switch(this.storageTool) {
            case RRDTOOL:
                rrd = RrdConvertUtils.dumpRrd(file.toFile());
                break;
            case JROBIN:
                rrd = RrdConvertUtils.dumpJrb(file.toFile());
                break;
            default:
                rrd = null;
        }
    } catch (final Exception e) {
        LOG.error("Can't parse JRB/RRD file: {}", file, e);
        return;
    }
    // Inject the samples from the RRD file to NewTS
    try {
        this.injectSamplesToNewts(resourcePath, group, rrd.getDataSources(), rrd.generateSamples());
    } catch (final Exception e) {
        LOG.error("Failed to convert file: {}", file, e);
        return;
    }
}
Also used : Path(java.nio.file.Path) ResourcePath(org.opennms.netmgt.model.ResourcePath) ResourcePath(org.opennms.netmgt.model.ResourcePath) AbstractRRD(org.opennms.netmgt.rrd.model.AbstractRRD) ParseException(org.apache.commons.cli.ParseException) IOException(java.io.IOException)

Aggregations

ResourcePath (org.opennms.netmgt.model.ResourcePath)51 OnmsResource (org.opennms.netmgt.model.OnmsResource)19 OnmsAttribute (org.opennms.netmgt.model.OnmsAttribute)14 MockResourceType (org.opennms.netmgt.mock.MockResourceType)9 Test (org.junit.Test)8 RrdGraphAttribute (org.opennms.netmgt.model.RrdGraphAttribute)8 HashSet (java.util.HashSet)6 Map (java.util.Map)6 Resource (org.opennms.newts.api.Resource)6 Path (java.nio.file.Path)5 ArrayList (java.util.ArrayList)5 Sample (org.opennms.newts.api.Sample)5 IOException (java.io.IOException)3 List (java.util.List)3 SnmpCollectionAgent (org.opennms.netmgt.collectd.SnmpCollectionAgent)3 CollectionSetBuilder (org.opennms.netmgt.collection.support.builder.CollectionSetBuilder)3 OnmsNode (org.opennms.netmgt.model.OnmsNode)3 Timestamp (org.opennms.newts.api.Timestamp)3 Optional (com.google.common.base.Optional)2 Preconditions (com.google.common.base.Preconditions)2