Last active
April 11, 2022 12:55
-
-
Save shitalmule04/1df5eacff77cd23ad47cd18926720402 to your computer and use it in GitHub Desktop.
Shell Script to check memory usage. If memory usage beyond 70%, should send mail.
This file contains hidden or 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
# !/bin/bash | |
# To find memory usage in MB | |
used=$(free -mt | grep Total | awk '{print $3}') | |
total=$(free -mt | grep Total | awk '{print $2}') | |
echo $used "MB used out of " $total "MB" | |
# TO calculate memory usage percentage | |
usage_percent= expr " $used * 100 / $total" | bc | |
# Check if memory usage beyond 70% or not, if yes then send mail | |
if [[ $usage_percent -gt 70 ]]; | |
then | |
echo "Oops! $HOSTNAME memory usage beyond 70%. Please take a note." \ | |
| mail -s "Memory usage beyond 70%" root@localhost | |
echo "Mail sent successfully.!!" | |
else | |
echo "Memory usage less than 70%" | |
fi | |
# Memory_Usage script should run every hour, every month between monday to friday | |
#write out current crontab | |
crontab -l > mycron | |
#echo new cron into cron file | |
echo "59 * * * 1-5 ./memory_usage.sh" >> mycron | |
#install new cron file after every hour | |
crontab mycron | |
rm mycron |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment