Page 1 of 1

How does NCPA calculate the percentage of cpu?

Posted: Thu Aug 02, 2018 9:33 am
by ingenieria.itoc
How does NCPA calculate the percentage of cpu and its cores?
Output NCPA: OK: Percent was 10.00 %, 14.00 %, 12.50 %, 29.40 %


Because with different value is obtained when we consult the proc/stat whit this command:
awk '$1~/cpu[0-9]/{usage=($2+$4)*100/($2+$4+$5); print $1": "usage"%"}' /proc/stat

Output command:
[root@MXAP-ITOCZCCS01 plugins]# awk '$1~/cpu[0-9]/{usage=($2+$4)*100/($2+$4+$5); print $1": "usage"%"}' /proc/stat
cpu0: 16.4363%
cpu1: 17.9964%
cpu2: 17.5832%
cpu3: 16.7218%

Regards

Re: How does NCPA calculate the percentage of cpu?

Posted: Thu Aug 02, 2018 10:14 am
by lmiltchev
There is no way to change the output of the plugin the way you want it to be, unless you come up with some kind of wrapper script that would do it.

Re: How does NCPA calculate the percentage of cpu?

Posted: Thu Aug 02, 2018 10:44 am
by mcapra
You could raise a GitHub issue for this functionality. It's totally doable:
https://github.com/NagiosEnterprises/ncpa/issues

More info:
https://github.com/NagiosEnterprises/nc ... #L124-L130
https://psutil.readthedocs.io/en/latest ... pu_percent
https://psutil.readthedocs.io/en/latest ... .cpu_times
When percpu is True return a list of named tuples for each logical CPU on the system. First element of the list refers to first CPU, second element to second CPU and so on.
Based on my reading of the implementation, your assumption is correct that the first value is cpu0, second cpu1, third cpu2, etc etc.

It's worth mentioning that running a plugin's output through sed / awk needs to be done with care because this by itself can strip out the exit code.

Re: How does NCPA calculate the percentage of cpu?

Posted: Thu Aug 02, 2018 6:18 pm
by ingenieria.itoc
Tnks for answer me

But I still have doubt

I found other commands, but all of them give me different outputs:

grep -Ei "cpu\s+" </proc/stat ; sleep 1 ; grep -Ei "cpu\s+" </proc/stat
top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}'
top -b -n2 -d 1 | awk "/^top/{i++}i--2" | grep -Ei "cpu\(s\)\s*:"
cat <(grep 'cpu ' /proc/stat) <(sleep 1 && grep 'cpu ' /proc/stat) | awk -v RS="" '{print ($13-$2+$15-$4)*100/($13-$2+$15-$4+$16-$5)}'[/list]


What is the best options for monitoring the cpu cores in percent ?

How konw if NCPA is the best practice for cpu cores?


Regards

Re: How does NCPA calculate the percentage of cpu?

Posted: Fri Aug 03, 2018 10:09 am
by lmiltchev
It's hard to say what is "the best option" for monitoring the cpu cores in percent... The "supported" way is using NCPA, however you are free to use a shell script if you wish. Nagios gives you a flexibility to do the same thing in many different ways. It is a user's choice.

Re: How does NCPA calculate the percentage of cpu?

Posted: Fri Aug 03, 2018 10:54 am
by rexconsulting
Note for /proc/stat, "All of the numbers reported in this file are aggregates since the system first booted" (http://www.linuxhowtos.org/System/procstat.htm).

Whereas /usr/local/nagios/libexec/check_ncpa.py seems to be a real-time sampling of each CPU. But then that does beg the question: a CPU average must be an average of utilization over some span of time, right? So what is the span of time that check_ncpa, or rather the ncpa_listener returns when queried by the check_ncpa?

Well I guess I could go read the code couldn't I?

Re: How does NCPA calculate the percentage of cpu?

Posted: Fri Aug 03, 2018 11:54 am
by lmiltchev
So what is the span of time that check_ncpa, or rather the ncpa_listener returns when queried by the check_ncpa?
0.5 sec

Re: How does NCPA calculate the percentage of cpu?

Posted: Fri Aug 03, 2018 12:19 pm
by rexconsulting
Thanks lmiltchev

Re: How does NCPA calculate the percentage of cpu?

Posted: Fri Aug 03, 2018 12:31 pm
by lmiltchev
You are welcome!