Search in sources :

Example 16 with FetchResults

use of org.opennms.netmgt.measurements.api.FetchResults in project opennms by OpenNMS.

the class RrdStatisticAttributeVisitorTest method testVisitTwice.

public void testVisitTwice() throws Exception {
    final RrdStatisticAttributeVisitor attributeVisitor1 = new RrdStatisticAttributeVisitor();
    attributeVisitor1.setFetchStrategy(m_fetchStrategy);
    attributeVisitor1.setConsolidationFunction("AVERAGE");
    attributeVisitor1.setStartTime(m_startTime);
    attributeVisitor1.setEndTime(m_endTime);
    attributeVisitor1.setStatisticVisitor(m_statisticVisitor);
    attributeVisitor1.afterPropertiesSet();
    final RrdStatisticAttributeVisitor attributeVisitor2 = new RrdStatisticAttributeVisitor();
    attributeVisitor2.setFetchStrategy(m_fetchStrategy);
    attributeVisitor2.setConsolidationFunction("AVERAGE");
    attributeVisitor2.setStartTime(m_startTime);
    attributeVisitor2.setEndTime(m_endTime);
    attributeVisitor2.setStatisticVisitor(m_statisticVisitor);
    attributeVisitor2.afterPropertiesSet();
    MockResourceType resourceType = new MockResourceType();
    resourceType.setName("interfaceSnmp");
    OnmsAttribute attribute = new RrdGraphAttribute("ifInOctets", "something", "something else");
    attribute.setResource(new OnmsResource("1", "Node One", resourceType, Collections.singleton(attribute), ResourcePath.get("foo")));
    Source source = new Source();
    source.setLabel("result");
    source.setResourceId(attribute.getResource().getId().toString());
    source.setAttribute(attribute.getName());
    source.setAggregation(attributeVisitor1.getConsolidationFunction().toUpperCase());
    expect(m_fetchStrategy.fetch(m_startTime, m_endTime, 1, 0, null, null, Collections.singletonList(source), false)).andReturn(new FetchResults(new long[] { m_startTime }, Collections.singletonMap("result", new double[] { 1.0 }), m_endTime - m_startTime, Collections.emptyMap()));
    m_statisticVisitor.visit(attribute, 1.0);
    expect(m_fetchStrategy.fetch(m_startTime, m_endTime, 1, 0, null, null, Collections.singletonList(source), false)).andReturn(new FetchResults(new long[] { m_startTime }, Collections.singletonMap("result", new double[] { 2.0 }), m_endTime - m_startTime, Collections.emptyMap()));
    m_statisticVisitor.visit(attribute, 2.0);
    m_mocks.replayAll();
    attributeVisitor1.visit(attribute);
    attributeVisitor2.visit(attribute);
    m_mocks.verifyAll();
}
Also used : OnmsResource(org.opennms.netmgt.model.OnmsResource) FetchResults(org.opennms.netmgt.measurements.api.FetchResults) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) Source(org.opennms.netmgt.measurements.model.Source) MockResourceType(org.opennms.netmgt.mock.MockResourceType)

Example 17 with FetchResults

use of org.opennms.netmgt.measurements.api.FetchResults in project opennms by OpenNMS.

the class RrdStatisticAttributeVisitor method visit.

/**
 * {@inheritDoc}
 */
@Override
public void visit(OnmsAttribute attribute) {
    if (!RrdGraphAttribute.class.isAssignableFrom(attribute.getClass())) {
        // Nothing to do if we can't cast to an RrdGraphAttribute
        return;
    }
    final Source source = new Source();
    source.setLabel("result");
    source.setResourceId(attribute.getResource().getId().toString());
    source.setAttribute(attribute.getName());
    source.setAggregation(m_consolidationFunction.toUpperCase());
    final FetchResults results;
    try {
        results = m_fetchStrategy.fetch(m_startTime, m_endTime, 1, 0, null, null, Collections.singletonList(source), false);
    } catch (final Exception e) {
        LOG.warn("Failed to fetch statistic: {}", source, e);
        return;
    }
    if (results == null) {
        LOG.warn("No statistic found: {}", source);
        return;
    }
    final double[] statistics = results.getColumns().get(source.getLabel());
    if (statistics == null || statistics.length == 0) {
        LOG.warn("Statistic is empty: {}", source);
        return;
    }
    final Aggregator aggregator = Aggregators.valueOf(m_consolidationFunction.toUpperCase()).get();
    Arrays.stream(statistics).filter(v -> (!Double.isNaN(v))).forEach(v -> {
        LOG.debug("Aggregating: {}", v);
        aggregator.aggregate(v);
    });
    double statistic = aggregator.getValue();
    LOG.debug("The value of {} is {}", attribute, statistic);
    /*
         * We don't want to do anything with NaN data, since
         * it means there is no data. We especially want to
         * stay away from it, because it will be sorted as
         * being higher than any numeric value, which will
         * leave our TopN report with most, if not all NaN
         * values at the top.
         */
    if (Double.isNaN(statistic)) {
        return;
    }
    m_statisticVisitor.visit(attribute, statistic);
}
Also used : Arrays(java.util.Arrays) Logger(org.slf4j.Logger) AttributeVisitor(org.opennms.netmgt.model.AttributeVisitor) LoggerFactory(org.slf4j.LoggerFactory) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) Supplier(java.util.function.Supplier) InitializingBean(org.springframework.beans.factory.InitializingBean) FetchResults(org.opennms.netmgt.measurements.api.FetchResults) MeasurementFetchStrategy(org.opennms.netmgt.measurements.api.MeasurementFetchStrategy) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) Collections(java.util.Collections) Source(org.opennms.netmgt.measurements.model.Source) AttributeStatisticVisitor(org.opennms.netmgt.model.AttributeStatisticVisitor) Assert(org.springframework.util.Assert) FetchResults(org.opennms.netmgt.measurements.api.FetchResults) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) Source(org.opennms.netmgt.measurements.model.Source)

Example 18 with FetchResults

use of org.opennms.netmgt.measurements.api.FetchResults in project opennms by OpenNMS.

the class JrobinFetchStrategy method fetchMeasurements.

/**
 * {@inheritDoc}
 */
@Override
protected FetchResults fetchMeasurements(long start, long end, long step, int maxrows, Map<Source, String> rrdsBySource, Map<String, Object> constants) throws RrdException {
    final long startInSeconds = (long) Math.floor(start / 1000d);
    final long endInSeconds = (long) Math.floor(end / 1000d);
    long stepInSeconds = (long) Math.floor(step / 1000d);
    // The step must be strictly positive
    if (stepInSeconds <= 0) {
        stepInSeconds = 1;
    }
    final DataProcessor dproc = new DataProcessor(startInSeconds, endInSeconds);
    if (maxrows > 0) {
        dproc.setPixelCount(maxrows);
    }
    dproc.setFetchRequestResolution(stepInSeconds);
    for (final Map.Entry<Source, String> entry : rrdsBySource.entrySet()) {
        final Source source = entry.getKey();
        final String rrdFile = entry.getValue();
        dproc.addDatasource(source.getLabel(), rrdFile, source.getEffectiveDataSource(), source.getAggregation());
    }
    try {
        dproc.processData();
    } catch (IOException e) {
        throw new RrdException("JRB processing failed.", e);
    }
    final long[] timestamps = dproc.getTimestamps();
    // Convert the timestamps to milliseconds
    for (int i = 0; i < timestamps.length; i++) {
        timestamps[i] *= 1000;
    }
    final Map<String, double[]> columns = Maps.newHashMapWithExpectedSize(rrdsBySource.keySet().size());
    for (Source source : rrdsBySource.keySet()) {
        columns.put(source.getLabel(), dproc.getValues(source.getLabel()));
    }
    return new FetchResults(timestamps, columns, dproc.getStep() * 1000, constants);
}
Also used : FetchResults(org.opennms.netmgt.measurements.api.FetchResults) DataProcessor(org.jrobin.data.DataProcessor) IOException(java.io.IOException) RrdException(org.jrobin.core.RrdException) Map(java.util.Map) Source(org.opennms.netmgt.measurements.model.Source)

Example 19 with FetchResults

use of org.opennms.netmgt.measurements.api.FetchResults in project opennms by OpenNMS.

the class ReportDefinitionTest method testUnfilteredResourceAttributeFilteringWithMatch.

public void testUnfilteredResourceAttributeFilteringWithMatch() throws Exception {
    OnmsAttribute rrdAttribute = new RrdGraphAttribute("IfInOctets", "something", "something else");
    ExternalValueAttribute externalValueAttribute = new ExternalValueAttribute("ifSpeed", "100000000");
    Set<OnmsAttribute> attributes = new HashSet<>();
    attributes.add(rrdAttribute);
    attributes.add(externalValueAttribute);
    MockResourceType resourceType = new MockResourceType();
    resourceType.setName("interfaceSnmp");
    OnmsResource resource = new OnmsResource("1", "Node One", resourceType, attributes, ResourcePath.get("foo"));
    EasyMock.expect(m_resourceDao.findTopLevelResources()).andReturn(Collections.singletonList(resource));
    ReportDefinition def = createReportDefinition();
    def.setResourceAttributeKey(externalValueAttribute.getName());
    def.setResourceAttributeValueMatch(externalValueAttribute.getValue());
    ReportInstance report = def.createReport(m_nodeDao, m_resourceDao, m_fetchStrategy, m_filterDao);
    rrdAttribute.setResource(new OnmsResource("1", "Node One", resourceType, Collections.singleton(rrdAttribute), ResourcePath.get("foo")));
    Source source = new Source();
    source.setLabel("result");
    source.setResourceId(rrdAttribute.getResource().getId().toString());
    source.setAttribute(rrdAttribute.getName());
    source.setAggregation("AVERAGE");
    FetchResults results = new FetchResults(new long[] { report.getStartTime() }, Collections.singletonMap("result", new double[] { 100.0 }), report.getEndTime() - report.getStartTime(), Collections.emptyMap());
    EasyMock.expect(m_fetchStrategy.fetch(report.getStartTime(), report.getEndTime(), 1, 0, null, null, Collections.singletonList(source), false)).andReturn(results);
    m_mocks.replayAll();
    report.walk();
    m_mocks.verifyAll();
    assertEquals("results size", 1, report.getResults().size());
    m_mocks.replayAll();
}
Also used : OnmsResource(org.opennms.netmgt.model.OnmsResource) FetchResults(org.opennms.netmgt.measurements.api.FetchResults) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) ExternalValueAttribute(org.opennms.netmgt.model.ExternalValueAttribute) Source(org.opennms.netmgt.measurements.model.Source) HashSet(java.util.HashSet) MockResourceType(org.opennms.netmgt.mock.MockResourceType)

Aggregations

FetchResults (org.opennms.netmgt.measurements.api.FetchResults)19 Source (org.opennms.netmgt.measurements.model.Source)18 RrdGraphAttribute (org.opennms.netmgt.model.RrdGraphAttribute)7 Test (org.junit.Test)6 OnmsResource (org.opennms.netmgt.model.OnmsResource)6 Map (java.util.Map)5 OnmsAttribute (org.opennms.netmgt.model.OnmsAttribute)5 MockResourceType (org.opennms.netmgt.mock.MockResourceType)4 RrdException (org.jrobin.core.RrdException)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Optional (com.google.common.base.Optional)1 Strings (com.google.common.base.Strings)1 Throwables (com.google.common.base.Throwables)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1