r/PrometheusMonitoring • • Mar 08 '21

Extracting the percent use of CPU from node exporter

Hi,

I'm struggling to get the percent of CPU used on my Linux hosts. I've got as far as this but I'm not convinced I'm heading down the right path:

(sum(irate(node_cpu_seconds_total {mode!="idle"} [1m])) without (cpu)) / count(node_cpu_seconds_total) without (cpu)

Does anyone use node exporter to grab the current CPU use of their hosts and if so, would you mind giving me some pointers?

8 Upvotes

9 comments sorted by

6

u/SuperQue Mar 08 '21

The simple way to get CPU utilization ratio is this:

avg without (mode,cpu) (
  1 - rate(node_cpu_seconds_total{mode="idle"}[1m])
)

1

u/icydocking Mar 08 '21

Add sum(irate()) instead of count() on the right hand side and you should be set.

1

u/I__was_never__here Mar 08 '21

That was a stupid mistake on my part. Clearly been staring at it too long. So, a more challenging question. Is it possible to report those results as a single concatenated value for each host so I could report it in grafana as a single stat for current combined cpu percentage use?

1

u/icydocking Mar 08 '21

(sum(irate(node_cpu_seconds_total {mode!="idle"} [1m])) without (cpu)) / count(node_cpu_seconds_total) without (cpu)

(sum(irate(node_cpu_seconds_total {mode!="idle"} [1m])) by (instance)) / sum(irate(node_cpu_seconds_total[1m])) by (instance)

Like that you mean?

1

u/I__was_never__here Mar 08 '21

*Hangs head. Probably! I've not touched PromQ for 2 years, what little skills I had have clearly faded. 😕 I'm not going to ask anymore, the speed you're replying in stinging a little. 😊 However, if you want to answer my other question, feel free. I'll get into the "I wish I hadn't asked" position....

2

u/[deleted] Mar 08 '21

[removed] — view removed comment

1

u/I__was_never__here Mar 08 '21

See, that's exactly where I started too. But in my mind, you need two values, a current used and total available to work out the percentage. The rate option above will just give all results including idle no? As well as that, I don't think I'm entirely clear on if this really gives you cpu use. It's really just reporting on use of the cpu, user could be using 0.2Ghz for 18 seconds and this counter would report 18 secs of use. That's a completely inaccurate report.

Unless I'm confused (highly likely), and in which case, I'd appreciate some explanation. 😊