Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get CPU usage percentage measured by Collectd in InfluxDB

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

like image 404
encodeflush Avatar asked Mar 03 '16 13:03

encodeflush


2 Answers

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:

  • precompute percentage on client
  • use other client for reporting stats (such as statsd, telegraf, etc.)

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.

like image 136
Tombart Avatar answered Sep 20 '22 07:09

Tombart


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.

like image 37
Maoz Zadok Avatar answered Sep 21 '22 07:09

Maoz Zadok