Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - RRD rrd_fetch() Function



The PHP RRD rrd_fetch() function is used to get data from an RRD file. With the help of this function, system performance data can be collected over time, which is helpful for figuring out and assessing trends.

Syntax

Below is the syntax of the PHP RRD rrd_fetch() function −

array rrd_fetch(string $filename, array $options)

Parameters

Below are the parameters of the rrd_fetch() function −

  • $filename − It is the path to the RRD file from which you have to fetch data.

  • $options − It is an array of options to specify what data to fetch. Common options are − AVERAGE, MIN, MAX, --start, --end.

Return Value

The rrd_fetch() function returns an associative array with the fetched data. If the operation fails, FALSE is returned.

PHP Version

The rrd_fetch() function is available from version 0.9.0 of the PECL rrd extension.

Example 1

First we will show you the basic example of the PHP RRD rrd_fetch() function to fetch average data from the last 24 hours.

<?php
   // Path to the RRD file
   $filename = "example.rrd";
   $options = ['AVERAGE', '--start', time() - 86400, '--end', time()];
   $data = rrd_fetch($filename, $options);
   print_r($data);
?>

Output

The above code will result something like this −

Array
(
    [filename] => example.rrd
    [start] => 1672485600
    [end] => 1672572000
    [step] => 300
    [ds] => Array
        (
            [load] => Array
                (
                    [0] => 0.25
                    [1] => 0.30
                    // ...
                )
        )
)

Example 2

In the below PHP code we will try to use the rrd_fetch() function and get the minimum values for the given period.

<?php
   // Path to the RRD file
   $filename = "example.rrd";
   $options = ['MIN', '--start', time() - 86400, '--end', time()];
   $data = rrd_fetch($filename, $options);
   print_r($data);
?> 

Output

After running the above program, it generates the following output −

Array
(
    [filename] => example.rrd
    [start] => 1672485600
    [end] => 1672572000
    [step] => 300
    [ds] => Array
        (
            [load] => Array
                (
                    [0] => 0.10
                    [1] => 0.15
                    // ...
                )
        )
)

Example 3

Now the below code retrieves the maximum values in the last hour with the help of rrd_fetch() function.

<?php
   // Path to the RRD file
   $filename = "myrrd.rrd";
   $options = ['MAX', '--start', time() - 3600, '--end', time()];
   $data = rrd_fetch($filename, $options);
   print_r($data);
?> 

Output

This will create the below output −

Array
(
    [filename] => myrrd.rrd
    [start] => 1672568400
    [end] => 1672572000
    [step] => 300
    [ds] => Array
        (
            [load] => Array
                (
                    [0] => 0.80
                    [1] => 0.85
                    // ...
                )
        )
)
php_function_reference.htm
Advertisements