Harden the four monitoring scripts
set -euo pipefail across all four, but added deliberately rather than pasted in -- each script needed the places where a non-zero exit is normal handled first, or strict mode would have made them worse: - sys_monitor / process_monitor: `ps | head -n 6` is a latent SIGPIPE. head closes the pipe after six lines, and on a host with enough processes ps fills the buffer and exits 141, which pipefail turns into a script abort -- on exactly the busy machine you wanted to inspect. Confirmed the mechanism (a large producer into head returns 141) and those pipelines now tolerate it. - security_audit: find exits non-zero when it cannot read a directory, which is routine when walking the whole filesystem. Without handling, set -e aborted the audit part way while still looking complete. Also notes that a clean report as non-root means little, since find cannot descend where it may not read. - network_info: iptables needs root, so the last section aborted the script for ordinary users. Now reports the failure, and falls back to nft where iptables is absent. process_monitor also no longer kills on sight. `pkill -x` by name can match several processes at once, and as root that is an easy way to take down more than intended. It now prints what it matched and asks, with FORCE=1 for unattended use and a refusal rather than a hang when there is no tty. All four run clean; the kill path was tested against a live process and left it alive.
This commit is contained in:
+11
-3
@@ -17,7 +17,15 @@
|
||||
#
|
||||
# Usage: sys_monitor.sh (no arguments)
|
||||
# Description: Prints system uptime, memory, disk usage, and top CPU/mem processes.
|
||||
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# `ps | head` is a SIGPIPE waiting to happen: head closes the pipe after
|
||||
# six lines, and on a host with enough processes ps fills the buffer and
|
||||
# dies with 141, which pipefail turns into a script exit. That is exactly
|
||||
# the busy machine you most want this to work on, so those two pipelines
|
||||
# tolerate it explicitly.
|
||||
|
||||
echo "==== System Uptime and Load ===="
|
||||
uptime
|
||||
|
||||
@@ -30,7 +38,7 @@ df -h -x tmpfs -x devtmpfs
|
||||
|
||||
echo -e "\n==== Top 5 Processes by CPU Usage ===="
|
||||
# Display header and top 5 CPU-consuming processes
|
||||
ps -eo pid,user,comm,%cpu --sort=-%cpu | head -n 6
|
||||
ps -eo pid,user,comm,%cpu --sort=-%cpu | head -n 6 || true
|
||||
|
||||
echo -e "\n==== Top 5 Processes by Memory Usage ===="
|
||||
ps -eo pid,user,comm,%mem --sort=-%mem | head -n 6
|
||||
ps -eo pid,user,comm,%mem --sort=-%mem | head -n 6 || true
|
||||
|
||||
Reference in New Issue
Block a user