GoAdmio

cpu

Show the server's CPU load and its top CPU-consuming processes, and send an alert when one process uses most of the CPU

On this page: Overview · Quick start · Syntax · Output · Alerts · Examples · Troubleshooting · Details · See also


Overview

/go/cpu prints a snapshot of how busy the server is. It shows:

If the busiest process is using most of the server's CPU, the command also emails and WhatsApps the server administrator. See Alerts.


Quick start

/go/cpu

Syntax

/go/cpu

The command has no arguments or options.


Output

----------------------------------------
CPU: 12% (4 Cores / 8 Threads)
----------------------------------------
USER   PID  %CPU %MEM    VSZ   RSS TTY  STAT START   TIME COMMAND
...
----------------------------------------
CPU: 12% (4 Cores / 8 Threads)
----------------------------------------
Part Meaning
CPU: <n>% The 1-minute load average divided by the number of threads. 100% means the load equals the number of threads. It is not the same as current CPU utilization, and it can go above 100% when work is queued.
<n> Cores / <n> Threads Physical cores and logical processors.
Process table The header plus the nine busiest processes, sorted by %CPU.

The separator lines are as wide as the terminal, or 40 characters when there is no terminal, for example when run by cron. The CPU line is printed twice: before and after the process table.

What %CPU means

%CPU comes from ps and shows each process's average CPU use since it started, not what it is using right now. A process that was busy hours ago can rank high, and one that has just started spinning can rank low. A single process can exceed 100% when it uses several threads.

Logging

Every run is appended to /var/log/go/cpu.log.


Alerts

After printing the report, the command looks only at the busiest process. It then works out how much of the whole server that process is using, in whole cores. If that figure is above 75%, the command sends two messages.

Channel Content
Email Subject Current <user> CPU Usage (<percent> %), body WARNING: <user> is using more than <percent>% of total CPU resources and Command: <command>.
WhatsApp The same text, with the title in bold.

Both messages go out through /go/sendmail and /go/sendwhatsapp, using the administrator email, mobile number, and WhatsApp instance and key stored in the panel's configuration.

In practice the alert fires only when the process is using at least about 76% of all threads, counted in whole cores:

Threads Whole cores the process must use Roughly %CPU in ps
1 1 100% or more
2 2 200% or more
4 4 400% or more
8 7 700% or more

There is no cooldown. If you run the command every minute and a process stays busy, you get an alert every minute.


Examples

# Show the CPU report
/go/cpu
# Show only the CPU line
/go/cpu | grep "^CPU" | head -n 1
# Check recent runs
tail /var/log/go/cpu.log

Troubleshooting

Symptom Likely cause Fix
Only nine processes are listed The header line counts as one of the ten lines. Expected. See Notes for maintainers.
No alert on a busy server The alert needs about 76% of all threads, counted in whole cores, and %CPU is a lifetime average. Use top for live usage.
CPU shows more than 100% It is the load average per thread, not utilization. Expected. Work is queued.
Alert arrives every run There is no cooldown. Run the command less often.
No email or WhatsApp message The administrator email, mobile number, or WhatsApp credentials are not set in the panel. Set them in the panel configuration and test /go/sendmail and /go/sendwhatsapp.
The alert shows php-fpm: and no pool name Only the first word of the command is reported. Find the pool with top.
The command pauses for a few seconds It looks up the server's public IP with curl, which has no time limit, and the result is not used. Check the server's internet access.
Core count looks too low on a multi-socket server Only unique core numbers are counted, and they can repeat across sockets. See Notes for maintainers.

Details

What it does

  1. Logs the call to /var/log/go/cpu.log.
  2. Reads the hostname, the server's public IP, and the administrator email, mobile number, and WhatsApp settings from the panel's configuration. These values are not used afterwards.
  3. Counts cores (unique core IDs from lscpu) and threads (processors in /proc/cpuinfo).
  4. Reads the 1-minute load average from /proc/loadavg and divides it by the number of threads.
  5. Prints the CPU line, the top processes from ps aux --sort=-%cpu (first 10 lines), and the CPU line again.
  6. Takes the busiest process, divides its %CPU by 100 and drops the fraction to get whole cores, and converts that to a percentage of all threads.
  7. If that percentage is above 75, sends the email and WhatsApp alerts.

Notes for maintainers

Issue Why it matters Suggested fix
head -n 10 includes the header Only nine processes are shown. Use head -n 11.
int($3/100) drops the fraction before the percentage is worked out A process using 190% counts as one core. The alert needs about 76% of all threads and a whole number of cores. Use int($3 / Threads) on the original %CPU.
The message says more than <percent>% with the computed value It doesn't say that the limit is 75%. State the limit, or show the actual percentage.
ps %CPU is a lifetime average Busy-now and busy-before processes are mixed up. Sample over a short interval, for example top -b -n 2 -d 1.
Only the busiest process is checked, though the comment says "one user or service" Many processes from one user can add up to a lot while none stands out. Add up %CPU per user before checking.
No cooldown Alert flood when scheduled often. Store the time of the last alert, and skip alerts for a while.
Hostname, ServerIP, AdminEmail, AdminMobile, WAInstanceID, and WAAPIKey are never used The public-IP lookup can hang, and the WhatsApp key is loaded for no reason. Remove them, or add --max-time to curl.
Cores are counted from unique core IDs Wrong on multi-socket servers, where IDs repeat per socket. Count unique socket and core pairs, for example lscpu -p=socket,core.
The label CPU is the load average Misleading, since load is not utilization. Rename it to Load, or compute real utilization.
Only $11 (first word) of the command is reported Loses the pool name or arguments. Report the full command and the PID.
Alert text contains literal \n Line breaks appear only if the helpers interpret them. Use real newlines, for example $'...'.
The CPU line is printed twice Redundant. Print it once.
Failures of sendmail and sendwhatsapp are ignored A broken alert path goes unnoticed. Log failures.

See also

Command Description
top Show the top CPU-consuming processes and their parent command
mem Check the top ten memory-consuming commands or services
loadavg Check the weekly server load average
slice Apply server-wide CPU and memory limits to user-.slice
httptop Display active HTTP requests in real time