1

I need to use the number of CPU cores in multiple calculations on a Grafana dashboard and was hoping to perform the query once and store in a variable. I can't find a way to return the data in a format that Grafana will accept.

Grafana Error:

Template variables could not be initialized: parse error at char 1: vector selector must contain label matchers or metric name

I have tried (PromQL):

count(node_cpu{instance="$host",mode="system"})

Returns:

Element {}, Value 2

Also (PromQL):

scalar(count(node_cpu{instance="$host",mode="system"}))

Returns:

Element scalar, Value 2

Using Grafana's query_result function like so:

query_result(count(node_cpu{instance="$host",mode="system"}))

I get the following values in the preview:

{} 2 1521669355000

The 2 in the middle is the one I want.

Any ideas how to make this work?

1

You can use Regex field to grab your value, in your case: /.* (.*) .*/

| improve this answer | |
1

You should avoid scalars for things like this as it doesn't work if for example you want to graph something with two hosts that might have different numbers of cores. I'd suggest doing this inside each query expression, rather than via a template variable.

| improve this answer | |
1

This works if you put this in the regex box in grafana

/.*\s(.*)\s.*/
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.