PHP - RRD rrd_xport() Function
The PHP RRD rrd_xport() function is used to export the information about the RRD file. This data can be translated to XML using a user-space PHP script and then restored as an RRD database file.
Syntax
Below is the syntax of the PHP RRD rrd_xport() function −
array rrd_xport (array $options)
Parameters
This function accepts $options parameter which is an array of options used to specify the data source, time range, and other settings for the export.
Return Value
The rrd_xport() function returns an array with information about RRD database file or FALSE on failure.
PHP Version
This function is available from version 0.9.0 of the PECL rrd extension onwards.
Example 1
First we will show you the basic example of the PHP RRD rrd_xport() function to export data from a single RRD file.
<?php // Define the options array here $options = [ "--start", "-1d", // Last day "DEF:myspeed=speed.rrd:speed:AVERAGE", "XPORT:myspeed:My Speed" ]; // Execute the rrd_xport function $result = rrd_xport($options); print_r($result); ?>
Output
The above code will result something like this −
Array
(
[0] => XML data...
)
Example 2
In this PHP code we will use the rrd_xport() function and export data from multiple RRD files.
<?php // Define the options array here $options = [ "--start", "-1d", // Last day "DEF:speed1=speed1.rrd:speed:AVERAGE", "DEF:speed2=speed2.rrd:speed:AVERAGE", "XPORT:speed1:Speed 1", "XPORT:speed2:Speed 2" ]; // Execute the rrd_xport function $result = rrd_xport($options); print_r($result); ?>
Output
After running the above program, it generates the following output −
Array
(
[data] => Array
(
[0] => Array
(
[timestamp] => 1625082000
[Speed 1] => 13.45
[Speed 2] => 14.01
)
[1] => Array
(
[timestamp] => 1625085600
[Speed 1] => 12.78
[Speed 2] => 13.67
)
// ...
)
[meta] => Array
(
[start] => 1625078400
[end] => 1625164800
[step] => 3600
)
)
Example 3
This example demonstrates how to specify a custom time range for data export while using the rrd_xport() function.
<?php // Define the options array here $options = [ "--start", "1625078400", // Specific start time "--end", "1625164800", // Specific end time "DEF:myspeed=speed.rrd:speed:AVERAGE", "XPORT:myspeed:My Speed" ]; $result = rrd_xport($options); print_r($result); ?>
Output
This will create the below output −
Array
(
[data] => Array
(
[0] => Array
(
[timestamp] => 1625078400
[myspeed] => 15.23
)
[1] => Array
(
[timestamp] => 1625082000
[myspeed] => 14.87
)
// ...
)
[meta] => Array
(
[start] => 1625078400
[end] => 1625164800
[step] => 3600
)
)