#!/bin/sh
TOTAL=`cat /proc/meminfo | grep MemTotal: | awk '{print $2}'`
USEDMEM=`cat /proc/meminfo | grep Active: | awk '{print $2}'`
LOG=/var/log/apache-free.log
LIMIT=95
DATA=`date +%Y-%m-%d" "%H:%M:%S`
PROCESS=$(ps aux r)
TOPPROCESS=$(ps -eo pid -eo pcpu -eo command | sort -k 2 -r | grep -v PID | head -n 1)
if [ "$USEDMEM" -gt 0 ]
then
USEDMEMPER=$(($USEDMEM * 100 / $TOTAL))
USEDMEMPERLOG=$USEDMEMPER
if [ $USEDMEMPER -lt $LIMIT ]; then
echo "$DATA | Memory: $USEDMEMPER%, (limit: $LIMIT%) | Do not restart!"
else
echo "$DATA | Memory limit reached ($LIMIT%): $USEDMEMPERLOG% | Restarting apache..."
# restart apache , centos'ta asagidaki sekilde yeniden baslar. Diger oslar icin farkli olabilir.
sudo /etc/init.d/httpd restart
TOTAL=`cat /proc/meminfo | grep MemTotal: | awk '{print $2}'`
USEDMEM=`cat /proc/meminfo | grep Active: | awk '{print $2}'`
USEDMEMPER=$(($USEDMEM * 100 / $TOTAL))
echo "$DATA | Memory limit reached ($LIMIT%): $USEDMEMPERLOG% | Memory after restart: $USEDMEMPER%" >> /var/log/apache-free.log 2>&1
tail -1 $LOG
# mail atmaya hazirlan
SUBJECT="SERVER RAM KULLANIM ISTATISLIK"
# Email Kime
# Email text mesaji
EMAILMESSAGE="/var/log/apache-free.log"
echo "Sistem Ram Yük durumu : $USEDMEM / $TOTAL "> $EMAILMESSAGE
echo "En çok kaynak kullanan islemler $TOPPROCESS" >>$EMAILMESSAGE
echo "Averaj $CPU_LOAD" >>$EMAILMESSAGE
echo "Aktif islemler..." >>$EMAILMESSAGE
echo "$PROCESS" >>$EMAILMESSAGE
mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
fi
fi