Search in sources :

Example 1 with RrdException

use of org.opennms.netmgt.rrd.RrdException in project opennms by OpenNMS.

the class RrdPersistOperationBuilder method commit.

/**
     * <p>commit</p>
     *
     * @throws org.opennms.netmgt.collection.api.PersistException if any.
     */
public void commit() throws PersistException {
    if (m_declarations.size() == 0) {
        // Nothing to do.  In fact, we'll get an error if we try to create an RRD file with no data sources            
        return;
    }
    try {
        final String ownerName = m_resource.getOwnerName();
        final String absolutePath = getResourceDir(m_resource);
        RrdMetaDataUtils.createMetaDataFile(absolutePath, m_rrdName, m_metaData);
        List<RrdDataSource> dataSources = getDataSources();
        if (dataSources != null && dataSources.size() > 0) {
            createRRD(m_rrdStrategy, ownerName, absolutePath, m_rrdName, getRepository().getStep(), dataSources, getRepository().getRraList());
            updateRRD(m_rrdStrategy, ownerName, absolutePath, m_rrdName, m_timeKeeper.getCurrentTime(), getValues());
        }
    } catch (FileNotFoundException e) {
        LoggerFactory.getLogger(getClass()).warn("Could not get resource directory: " + e.getMessage(), e);
        return;
    } catch (RrdException e) {
        throw new PersistException(e);
    }
}
Also used : PersistException(org.opennms.netmgt.collection.api.PersistException) FileNotFoundException(java.io.FileNotFoundException) RrdDataSource(org.opennms.netmgt.rrd.RrdDataSource) RrdException(org.opennms.netmgt.rrd.RrdException)

Example 2 with RrdException

use of org.opennms.netmgt.rrd.RrdException in project opennms by OpenNMS.

the class AbstractJniRrdStrategy method createGraphReturnDetails.

/** {@inheritDoc} */
@Override
public RrdGraphDetails createGraphReturnDetails(String command, File workDir) throws IOException, org.opennms.netmgt.rrd.RrdException {
    // Creating Temp PNG File
    File pngFile = File.createTempFile("opennms.rrdtool.", ".png");
    command = command.replaceFirst("graph - ", "graph " + pngFile.getAbsolutePath() + " ");
    int width;
    int height;
    String[] printLines;
    InputStream pngStream;
    try {
        // Executing RRD Command
        InputStream is = createGraph(command, workDir);
        // Processing Command Output
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        try {
            String line = null;
            if ((line = reader.readLine()) == null) {
                throw new IOException("No output from the createGraph() command");
            }
            String[] s = line.split("x");
            width = Integer.parseInt(s[0]);
            height = Integer.parseInt(s[1]);
            List<String> printLinesList = new ArrayList<String>();
            while ((line = reader.readLine()) != null) {
                printLinesList.add(line);
            }
            printLines = printLinesList.toArray(new String[printLinesList.size()]);
        } finally {
            reader.close();
        }
        // Creating PNG InputStream
        byte[] byteArray = FileCopyUtils.copyToByteArray(pngFile);
        pngStream = new ByteArrayInputStream(byteArray);
    } catch (Throwable e) {
        throw new RrdException("Can't execute command " + command, e);
    } finally {
        if (!pngFile.delete()) {
            LOG.warn("Could not delete file: {}", pngFile.getPath());
        }
    }
    // Creating Graph Details
    return new JniGraphDetails(width, height, printLines, pngStream);
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) RrdException(org.opennms.netmgt.rrd.RrdException) File(java.io.File)

Example 3 with RrdException

use of org.opennms.netmgt.rrd.RrdException in project opennms by OpenNMS.

the class AbstractJniRrdStrategy method createGraphAsByteArray.

private byte[] createGraphAsByteArray(String command, File workDir) throws IOException, RrdException {
    String[] commandArray = StringUtils.createCommandArray(command);
    Process process;
    try {
        process = Runtime.getRuntime().exec(commandArray, null, workDir);
    } catch (IOException e) {
        IOException newE = new IOException("IOException thrown while executing command '" + command + "' in " + workDir.getAbsolutePath() + ": " + e);
        newE.initCause(e);
        throw newE;
    }
    // this closes the stream when its finished
    byte[] byteArray = FileCopyUtils.copyToByteArray(process.getInputStream());
    // this close the stream when its finished
    String errors = FileCopyUtils.copyToString(new InputStreamReader(process.getErrorStream()));
    // one particular warning message that originates in libart should be ignored
    if (errors.length() > 0 && errors.contains(IGNORABLE_LIBART_WARNING_STRING)) {
        LOG.debug("Ignoring libart warning message in rrdtool stderr stream: {}", IGNORABLE_LIBART_WARNING_STRING);
        errors = errors.replaceAll(IGNORABLE_LIBART_WARNING_REGEX, "");
    }
    if (errors.length() > 0) {
        throw new RrdException(errors);
    }
    return byteArray;
}
Also used : InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) RrdException(org.opennms.netmgt.rrd.RrdException)

Example 4 with RrdException

use of org.opennms.netmgt.rrd.RrdException in project opennms by OpenNMS.

the class RrdPersistOperationBuilder method createRRD.

/**
     * <p>createRRD</p>
     *
     * @param creator a {@link java.lang.String} object.
     * @param directory a {@link java.lang.String} object.
     * @param rrdName a {@link java.lang.String} object.
     * @param step a int.
     * @param dataSources a {@link java.util.List} object.
     * @param rraList a {@link java.util.List} object.
     * @return a boolean.
     * @throws org.opennms.netmgt.rrd.RrdException if any.
     */
private static boolean createRRD(RrdStrategy<?, ?> rrdStrategy, String creator, String directory, String rrdName, int step, List<RrdDataSource> dataSources, List<String> rraList) throws RrdException {
    Object def = null;
    try {
        RrdStrategy<Object, Object> strategy = toGenericType(rrdStrategy);
        def = strategy.createDefinition(creator, directory, rrdName, step, dataSources, rraList);
        // def can be null if the rrd-db exists already, but doesn't have to be (see MultiOutput/QueuingRrdStrategy
        strategy.createFile(def);
        return true;
    } catch (Throwable e) {
        String path = directory + File.separator + rrdName + rrdStrategy.getDefaultFileExtension();
        LOG.error("createRRD: An error occurred creating rrdfile {}", path, e);
        throw new org.opennms.netmgt.rrd.RrdException("An error occurred creating rrdfile " + path + ": " + e, e);
    }
}
Also used : RrdException(org.opennms.netmgt.rrd.RrdException)

Example 5 with RrdException

use of org.opennms.netmgt.rrd.RrdException in project opennms by OpenNMS.

the class RrdPersistOperationBuilder method updateRRD.

/**
     * Add datapoints to a round robin database.
     *
     * @param owner the owner of the file. This is used in log messages
     * @param repositoryDir the directory the file resides in
     * @param rrdName the name for the rrd file.
     * @param timestamp the timestamp in millis to use for the rrd update (this
     * gets rounded to the nearest second)
     * @param val a colon separated list of values representing the updates for
     * datasources for this rrd
     * @throws org.opennms.netmgt.rrd.RrdException if any.
     */
private static void updateRRD(RrdStrategy<?, ?> rrdStrategy, String owner, String repositoryDir, String rrdName, long timestamp, String val) throws RrdException {
    // Issue the RRD update
    String rrdFile = repositoryDir + File.separator + rrdName + rrdStrategy.getDefaultFileExtension();
    long time = (timestamp + 500L) / 1000L;
    String updateVal = Long.toString(time) + ":" + val;
    LOG.info("updateRRD: updating RRD file {} with values '{}'", rrdFile, updateVal);
    RrdStrategy<Object, Object> strategy = toGenericType(rrdStrategy);
    Object rrd = null;
    try {
        rrd = strategy.openFile(rrdFile);
        strategy.updateFile(rrd, owner, updateVal);
    } catch (Throwable e) {
        LOG.error("updateRRD: Error updating RRD file {} with values '{}'", rrdFile, updateVal, e);
        throw new org.opennms.netmgt.rrd.RrdException("Error updating RRD file " + rrdFile + " with values '" + updateVal + "': " + e, e);
    } finally {
        try {
            if (rrd != null) {
                strategy.closeFile(rrd);
            }
        } catch (Throwable e) {
            LOG.error("updateRRD: Exception closing RRD file {}", rrdFile, e);
            throw new org.opennms.netmgt.rrd.RrdException("Exception closing RRD file " + rrdFile + ": " + e, e);
        }
    }
    LOG.debug("updateRRD: RRD update command completed.");
}
Also used : RrdException(org.opennms.netmgt.rrd.RrdException)

Aggregations

RrdException (org.opennms.netmgt.rrd.RrdException)6 File (java.io.File)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 PersistException (org.opennms.netmgt.collection.api.PersistException)1 RrdDataSource (org.opennms.netmgt.rrd.RrdDataSource)1 ThrowableAnticipator (org.opennms.test.ThrowableAnticipator)1