From d86142b18c51ed3e2edd82cd44fa9cc871addd23 Mon Sep 17 00:00:00 2001 From: 100daysofdevops <100daysofdevops@gmail.com> Date: Mon, 13 Mar 2023 16:23:54 -0700 Subject: [PATCH] Script to calculates the memory utilization of a Linux system and send alert if threshold exceed --- check_system_utilization_metrics/README.md | 24 +++++++++ .../check_memory_usage.sh | 50 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 check_system_utilization_metrics/README.md create mode 100644 check_system_utilization_metrics/check_memory_usage.sh diff --git a/check_system_utilization_metrics/README.md b/check_system_utilization_metrics/README.md new file mode 100644 index 0000000..4597972 --- /dev/null +++ b/check_system_utilization_metrics/README.md @@ -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. + diff --git a/check_system_utilization_metrics/check_memory_usage.sh b/check_system_utilization_metrics/check_memory_usage.sh new file mode 100644 index 0000000..ec312bd --- /dev/null +++ b/check_system_utilization_metrics/check_memory_usage.sh @@ -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="recipient@example.com" + echo "$email_body" | mail -s "$email_subject" "$recipient_email" +else + echo "Memory utilization is below the threshold of $threshold%" +fi \ No newline at end of file