Search in sources :

Example 1 with RrdException

use of org.jrobin.core.RrdException in project opennms by OpenNMS.

the class SpikeHunter method printReplacementsToUser.

private static void printReplacementsToUser(FetchData data, String dsName, double[] newValues, List<Integer> violatorIndices) {
    long[] timestamps = data.getTimestamps();
    double[] origValues = null;
    try {
        origValues = data.getValues(dsName);
    } catch (RrdException e) {
        System.out.println("RRD Exception trying to get values from RRD file: " + e.getMessage());
    }
    for (int i : violatorIndices) {
        Date sampleDate = new Date(timestamps[i] * 1000);
        printToUser("   Sample with timestamp " + sampleDate + " and value " + origValues[i] + " replaced by value " + newValues[i]);
    }
}
Also used : RrdException(org.jrobin.core.RrdException) Date(java.util.Date)

Example 2 with RrdException

use of org.jrobin.core.RrdException in project opennms by OpenNMS.

the class JRrd2FetchStrategy method fetchMeasurements.

@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;
    }
    // Use labels without spaces when executing the xport command
    // These are mapped back to the requested labels in the response
    final Map<String, String> labelMap = Maps.newHashMap();
    int k = 0;
    List<String> argv = Lists.newLinkedList();
    for (final Map.Entry<Source, String> entry : rrdsBySource.entrySet()) {
        final Source source = entry.getKey();
        final String rrdFile = entry.getValue();
        final String tempLabel = Integer.toString(++k);
        labelMap.put(tempLabel, source.getLabel());
        argv.add(String.format("DEF:%s=%s:%s:%s", tempLabel, Utils.escapeColons(rrdFile), Utils.escapeColons(source.getEffectiveDataSource()), source.getAggregation()));
        argv.add(String.format("XPORT:%s:%s", tempLabel, tempLabel));
    }
    org.opennms.netmgt.rrd.jrrd2.api.FetchResults xportResults;
    try {
        xportResults = jrrd2.xport(startInSeconds, endInSeconds, stepInSeconds, maxrows, argv.toArray(new String[argv.size()]));
    } catch (JRrd2Exception e) {
        throw new RrdException("Xport failed.", e);
    }
    // Convert to ms
    final long[] timestamps = xportResults.getTimestamps();
    for (int i = 0; i < timestamps.length; i++) {
        timestamps[i] *= 1000;
    }
    // Map the column labels from their temporary values to their requested values
    Map<String, double[]> valuesByTempLabel = xportResults.getColumnsWithValues();
    Map<String, double[]> valuesByLabel = Maps.newLinkedHashMap();
    for (Entry<String, double[]> entry : valuesByTempLabel.entrySet()) {
        valuesByLabel.put(labelMap.get(entry.getKey()), entry.getValue());
    }
    return new FetchResults(timestamps, valuesByLabel, xportResults.getStep() * 1000, constants);
}
Also used : JRrd2Exception(org.opennms.netmgt.rrd.jrrd2.api.JRrd2Exception) Source(org.opennms.netmgt.measurements.model.Source) FetchResults(org.opennms.netmgt.measurements.api.FetchResults) RrdException(org.jrobin.core.RrdException) Map(java.util.Map)

Example 3 with RrdException

use of org.jrobin.core.RrdException in project opennms by OpenNMS.

the class RrdtoolXportFetchStrategy 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 {
    String rrdBinary = System.getProperty("rrd.binary");
    if (rrdBinary == null) {
        throw new RrdException("No RRD binary is set.");
    }
    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 CommandLine cmdLine = new CommandLine(rrdBinary);
    cmdLine.addArgument("xport");
    cmdLine.addArgument("--step");
    cmdLine.addArgument("" + stepInSeconds);
    cmdLine.addArgument("--start");
    cmdLine.addArgument("" + startInSeconds);
    cmdLine.addArgument("--end");
    cmdLine.addArgument("" + endInSeconds);
    if (maxrows > 0) {
        cmdLine.addArgument("--maxrows");
        cmdLine.addArgument("" + maxrows);
    }
    // Use labels without spaces when executing the xport command
    // These are mapped back to the requested labels in the response
    final Map<String, String> labelMap = Maps.newHashMap();
    int k = 0;
    for (final Map.Entry<Source, String> entry : rrdsBySource.entrySet()) {
        final Source source = entry.getKey();
        final String rrdFile = entry.getValue();
        final String tempLabel = Integer.toString(++k);
        labelMap.put(tempLabel, source.getLabel());
        cmdLine.addArgument(String.format("DEF:%s=%s:%s:%s", tempLabel, Utils.escapeColons(rrdFile), Utils.escapeColons(source.getEffectiveDataSource()), source.getAggregation()));
        cmdLine.addArgument(String.format("XPORT:%s:%s", tempLabel, tempLabel));
    }
    // Use commons-exec to execute rrdtool
    final DefaultExecutor executor = new DefaultExecutor();
    // Capture stdout/stderr
    final ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    final ByteArrayOutputStream stderr = new ByteArrayOutputStream();
    executor.setStreamHandler(new PumpStreamHandler(stdout, stderr, null));
    // Fail if we get a non-zero exit code
    executor.setExitValue(0);
    // Fail if the process takes too long
    final ExecuteWatchdog watchdog = new ExecuteWatchdog(XPORT_TIMEOUT_MS);
    executor.setWatchdog(watchdog);
    // Export
    RrdXport rrdXport;
    try {
        LOG.debug("Executing: {}", cmdLine);
        executor.execute(cmdLine);
        final XMLReader xmlReader = XMLReaderFactory.createXMLReader();
        xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        final SAXSource source = new SAXSource(xmlReader, new InputSource(new StringReader(stdout.toString())));
        final JAXBContext jc = JAXBContext.newInstance(RrdXport.class);
        final Unmarshaller u = jc.createUnmarshaller();
        rrdXport = (RrdXport) u.unmarshal(source);
    } catch (IOException e) {
        throw new RrdException("An error occured while executing '" + StringUtils.join(cmdLine.toStrings(), " ") + "' with stderr: " + stderr.toString(), e);
    } catch (SAXException | JAXBException e) {
        throw new RrdException("The output generated by 'rrdtool xport' could not be parsed.", e);
    }
    final int numRows = rrdXport.getRows().size();
    final int numColumns = rrdXport.getMeta().getLegends().size();
    final long xportStartInMs = rrdXport.getMeta().getStart() * 1000;
    final long xportStepInMs = rrdXport.getMeta().getStep() * 1000;
    final long[] timestamps = new long[numRows];
    final double[][] values = new double[numColumns][numRows];
    // Convert rows to columns
    int i = 0;
    for (final XRow row : rrdXport.getRows()) {
        // Derive the timestamp from the start and step since newer versions
        // of rrdtool no longer include it as part of the rows
        timestamps[i] = xportStartInMs + xportStepInMs * i;
        for (int j = 0; j < numColumns; j++) {
            if (row.getValues() == null) {
                // NMS-7710: Avoid NPEs, in certain cases the list of values may be null
                throw new RrdException("The output generated by 'rrdtool xport' was not recognized. Try upgrading your rrdtool binaries.");
            }
            values[j][i] = row.getValues().get(j);
        }
        i++;
    }
    // Map the columns by label
    // The legend entries are in the same order as the column values
    final Map<String, double[]> columns = Maps.newHashMapWithExpectedSize(numColumns);
    i = 0;
    for (String label : rrdXport.getMeta().getLegends()) {
        columns.put(labelMap.get(label), values[i++]);
    }
    return new FetchResults(timestamps, columns, xportStepInMs, constants);
}
Also used : InputSource(org.xml.sax.InputSource) JAXBContext(javax.xml.bind.JAXBContext) XRow(org.opennms.netmgt.rrd.model.XRow) RrdXport(org.opennms.netmgt.rrd.model.RrdXport) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) Source(org.opennms.netmgt.measurements.model.Source) SAXException(org.xml.sax.SAXException) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) FetchResults(org.opennms.netmgt.measurements.api.FetchResults) StringReader(java.io.StringReader) RrdException(org.jrobin.core.RrdException) Unmarshaller(javax.xml.bind.Unmarshaller) XMLReader(org.xml.sax.XMLReader) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) JAXBException(javax.xml.bind.JAXBException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) CommandLine(org.apache.commons.exec.CommandLine) SAXSource(javax.xml.transform.sax.SAXSource) Map(java.util.Map)

Example 4 with RrdException

use of org.jrobin.core.RrdException in project opennms by OpenNMS.

the class GraphResultsController method handleRequestInternal.

/**
 * {@inheritDoc}
 */
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String[] requiredParameters = new String[] { "resourceId", "reports" };
    for (String requiredParameter : requiredParameters) {
        if (request.getParameter(requiredParameter) == null) {
            throw new MissingParameterException(requiredParameter, requiredParameters);
        }
    }
    ResourceId[] resourceIds = Arrays.stream(request.getParameterValues("resourceId")).map(ResourceId::fromString).toArray(ResourceId[]::new);
    String[] reports = request.getParameterValues("reports");
    // see if the start and end time were explicitly set as params
    String start = request.getParameter("start");
    String end = request.getParameter("end");
    String relativeTime = request.getParameter("relativetime");
    final String startMonth = request.getParameter("startMonth");
    final String startDate = request.getParameter("startDate");
    final String startYear = request.getParameter("startYear");
    final String startHour = request.getParameter("startHour");
    final String endMonth = request.getParameter("endMonth");
    final String endDate = request.getParameter("endDate");
    final String endYear = request.getParameter("endYear");
    final String endHour = request.getParameter("endHour");
    long startLong = 0;
    long endLong = 0;
    if (start != null || end != null) {
        String[] ourRequiredParameters = new String[] { "start", "end" };
        if (start == null) {
            throw new MissingParameterException("start", ourRequiredParameters);
        }
        if (end == null) {
            throw new MissingParameterException("end", ourRequiredParameters);
        }
        // The following is very similar to RrdGraphController.parseTimes, but modified for the local context a bit
        // There's merging possibilities, but I don't know how (common parent class seems wrong; service bean for a single
        // method isn't much better.  Ideas?
        // Try a simple 'long' parsing.  If either fails, do a full parse.  If one is a straight 'long' but the other isn't
        // that's fine, the TimeParser code will handle it fine (as long as we convert milliseconds to seconds)
        // Indeed, we *have* to use TimeParse for both to ensure any relative references (using "start" or "end") work correctly.
        // NB: can't do a "safe" parsing using the WebSecurityUtils; if we did, it would filter out all the possible rrdfetch
        // format text and always work :)
        boolean startIsInteger = false;
        boolean endIsInteger = false;
        // is expected for epoch times by TimeParser
        try {
            startLong = Long.valueOf(start);
            startIsInteger = true;
            start = "" + (startLong / 1000);
        } catch (NumberFormatException e) {
        }
        try {
            endLong = Long.valueOf(end);
            endIsInteger = true;
            end = "" + (endLong / 1000);
        } catch (NumberFormatException e) {
        }
        if (!endIsInteger || !startIsInteger) {
            // One or both of start/end aren't integers, so we need to do full parsing using TimeParser
            TimeParser startParser = new TimeParser(start);
            TimeParser endParser = new TimeParser(end);
            try {
                TimeSpec specStart = startParser.parse();
                TimeSpec specEnd = endParser.parse();
                long[] results = TimeSpec.getTimestamps(specStart, specEnd);
                // Multiply by 1000.  TimeSpec returns timestamps in Seconds, not Milliseconds.
                startLong = results[0] * 1000;
                endLong = results[1] * 1000;
            } catch (RrdException e1) {
                throw new IllegalArgumentException("Could not parse start '" + start + "' and end '" + end + "' as valid time specifications", e1);
            }
        }
    } else if (startMonth != null || startDate != null || startYear != null || startHour != null || endMonth != null || endDate != null || endYear != null || endHour != null) {
        String[] ourRequiredParameters = new String[] { "startMonth", "startDate", "startYear", "startHour", "endMonth", "endDate", "endYear", "endHour" };
        for (String requiredParameter : ourRequiredParameters) {
            if (request.getParameter(requiredParameter) == null) {
                throw new MissingParameterException(requiredParameter, ourRequiredParameters);
            }
        }
        Calendar startCal = Calendar.getInstance();
        startCal.set(Calendar.MONTH, WebSecurityUtils.safeParseInt(startMonth));
        startCal.set(Calendar.DATE, WebSecurityUtils.safeParseInt(startDate));
        startCal.set(Calendar.YEAR, WebSecurityUtils.safeParseInt(startYear));
        startCal.set(Calendar.HOUR_OF_DAY, WebSecurityUtils.safeParseInt(startHour));
        startCal.set(Calendar.MINUTE, 0);
        startCal.set(Calendar.SECOND, 0);
        startCal.set(Calendar.MILLISECOND, 0);
        Calendar endCal = Calendar.getInstance();
        endCal.set(Calendar.MONTH, WebSecurityUtils.safeParseInt(endMonth));
        endCal.set(Calendar.DATE, WebSecurityUtils.safeParseInt(endDate));
        endCal.set(Calendar.YEAR, WebSecurityUtils.safeParseInt(endYear));
        endCal.set(Calendar.HOUR_OF_DAY, WebSecurityUtils.safeParseInt(endHour));
        endCal.set(Calendar.MINUTE, 0);
        endCal.set(Calendar.SECOND, 0);
        endCal.set(Calendar.MILLISECOND, 0);
        startLong = startCal.getTime().getTime();
        endLong = endCal.getTime().getTime();
    } else {
        if (relativeTime == null) {
            relativeTime = s_periods[0].getId();
        }
        RelativeTimePeriod period = RelativeTimePeriod.getPeriodByIdOrDefault(s_periods, relativeTime, s_periods[0]);
        long[] times = period.getStartAndEndTimes();
        startLong = times[0];
        endLong = times[1];
    }
    // The 'matching' parameter is going to work only for one resource.
    String matching = request.getParameter("matching");
    if (matching != null) {
        reports = getSuggestedReports(resourceIds[0], matching);
    }
    ModelAndView modelAndView = null;
    try {
        GraphResults model = m_graphResultsService.findResults(resourceIds, reports, startLong, endLong, relativeTime);
        modelAndView = new ModelAndView("/graph/results", "results", model);
    } catch (Exception e) {
        LOG.warn("Can't get graph results", e);
        modelAndView = new ModelAndView("/graph/results-error");
    }
    modelAndView.addObject("loggedIn", request.getRemoteUser() != null);
    return modelAndView;
}
Also used : Calendar(java.util.Calendar) ModelAndView(org.springframework.web.servlet.ModelAndView) MissingParameterException(org.opennms.web.servlet.MissingParameterException) RrdException(org.jrobin.core.RrdException) TimeSpec(org.jrobin.core.timespec.TimeSpec) RelativeTimePeriod(org.opennms.web.svclayer.model.RelativeTimePeriod) ResourceId(org.opennms.netmgt.model.ResourceId) GraphResults(org.opennms.web.svclayer.model.GraphResults) MissingParameterException(org.opennms.web.servlet.MissingParameterException) RrdException(org.jrobin.core.RrdException) TimeParser(org.jrobin.core.timespec.TimeParser)

Example 5 with RrdException

use of org.jrobin.core.RrdException in project i2p.i2p by i2p.

the class SummaryListener method startListening.

/**
 *  @return success
 */
public boolean startListening() {
    RateStat rs = _rate.getRateStat();
    long period = _rate.getPeriod();
    String baseName = rs.getName() + "." + period;
    _name = createName(_context, baseName);
    _eventName = createName(_context, baseName + ".events");
    File rrdFile = null;
    try {
        RrdBackendFactory factory = RrdBackendFactory.getFactory(getBackendName());
        String rrdDefName;
        if (_isPersistent) {
            // generate full path for persistent RRD files
            File rrdDir = new SecureFile(_context.getRouterDir(), RRD_DIR);
            rrdFile = new File(rrdDir, RRD_PREFIX + _name + RRD_SUFFIX);
            rrdDefName = rrdFile.getAbsolutePath();
            if (rrdFile.exists()) {
                _db = new RrdDb(rrdDefName, factory);
                Archive arch = _db.getArchive(CF, STEPS);
                if (arch == null)
                    throw new IOException("No average CF in " + rrdDefName);
                _rows = arch.getRows();
                if (_log.shouldLog(Log.INFO))
                    _log.info("Existing RRD " + baseName + " (" + rrdDefName + ") with " + _rows + " rows consuming " + _db.getRrdBackend().getLength() + " bytes");
            } else {
                rrdDir.mkdir();
            }
        } else {
            rrdDefName = _name;
        }
        if (_db == null) {
            // not persistent or not previously existing
            RrdDef def = new RrdDef(rrdDefName, now() / 1000, period / 1000);
            // for info on the heartbeat, xff, steps, etc, see the rrdcreate man page, aka
            // http://www.jrobin.org/support/man/rrdcreate.html
            long heartbeat = period * 10 / 1000;
            def.addDatasource(_name, "GAUGE", heartbeat, Double.NaN, Double.NaN);
            def.addDatasource(_eventName, "GAUGE", heartbeat, 0, Double.NaN);
            if (_isPersistent) {
                _rows = (int) Math.max(MIN_ROWS, Math.min(MAX_ROWS, THREE_MONTHS / period));
            } else {
                _rows = MIN_ROWS;
            }
            def.addArchive(CF, XFF, STEPS, _rows);
            _db = new RrdDb(def, factory);
            if (_isPersistent)
                SecureFileOutputStream.setPerms(new File(rrdDefName));
            if (_log.shouldLog(Log.INFO))
                _log.info("New RRD " + baseName + " (" + rrdDefName + ") with " + _rows + " rows consuming " + _db.getRrdBackend().getLength() + " bytes");
        }
        _sample = _db.createSample();
        _renderer = new SummaryRenderer(_context, this);
        _rate.setSummaryListener(this);
        return true;
    } catch (OutOfMemoryError oom) {
        _log.error("Error starting RRD for stat " + baseName, oom);
    } catch (RrdException re) {
        _log.error("Error starting RRD for stat " + baseName, re);
        // corrupt file?
        if (_isPersistent && rrdFile != null)
            rrdFile.delete();
    } catch (IOException ioe) {
        _log.error("Error starting RRD for stat " + baseName, ioe);
    } catch (Throwable t) {
        _log.error("Error starting RRD for stat " + baseName, t);
    }
    return false;
}
Also used : Archive(org.jrobin.core.Archive) RrdDef(org.jrobin.core.RrdDef) SecureFile(net.i2p.util.SecureFile) IOException(java.io.IOException) RateStat(net.i2p.stat.RateStat) RrdBackendFactory(org.jrobin.core.RrdBackendFactory) RrdDb(org.jrobin.core.RrdDb) RrdException(org.jrobin.core.RrdException) SecureFile(net.i2p.util.SecureFile) File(java.io.File)

Aggregations

RrdException (org.jrobin.core.RrdException)37 Node (org.w3c.dom.Node)13 IOException (java.io.IOException)10 Map (java.util.Map)4 Date (java.util.Date)3 FetchData (org.jrobin.core.FetchData)3 RrdDb (org.jrobin.core.RrdDb)3 FetchResults (org.opennms.netmgt.measurements.api.FetchResults)3 Source (org.opennms.netmgt.measurements.model.Source)3 File (java.io.File)2 Calendar (java.util.Calendar)2 JAXBContext (javax.xml.bind.JAXBContext)2 Unmarshaller (javax.xml.bind.Unmarshaller)2 SAXSource (javax.xml.transform.sax.SAXSource)2 TimeParser (org.jrobin.core.timespec.TimeParser)2 TimeSpec (org.jrobin.core.timespec.TimeSpec)2 Color (java.awt.Color)1 Font (java.awt.Font)1 Graphics (java.awt.Graphics)1 BufferedImage (java.awt.image.BufferedImage)1