How to get CPU usage percentage measured by Collectd in InfluxDB

encodeflush picture encodeflush · Mar 3, 2016 · Viewed 14.7k times · Source

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

Answer

Tombart picture Tombart · Apr 16, 2016

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.