icingaweb2-module-graphite/library/Graphite/GraphiteUtil.php
2015-06-25 15:26:01 +02:00

87 lines
2.1 KiB
PHP

<?php
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)) {
return $m[1];
}
return array();
}
/**
* 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);
if (! empty($varnames)) {
$parts = preg_split(self::$variablePattern, $pattern);
foreach ($parts as $key => $val) {
$parts[$key] = preg_quote($val, '/');
}
$regex = '/' . implode('([^\.]+?)', $parts) . '/';
if (preg_match($regex, $metric, $m)) {
array_shift($m);
$vars = array_combine($varnames, $m);
}
}
return $vars;
}
}