Example #1
0
 public function testBuildCommandException()
 {
     global $config;
     $config['rrdcached'] = '';
     $config['rrdtool_version'] = '1.4';
     $this->setExpectedException('LibreNMS\\Exceptions\\FileExistsException');
     // use this file, since it is guaranteed to exist
     rrdtool_build_command('create', __FILE__, 'o');
 }
Example #2
0
/**
 * Generates and pipes a command to rrdtool
 *
 * @internal
 * @param string $command create, update, updatev, graph, graphv, dump, restore, fetch, tune, first, last, lastupdate, info, resize, xport, flushcached
 * @param string $filename The full patth to the rrd file
 * @param string $options rrdtool command options
 * @return array the output of stdout and stderr in an array
 * @throws FileExistsException thrown when a create command is set to rrdtool < 1.4 and the rrd already exists
 * @throws Exception thrown when the rrdtool process(s) cannot be started
 */
function rrdtool($command, $filename, $options)
{
    global $config, $debug, $vdebug, $rrd_async_process, $rrd_sync_process;
    try {
        $cmd = rrdtool_build_command($command, $filename, $options);
    } catch (FileExistsException $e) {
        c_echo('RRD[%g' . $filename . " already exists%n]\n", $debug);
        return array(null, null);
    }
    c_echo("RRD[%g{$cmd}%n]\n", $debug);
    // do not write rrd files, but allow read-only commands
    $ro_commands = array('graph', 'graphv', 'dump', 'fetch', 'first', 'last', 'lastupdate', 'info', 'xport');
    if (!empty($config['norrd']) && !in_array($command, $ro_commands)) {
        c_echo('[%rRRD Disabled%n]');
        return array(null, null);
    }
    // send the command!
    if ($command == 'last' && rrdtool_initialize(false)) {
        // send this to our synchronous process so output is guaranteed
        $output = $rrd_sync_process->sendCommand($cmd);
    } elseif (rrdtool_initialize()) {
        // don't care about the return of other commands, so send them to the faster async process
        $output = $rrd_async_process->sendCommand($cmd);
    } else {
        throw new Exception('rrdtool could not start');
    }
    if ($vdebug) {
        echo 'RRDtool Output: ';
        echo $output[0];
        echo $output[1];
    }
    return $output;
}