Skip to content

Instantly share code, notes, and snippets.

@shitalmule04
Last active April 11, 2022 12:55
Show Gist options
  • Save shitalmule04/1df5eacff77cd23ad47cd18926720402 to your computer and use it in GitHub Desktop.
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.
# !/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