note 107629 modified in function.rrd-info by danbrown
| From: | danbrown@php.net | Date: | Thu, 10 May 2012 20:27:47 +0000 |
| Subject: | note 107629 modified in function.rrd-info by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-187809@lists.php.net to get a copy of this message | ||
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: <?php $filename =
"/path/to/your/rrd/file/localhost_mem_swap_26.rrd"; $info =
rrdinfo($filename); ?>
--was--
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);
http://php.net/manual/en/function.rrd-info.php