Operations 13 min read

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.

IT Services Circle
IT Services Circle
IT Services Circle
Shell Script for Collecting Linux CPU, Memory, and Disk I/O Metrics

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

monitoringperformanceLinuxBashSystemMetrics
IT Services Circle
Written by

IT Services Circle

Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.