Create a Script to Analyze Basic Server Performance Stats

Create a Script to Analyze Basic Server Performance Stats

Status
In progress
Created
Nov 25, 2024 09:54 AM
Tags
Project
šŸ§ 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 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:
notion image
CentOS:
notion image
OpenSuse:
notion image

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
  1. 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.
  1. grep '%Cpu'
      • Filters the lines containing %Cpu, where CPU statistics are shown.
  1. tail -1
      • Takes the last occurrence of %Cpu, which corresponds to the second iteration.
  1. grep -P '(....|...) id,'
      • This ensures the matching of id (idle time) value, regardless of whether it is 3 or 4 characters wide, such as 97.4 or 7.2.
  1. 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

Ā 

Disk Usage

Top 5 Processes by CPU Usage

Top 5 Processes by Memory Usage

OS Version

Uptime

Load Average

Logged in Users

Failed Login Attempt