Commit c7a488dc authored by Franco Fichtner's avatar Franco Fichtner

rrd: naming conventions and a side note

We have a function pair rrd_import() / rrd_export() now, which do
wildly different things even though they've been used for a single
purpose:

(1) rrd_export() writes a string, appends it to the config.xml

(2) rrd_import() takes a parsed config portion, but not as an
    argument, rather as a global config element

(3) There was also an older backup clue which exported files, but
    simply packed them up for restore after boot

If we can get the system to unify the usage we'll end up with a
neat way of backing, restoring and migrating RRD data, although
I'm not sure if this can be fully compatible with the old files.
parent 4ca71c32
......@@ -372,7 +372,7 @@ EOD;
# Create gateway quality RRD with settings more suitable for OPNsense graph set,
# since apinger uses default step (300; 5 minutes) and other settings that don't
# match the OPNsense gateway quality graph set.
create_gateway_quality_rrd("/var/db/rrd/{$gateway['name']}-quality.rrd");
rrd_create_gateway_quality("/var/db/rrd/{$gateway['name']}-quality.rrd");
}
$gateways_arr = return_gateways_array();
......
......@@ -26,7 +26,7 @@
POSSIBILITY OF SUCH DAMAGE.
*/
function create_new_rrd($rrdcreatecmd)
function rrd_create($rrdcreatecmd)
{
$rrdcreateoutput = array();
$rrdcreatereturn = 0;
......@@ -39,7 +39,7 @@ function create_new_rrd($rrdcreatecmd)
return $rrdcreatereturn;
}
function enable_rrd_graphing($verbose = false)
function rrd_configure($verbose = false)
{
global $config;
......@@ -158,7 +158,7 @@ function enable_rrd_graphing($verbose = false)
$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
create_new_rrd($rrdcreate);
rrd_create($rrdcreate);
unset($rrdcreate);
}
......@@ -191,7 +191,7 @@ function enable_rrd_graphing($verbose = false)
$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
create_new_rrd($rrdcreate);
rrd_create($rrdcreate);
unset($rrdcreate);
}
......@@ -220,7 +220,7 @@ function enable_rrd_graphing($verbose = false)
$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
create_new_rrd($rrdcreate);
rrd_create($rrdcreate);
unset($rrdcreate);
}
......@@ -245,7 +245,7 @@ function enable_rrd_graphing($verbose = false)
$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
create_new_rrd($rrdcreate);
rrd_create($rrdcreate);
unset($rrdcreate);
}
......@@ -285,7 +285,7 @@ function enable_rrd_graphing($verbose = false)
$rrdcreate .= "RRA:AVERAGE:0.5:5:720 ";
$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
create_new_rrd($rrdcreate);
rrd_create($rrdcreate);
unset($rrdcreate);
}
......@@ -318,7 +318,7 @@ function enable_rrd_graphing($verbose = false)
$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
create_new_rrd($rrdcreate);
rrd_create($rrdcreate);
unset($rrdcreate);
}
......@@ -352,7 +352,7 @@ function enable_rrd_graphing($verbose = false)
$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
create_new_rrd($rrdcreate);
rrd_create($rrdcreate);
unset($rrdcreate);
}
......@@ -389,7 +389,7 @@ function enable_rrd_graphing($verbose = false)
$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
$rrdcreate .= "RRA:MAX:0.5:1440:2284";
create_new_rrd($rrdcreate);
rrd_create($rrdcreate);
unset($rrdcreate);
}
......@@ -425,7 +425,7 @@ function enable_rrd_graphing($verbose = false)
$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
$rrdcreate .= "RRA:MAX:0.5:1440:2284";
create_new_rrd($rrdcreate);
rrd_create($rrdcreate);
unset($rrdcreate);
}
......@@ -466,7 +466,7 @@ function enable_rrd_graphing($verbose = false)
$rrdcreate .= "RRA:MAX:0.5:60:1860 ";
$rrdcreate .= "RRA:MAX:0.5:1440:2284 ";
create_new_rrd($rrdcreate);
rrd_create($rrdcreate);
unset($rrdcreate);
}
......@@ -512,7 +512,7 @@ function enable_rrd_graphing($verbose = false)
$rrdcreate .= "RRA:LAST:0.5:60:1000 ";
$rrdcreate .= "RRA:LAST:0.5:720:3000 ";
create_new_rrd($rrdcreate);
rrd_create($rrdcreate);
}
/* enter UNKNOWN values in the RRD so it knows we rebooted. */
......@@ -537,13 +537,13 @@ function enable_rrd_graphing($verbose = false)
unset($rrdupdatesh);
/* kill off traffic collectors */
kill_traffic_collector();
rrd_stop();
/* start traffic collector */
mwexec_bg("/bin/sh $updaterrdscript");
} else {
/* kill off traffic collectors */
kill_traffic_collector();
rrd_stop();
}
$databases = glob("{$rrddbpath}/*.rrd");
......@@ -556,7 +556,7 @@ function enable_rrd_graphing($verbose = false)
}
}
function create_gateway_quality_rrd($rrd_file)
function rrd_create_gateway_quality($rrd_file)
{
$rrdinterval = 60;
$valid = $rrdinterval * 2;
......@@ -572,7 +572,7 @@ function create_gateway_quality_rrd($rrd_file)
$rrdcreate .= "RRA:AVERAGE:0.5:60:1860 ";
$rrdcreate .= "RRA:AVERAGE:0.5:1440:2284 ";
create_new_rrd($rrdcreate);
rrd_create($rrdcreate);
unset($rrdcreate);
}
......@@ -583,7 +583,81 @@ function create_gateway_quality_rrd($rrd_file)
unset($rrdtool, $rrdinterval, $valid, $rrd_file);
}
function kill_traffic_collector()
function rrd_stop()
{
killbypid('/var/run/updaterrd.sh.pid');
killbypid('/var/run/updaterrd.sh.pid', 'TERM', true);
}
function rrd_export()
{
$rrddbpath = '/var/db/rrd';
$result = "\t<rrddata>\n";
$rrd_files = glob("{$rrddbpath}/*.rrd");
$xml_files = array();
foreach ($rrd_files as $rrd_file) {
$basename = basename($rrd_file);
$xml_file = preg_replace('/\.rrd$/', ".xml", $rrd_file);
exec("/usr/local/bin/rrdtool dump '{$rrd_file}' '{$xml_file}'");
$xml_data = @file_get_contents($xml_file);
@unlink($xml_file);
if ($xml_data !== false) {
$result .= "\t\t<rrddatafile>\n";
$result .= "\t\t\t<filename>{$basename}</filename>\n";
$result .= "\t\t\t<xmldata>" . base64_encode(gzdeflate($xml_data)) . "</xmldata>\n";
$result .= "\t\t</rrddatafile>\n";
}
}
$result .= "\t</rrddata>\n";
return $result;
}
function rrd_import()
{
global $config;
foreach ($config['rrddata']['rrddatafile'] as $rrd) {
if (!empty($rrd['xmldata'])) {
$rrd_file = "/var/db/rrd/{$rrd['filename']}";
$xml_file = preg_replace('/\.rrd$/', ".xml", $rrd_file);
if (file_put_contents($xml_file, gzinflate(base64_decode($rrd['xmldata']))) === false) {
log_error("Cannot write $xml_file");
continue;
}
$output = array();
$status = null;
exec("/usr/local/bin/rrdtool restore -f '{$xml_file}' '{$rrd_file}'", $output, $status);
if ($status) {
log_error("rrdtool restore -f '{$xml_file}' '{$rrd_file}' failed returning {$status}.");
continue;
}
unlink($xml_file);
} elseif (!empty($rrd['data'])) {
/* rrd backup format */
$rrd_file = "/var/db/rrd/{$rrd['filename']}";
$rrd_fd = fopen($rrd_file, "w");
if (!$rrd_fd) {
log_error("Cannot write $rrd_file");
continue;
}
$data = base64_decode($rrd['data']);
/* Try to decompress the data. */
$dcomp = @gzinflate($data);
if ($dcomp) {
/* If the decompression worked, write the decompressed data */
if (fwrite($rrd_fd, $dcomp) === false) {
log_error("fwrite $rrd_file failed");
continue;
}
} elseif (fwrite($rrd_fd, $data) === false) {
/* If the decompression failed, it wasn't compressed, so write raw data */
log_error("fwrite $rrd_file failed");
continue;
}
if (fclose($rrd_fd) === false) {
log_error("fclose $rrd_file failed");
continue;
}
}
}
}
......@@ -2107,7 +2107,7 @@ function upgrade_054_to_055()
$rrdcreate .= "RRA:AVERAGE:0.5:60:1000 ";
$rrdcreate .= "RRA:AVERAGE:0.5:720:1000 ";
create_new_rrd("$rrdcreate");
rrd_create("$rrdcreate");
/* create temporary xml from new RRD */
dump_rrd_to_xml("/tmp/{$databasetmp}", "/tmp/{$xmldumptmp}");
......@@ -2136,7 +2136,7 @@ function upgrade_054_to_055()
mwexec("$rrdtool resize {$rrddbpath}/{$database} 3 GROW 2000;/bin/mv resize.rrd {$rrddbpath}/{$database} 2>&1");
unset($rrdxmlarray);
}
enable_rrd_graphing();
rrd_configure();
}
function upgrade_055_to_056() {
......@@ -2708,7 +2708,7 @@ function upgrade_080_to_081()
@unlink("/tmp/{$xmldump}");
@unlink("/tmp/{$xmldumpnew}");
}
enable_rrd_graphing();
rrd_configure();
foreach($config['filter']['rule'] as & $rule) {
if (isset($rule['protocol']) && !empty($rule['protocol']))
......@@ -2934,7 +2934,7 @@ function upgrade_095_to_096()
mwexec("{$cmd} 2>&1");
}
enable_rrd_graphing();
rrd_configure();
}
function upgrade_096_to_097()
......
......@@ -122,7 +122,7 @@ plugins_configure('bootup', true);
/* start IPsec tunnels */
$ipsec_dynamic_hosts = ipsec_configure_do(true);
enable_rrd_graphing(true);
rrd_configure(true);
system_powerd_configure(true);
/*
......
......@@ -47,7 +47,7 @@ if (set_networking_interfaces_ports()) {
killbypid("{$g['dhcpd_chroot_path']}/var/run/dhcpdv6.pid", 'TERM', true);
interfaces_configure(true);
enable_rrd_graphing(true);
rrd_configure(true);
}
system_console_unmute();
......@@ -185,7 +185,7 @@ if (!is_ipaddr($oldip) || $curwanip != $oldip || !is_ipaddrv4($config['interface
plugins_configure('interface', false, array($interface));
/* reload graphing functions */
enable_rrd_graphing();
rrd_configure();
}
/* reload filter, don't try to sync to carp slave */
......
......@@ -136,4 +136,4 @@ if (is_ipaddrv6($oldipv6)) {
plugins_configure('interface', false, array($interface));
/* reload graphing functions */
enable_rrd_graphing();
rrd_configure();
......@@ -68,7 +68,7 @@ plugins_configure('local', true);
log_error("rc.reload_all: Reloading RRD graphing");
enable_rrd_graphing(true);
rrd_configure(true);
log_error("rc.reload_all: Reloading filter configuration.");
......
......@@ -13,4 +13,4 @@ if (count($argv) > 1 && is_numeric($argv[1])) {
}
system_webgui_configure(true);
enable_rrd_graphing(true);
rrd_configure(true);
......@@ -65,78 +65,6 @@ function restore_config_section($section_name, $new_contents)
return true;
}
function rrd_data_xml()
{
$rrddbpath = '/var/db/rrd';
$result = "\t<rrddata>\n";
$rrd_files = glob("{$rrddbpath}/*.rrd");
$xml_files = array();
foreach ($rrd_files as $rrd_file) {
$basename = basename($rrd_file);
$xml_file = preg_replace('/\.rrd$/', ".xml", $rrd_file);
exec("/usr/local/bin/rrdtool dump '{$rrd_file}' '{$xml_file}'");
$xml_data = @file_get_contents($xml_file);
@unlink($xml_file);
if ($xml_data !== false) {
$result .= "\t\t<rrddatafile>\n";
$result .= "\t\t\t<filename>{$basename}</filename>\n";
$result .= "\t\t\t<xmldata>" . base64_encode(gzdeflate($xml_data)) . "</xmldata>\n";
$result .= "\t\t</rrddatafile>\n";
}
}
$result .= "\t</rrddata>\n";
return $result;
}
function restore_rrddata() {
global $config;
foreach($config['rrddata']['rrddatafile'] as $rrd) {
if (!empty($rrd['xmldata'])) {
$rrd_file = "/var/db/rrd/{$rrd['filename']}";
$xml_file = preg_replace('/\.rrd$/', ".xml", $rrd_file);
if (file_put_contents($xml_file, gzinflate(base64_decode($rrd['xmldata']))) === false) {
log_error("Cannot write $xml_file");
continue;
}
$output = array();
$status = null;
exec("/usr/local/bin/rrdtool restore -f '{$xml_file}' '{$rrd_file}'", $output, $status);
if ($status) {
log_error("rrdtool restore -f '{$xml_file}' '{$rrd_file}' failed returning {$status}.");
continue;
}
unlink($xml_file);
} elseif (!empty($rrd['data'])) {
/* rrd backup format */
$rrd_file = "/var/db/rrd/{$rrd['filename']}";
$rrd_fd = fopen($rrd_file, "w");
if (!$rrd_fd) {
log_error("Cannot write $rrd_file");
continue;
}
$data = base64_decode($rrd['data']);
/* Try to decompress the data. */
$dcomp = @gzinflate($data);
if ($dcomp) {
/* If the decompression worked, write the decompressed data */
if (fwrite($rrd_fd, $dcomp) === false) {
log_error("fwrite $rrd_file failed");
continue;
}
} elseif (fwrite($rrd_fd, $data) === false) {
/* If the decompression failed, it wasn't compressed, so write raw data */
log_error("fwrite $rrd_file failed");
continue;
}
if (fclose($rrd_fd) === false) {
log_error("fclose $rrd_file failed");
continue;
}
}
}
}
$areas = array(
'OPNsense' => gettext('OPNsense Additions'), /* XXX need specifics */
'aliases' => gettext('Aliases'),
......@@ -220,7 +148,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
/* backup RRD data */
if (empty($_POST['donotbackuprrd'])) {
$rrd_data_xml = rrd_data_xml();
$rrd_data_xml = rrd_export();
$closing_tag = "</opnsense>";
$data = str_replace($closing_tag, $rrd_data_xml . $closing_tag, $data);
}
......@@ -286,7 +214,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$input_errors[] = gettext("You have selected to restore an area but we could not locate the correct xml tag.");
} else {
if (!empty($config['rrddata'])) {
restore_rrddata();
/* XXX we should point to the data... */
rrd_import();
unset($config['rrddata']);
write_config();
convert_config();
......@@ -308,7 +237,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$config = parse_config();
/* extract out rrd items, unset from $config when done */
if($config['rrddata']) {
restore_rrddata();
/* XXX we should point to the data... */
rrd_import();
unset($config['rrddata']);
write_config();
convert_config();
......
......@@ -526,7 +526,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
/* sync filter configuration */
setup_gateways_monitor();
filter_configure();
enable_rrd_graphing();
rrd_configure();
if (is_subsystem_dirty('staticroutes') && (system_routing_configure() == 0)) {
clear_subsystem_dirty('staticroutes');
}
......
......@@ -310,7 +310,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($changes > 0) {
// reload filter, rrd when interfaces have changed (original from apply action)
filter_configure();
enable_rrd_graphing();
rrd_configure();
}
header(url_safe('Location: /interfaces_assign.php'));
exit;
......
......@@ -55,7 +55,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
write_config();
}
enable_rrd_graphing();
rrd_configure();
setup_gateways_monitor();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment