9

I'm collecting cpu usage measured in jiffies by Collectd 5.4.0 and then storing the results in InfluxDB 0.9.4. I use the following query to get cpu percentage from InfluxDB:

SELECT MEAN(value) FROM cpu_value WHERE time >= '' and time <= '' GROUP BY type,type_instance

But when I plot the result it makes no sense. There is no pattern in cpu usage. Please let me know If I do something wrong.

Thanks

hossein
  • 336
  • 3
  • 11
  • 23
  • Your question is far too vague to answer, as we have no idea what pattern you expect to see and what you see instead. Please share actual output and tell us what you expected to see instead. – beckettsean Mar 06 '16 at 23:19
  • Also, why do you have `time >= '' and time <= ''`? What are you trying to accomplish with that? It is nonsense grammar for InfluxQL. – beckettsean Mar 06 '16 at 23:20

2 Answers2

15

Since Collectd 5.5 you can get values in percentage instead of jiffies:

<Plugin cpu>
  ReportByState = true
  ReportByCpu = true
  ValuesPercentage = true
</Plugin>

Then you can write query like:

SELECT mean("value") FROM "cpu_value" WHERE 
  "type_instance" =~ /user|system|nice|irq/ 
  AND "type" = 'percent' AND $timeFilter 
  GROUP BY time($interval), "host"

If you can upgrade it might be the easiest option. Otherwise you can:

With InfluxDB 0.12 you can perform arithmetic operations between fields like:

SELECT MEAN(usage_system) + MEAN(usage_user) AS cpu_total
FROM cpu

However for using this you would have to report from collectd user|system|nice|irq as FIELDS not TAGS.

Community
  • 1
  • 1
Tombart
  • 30,520
  • 16
  • 123
  • 136
  • Your query, SELECT mean("value") FROM "cpu_value" WHERE "type_instance" =~ /user|system|nice|irq/ AND "type" = 'percent' AND $timeFilter GROUP BY time($interval), "host", it doesnt look correct. It will return average % of user|system|nice|irq, when cpu usage should be the sum of them – lipeiran Mar 01 '17 at 19:20
  • That depends on `time($interval)` and frequency of sending metrics into InfluxDB. On 4 cores system where the maximum is 400% CPU usage with 10s metrics frequency and `$interval = 1m` you could get 2400% CPU with `SUM(value)` which doesn't look correct. If your `time($interval)` is the same as frequency of metrics collection you could get results that would look correct. – Tombart Mar 02 '17 at 09:56
  • I tested the following query taken from grafana dashboard id=4775 and works pretty accurately: "SELECT mean("value") *-1+100 FROM "cpu_value" WHERE ("host" =~ /^$host$/ AND "type" = 'percent' AND "type_instance" = 'idle') AND $timeFilter GROUP BY time($__interval) fill(null)". The approach is to substract the idle time. – ibai Feb 20 '20 at 19:08
2

this is my query, I use it with percent unit (on Axes tab), but stack+percent (on display tab) make sense as well

SELECT non_negative_derivative(mean("value"), 1s) FROM "cpu_value" WHERE "type_instance" =~ /(idle|system|user|wait)/ AND $timeFilter GROUP BY time($interval), "type_instance" fill(null)

The non_nagative_derivative(1s) can be replaced with derivative(1s), I had some of negative value when values was missing.

Maoz Zadok
  • 4,871
  • 3
  • 33
  • 43