note 107629 added to function.rrd-info

From: Date: Wed, 22 Feb 2012 15:45:25 +0000
Subject: note 107629 added to function.rrd-info
Groups: php.notes 
Request: Send a blank email to php-notes+get-186281@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: $filename = "/path/to/your/rrd/file/localhost_mem_swap_26.rrd"; $info = rrdinfo($filename); ---- Server IP: 69.147.83.197 Probable Submitter: 190.55.70.73 ---- Manual Page -- http://www.php.net/manual/en/function.rrd-info.php Edit -- https://master.php.net/note/edit/107629 Del: integrated -- https://master.php.net/note/delete/107629/integrated Del: useless -- https://master.php.net/note/delete/107629/useless Del: bad code -- https://master.php.net/note/delete/107629/bad+code Del: spam -- https://master.php.net/note/delete/107629/spam Del: non-english -- https://master.php.net/note/delete/107629/non-english Del: in docs -- https://master.php.net/note/delete/107629/in+docs Del: other reasons-- https://master.php.net/note/delete/107629 Reject -- https://master.php.net/note/reject/107629 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#186281) next »