Search in sources :

Example 1 with RrdGraphAttribute

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

the class NewtsResourceStorageDaoTest method getAttributes.

@Test
public void getAttributes() {
    // Attributes are empty when the resource does not exist
    replay();
    assertEquals(0, m_nrs.getAttributes(ResourcePath.get("should", "not", "exist")).size());
    verify();
    // Metrics from all buckets should be present
    index(ResourcePath.get("a", "bucket1"), Sets.newHashSet("metric11", "metric12"));
    index(ResourcePath.get("a", "bucket2"), Sets.newHashSet("metric21", "metric22"));
    replay();
    Set<OnmsAttribute> attributes = m_nrs.getAttributes(ResourcePath.get("a"));
    assertEquals(4, attributes.size());
    // Verify the properties of a specific attribute
    RrdGraphAttribute metric11 = null;
    for (OnmsAttribute attribute : attributes) {
        if (attribute instanceof RrdGraphAttribute) {
            RrdGraphAttribute graphAttribute = (RrdGraphAttribute) attribute;
            if ("metric11".equals(graphAttribute.getName())) {
                metric11 = graphAttribute;
            }
        }
    }
    assertNotNull(metric11);
    verify();
}
Also used : OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) Test(org.junit.Test)

Example 2 with RrdGraphAttribute

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

the class DefaultRrdGraphService method getRrdNames.

private static String[] getRrdNames(OnmsResource resource, String[] dsNames) {
    String[] rrds = new String[dsNames.length];
    Map<String, RrdGraphAttribute> attributes = resource.getRrdGraphAttributes();
    for (int i = 0; i < dsNames.length; i++) {
        RrdGraphAttribute attribute = attributes.get(dsNames[i]);
        if (attribute == null) {
            throw new IllegalArgumentException("RRD attribute '" + dsNames[i] + "' is not available on resource '" + resource.getId() + "'.  Available RRD attributes: " + StringUtils.collectionToDelimitedString(attributes.keySet(), ", "));
        }
        rrds[i] = RrdFileConstants.escapeForGraphing(attribute.getRrdRelativePath());
    }
    return rrds;
}
Also used : RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute)

Example 3 with RrdGraphAttribute

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

the class NewtsFetchStrategy method fetch.

@Override
public FetchResults fetch(long start, long end, long step, int maxrows, Long interval, Long heartbeat, List<Source> sources, boolean relaxed) {
    final LateAggregationParams lag = getLagParams(step, interval, heartbeat);
    final Optional<Timestamp> startTs = Optional.of(Timestamp.fromEpochMillis(start));
    final Optional<Timestamp> endTs = Optional.of(Timestamp.fromEpochMillis(end));
    final Map<String, Object> constants = Maps.newHashMap();
    // Group the sources by resource id to avoid calling the ResourceDao
    // multiple times for the same resource
    Map<ResourceId, List<Source>> sourcesByResourceId = sources.stream().collect(Collectors.groupingBy((source) -> ResourceId.fromString(source.getResourceId())));
    // Lookup the OnmsResources in parallel
    Map<ResourceId, Future<OnmsResource>> resourceFuturesById = Maps.newHashMapWithExpectedSize(sourcesByResourceId.size());
    for (ResourceId resourceId : sourcesByResourceId.keySet()) {
        resourceFuturesById.put(resourceId, threadPool.submit(getResourceByIdCallable(resourceId)));
    }
    // Gather the results, fail if any of the resources were not found
    Map<OnmsResource, List<Source>> sourcesByResource = Maps.newHashMapWithExpectedSize(sourcesByResourceId.size());
    for (Entry<ResourceId, Future<OnmsResource>> entry : resourceFuturesById.entrySet()) {
        try {
            OnmsResource resource = entry.getValue().get();
            if (resource == null) {
                if (relaxed)
                    continue;
                LOG.error("No resource with id: {}", entry.getKey());
                return null;
            }
            sourcesByResource.put(resource, sourcesByResourceId.get(entry.getKey()));
        } catch (ExecutionException | InterruptedException e) {
            throw Throwables.propagate(e);
        }
    }
    // Now group the sources by Newts Resource ID, which differs from the OpenNMS Resource ID.
    Map<String, List<Source>> sourcesByNewtsResourceId = Maps.newHashMap();
    for (Entry<OnmsResource, List<Source>> entry : sourcesByResource.entrySet()) {
        final OnmsResource resource = entry.getKey();
        for (Source source : entry.getValue()) {
            // Gather the values from strings.properties
            Utils.convertStringAttributesToConstants(source.getLabel(), resource.getStringPropertyAttributes(), constants);
            // Grab the attribute that matches the source
            RrdGraphAttribute rrdGraphAttribute = resource.getRrdGraphAttributes().get(source.getAttribute());
            if (rrdGraphAttribute == null && !Strings.isNullOrEmpty(source.getFallbackAttribute())) {
                LOG.error("No attribute with name '{}', using fallback-attribute with name '{}'", source.getAttribute(), source.getFallbackAttribute());
                source.setAttribute(source.getFallbackAttribute());
                source.setFallbackAttribute(null);
                rrdGraphAttribute = resource.getRrdGraphAttributes().get(source.getAttribute());
            }
            if (rrdGraphAttribute == null) {
                if (relaxed)
                    continue;
                LOG.error("No attribute with name: {}", source.getAttribute());
                return null;
            }
            // The Newts Resource ID is stored in the rrdFile attribute
            String newtsResourceId = rrdGraphAttribute.getRrdRelativePath();
            // Remove the file separator prefix, added by the RrdGraphAttribute class
            if (newtsResourceId.startsWith(File.separator)) {
                newtsResourceId = newtsResourceId.substring(File.separator.length(), newtsResourceId.length());
            }
            List<Source> listOfSources = sourcesByNewtsResourceId.get(newtsResourceId);
            // Create the list if it doesn't exist
            if (listOfSources == null) {
                listOfSources = Lists.newLinkedList();
                sourcesByNewtsResourceId.put(newtsResourceId, listOfSources);
            }
            listOfSources.add(source);
        }
    }
    // The Newts API only allows us to perform a query using a single (Newts) Resource ID,
    // so we perform multiple queries in parallel, and aggregate the results.
    Map<String, Future<Collection<Row<Measurement>>>> measurementsByNewtsResourceId = Maps.newHashMapWithExpectedSize(sourcesByNewtsResourceId.size());
    for (Entry<String, List<Source>> entry : sourcesByNewtsResourceId.entrySet()) {
        measurementsByNewtsResourceId.put(entry.getKey(), threadPool.submit(getMeasurementsForResourceCallable(entry.getKey(), entry.getValue(), startTs, endTs, lag)));
    }
    long[] timestamps = null;
    Map<String, double[]> columns = Maps.newHashMap();
    for (Entry<String, Future<Collection<Row<Measurement>>>> entry : measurementsByNewtsResourceId.entrySet()) {
        Collection<Row<Measurement>> rows;
        try {
            rows = entry.getValue().get();
        } catch (InterruptedException | ExecutionException e) {
            throw Throwables.propagate(e);
        }
        final int N = rows.size();
        if (timestamps == null) {
            timestamps = new long[N];
            int k = 0;
            for (final Row<Measurement> row : rows) {
                timestamps[k] = row.getTimestamp().asMillis();
                k++;
            }
        }
        int k = 0;
        for (Row<Measurement> row : rows) {
            for (Measurement measurement : row.getElements()) {
                double[] column = columns.get(measurement.getName());
                if (column == null) {
                    column = new double[N];
                    columns.put(measurement.getName(), column);
                }
                column[k] = measurement.getValue();
            }
            k += 1;
        }
    }
    FetchResults fetchResults = new FetchResults(timestamps, columns, lag.getStep(), constants);
    if (relaxed) {
        Utils.fillMissingValues(fetchResults, sources);
    }
    LOG.trace("Fetch results: {}", fetchResults);
    return fetchResults;
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Context(org.opennms.newts.api.Context) StandardAggregationFunctions(org.opennms.newts.api.query.StandardAggregationFunctions) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Callable(java.util.concurrent.Callable) AggregationFunction(org.opennms.newts.api.query.AggregationFunction) Utils(org.opennms.netmgt.measurements.utils.Utils) SampleRepository(org.opennms.newts.api.SampleRepository) Strings(com.google.common.base.Strings) Future(java.util.concurrent.Future) Lists(com.google.common.collect.Lists) Optional(com.google.common.base.Optional) Map(java.util.Map) OnmsResource(org.opennms.netmgt.model.OnmsResource) ThreadFactory(java.util.concurrent.ThreadFactory) ResultDescriptor(org.opennms.newts.api.query.ResultDescriptor) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) ExecutorService(java.util.concurrent.ExecutorService) SampleSelectCallback(org.opennms.newts.api.SampleSelectCallback) ResourceId(org.opennms.netmgt.model.ResourceId) Logger(org.slf4j.Logger) Semaphore(java.util.concurrent.Semaphore) Collection(java.util.Collection) Resource(org.opennms.newts.api.Resource) Throwables(com.google.common.base.Throwables) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) File(java.io.File) Executors(java.util.concurrent.Executors) Row(org.opennms.newts.api.Results.Row) ExecutionException(java.util.concurrent.ExecutionException) FetchResults(org.opennms.netmgt.measurements.api.FetchResults) List(java.util.List) Duration(org.opennms.newts.api.Duration) Results(org.opennms.newts.api.Results) MeasurementFetchStrategy(org.opennms.netmgt.measurements.api.MeasurementFetchStrategy) Measurement(org.opennms.newts.api.Measurement) Entry(java.util.Map.Entry) Timestamp(org.opennms.newts.api.Timestamp) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ResourceDao(org.opennms.netmgt.dao.api.ResourceDao) Source(org.opennms.netmgt.measurements.model.Source) Measurement(org.opennms.newts.api.Measurement) Timestamp(org.opennms.newts.api.Timestamp) Source(org.opennms.netmgt.measurements.model.Source) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) FetchResults(org.opennms.netmgt.measurements.api.FetchResults) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException) OnmsResource(org.opennms.netmgt.model.OnmsResource) ResourceId(org.opennms.netmgt.model.ResourceId) Future(java.util.concurrent.Future) Row(org.opennms.newts.api.Results.Row)

Example 4 with RrdGraphAttribute

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

the class NrtController method getRequiredRrdGraphAttributes.

public Set<RrdGraphAttribute> getRequiredRrdGraphAttributes(OnmsResource reportResource, PrefabGraph prefabGraph) {
    Map<String, RrdGraphAttribute> available = reportResource.getRrdGraphAttributes();
    Set<RrdGraphAttribute> reqAttrs = new LinkedHashSet<RrdGraphAttribute>();
    for (String attrName : prefabGraph.getColumns()) {
        RrdGraphAttribute attr = available.get(attrName);
        if (attr != null) {
            reqAttrs.add(attr);
        }
    }
    return reqAttrs;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute)

Example 5 with RrdGraphAttribute

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

the class DefaultRrdDaoTest method testFetchLastValueInRange.

public void testFetchLastValueInRange() throws Exception {
    String rrdDir = "snmp" + File.separator + "1" + File.separator + "eth0";
    String rrdFile = "ifInOctets.jrb";
    OnmsResource topResource = new OnmsResource("1", "Node One", new MockResourceType(), new HashSet<OnmsAttribute>(0), new ResourcePath("foo"));
    OnmsAttribute attribute = new RrdGraphAttribute("ifInOctets", rrdDir, rrdFile);
    HashSet<OnmsAttribute> attributeSet = new HashSet<OnmsAttribute>(1);
    attributeSet.add(attribute);
    MockResourceType childResourceType = new MockResourceType();
    OnmsResource childResource = new OnmsResource("eth0", "Interface One: eth0", childResourceType, attributeSet, new ResourcePath("foo"));
    childResource.setParent(topResource);
    int interval = 300000;
    int range = 300000;
    Double expectedValue = new Double(1.0);
    String fullRrdFilePath = m_dao.getRrdBaseDirectory().getAbsolutePath() + File.separator + rrdDir + File.separator + rrdFile;
    expect(m_rrdStrategy.fetchLastValueInRange(fullRrdFilePath, attribute.getName(), interval, range)).andReturn(expectedValue);
    m_mocks.replayAll();
    Double value = m_dao.getLastFetchValue(attribute, interval, range);
    m_mocks.verifyAll();
    assertNotNull("last fetched value must not be null, but was null", value);
    assertEquals("last fetched value", expectedValue, value);
}
Also used : OnmsResource(org.opennms.netmgt.model.OnmsResource) ResourcePath(org.opennms.netmgt.model.ResourcePath) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) MockResourceType(org.opennms.netmgt.mock.MockResourceType) HashSet(java.util.HashSet)

Aggregations

RrdGraphAttribute (org.opennms.netmgt.model.RrdGraphAttribute)36 OnmsResource (org.opennms.netmgt.model.OnmsResource)23 OnmsAttribute (org.opennms.netmgt.model.OnmsAttribute)21 MockResourceType (org.opennms.netmgt.mock.MockResourceType)16 HashSet (java.util.HashSet)11 Source (org.opennms.netmgt.measurements.model.Source)9 FetchResults (org.opennms.netmgt.measurements.api.FetchResults)8 ResourcePath (org.opennms.netmgt.model.ResourcePath)8 Map (java.util.Map)5 ExternalValueAttribute (org.opennms.netmgt.model.ExternalValueAttribute)5 OnmsNode (org.opennms.netmgt.model.OnmsNode)5 File (java.io.File)4 List (java.util.List)4 Test (org.junit.Test)4 PrefabGraph (org.opennms.netmgt.model.PrefabGraph)4 ResourceId (org.opennms.netmgt.model.ResourceId)4 LinkedHashSet (java.util.LinkedHashSet)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Optional (com.google.common.base.Optional)2 Throwables (com.google.common.base.Throwables)2