Search in sources :

Example 16 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)

Example 17 with RrdGraphAttribute

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

the class PropertiesGraphDaoIT method testGetPrefabGraphsForResourceWithSuppressUnused.

@Test
public void testGetPrefabGraphsForResourceWithSuppressUnused() {
    MockResourceType resourceType = new MockResourceType();
    resourceType.setName("interface");
    HashSet<OnmsAttribute> attributes = new HashSet<OnmsAttribute>(0);
    attributes.add(new RrdGraphAttribute("ifHCInOctets", "", ""));
    attributes.add(new RrdGraphAttribute("ifHCOutOctets", "", ""));
    attributes.add(new ExternalValueAttribute("ifSpeed", ""));
    OnmsResource resource = new OnmsResource("node", "1", resourceType, attributes, ResourcePath.get("foo"));
    PrefabGraph[] graphs = m_dao.getPrefabGraphsForResource(resource);
    assertEquals("prefab graph array size", 1, graphs.length);
    assertEquals("prefab graph[0] name", "mib2.HCbits", graphs[0].getName());
}
Also used : OnmsResource(org.opennms.netmgt.model.OnmsResource) PrefabGraph(org.opennms.netmgt.model.PrefabGraph) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) ExternalValueAttribute(org.opennms.netmgt.model.ExternalValueAttribute) MockResourceType(org.opennms.netmgt.mock.MockResourceType) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 18 with RrdGraphAttribute

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

the class RrdResourceAttributeUtilsTest method createResource.

private OnmsResource createResource() {
    OnmsResource topResource = new OnmsResource("1", "Node One", new MockResourceType(), new HashSet<OnmsAttribute>(0), new ResourcePath("foo"));
    Set<OnmsAttribute> attributes = new HashSet<OnmsAttribute>(1);
    attributes.add(new RrdGraphAttribute("foo", "1/eth0", "foo.jrb"));
    OnmsResource childResource = new OnmsResource("eth0", "Interface eth0", new MockResourceType(), attributes, new ResourcePath("foo"));
    childResource.setParent(topResource);
    return childResource;
}
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)

Example 19 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 : 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 20 with RrdGraphAttribute

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

the class DefaultRrdDao method getPrintValues.

/**
 * <p>getPrintValues</p>
 *
 * @param attribute a {@link org.opennms.netmgt.model.OnmsAttribute} object.
 * @param rraConsolidationFunction a {@link java.lang.String} object.
 * @param startTimeInMillis a long.
 * @param endTimeInMillis a long.
 * @param printFunctions a {@link java.lang.String} object.
 * @return an array of double.
 */
@Override
public double[] getPrintValues(OnmsAttribute attribute, String rraConsolidationFunction, long startTimeInMillis, long endTimeInMillis, String... printFunctions) {
    Assert.notNull(attribute, "attribute argument must not be null");
    Assert.notNull(rraConsolidationFunction, "rraConsolicationFunction argument must not be null");
    Assert.isTrue(endTimeInMillis > startTimeInMillis, "end argument must be after start argument");
    Assert.isAssignable(attribute.getClass(), RrdGraphAttribute.class, "attribute argument must be assignable to RrdGraphAttribute");
    // if no printFunctions are given just use the rraConsolidationFunction
    if (printFunctions.length < 1) {
        printFunctions = new String[] { rraConsolidationFunction };
    }
    RrdGraphAttribute rrdAttribute = (RrdGraphAttribute) attribute;
    String[] command = new String[] { m_rrdBinaryPath, "graph", "-", "--start=" + (startTimeInMillis / 1000), "--end=" + (endTimeInMillis / 1000), "DEF:ds1=" + RrdFileConstants.escapeForGraphing(rrdAttribute.getRrdRelativePath()) + ":" + attribute.getName() + ":" + rraConsolidationFunction };
    String[] printDefs = new String[printFunctions.length];
    for (int i = 0; i < printFunctions.length; i++) {
        printDefs[i] = "PRINT:ds1:" + printFunctions[i] + ":\"%le\"";
    }
    String commandString = StringUtils.arrayToDelimitedString(command, " ") + ' ' + StringUtils.arrayToDelimitedString(printDefs, " ");
    LOG.debug("commandString: {}", commandString);
    RrdGraphDetails graphDetails;
    try {
        graphDetails = m_rrdStrategy.createGraphReturnDetails(commandString, m_rrdBaseDirectory);
    } catch (Throwable e) {
        throw new DataAccessResourceFailureException("Failure when generating graph to get data with command '" + commandString + "'", e);
    }
    String[] printLines;
    try {
        printLines = graphDetails.getPrintLines();
    } catch (Throwable e) {
        throw new DataAccessResourceFailureException("Failure to get print lines from graph after graphing with command '" + commandString + "'", e);
    }
    if (printLines.length != printFunctions.length) {
        throw new DataAccessResourceFailureException("Returned number of print lines should be " + printFunctions.length + ", but was " + printLines.length + " from command: " + commandString);
    }
    double[] values = new double[printLines.length];
    for (int i = 0; i < printLines.length; i++) {
        if (printLines[i].toLowerCase().endsWith("nan")) {
            values[i] = Double.NaN;
        } else {
            try {
                // To avoid NMS-5592 ~ 2,670374e+03 floating point issue.
                values[i] = Double.parseDouble(printLines[i].replace(",", "."));
            } catch (NumberFormatException e) {
                throw new DataAccessResourceFailureException("Value of line " + (i + 1) + " of output from RRD is not a valid floating point number: '" + printLines[i] + "'");
            }
        }
    }
    return values;
}
Also used : DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) RrdGraphDetails(org.opennms.netmgt.rrd.RrdGraphDetails)

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