forked from 100daysofdevops/N-days-of-automation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Script to calculates the memory utilization of a Linux system and sen…
…d alert if threshold exceed
- Loading branch information
100daysofdevops
committed
Mar 13, 2023
1 parent
fcfbc0c
commit d86142b
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Foobar | ||
|
||
Memory Utilization Check Shell Script | ||
|
||
This shell script calculates the memory utilization of a Linux system by subtracting the free, shared, and buffer cache memory from the total memory. If the memory utilization exceeds the threshold (default: 70%), the script will also send an email alert to the specified recipient. | ||
|
||
## Requirements | ||
|
||
``` | ||
Bash shell | ||
mail command (for email alerts) | ||
``` | ||
|
||
## Usage | ||
|
||
``` | ||
bash check_memory_usage.sh | ||
``` | ||
|
||
## Contributing | ||
|
||
Contributions are welcome! If you find any issues or have any suggestions for improvements, please submit an issue or pull request on GitHub. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/bin/bash | ||
# Script to Calculate the memory utilization after subtracting the free, shared, and buffer cache memory from the total memory | ||
|
||
|
||
# Set the memory average threshold | ||
THRESHOLD=70 | ||
|
||
# Get the total available memory in the system | ||
total_memory=$(awk '/MemTotal/ {print $2}' /proc/meminfo) | ||
# Get free memory | ||
free_memory=$(awk '/MemFree/ {print $2}' /proc/meminfo) | ||
# Get shared memory | ||
shared_memory=$(awk '/Shmem/ {print $2}' /proc/meminfo) | ||
# Get buffer cache memory | ||
buffer_cache_memory=$(awk '/Buffers/ {print $2}' /proc/meminfo) | ||
|
||
# Check if the memory value is numeric | ||
for val in $total_memory $free_memory $shared_memory $buffer_cache_memory; do | ||
if ! [[ $val =~ ^[0-9]+$ ]]; then | ||
echo "Error: Invalid memory value" | ||
exit 1 | ||
fi | ||
done | ||
|
||
# Calculate memory utilization | ||
used_memory=$((total_memory - free_memory - shared_memory - buffer_cache_memory)) | ||
memory_utilization=$((used_memory * 100 / total_memory)) | ||
|
||
# Check if mail command is installed | ||
if ! type "mail" > /dev/null | ||
then | ||
echo "mail command is not installed. Installing now..." | ||
sudo yum -y install mailx | ||
else | ||
echo "mail command is already installed" | ||
fi | ||
|
||
# Check memory utilization against threshold | ||
if [ $memory_utilization -gt $THRESHOLD ] | ||
then | ||
echo "Memory utilization is above the threshold of $threshold%" | ||
|
||
# Send email alert | ||
email_subject="Memory utilization is above threshold" | ||
email_body="Memory utilization is currently at $memory_utilization%. Please check the server immediately." | ||
recipient_email="[email protected]" | ||
echo "$email_body" | mail -s "$email_subject" "$recipient_email" | ||
else | ||
echo "Memory utilization is below the threshold of $threshold%" | ||
fi |