Tuesday, November 27, 2007

Swap monitoring Script

The script below can be run from a crontab, It will grep the status of swap memory in a server and email it to you if the ratio of swap used exceeds 40%.
I think this is very useful !


FREE=`/usr/sbin/swap -s |awk '{print $11}'|cut -f1 -d 'k' `
USED=`/usr/sbin/swap -s |awk '{print $9}'|cut -f1 -d 'k' `
TOTAL=`expr $FREE + $USED`
LEFT=`expr $TOTAL - $USED`
RATIO=`expr $USED \* 100 / $TOTAL`
echo `date` > /tmp/swap.log
echo "Space left : $LEFT " >>/tmp/swap.log
echo "Total space : $TOTAL " >>/tmp/swap.log
echo "" >> /tmp/swap.log
echo "$RATIO% of the available swap space is utilized" >> /tmp/swap.log
echo "" >> /tmp/swap.log
ps -e -o user,pid,ppid,vsz,rss,time,comm | sort -k 4rn | head -15 >> /tmp/swap.log
echo "" >> /tmp/swap.log
/opt/freeware/bin/top >> /tmp/swap.log
#change the threshold here:
if [ $RATIO -ge 40 ]
then mailx -s "Swap issue on `hostname`" fayoubi@gmail.com < /tmp/swap.log
fi
rm /tmp/swap.log


-Thanks for reading

2 comments:

Rachid Akhmouch said...

Salam bba Fahd,
je suis tombé sur ton blog comme par hasard parcke je voulè en créer un aussi. je trouve le tien assez sympa du coup je m'en suis trop inspiré.

Une question en relation avec le script du mailing relatif à une utilisation de mémoire > X %, peux-tu expliquer un peu plus le script, il faut dire que j'en ai trop besoin. On a en effet un Pb de dépassement de mémoire dans le /var mais nous n'avons toujours pas mis en place de script pour nous avéritr, il fo a chak fois se conecter au serveur et checker.Merci pour ta réponse ou sallam lia 3la Hicham si tu le vois.

fayoubi said...

Thanks to Rachid for pointing this out, the script is useless unless it is compbined with a cron (scheduled task).

1. Copy and save the script in the root home directory. call swap_check.sh
2. Create a cron that runs every few minutes. in my case I have set to 4 minutes like this:
$ crontab -e

#Added By Fahd Ayoubi (Monitoring Swap Memory Usage)
00,04,08,12,16,20,24,28,32,36,40,44,48,52,56 * * * * $ORACLE_HOME/swap_check.sh

The cron will run every 4 minutes. it will run the top command, and capture memory info, based on what you have and being used (i have mine set to 40%) all that data will be dumped into a log file and emailed to you or a production group.
Makes sense ?