From a1fc70ead8f15fa79da7f4b67b08cfadb17040d0 Mon Sep 17 00:00:00 2001 From: Pim van den Berg Date: Sun, 19 Sep 2010 13:17:31 +0200 Subject: inc/collectd: simplify the way collectd_plugindata gathers its data A couple of preg_replaces are replaced by one simple but effective preg_match. --- inc/collectd.inc.php | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) (limited to 'inc') diff --git a/inc/collectd.inc.php b/inc/collectd.inc.php index f5769c0..7f88456 100644 --- a/inc/collectd.inc.php +++ b/inc/collectd.inc.php @@ -31,31 +31,18 @@ function collectd_plugindata($host) { if (!$files) return false; - $data; - $i = 0; + $data = array(); foreach($files as $item) { - unset($part); - - # split item by plugin/type - $part = explode('/', $item); - $part[1] = preg_replace('/\.rrd/', '', $part[1]); - - # plugin - $data[$i]['p'] = preg_replace('/-.+/', '', $part[0]); - - # plugin instance - if(preg_match('/-/', $part[0])) - $data[$i]['pi'] = preg_replace('/^[a-z_]+\-/', '', $part[0]); - - # type - $data[$i]['t'] = preg_replace('/-.+/', '', $part[1]); - - # type instance - if(preg_match('/-/', $part[1])) - $data[$i]['ti'] = preg_replace('/^[a-z_]+\-/', '', $part[1]); - - $i++; + preg_match('#([\w_]+)(?:\-(.+))?/([\w_]+)(?:\-(.+))?\.rrd#', $item, $matches); + + $data[] = array( + 'p' => $matches[1], + 'pi' => isset($matches[2]) ? $matches[2] : '', + 't' => $matches[3], + 'ti' => isset($matches[4]) ? $matches[4] : '', + ); } + return($data); } -- cgit v1.1