Linux – How does SNMP get CPU usage on Linux

linuxsnmp

Does a SNMP call to get CPU usage for a Linux box eventually just read the /proc/stat file?

Best Answer

Sigh1. Fortunately you don't need /proc/stat

man 5 proc:

/proc/loadavg

The first three fields in this file are load average figures giving the number of jobs in the run queue (state R) or waiting for disk I/O (state D) averaged over 1, 5, and 15 minutes. They are the same as the load average numbers given by uptime(1) and other programs. The fourth field consists of two numbers separated by a slash (/). The first of these is the number of currently runnable kernel scheduling entities (processes, threads). The value after the slash is the number of kernel scheduling entities that currently exist on the system. The fifth field is the PID of the process that was most recently created on the system.

But you probably need to consult the source for http://www.net-snmp.org/ to determine what they actually use:

net-snmp-5.7.3/agent/mibgroup/ucd-snmp/loadave.c :

#elif defined(linux)
{
    FILE           *in = fopen("/proc/loadavg", "r");
    if (!in) {
        NETSNMP_LOGONCE((LOG_ERR, "snmpd: cannot open /proc/loadavg\n"));
        return (-1);
    }

Footnote 1. Sometimes you really can't chose who you work with.

In response to your comments, again, sigh. Since only the kernel is aware of what it is actually doing, any monitoring will, one way or the other, need to interact with the kernel to retrieve such information. The common interface to interact with the kernel is /proc/ although other methods can be contrived as well (auditd and kerneltap come to mind). But those are hardly "more lightweight" at all....

There will always be a certain amount of observer effect and impact caused by monitoring.

The only zero impact method is not to do any monitoring at all. And then whoever has pager duty can claim that since no alerts were observed the system isn't down either.
I would call that a win!