This commit is contained in:
Jonas Maier 2025-12-04 00:04:50 +01:00
parent a0d4969463
commit ab3a3e94ba
Signed by: jonas
SSH Key Fingerprint: SHA256:yRTjlpb3jSdw2EnZLAWyB4AghBPI8tu42eFXiICyb1U

76
modules/cpu.sh Normal file
View File

@ -0,0 +1,76 @@
#!/bin/sh
name='CPU Load'
recommend=1
available=1
cpu_count=
set +e # Disable exit on error for this section
# BSD / macOS
if [ -z "$cpu_count" ]; then
cpu_count=$(sysctl -n hw.ncpu 2>/dev/null)
fi
# Linux: /proc/cpuinfo
if [ -z "$cpu_count" ]; then
if [ -r /proc/cpuinfo ]; then
cpu_count=$(awk '/^processor[[:space:]]*:/ {n++} END{print n}' /proc/cpuinfo 2>/dev/null)
fi
fi
# Solaris
if [ -z "$cpu_count" ]; then
cpu_count=$(psrinfo -p 2>/dev/null)
fi
# AIX
if [ -z "$cpu_count" ]; then
cpu_count=$(lsdev -Cc processor 2>/dev/null | wc -l | awk '{print $1}')
fi
# HP-UX
if [ -z "$cpu_count" ]; then
cpu_count=$(ioscan -kC processor 2>/dev/null | grep processor | wc -l | awk '{print $1}')
fi
set -e # Re-enable exit on error
if [ -z "$cpu_count" ] || [ "$cpu_count" -lt 1 ] 2>/dev/null; then
# could not determine cpu count.
available=0
fi
check() {
# Extract last three numeric fields; remove commas.
set -- $(uptime | awk '
{
for (i = NF - 2; i <= NF; i++) {
gsub(",", "", $i);
printf "%s ", $i;
}
}
')
load1="$1"
load5="$2"
load15="$3"
warn1=$(awk -v l="$load1" -v c="$cpu_count" 'BEGIN { print (l >= c) ? 1 : 0 }')
warn5=$(awk -v l="$load5" -v c="$cpu_count" 'BEGIN { print (l >= c) ? 1 : 0 }')
if [ "$warn1" -eq 1 ] 2>/dev/null; then
warn "load_1m" "$load1" "High 1-minute load average: $load1 (>= $cpu_count CPUs)"
else
ok "load_1m" "$load1" "1-minute load average is normal: $load1 (< $cpu_count CPUs)"
fi
if [ "$warn5" -eq 1 ] 2>/dev/null; then
warn "load_1m" "$load5" "High 5-minute load average: $load5 (>= $cpu_count CPUs)"
else
ok "load_1m" "$load5" "5-minute load average is normal: $load5 (< $cpu_count CPUs)"
fi
}