note 107629 modified in function.rrd-info by danbrown

From: 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:&#13;&#10;Array&#13;&#10;(&#13;&#10; [filename] =&#62; /var/www/cacti/rra/localhost_mem_swap_26.rrd&#13;&#10; [rrd_version] =&#62; 0003&#13;&#10; [step] =&#62; 300&#13;&#10; [last_update] =&#62; 1329924661&#13;&#10; [header_size] =&#62; 1892&#13;&#10; [ds[mem_swap].index] =&#62; 0&#13;&#10; [ds[mem_swap].type] =&#62; GAUGE&#13;&#10; [ds[mem_swap].minimal_heartbeat] =&#62; 600&#13;&#10; [ds[mem_swap].min] =&#62; 0&#13;&#10; [ds[mem_swap].max] =&#62; NAN&#13;&#10; [ds[mem_swap].last_ds] =&#62; 2084332&#13;&#10; [ds[mem_swap].value] =&#62; 127144252&#13;&#10; [ds[mem_swap].unknown_sec] =&#62; 0&#13;&#10; [rra[0].cf] =&#62; AVERAGE&#13;&#10; [rra[0].rows] =&#62; 600&#13;&#10; [rra[0].cur_row] =&#62; 266&#13;&#10; [rra[0].pdp_per_row] =&#62; 1&#13;&#10; [rra[0].xff] =&#62; 0.5&#13;&#10; [rra[0].cdp_prep[0].value] =&#62; NAN&#13;&#10; [rra[0].cdp_prep[0].unknown_datapoints] =&#62; 0&#13;&#10;)&#13;&#10;I made a function that converts that into a real array structure:&#13;&#10;&#60;?php&#13;&#10;&#13;&#10;function rrdinfo($filename) {&#13;&#10;&#13;&#10; /* &#13;&#10; Inner functions to make them inaccesible from the outside of the main function&#13;&#10; */&#13;&#10; function add($key, $value, &#38;$main_table) {&#13;&#10; if (is_array($value)) {&#13;&#10; foreach ($value as $k =&#62; $v) {&#13;&#10; add($k, $v, $main_table[$key]);&#13;&#10; }&#13;&#10; } else {&#13;&#10; $main_table[$key] = $value;&#13;&#10; }&#13;&#10; }&#13;&#10;&#13;&#10; function toobj($key, $value) {&#13;&#10; $matches = array();&#13;&#10; if (preg_match(&#39;/^\\[(.*)\\]$/&#39;, $key, $matches)) {&#13;&#10; $key = $matches[1];&#13;&#10; }&#13;&#10; if (preg_match(&#39;/(.*?)\\[(.*?)\\]\\.(.*)/&#39;, $key, $matches)) {&#13;&#10; $matches2 = array();&#13;&#10; if (preg_match(&#39;/(.*?)\\[(.*?)\\]\\.(.*)/&#39;, $matches[3], $matches2)) {&#13;&#10; $ret_key = $matches[1]; &#13;&#10; list($k, $v) = toobj($matches[3], $value); &#13;&#10; $ret_val = array($matches[2] =&#62; array($k =&#62; $v));&#13;&#10; } else {&#13;&#10; $ret_key = $matches[1]; &#13;&#10; $ret_val = array($matches[2] =&#62; array ($matches[3] =&#62; $value));&#13;&#10; }&#13;&#10; } else {&#13;&#10; $ret_key = $key;&#13;&#10; $ret_val = $value;&#13;&#10; }&#13;&#10; return array($ret_key, $ret_val);&#13;&#10; }&#13;&#10;&#13;&#10; /*&#13;&#10; Main program code&#13;&#10; */&#13;&#10; $main_table = array();&#13;&#10; $info = rrd_info($filename);&#13;&#10; foreach ($info as $ds_key =&#62; $ds_value) {&#13;&#10; list ($key, $value) = toobj($ds_key, $ds_value);&#13;&#10; add($key, $value, $main_table);&#13;&#10; }&#13;&#10; return $main_table;&#13;&#10;}&#13;&#10;?&#62;&#13;&#10;&#13;&#10;And this is the way to use it:&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;$filename = &#34;/path/to/your/rrd/file/localhost_mem_swap_26.rrd&#34;;&#13;&#10;$info = rrdinfo($filename);&#13;&#10;?&#62; --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

« previous php.notes (#187809) next »