#!/bin/sh
ramusage=$(free | awk '/Mem/{printf("RAM Usage: %.2f
"), $3/$2*100}'| awk '{print $3}')
if [ "$ramusage" > 20 ]; then
SUBJECT="ATTENTION: Memory Utilization is High on $(hostname) at $(date)"
MESSAGE="/tmp/Mail.out"
TO="2daygeek@gmail.com"
echo "Memory Current Usage is: $ramusage%" >> $MESSAGE
echo "" >> $MESSAGE
echo "------------------------------------------------------------------" >> $MESSAGE
echo "Top Memory Consuming Process Using top command" >> $MESSAGE
echo "------------------------------------------------------------------" >> $MESSAGE
echo "$(top -b -o +%MEM | head -n 20)" >> $MESSAGE
echo "" >> $MESSAGE
echo "------------------------------------------------------------------" >> $MESSAGE
echo "Top Memory Consuming Process Using ps command" >> $MESSAGE
echo "------------------------------------------------------------------" >> $MESSAGE
echo "$(ps -eo pid,ppid,%mem,%Memory,cmd --sort=-%mem | head)" >> $MESSAGE
mail -s "$SUBJECT" "$TO" < $MESSAGE
rm /tmp/Mail.out
fi
# crontab -e
# */5 * * * * /bin/bash /opt/scripts/memory-alert.sh
# /usr/bin/free | awk '/Mem/{printf("RAM Usage: %.2f%
"), $3/$2*100}' | awk '{print $3}' | awk '{ if($1 > 20) print $0;}' | mail -s "High Memory Alert" 1103324414@qq.com