RRD 函数
在线手册:中文 英文
PHP手册

rrd_info

(PECL rrd >= 0.9.0)

rrd_infoGets information about rrd file

说明

array rrd_info ( string $filename )

Returns information about particular RRD database file.

参数

file

RRD database file name.

返回值

Array with information about requsted RRD file, FALSE when error occurs.


RRD 函数
在线手册:中文 英文
PHP手册
PHP手册 - N: Gets information about rrd file

用户评论:

cfsalguero at gmail dot com (22-Feb-2012 11:45)

rrd_info will return an array where the keys are all strings that represents another arrays like in this example:
Array
(
    [filename] => /var/www/cacti/rra/localhost_mem_swap_26.rrd
    [rrd_version] => 0003
    [step] => 300
    [last_update] => 1329924661
    [header_size] => 1892
    [ds[mem_swap].index] => 0
    [ds[mem_swap].type] => GAUGE
    [ds[mem_swap].minimal_heartbeat] => 600
    [ds[mem_swap].min] => 0
    [ds[mem_swap].max] => NAN
    [ds[mem_swap].last_ds] => 2084332
    [ds[mem_swap].value] => 127144252
    [ds[mem_swap].unknown_sec] => 0
    [rra[0].cf] => AVERAGE
    [rra[0].rows] => 600
    [rra[0].cur_row] => 266
    [rra[0].pdp_per_row] => 1
    [rra[0].xff] => 0.5
    [rra[0].cdp_prep[0].value] => NAN
    [rra[0].cdp_prep[0].unknown_datapoints] => 0
)
I made a function that converts that into a real array structure:
<?php

function rrdinfo($filename) {

   
/*
       Inner functions to make them inaccesible from the outside of the main function
    */
   
function add($key, $value, &$main_table) {
        if (
is_array($value)) {
            foreach (
$value as $k => $v) {
               
add($k, $v, $main_table[$key]);
            }
        } else {
           
$main_table[$key] = $value;
        }
    }

    function
toobj($key, $value) {
       
$matches = array();
        if (
preg_match('/^\[(.*)\]$/', $key, $matches)) {
           
$key = $matches[1];
        }
        if (
preg_match('/(.*?)\[(.*?)\]\.(.*)/', $key, $matches)) {
           
$matches2 = array();
            if (
preg_match('/(.*?)\[(.*?)\]\.(.*)/', $matches[3], $matches2)) {
               
$ret_key = $matches[1];                                
                list(
$k, $v) = toobj($matches[3], $value);
               
$ret_val = array($matches[2] => array($k => $v));
            } else {
               
$ret_key = $matches[1];                                
               
$ret_val = array($matches[2] => array ($matches[3] => $value));
            }
        } else {
           
$ret_key = $key;
           
$ret_val = $value;
        }
        return array(
$ret_key, $ret_val);
    }

   
/*
       Main program code
    */
   
$main_table = array();
   
$info = rrd_info($filename);
    foreach (
$info as $ds_key => $ds_value) {
        list (
$key, $value) = toobj($ds_key, $ds_value);
        
add($key, $value, $main_table);
    }
    return
$main_table;
}

And
this is the way to use it:

$filename = "/path/to/your/rrd/file/localhost_mem_swap_26.rrd";
$info = rrdinfo($filename);