Raw load average is misleading because a load of 4 is healthy on an eight-core box yet catastrophic on a single core. This script solves that by normalising load: it reads the one-minute figure from /proc/loadavg, divides it by the core count from nproc, and compares the resulting per-core ratio against LOAD_RATIO_LIMIT (1.50 by default, meaning 50% over saturation). Because Bash cannot do floating-point arithmetic, all math and comparisons are delegated to awk, which prints the ratio and a boolean over-limit flag. When the threshold is breached the script composes a detailed report that includes the one, five, and fifteen-minute load values, the normalised ratio, and the top CPU-consuming processes from ps sorted by %cpu. That snapshot helps you diagnose the cause immediately rather than just learning that something is wrong. Run it from cron every few minutes for early warning of runaway processes or capacity problems.
CPU Load Average Alert
Warns by email when the one-minute load average per CPU core exceeds a configurable ratio.
2 views
Share
Script
Download .shbash
#!/usr/bin/env bash
#
# cpu_load_alert.sh
# Alerts when the system 1-minute load average exceeds a per-core threshold.
# Load is normalised by CPU count so the script behaves the same on any host.
#
set -euo pipefail
# ---- Configuration -----------------------------------------------------------
# Alert when (load1 / cores) is greater than this ratio. 1.00 == fully loaded.
LOAD_RATIO_LIMIT="${LOAD_RATIO_LIMIT:-1.50}"
ALERT_EMAIL="${ALERT_EMAIL:-root@localhost}"
HOSTNAME_FQDN="$(hostname -f 2>/dev/null || hostname)"
# ---- Gather metrics ----------------------------------------------------------
# /proc/loadavg fields: load1 load5 load15 running/total lastpid
read -r load1 load5 load15 _ < /proc/loadavg
cores="$(nproc)"
# Compute the normalised ratio with awk (bash has no floating-point math).
ratio="$(awk -v l="${load1}" -v c="${cores}" 'BEGIN { printf "%.2f", l / c }')"
# Compare the floats; awk returns 1 (true) when over the limit.
over_limit="$(awk -v r="${ratio}" -v lim="${LOAD_RATIO_LIMIT}" \
'BEGIN { print (r > lim) ? 1 : 0 }')"
# ---- Report ------------------------------------------------------------------
if [[ "${over_limit}" -eq 1 ]]; then
subject="[CPU ALERT] ${HOSTNAME_FQDN}: load ratio ${ratio} (>${LOAD_RATIO_LIMIT})"
{
echo "High CPU load detected on ${HOSTNAME_FQDN}."
echo
echo "Cores : ${cores}"
echo "Load (1/5/15 min): ${load1} / ${load5} / ${load15}"
echo "Per-core ratio : ${ratio} (limit ${LOAD_RATIO_LIMIT})"
echo
echo "Top CPU consumers:"
ps -eo pid,comm,%cpu --sort=-%cpu | head -n 6
echo
echo "Checked at: $(date '+%Y-%m-%d %H:%M:%S %Z')"
} | mail -s "${subject}" "${ALERT_EMAIL}"
echo "Alert sent: ratio ${ratio} over ${LOAD_RATIO_LIMIT}."
else
echo "OK: load ratio ${ratio} within limit ${LOAD_RATIO_LIMIT}."
fiHow to run
Review the script first, then download or copy it and run it in your environment.
You might also like
bashlinuxBeginner
Disk Usage Threshold Alert
Emails an administrator whenever any mounted filesystem climbs above a configurable usage percentage.
bashlinuxIntermediate
Backup All PostgreSQL Databases with pg_dumpall
Dumps the entire PostgreSQL cluster, including roles, to one compressed file.
bashlinuxBeginner
List GCP compute instances
Show every Compute Engine VM in a project with zone, machine type, status, and IPs.