GraphiteUtil: cleanup, comments

This commit is contained in:
Thomas Gelf 2015-06-25 15:26:01 +02:00
parent 8e2077e02d
commit f28f2aabbf

View file

@ -2,10 +2,34 @@
namespace Icinga\Module\Graphite;
/**
* Utility class offering Graphite-related helpers
*/
class GraphiteUtil
{
/**
* Regex pattern matching $varname
*
* @var string
*/
protected static $variablePattern = '/\$(\w+)/';
/**
* Extract a list of variable names from a given filter string
*
* Example
* -------
* <code>
* $pattern = 'base.$group.$hostname.*.value';
* echo implode(', ', GraphiteUtil::extractVariableNames($pattern));
*
* // Gives: group, hostname
* </code>
*
* @param string $pattern Filter pattern
*
* @return array
*/
public static function extractVariableNames($pattern)
{
if (preg_match_all(self::$variablePattern, $pattern, $m)) {
@ -15,7 +39,32 @@ class GraphiteUtil
return array();
}
public static function extractVars($string, $pattern)
/**
* Extracts a key/value list with all variables filled in a given metric
* for a given filter pattern
*
* Example
* -------
* <code>
* $pattern = 'base.$group.$hostname.*.value';
* $metric = 'base.servers.www1.whatever.value';
* print_r(GraphiteUtil::extractVars($metric, $pattern));
* </code>
*
* Output
* ------
* Array
* (
* [group] => servers
* [hostname] => www1
* )
*
* @param string $metric Metric string
* @param string $pattern Filter pattern
*
* @return array
*/
public static function extractVars($metric, $pattern)
{
$vars = array();
$varnames = self::extractVariableNames($pattern);
@ -27,7 +76,7 @@ class GraphiteUtil
}
$regex = '/' . implode('([^\.]+?)', $parts) . '/';
if (preg_match($regex, $string, $m)) {
if (preg_match($regex, $metric, $m)) {
array_shift($m);
$vars = array_combine($varnames, $m);
}