How does NCPA calculate the percentage of cpu?

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
ingenieria.itoc
Posts: 10
Joined: Mon Oct 10, 2016 1:46 pm

How does NCPA calculate the percentage of cpu?

Post 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
Last edited by ingenieria.itoc on Thu Aug 02, 2018 12:38 pm, edited 1 time in total.
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: How does NCPA calculate the percentage of cpu?

Post 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.
Be sure to check out our Knowledgebase for helpful articles and solutions!
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: How does NCPA calculate the percentage of cpu?

Post 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.
Former Nagios employee
https://www.mcapra.com/
ingenieria.itoc
Posts: 10
Joined: Mon Oct 10, 2016 1:46 pm

Re: How does NCPA calculate the percentage of cpu?

Post 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
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: How does NCPA calculate the percentage of cpu?

Post 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.
Be sure to check out our Knowledgebase for helpful articles and solutions!
User avatar
rexconsulting
Posts: 60
Joined: Fri May 04, 2012 4:27 pm
Location: Oakland, CA
Contact:

Re: How does NCPA calculate the percentage of cpu?

Post 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?
CP
--
Chris Paul
Rex Consulting, Inc
5652 Florence Terrace, Oakland, CA 94611
email: [email protected]
web: http://www.rexconsulting.net
phone, toll-free: +1 (888) 403-8996 ext 1
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: How does NCPA calculate the percentage of cpu?

Post 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
Be sure to check out our Knowledgebase for helpful articles and solutions!
User avatar
rexconsulting
Posts: 60
Joined: Fri May 04, 2012 4:27 pm
Location: Oakland, CA
Contact:

Re: How does NCPA calculate the percentage of cpu?

Post by rexconsulting »

Thanks lmiltchev
CP
--
Chris Paul
Rex Consulting, Inc
5652 Florence Terrace, Oakland, CA 94611
email: [email protected]
web: http://www.rexconsulting.net
phone, toll-free: +1 (888) 403-8996 ext 1
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: How does NCPA calculate the percentage of cpu?

Post by lmiltchev »

You are welcome!
Be sure to check out our Knowledgebase for helpful articles and solutions!
Locked