Search in sources :

Example 26 with RrdGraphAttribute

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

the class RrdResourceAttributeUtils method loadRrdAttributes.

private static void loadRrdAttributes(File rrdDirectory, String relativePath, Set<OnmsAttribute> attributes, final String rrdFileSuffix) {
    int suffixLength = rrdFileSuffix.length();
    File resourceDir = new File(rrdDirectory, relativePath);
    FilenameFilter filter = new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
            return name.endsWith(rrdFileSuffix);
        }
    };
    File[] files = resourceDir.listFiles(filter);
    if (files == null) {
        return;
    }
    for (final File file : files) {
        String fileName = file.getName();
        if (ResourceTypeUtils.isStoreByGroup() && !ResourceTypeUtils.isResponseTime(relativePath)) {
            String groupName = fileName.substring(0, fileName.length() - suffixLength);
            Properties props = getDsProperties(resourceDir);
            for (Object o : props.keySet()) {
                String dsName = (String) o;
                if (props.getProperty(dsName).equals(groupName)) {
                    attributes.add(new RrdGraphAttribute(dsName, relativePath, fileName));
                }
            }
        } else {
            String dsName = fileName.substring(0, fileName.length() - suffixLength);
            attributes.add(new RrdGraphAttribute(dsName, relativePath, fileName));
        }
    }
}
Also used : FilenameFilter(java.io.FilenameFilter) Properties(java.util.Properties) File(java.io.File) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute)

Example 27 with RrdGraphAttribute

use of org.opennms.netmgt.model.RrdGraphAttribute 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 : ResourceId(org.opennms.netmgt.model.ResourceId) 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 28 with RrdGraphAttribute

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

the class RrdStatisticAttributeVisitorTest method testVisitWithRrdAttribute.

public void testVisitWithRrdAttribute() throws Exception {
    RrdStatisticAttributeVisitor attributeVisitor = new RrdStatisticAttributeVisitor();
    attributeVisitor.setFetchStrategy(m_fetchStrategy);
    attributeVisitor.setConsolidationFunction("AVERAGE");
    attributeVisitor.setStartTime(m_startTime);
    attributeVisitor.setEndTime(m_endTime);
    attributeVisitor.setStatisticVisitor(m_statisticVisitor);
    attributeVisitor.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(attributeVisitor.getConsolidationFunction().toUpperCase());
    FetchResults results = new FetchResults(new long[] { m_startTime }, Collections.singletonMap("result", new double[] { 1.0 }), m_endTime - m_startTime, Collections.emptyMap());
    expect(m_fetchStrategy.fetch(m_startTime, m_endTime, 1, 0, null, null, Collections.singletonList(source), false)).andReturn(results);
    m_statisticVisitor.visit(attribute, 1.0);
    m_mocks.replayAll();
    attributeVisitor.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 29 with RrdGraphAttribute

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

the class RrdStatisticAttributeVisitorTest method testVisitWithNotANumberRrdAttribute.

public void testVisitWithNotANumberRrdAttribute() throws Exception {
    RrdStatisticAttributeVisitor attributeVisitor = new RrdStatisticAttributeVisitor();
    attributeVisitor.setFetchStrategy(m_fetchStrategy);
    attributeVisitor.setConsolidationFunction("AVERAGE");
    attributeVisitor.setStartTime(m_startTime);
    attributeVisitor.setEndTime(m_endTime);
    attributeVisitor.setStatisticVisitor(m_statisticVisitor);
    attributeVisitor.afterPropertiesSet();
    MockResourceType resourceType = new MockResourceType();
    resourceType.setName("somethingOtherThanInterfaceSnmp");
    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(attributeVisitor.getConsolidationFunction().toUpperCase());
    FetchResults results = new FetchResults(new long[] {}, Collections.singletonMap("result", new double[] {}), m_endTime - m_startTime, Collections.emptyMap());
    expect(m_fetchStrategy.fetch(m_startTime, m_endTime, 1, 0, null, null, Collections.singletonList(source), false)).andReturn(results);
    m_mocks.replayAll();
    attributeVisitor.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 30 with RrdGraphAttribute

use of org.opennms.netmgt.model.RrdGraphAttribute 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)

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 ResourceId (org.opennms.netmgt.model.ResourceId)5 File (java.io.File)4 List (java.util.List)4 Test (org.junit.Test)4 PrefabGraph (org.opennms.netmgt.model.PrefabGraph)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