cpu.idle value for topbeat does not match command line
I was asked to post this here. The original thread is here: https://discuss.elastic.co/t/topbeat-top-command-line-discrepancies/52241. I've copied the relevant portions below.
I am using topbeat version 1.2.3 with ELK to monitor CentOS 6.8 server performance. I am trying to recreate the % idle value reported from the top command:
Cpu(s): 2.7%us, 1.0%sy, 2.8%ni, 90.5%id, 2.5%wa, 0.0%hi, 0.2%si, 0.3%st
In logstash I have this filter:
ruby {
code => "event['cpu']['total'] = event['cpu']['user'] + event['cpu']['nice'] + event['cpu']['system'] + event['cpu']['idle'] + event['cpu']['iowait'] + event['cpu']['irq'] + event['cpu']['softirq'] + event['cpu']['steal']"
code => "event['cpu']['idle_p'] = 100.0 * event['cpu']['idle'] / event['cpu']['total']"
}
I captured both top command line output and topbeat output below. The samples are from the same one minute time period. Topbeat and command line top were both sampling every 10 seconds. As can be seen the reported values are very different. To confirm the problem wasn't due to my logstash configs I also logged topbeat output directly to a file. It seems the cpu.idle field reported by topbeat is the cause of the difference I'm seeing.
top -b -d 10
Cpu(s): 2.7%us, 1.6%sy, 1.7%ni, 79.5%id, 13.5%wa, 0.0%hi, 0.0%si, 1.0%st
Cpu(s): 1.5%us, 1.6%sy, 2.6%ni, 47.4%id, 45.6%wa, 0.0%hi, 0.1%si, 1.3%st
Cpu(s): 5.8%us, 2.2%sy, 3.5%ni, 50.4%id, 36.4%wa, 0.0%hi, 0.0%si, 1.7%st
Cpu(s): 5.8%us, 1.7%sy, 3.1%ni, 46.9%id, 40.7%wa, 0.0%hi, 0.1%si, 1.7%st
Cpu(s): 3.2%us, 2.7%sy, 2.6%ni, 31.0%id, 58.5%wa, 0.0%hi, 0.1%si, 2.0%st
Cpu(s): 6.5%us, 2.4%sy, 2.8%ni, 18.4%id, 68.3%wa, 0.0%hi, 0.1%si, 1.6%st
Cpu(s): 2.0%us, 1.8%sy, 6.7%ni, 55.5%id, 32.2%wa, 0.0%hi, 0.0%si, 1.8%st
Topbeat:
June 10th 2016, 12:12:25.420 79.512
June 10th 2016, 12:12:15.420 79.512
June 10th 2016, 12:12:05.420 79.513
June 10th 2016, 12:11:55.420 79.513
June 10th 2016, 12:11:45.420 79.513
June 10th 2016, 12:11:35.433 79.514
It will be better to have same names of fields as reported in top command or provide description too to match fields for verification of input and output and if any modification in data format is done as reported by OS command output vs data reported in tabular form in Kibana. Given the bug in reporting in CPU usage, it is not clear if the data is collected for individual cpus or is sum of of all the cpus (cores) on the node. This makes validation of results difficult.
My understanding (see reference below) is that per core CPU metrics may be turned on or off via topbeat's YAML config cpu_per_core option. For the data I've reported cpu_per_core was off.
https://www.elastic.co/guide/en/beats/topbeat/current/configuration-input.html
The point is that event['cpu']['user'], event['cpu']['nice'], event['cpu']['system'], event['cpu']['idle'], event['cpu']['iowait'], event['cpu']['irq'], event['cpu']['softirq'], event['cpu']['steal'] are measured in CPU samples which, in turn, measured from system start and grows continuously.
Precisely speaking your calculated event['cpu']['idle_p'] is the idle percent for all the uptime, not for the last minute. The idle percent requested in https://github.com/elastic/beats/issues/1356 is how idle time between samples should be measured.
@vchav73 Your calculated value of event['cpu']['idle_p'] is the percentage of idle time since boot. Due to the much larger time window, the idle values do not change much as you observed.
The top command calculates percentages by looking at the change in CPU time values between samples. When you first run it, it has no previous sample to compare to, so these initial values are the percentages since boot. So the initial reported value should be close to your calculated value.
In https://github.com/elastic/beats/pull/1705 @monicasarbu added an idle % value that is calculated from the difference between to samples. These changes are in Metricbeat if you want to try them.
This isn't a bug in beats as far as I can tell so I'm closing, but feel free to re-open if there's more to discuss.