How to Calculate Linux System Load Average in percentage
In the world of Linux system administration, monitoring system load is a fundamental task. It helps in understanding how well the system is performing and whether it is under any strain.
Today, we're going to explore a simple Bash script that can provide a clear snapshot of the system's load average and how this translates to CPU usage.
We will also learn how to figure out this CPU use in percentage. This script is a powerful tool for Linux system administrators.
Understanding Key CPU Metrics:
1. CPU Usage Percentage
This metric indicates the percentage of the CPU's capacity currently in use.
2. Load Average
Load average represents the average system load over a specified period (1, 5, and 15 minutes).
For a system with a single CPU, a load average of 1.0 signifies full utilization.
On a multi-core system, a load average equal to the number of cores means full utilization.
Values exceeding the number of cores indicate queued processes waiting for CPU time, potentially leading to slower performance.
3. Context Switches
This metric counts the number of times the CPU switches from processing one thread or process to another.
High numbers of context switches may indicate that the CPU is spending a lot of time switching between tasks, which can reduce efficiency.
4. CPU Time Breakdown
User Time: Time spent on user-mode operations (non-kernel code).
System Time: Time spent on kernel-mode operations.
Idle Time: Time when the CPU is not being used.
I/O Wait Time: Time waiting for I/O operations to complete.
5. Interrupts Per Second
This measures the number of hardware interrupts that the CPU receives per second.
A high number of interrupts could indicate busy or malfunctioning hardware
Important key Metrics of the script:
Fetching Load Averages:
The script uses the uptime command to fetch load averages for 1, 5, and 15 minutes.
Determining CPU Cores:
The number of CPU cores is fetched using the nproc command.
The load average needs to be evaluated in the context of the available cores.
Calculating Percentages:
The script then calculates the load percentage for each interval.
This is done by dividing the load average by the number of CPU cores and multiplying by 100.
Formula: It uses the formula (load average / number of CPU cores) * 100 to calculate this percentage.
Important Note:
Based on the script output, if the system load exceeds 100%, it indicates an abnormal state where multiple processes are in the run queue, waiting for CPU resources. This suggests that the system is overloaded.
Conversely, if the system load is less than 100%, it can be considered normal, indicating that the system is operating within its CPU usage threshold and is not overloaded.
Linux System Load Average bash script:
#!/bin/bash
#Author: www.virtualnetworkingconcept.com
Hostnames=$(uname -n | cut -d. -f1)
os_type=$(uname -s)
case "$os_type" in
Linux)
# Define color variables
GREEN="\033[0;32m"
YELLOW="\033[0;33m"
NC="\033[0m" # No Color
cputhreshold=99.0
# Get the CPU load averages
load_avg_1min=$(uptime | awk -F'load average: ' '{print $2}' | cut -d ',' -f1 | tr -d ' ')
load_avg_5min=$(uptime | awk -F'load average: ' '{print $2}' | cut -d ',' -f2 | tr -d ' ')
load_avg_15min=$(uptime| awk -F'load average: ' '{print $2}' | cut -d ',' -f3 | tr -d ' ')
# Get the number of CPU cores
cpu_core=$(nproc --all)
# Calculate percentages
percentage_1min=$(echo "scale=2; ($load_avg_1min / $cpu_core) * 100" | bc)
percentage_5min=$(echo "scale=2; ($load_avg_5min / $cpu_core) * 100" | bc)
percentage_15min=$(echo "scale=2; ($load_avg_15min / $cpu_core) * 100" | bc)
if (( $(echo "$percentage_1min >= $cputhreshold" | bc -l) )); then
# Print the table header in a box
printf "+------------------+-------------------+------------------+-----------------+--------+\n"
printf "| %-16s | %-17s | %-16s | %-14s | %-06s |\n" "HostName" "Time Intervals" "Load Average" "System Load (%)" "Status"
printf "+------------------+-------------------+------------------+-----------------+--------+\n"
# Print the table rows
printf "| %-16s | %-17s | %-16s | %-15s | ${YELLOW}%-06s${NC} |\n" "" "1 minute" "$load_avg_1min" "$percentage_1min%" ""
printf "| %-16s | %-17s | %-16s | %-15s | ${YELLOW}%-06s${NC} |\n" "$Hostnames" "5 minutes" "$load_avg_5min" "$percentage_5min%" "High"
printf "| %-16s | %-17s | %-16s | %-15s | ${YELLOW}%-06s${NC} |\n" "" "15 minutes" "$load_avg_15min" "$percentage_15min%" ""
# Print the bottom border of the table box
printf "+------------------+-------------------+------------------+-----------------+--------+\n"
else
# Print the table header in a box
printf "+------------------+-------------------+------------------+-----------------+--------+\n"
printf "| %-16s | %-17s | %-16s | %-14s | %-06s |\n" "HostName" "Time Intervals" "Load Average" "System Load (%)" "Status"
printf "+------------------+-------------------+------------------+-----------------+--------+\n"
# Print the table rows
printf "| %-16s | %-17s | %-16s | %-15s | ${GREEN}%-06s${NC} |\n" "" "1 minute" "$load_avg_1min" "$percentage_1min%" ""
printf "| %-16s | %-17s | %-16s | %-15s | ${GREEN}%-06s${NC} |\n" "$Hostnames" "5 minutes" "$load_avg_5min" "$percentage_5min%" "Normal"
printf "| %-16s | %-17s | %-16s | %-15s | ${GREEN}%-06s${NC} |\n" "" "15 minutes" "$load_avg_15min" "$percentage_15min%" ""
# Print the bottom border of the table box
printf "+------------------+-------------------+------------------+-----------------+--------+\n"
fi
;;
*)
echo -e "This script is not supported for $os_type - $Hostnames"
;;
esac
Script output: Below is the referenced script output for calculating system load average usage in percentage. Download
Script Output:
centos# sh linux_load_avg.sh
+------------------+-------------------+------------------+-----------------+--------+
| HostName | Time Intervals | Load Average | System Load (%) | Status |
+------------------+-------------------+------------------+-----------------+--------+
| | 1 minute | 0.16 | 8.00% | |
| centos | 5 minutes | 0.05 | 2.00% | Normal |
| | 15 minutes | 0.06 | 3.00% | |
+------------------+-------------------+------------------+-----------------+--------+
Conclusion:
If you are taking care of Linux servers, it's very important to know about CPU usage.
Good CPU usage means your servers will work better and more reliably.
This simple yet effective Bash script provides a snapshot of the system's load average and its impact in terms of CPU usage.
By regularly running this script, system administrators can stay ahead in managing system resources efficiently, ensuring that the Linux servers remain healthy and perform optimally.