Shell Script for Collecting Linux CPU, Memory, and Disk I/O Metrics
This article presents a Bash script that gathers comprehensive Linux system metrics—including CPU core count, utilization percentages, context switches, interrupts, load averages, memory and swap usage, and disk I/O statistics—explaining each command and its purpose for effective server monitoring.
Today we share a Bash script that gathers Linux system information such as CPU core count, utilization percentages, context switches, interrupts, load averages, memory usage, swap statistics, and disk I/O metrics.
The script first determines the server's IP address using ifconfig | grep inet | grep -vE 'inet6|127.0.0.1' | awk '{print $2}' , then extracts the total number of CPU cores from /proc/cpuinfo .
CPU usage is obtained via top -b -n 1 combined with grep Cpu , awk , and cut to isolate user, system, idle, and iowait percentages.
Context switches and interrupt counts are read from vmstat -n 1 1 using sed and awk , while the task queue length is also derived from the same output.
Load averages for the past 1, 5, and 15 minutes are parsed from uptime with awk and cut .
Memory statistics, including total, used, free, and swap values, are collected with free and filtered via grep , awk , and sed .
Disk I/O statistics for a specific device (e.g., /dev/sda) are obtained using iostat -kx , then filtered with grep and awk to report read/write request rates, queue length, average request time, service time, and CPU utilization.
Finally, the script echoes each metric with descriptive labels, providing a concise snapshot of the server's performance.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.