Skip to content

Commit 2a0f989

Browse files
committed
Replace ps with top in collect_top_procs
- Use top -b -n 2 for accurate per-CPU sampling - Extract second snapshot via awk, skip stale data - Update BATS mock to emit two-iteration output - Drop system mktemp from mock for BSD/GNU compat
1 parent 74760d6 commit 2a0f989

2 files changed

Lines changed: 39 additions & 7 deletions

File tree

opt/monitoring/high-load.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,14 @@ collect_top_procs() {
249249
TOP_PROCS_FILE=$(mktemp /run/bash-sys-monitor/high-load-top-XXXXXX.txt)
250250
trap 'rm -f "${TOP_PROCS_FILE}"' EXIT
251251

252-
LC_ALL=C ps aux --sort=-%cpu | head -n $((TOP_PROCS_COUNT + 1)) >"${TOP_PROCS_FILE}"
252+
local raw_file
253+
raw_file=$(mktemp /run/bash-sys-monitor/high-load-top-raw-XXXXXX.txt)
254+
255+
LC_ALL=C top -b -n 2 -d 1 -c -o +%CPU -w 160 >"${raw_file}" 2>/dev/null || true
256+
257+
awk -v c="${TOP_PROCS_COUNT}" '/PID USER/ { m++; if (m == 2) { print; p=0; while(p < c && getline > 0) { if ($0 !~ /top -b -n 2/ && NF > 0) { print; p++ } } exit } }' "${raw_file}" >"${TOP_PROCS_FILE}" || true
258+
259+
rm -f "${raw_file}"
253260
debug "Top processes written to ${TOP_PROCS_FILE}"
254261
}
255262

tests/high-load.bats

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,38 @@ setup() {
245245
}
246246

247247
@test "collect_top_procs creates temp file with process data" {
248-
mktemp() { command mktemp "${TEST_TMPDIR}/high-load-top-XXXXXX.txt"; }
249-
ps() {
250-
echo "USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND"
251-
echo "root 1 0.1 0.0 12345 6789 ? Ss 00:00 0:01 /sbin/init"
252-
echo "www-data 100 95.0 2.5 987654 65432 ? R 00:01 5:30 /usr/sbin/apache2"
248+
local mock_top_file="${TEST_TMPDIR}/mock_top_procs.txt"
249+
local mock_raw_file="${TEST_TMPDIR}/mock_top_raw.txt"
250+
251+
mktemp() {
252+
case "$1" in
253+
*raw*) printf '%s' "${mock_raw_file}" ;;
254+
*) printf '%s' "${mock_top_file}" ;;
255+
esac
256+
}
257+
258+
top() {
259+
printf '%s\n' \
260+
'top - 12:00:00 up 1 day, 1:00, 0 users, load average: 4.50, 4.00, 3.50' \
261+
'Tasks: 100 total, 1 running, 99 sleeping, 0 stopped, 0 zombie' \
262+
'%Cpu(s): 50.0 us, 10.0 sy, 0.0 ni, 40.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st' \
263+
'MiB Mem : 8192.0 total, 2048.0 free, 4096.0 used, 2048.0 buff/cache' \
264+
'MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 6000.0 avail Mem' \
265+
'' \
266+
'PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND' \
267+
' 1 root 20 0 25132 14760 9156 S 0.0 0.1 20:30.11 /sbin/init' \
268+
'100 www-data 20 0 987654 65432 12345 R 95.0 2.5 5:30.00 /usr/sbin/apache2' \
269+
'' \
270+
'top - 12:00:01 up 1 day, 1:01, 0 users, load average: 4.45, 4.00, 3.50' \
271+
'Tasks: 100 total, 1 running, 99 sleeping, 0 stopped, 0 zombie' \
272+
'%Cpu(s): 50.0 us, 10.0 sy, 0.0 ni, 40.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st' \
273+
'MiB Mem : 8192.0 total, 2048.0 free, 4096.0 used, 2048.0 buff/cache' \
274+
'MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 6000.0 avail Mem' \
275+
'' \
276+
'PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND' \
277+
' 1 root 20 0 25132 14760 9156 S 0.0 0.1 20:30.11 /sbin/init' \
278+
'100 www-data 20 0 987654 65432 12345 R 95.0 2.5 5:30.00 /usr/sbin/apache2'
253279
}
254-
head() { command head "$@"; }
255280

256281
collect_top_procs
257282
[ -f "${TOP_PROCS_FILE}" ]

0 commit comments

Comments
 (0)