š§ The project requirements are taken from roadmap.sh. Feel free to take a look and do your own if you interested.
Goal
Write a script to analyse performance stats.
Requirements
You are required to write a script
server-stats.sh
that can analyse basic server performance stats. You should be able to run the script to any Linux server and it should give you the following stats:- Total CPU usage
- Total memory usage (Free & Used including percentage
- Total disk usage (Free & Used including percentage)
- Top 5 processes by CPU usage
- Top 5 processes by memory usage
Stretch goal:
- OS Version
- Uptime
- Load average
- Logged in users
- Failed login attempts
Getting StartedWrite the ScriptBash ScriptCPU UsageMemory UsageDisk UsageTop 5 Processes by CPU UsageTop 5 Processes by Memory UsageOS VersionUptimeLoad AverageLogged in UsersFailed Login Attempt
Getting Started
So letās analyze the requirements. This project is going to demonstrate how we can use Bash script to automate repetitive task. Perhaps this is for weekly or daily server checkup on system performance or it can be extended for high-level programming language like summarizing it on HTML page.
First thing, I see the scope that the script must be run on any Linux server. So that mean we need to create test environment consisting of several Linux distribution. So first, I will made that up on my own laptop using Hyper-V. You can check my article on how to setup local Hyper-V so we can have data-center like environment. Letās scope our project into 3 Linux server distribution that are in separate Linux distros family, Ubuntu to represent Debian-based distribution, CentOS to represent Red Hat-based distribution, and OpenSUSE to represent SUSE-based distribution.
Letās prepare the VMs first:
Ubuntu:
CentOS:
OpenSuse:
Write the Script
Bash Script
So letās by one find out what script can be run one all three Linxu system. All three of our Linux system is running BASH version 5. You can check BASH version using command:
echo ${BASH_VERSION}
Now letās make the script using vim text editor from the Ubuntu:
vim server-stats.sh
# server-stats.sh
First, letās find out what command to get the current total CPU usage.
CPU Usage
Before that, letās understand following Linux command. Use command
man <command name>
if you want to ge the manual page of the command directly on Linux. grep
grep searchs for patterns in file. The format itself is
grep <the pattern>
run towards a file or a line in terminal. Iām not going to describe in detail about pattern that is written in regular expression, perhaps in another blog post, but keep in mind that if we want to search specific thing in a file, use grep.tail
tail is basically output the last part of files. By default it print the last 10 lines of file to standard output.
awk
awk is shorthand for gawk, the pattern scanning and processing langugae. So it is same as grept hat it processed text-based format, but while grep are designed for search text based on pattern, awk is a full-fledged progamming language that allows for manipulation and extraction of those text files, like doing calculation, add conditional, etc.
top
top is program that provides a dynamic real-time view of a running system. It can display system summary information as well as a list of processes or threads currently being managed by the Linux kernel. So it is program to read the current system information.
The command:
top -bn2 | grep '%Cpu' | tail -1 | grep -P '(....|...) id,'| \ awk '{print "CPU Usage: " 100-$8 "%"}'
Explanation
top -bn2
- Runs
top
in batch mode (b
) with 2 iterations (n2
). The first iteration can sometimes show inaccurate results, so the second iteration gives more accurate data.
grep '%Cpu'
- Filters the lines containing
%Cpu
, where CPU statistics are shown.
tail -1
- Takes the last occurrence of
%Cpu
, which corresponds to the second iteration.
grep -P '(....|...) id,'
- This ensures the matching of
id
(idle time) value, regardless of whether it is 3 or 4 characters wide, such as97.4
or7.2
.
awk '{print "CPU Usage: " 100-$8 "%"}'
- Subtracts the idle CPU percentage (
$8
) from 100 to calculate CPU usage. $8
corresponds to the idle (id
) percentage value.
Memory Usage
Ā