1

I use macro to define core calculations. And I'd like to have as less params to define for the macro as possible. So, I have a macro that base it's calculations on the variable $period. Then I try to use a variable that related to period in the graphite query, but I receive an error.

Simplified code:

# defined outside a macro
$period_min = 5

#macro, skipping irrelevant variables
$control_period_min = $period_min * 3

$periods = graphiteBand( $expr, "${control_period_min}m", "1h", "tag", 1 )

Produces:

graphiteBand: time: unknown unit * in duration 5 * 3m

I wonder, is it possible to implement the same logic different way?

Microfed
  • 2,832
  • 22
  • 25

1 Answers1

1

Variables are just string replacement. Since you have put it in quotes, you are making a string that would be literally "5 * 3".

What you can do is use the tod() function:

# defined outside a macro
$period_min = 5

#macro, skipping irrelevant variables
$control_period_min = $period_min * 3

avg(q("avg:rate:os.cpu{host=*bosun*}", tod($control_period_min), ""))
Kyle Brandt
  • 26,938
  • 37
  • 124
  • 165
  • I was looking for something like 'tod' but couldn't find anything. Thank you a lot for helping! – Microfed Nov 21 '16 at 12:45
  • By the way, for the future readers: be aware that tod accepts seconds, not minutes! So, proper value for my example will be `$control_period_sec = $period_min * 3 * 60` – Microfed Nov 21 '16 at 12:50
  • 1
    @Microfed also see the `d()` func where you can return the number of s a second from the string. They can work together: i.e `tod(d("5m") * 5)` – Kyle Brandt Nov 21 '16 at 12:56