DingGGu
DingGGu

Reputation: 185

PHP-RRD Wasn't Work

I am trying to create rrd graph by php5-rrd libraires. I am trying to draw (LASTHOUR,LASTDAY,LASTWEEK,LASTMONTH) graphs. but not work well, (maybe my RRA settings are missing). i try to use "rrd_update" function. but doesn't work. so try to use "rrd_fetch" function. but all data appear on -NaN.

This is my console log.

1426322479:6249:1817
Array
(
    [start] => 1426318800
    [end] => 1426322700
    [step] => 300
    [data] => Array
        (
            [Users] => Array
                (
                    [1426319100] => NAN
                    [1426319400] => NAN
                    [1426319700] => NAN
                    [1426320000] => NAN
                    [1426320300] => NAN
                    [1426320600] => NAN
                    [1426320900] => NAN
                    [1426321200] => NAN
                    [1426321500] => NAN
                    [1426321800] => NAN
                    [1426322100] => NAN
                    [1426322400] => NAN
                    [1426322700] => NAN
                )

            [Games] => Array
                (
                    [1426319100] => NAN
                    [1426319400] => NAN
                    [1426319700] => NAN
                    [1426320000] => NAN
                    [1426320300] => NAN
                    [1426320600] => NAN
                    [1426320900] => NAN
                    [1426321200] => NAN
                    [1426321500] => NAN
                    [1426321800] => NAN
                    [1426322100] => NAN
                    [1426322400] => NAN
                    [1426322700] => NAN
                )

        )

)

This is my PHP Code

$rrdFile = dirname(__FILE__) . "/speed.rrd";

//create rrd file
rrd_create($rrdFile,
    array(
        "DS:Users:GAUGE:600:0:U",
        "DS:Games:GAUGE:600:0:U",
        "RRA:AVERAGE:0.5:1:1440",
        "RRA:AVERAGE:0.5:5:1440",
        "RRA:AVERAGE:0.5:30:800",
        "RRA:AVERAGE:0.5:120:800",
        "RRA:AVERAGE:0.5:1440:80"
    )
);

//update rrd file
if(!(rrd_update($rrdFile,
    array(
        "N:".rand(0,9999).":".rand(0,9999)
    )
))) {
    $err = rrd_error();
    echo "rrd_update() ERROR: $err\n";
}

// TEST CODE
$result = rrd_fetch( $rrdFile, array( "AVERAGE", "--resolution", "100", "--start", "-1h", "--end", "start+1h" ) );

print_r($result);


//graph output
if(!is_array(rrd_graph(dirname(__FILE__) . "/speed.png",
    array(
        "--start", "-1h",
        "--title", "Status",
        "--vertical-label", "Users & Games",
        "--width", "600",
        "--height", "200",
        "DEF:Users=$rrdFile:Users:AVERAGE",
        "DEF:Games=$rrdFile:Games:AVERAGE",
        "CDEF:cUsers=Users",
        "CDEF:cGames=Games",
        "LINE:cUsers#FF0000",
        "LINE:cGames#00FF00"
    )
))) {
    $err = rrd_error();
    echo "rrd_graph() ERROR: $err\n";
}

Upvotes: 0

Views: 1087

Answers (1)

Steve Shipway
Steve Shipway

Reputation: 4072

You do not have enough sample data, hence your graph and RRAs are empty.

Your graph is set to graph the last 1 hour; since you have the default interval (5min) this means only 12 samples from the highest-resolution RRA.

However, you don't have any data being put in! You only call rrd_update once, with a timestamp of 'now', and this is insufficient to fill even one RRA entry (you will need at least two samples, with a 5min time separation, to completely fill an RRA entry).

If you wish to generate some test data, start with a timestamp 3600 seconds in the past, and add 12 samples, increasing the timestamp by 300 each iteration. This will result in 11 entries in your primary (5-minute) RRA, and maybe 2 in your secondary 25-minute RRA.

Upvotes: 1

Related Questions