Skip to content

Commit

Permalink
Script to calculates the memory utilization of a Linux system and sen…
Browse files Browse the repository at this point in the history
…d alert if threshold exceed
  • Loading branch information
100daysofdevops committed Mar 13, 2023
1 parent fcfbc0c commit d86142b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
24 changes: 24 additions & 0 deletions check_system_utilization_metrics/README.md
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.

50 changes: 50 additions & 0 deletions check_system_utilization_metrics/check_memory_usage.sh
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

0 comments on commit d86142b

Please sign in to comment.