Skip to content

Commit ccd95cf

Browse files
authored
Merge pull request #217 from Icinga/feature/combine-two-metrics-187
Make all macros used in a template's metrics filters available in its functions, too
2 parents 9053fe0 + ba1ed24 commit ccd95cf

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

doc/04-Templates.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ The metric in question can be referenced in the function call using the macro
125125

126126
alias(color(scale($metric$, 1000), '#1a7dd7'), 'Round trip time (ms)')
127127

128+
In addition you may utilize all other macros here as well:
129+
130+
alias(color(scale(divideSeries($metric$, $service_name_template$.perfdata.$disk$.max), 100), '#1a7dd7'), 'Used (%)')
131+
128132
## Template Example <a id="templates-example"></a>
129133

130134
The configuration examples used in this document are borrowed from the template

library/Graphite/Graphing/Templates.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -199,17 +199,6 @@ function($option) {
199199
);
200200
}
201201

202-
if ($curves[$curve][1]->getMacros() !== ['metric']) {
203-
throw new ConfigurationError(
204-
'Bad function "%s" for curve "%s" of template "%s" in file "%s":'
205-
. ' function definitions of templates must include the macro $metric$ and no other ones',
206-
$template['functions'][$curve],
207-
$curve,
208-
$templateName,
209-
$path
210-
);
211-
}
212-
213202
unset($template['functions'][$curve]);
214203
} else {
215204
$curves[$curve][1] = new MacroTemplate('$metric$');

library/Graphite/Web/Widget/GraphImage.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,26 @@ public function render()
8484
$params->set($key, $value->resolve($variables));
8585
}
8686

87-
foreach ($this->chart->getMetrics() as $curveName => $metric) {
88-
$params->add('target', $template->getCurves()[$curveName][1]->resolve([
89-
'metric' => $graphiteWebClient->escapeMetricPath($metric)
90-
]));
87+
$metrics = $this->chart->getMetrics();
88+
89+
foreach ($metrics as &$metric) {
90+
$metric = $graphiteWebClient->escapeMetricPath($metric);
91+
}
92+
unset($metric);
93+
94+
$allVars = [];
95+
96+
foreach ($template->getCurves() as $curveName => $curve) {
97+
$vars = $curve[0]->reverseResolve($metrics[$curveName]);
98+
99+
if ($vars !== false) {
100+
$allVars = array_merge($allVars, $vars);
101+
}
102+
}
103+
104+
foreach ($metrics as $curveName => $metric) {
105+
$allVars['metric'] = $metric;
106+
$params->add('target', $template->getCurves()[$curveName][1]->resolve($allVars));
91107
}
92108

93109
$url = Url::fromPath('/render')->setParams($params);

0 commit comments

Comments
 (0)