0

I am very new to promql. I want to know, what is the maximum cpu usage in last 7 days using promql for each instace. And figure out under utilized instance, by checking if its maximum cpu usage never crossed 20% in last 7 days. I am not able to decide which of the below will give me desired result ?

max(rate(node_cpu_seconds_total{service="x",mode!="idle"}[7d]) *100 ) by (instance)

and

max( max_over_time(rate(node_cpu_seconds_total{service="x",mode!="idle"}[5m])[7d:])*100) by (instance)

Or there is any other better way to get the same result.

Laxmikant
  • 1,551
  • 1
  • 13
  • 30

1 Answers1

1

Difference between max and max_over_time

max returns maximum among time series returned by inner selector. Result is single time series.

max_over_time returns maximum value of every time series over specified range. Result contains number of values equal to number of inner time series.

Consider this demo.

Same logic applies to all <aggregation> vs. <aggregation>_over_time functions.

Maximum CPU usage

I want to know, what is the maximum CPU usage in last 7 days using promql for each instance.

First of all I'm assuming here you are using node_exporter's metrics, and the following message will be based on this assumption, though I don't know what service="x" means in your selector. Hopefully it is just static label applied to target.

Regarding calculation of CPU utilization everything is not as simple as you tried.

I believe, simplest query to get CPU utilization of an instance is this:

100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[2m])) * 100)

Taken from this answer.

So, to get maximum value over last week you could use this:

max_over_time((100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[2m])) * 100))[7d:])
markalex
  • 8,623
  • 2
  • 7
  • 32
  • Thanks for the response. I ran the query, interestingly i get no data on Prometheus console however graph shows the data . – Laxmikant Apr 14 '23 at 14:10
  • @Laxmikant, what do you mean? This should work both in table and graph mode. – markalex Apr 14 '23 at 14:13
  • And as you mentioned it gives me range of values for each instance for 7 days... However I want just 1 maximum value per instance.. For example if avg cpu usage of instance i1 gone max up to 65% in last 7 days .. the output should be {instance="i1"} and value=65 – Laxmikant Apr 14 '23 at 14:38
  • Last query from my answer should produce only one series per instance. And if you in table mode - one value: maximum over last 7 days. If you want to see only those, which over threshold, just add `>65` at the end of the query. – markalex Apr 14 '23 at 14:45