3

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?

virullius
  • 988
  • 8
  • 22

3 Answers3

1

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

/.*\s(.*)\s.*/
bjoster
  • 4,423
  • 5
  • 22
  • 32
fargusto
  • 11
  • 1
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.

brian-brazil
  • 3,904
  • 1
  • 20
  • 15
1

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

drrzmr
  • 26
  • 2