PDA

View Full Version : Script to Monitor Memory Usage (usefull with SLM)


ashokjp
02-16-2007, 12:57 PM
I know many of you might not be annoyed by the new SLM system.
Here i am attaching a script that should be usefull to atleast 50% of you people.

What this bash script does ?
It monitors memory usage with cron set, and when it exceeds the value set by you, it runs the commands you prefer and also notifies you by email.

Now lets move on to get on with it (for beginners in shell)
Go on with the following commands as it is
# touch /root/memon
# chmod 0755 /root/memon
# nano /root/memon

-- Copy Paste from below the Code: tag excluding this line ----
EMAIL="root"
SUBJECT="Memory Alert"
FILE="/root/tmpmu"
TRIGGER=li
MF="$(grep MemF /proc/meminfo | awk '{print $2}')"
MemFree="$(( ${MF} / 1024 ))"
MT="$(grep MemT /proc/meminfo | awk '{print $2}')"
MemTotal="$(( ${MT} / 1024 ))"
MU="$(( ${MT} - ${MF} ))"
MemUsed="$(( ${MU} /1024 ))"

echo "Hostname: $(hostname)" > $FILE
echo "Local Date & Time : $(date)" >> $FILE
echo "" >> $FILE

echo Memory Usage(Used/Burst RAM): $MemUsed/$MemTotal >> $FILE
echo "" >> $FILE

if [ $MemUsed -gt $TRIGGER ]; then
echo "$(abc1)" >> $FILE
echo "$(abc2)" >> $FILE
echo "$(abc3)" >> $FILE

/bin/mail -s "$SUBJECT" "$EMAIL" < $FILE
fi
echo > $FILE
-- Copy Paste till above excluding this line ----

replace abc1, abc2, abc3 with commands you would like to run once the memory exceeds the limit set (replace li with the memory monitor trigger value)

Once done, press CTRL + X and save the file

Now we will move on to setup crontab.
# crontab -e
Add this line below
* * * * * /bin/sh /root/memon
This will run the script every minute. Its better not to put too much intrevals between runs. The script performs restarts and notification only when it exceeds memory trigger set by you.

My preference towards abc1 would be
abc1= /scripts/restartsrv spamd

I hope this helps :)

ashokjp
02-17-2007, 06:00 PM
Just a small addition to the script. I am pasting the whole script with modified stuff.
Just one more addition that in the alert mail sent, it will also tell you how much burst ram you are using out of how much ram you were allotted. :rolleyes:


EMAIL="root" # EMAIL ID TO WHICH EMAIL SHOULD BE SENT
SUBJECT="Memory Alert" # SUBJECT OF EMAIL SENT
FILE="/root/tmpmu" # TEMP FILE TO WHICH EMAIL DATA IS WRITTEN
TRIGGER=205 # TRIGGER VALUE AT WHICH CMD's SHOULD BE EXECUTED
BURST=1024 # BURST RAM ALLOTTED
GUD=256 # GURANTEED RAM

#---------------------------DONOT CHANGE ANYTHING BELOW THIS -----------------------------
MF="$(grep MemF /proc/meminfo | awk '{print $2}')"
MemFree="$(( ${MF} / 1024 ))"
MT="$(grep MemT /proc/meminfo | awk '{print $2}')"
MemTotal="$(( ${MT} / 1024 ))"
MU="$(( ${MT} - ${MF} ))"
MemUsed="$(( ${MU} /1024 ))"
BRU=0
BRTT="$(( ${BURST} - ${GUD} ))"
if [ $MemUsed -gt $GUD ]; then
BRU="$(( ${MemUsed} - ${GUD} ))"
fi

echo "Hostname: $(hostname)" > $FILE
echo "Local Date & Time : $(date)" >> $FILE
echo "" >> $FILE

echo Memory Usage(Used/Guaranteed RAM): $MemUsed/$GUD >> $FILE
echo Burst Usage: $BRU/$BRTT >> $FILE
echo "" >> $FILE

if [ $MemUsed -gt $TRIGGER ]; then
#--------SET THE BELOW COMMANDS INSIDE THE BRACKETS WHICH YOU WANT TO RUN ON TRIGGER-------
echo "$(/etc/rc.d/init.d/exim restart)" >> $FILE
echo "$(/etc/rc.d/init.d/mysql restart)" >> $FILE
#echo "$(abc3)" >> $FILE

/bin/mail -s "$SUBJECT" "$EMAIL" < $FILE
fi
echo > $FILE

Please make sure you set values for all variables appropriately. For newbies to shell, you are free to post out here for any help. :)

vps-vince
02-19-2007, 07:41 PM
Hi,
This looks interesting, although far too advanced for me to understand.

Can you provide a couple of examples of the email output.

By the way, can this not be done with a PHP script?

Thanks,

- Vince

ashokjp
02-19-2007, 07:44 PM
Hi,
This looks interesting, although far too advanced for me to understand.

Can you provide a couple of examples of the email output.

By the way, can this not be done with a PHP script?

Thanks,

- Vince

I havent thought about doing with php but it should work with open_basedir restriction removed for the particular domain
The file /proc/meminfo contains the memory usage information.
So you can read it and manipulate.

But if you prefer using bash script itself, you can just copy paste the code above and assign appropriate values to variables and start using.

Let me know if you need any help ;)

vps-vince
02-19-2007, 08:01 PM
Due to security issues, I would dread removing open_basedir for any domain on my VPS.

Can you suggest what you have for abc1, 2 and 3?

Thanks,

- Vince

ashokjp
02-19-2007, 08:10 PM
Hi,
This looks interesting, although far too advanced for me to understand.

Can you provide a couple of examples of the email output.

By the way, can this not be done with a PHP script?

Thanks,

- Vince

Due to security issues, I would dread removing open_basedir for any domain on my VPS.

Can you suggest what you have for abc1, 2 and 3?

Thanks,

- Vince
I would suggest

abc1 = /scripts/restartsrv spamd
(or /etc/rc.d/init.d/exim restart)

abc2 = /etc/rc.d/init.d/mysql restart

/scripts/restartsrv spamd - Restarts spamd alone
Spamd is a bit of memory consuming process.
/etc/rc.d/init.d/exim restart - Restarts exim, spamd antirelayd.
So if spamd by itself doesnt reduce ur consumption much, you can go for the exim restart command.

if u r setting mysql(ie. abc2) make sure u reduce your trigger limit a bit. say if u have 256MB RAM, put the trigger around 200. also make sure you have sim or any such service running which would restart mysql just incase the script executing mysql restart fails to perform.

ashokjp
02-19-2007, 08:23 PM
I have modified my second post, added default values for variables so that any body can start using by just copy-paste :)

You can change values as per your environment.

Tony
02-19-2007, 09:35 PM
Oooh, nice script... I swear I've seen something similar to that elsewhere on here... :rolleyes:

MarkB
02-20-2007, 03:06 AM
Oooh, nice script... I swear I've seen something similar to that elsewhere on here... :rolleyes:
Yeppers, would be nice if guys would add few comments, like "based on..." or "inspired by..."

I feel for ya, hehe

ashokjp
02-20-2007, 06:45 AM
Oooh, nice script... I swear I've seen something similar to that elsewhere on here... :rolleyes:

Well Tony i can double swear that this isnt any copied stuff.
Actually any guy familiar with bash or can find some bash resources can make this script in less than an hour.:rolleyes:

I just had this for my personal use with some basics, thought it would be usefull to others and so i put this here.

Tony
02-20-2007, 04:29 PM
Yeppers, would be nice if guys would add few comments, like "based on..." or "inspired by..."

I feel for ya, hehe

Heh, usually I just let it slide... I was tired and irritable last night tho, and arguing with a 2000 line perl script. Tbh, I'd take 'Tony, your script was a real piece of crap, and I hated it, so I wrote this one instead' over nothing at all. :cool:

Well Tony i can double swear that this isnt any copied stuff.
Actually any guy familiar with bash or can find some bash resources can make this script in less than an hour.:rolleyes:

I just had this for my personal use with some basics, thought it would be usefull to others and so i put this here.


Not accusing you of anything directly ashok, but there have been a few cases of people on here blatantly plagiarizing other scripts without leaving credit in the comments.

ashokjp
02-20-2007, 04:33 PM
:) No issues, i was just telling since you expressed doubt that you saw this elsewhere. I compiled this on own. All i can say helped is some bash scripting guides which fixed some issues in some statments for me.. :P

Zaf
02-20-2007, 06:11 PM
Hey Tony, you've been looking into my VPS ??? ;-) *j/k*
You might have seen a similar script on my VPS....been using it for my own reference for more than a year now.

Infact, I've even sent some of the code of that script to someone here about a year back and that might still be there in my PM sent items

I agree with ashok that anyone with scripting knowledge can come up with similar stuff as in this script.

Good luck

ashokjp
02-21-2007, 12:35 PM
Guys lets not waste on who all has it.

What i wanted to clear is this isnt a copied stuff and above all this is not such a big thing. It is bash basics.

I would like to continue this topic who are beginners in shell and are facing issue in handling this and i and other members will be happy to help them.

Guys if u had this script earlier, maybe lot others had. at least someone should take the courtesy to share it right ? I just did that. Please co-operate and let this topic continue without much unnecessary argument ;)

Zaf
02-21-2007, 06:32 PM
Wrote that in a jovial mood. Did not intend to prove my point or any other such thing.
As far as sharing ones development is concerned...there were various reasons why I did not release that script....primarily there wasnt too much of a need for such logging at that time (SLM wasnt there then). Also, I was extremely busy with other commitments to give time for the finishing touches required for that script.

Apologies if anyone felt offended with that post.

Atomm
02-27-2007, 05:16 PM
If crond is killed by SLM, wouldn't it render this script useless?

ashokjp
02-28-2007, 06:50 AM
If crond is killed by SLM, wouldn't it render this script useless?

This script is a simple bash script which can remove the effort of sitting in the system and restarting memory eating services while slm is in action.

if you set the trigger value to say 210 or 220 for 256MB ram users or 490 or 500 for 512 mb ram users, this script while in cron will automatically restart the vulgur processes, which will reduce your ram usage and at the same time the service wont be affected much than a fraction of second when the restart happens.

If you set 300 to 256 ram users, it is likely that you will find this script useless.
:rolleyes:

DeanClinton
02-28-2007, 09:20 AM
Fingers crossed its not a cpanel update it kills eh?

ashokjp
02-28-2007, 09:22 AM
Fingers crossed its not a cpanel update it kills eh?

My script doesnot kill any process. And ya it does restart services BUT only which you specify in the script.

FFR
03-02-2007, 03:12 PM
Hello ashokjp !

I want to use Your script.

I must execute command : service httpd startssl

What correct syntax of this command?


Thanks.

ashokjp
03-02-2007, 03:16 PM
this script runs in cron and hence a httpd restart would be suggested
If httpd is already running on ssl, when it restarts it will restart ssl too

so this command would be fine
/etc/rc.d/init.d/httpd restart

if you still want the startssl command try this
/etc/rc.d/init.d/httpd startssl

zolee1
03-07-2007, 01:52 PM
ashokjp, thanks very much for the script. I added it to both of our VPSs and it works great, no crash since then.

I set the (suggested)
/etc/rc.d/init.d/exim restart
and
/etc/rc.d/init.d/mysql restart
processes to restart.

We also updated to Power1PlusLite (128MB extra memory) , and just received an email that PowerVPS did it for free due to the change in policy. Now, this is what I call a great company!

Thanks again ashokjp and PowerVPS!

Z-

ashokjp
03-07-2007, 02:00 PM
ashokjp, thanks very much for the script. I added it to both of our VPSs and it works great, no crash since then.

I set the (suggested)
/etc/rc.d/init.d/exim restart
and
/etc/rc.d/init.d/mysql restart
processes to restart.

We also updated to Power1PlusLite (128MB extra memory) , and just received an email that PowerVPS did it for free due to the change in policy. Now, this is what I call a great company!

Thanks again ashokjp and PowerVPS!

Z-
Glad to hear that you found it good.
you mean free 128 MB upgrade ?:confused:

zolee1
03-07-2007, 02:07 PM
Yepp. I am not sure what they offer for the more expensive packages. We used to have POWER-1 with 256MB Memory and upgraded to Power1 Plus Lite. According to the support team PowerVPS's policy is changed recently and they offer this upgrade for no additional charge. I hope it will help out others as well.

Z-

ashokjp
03-07-2007, 02:21 PM
ashokjp, thanks very much for the script. I added it to both of our VPSs and it works great, no crash since then.

I set the (suggested)
/etc/rc.d/init.d/exim restart
and
/etc/rc.d/init.d/mysql restart
processes to restart.

We also updated to Power1PlusLite (128MB extra memory) , and just received an email that PowerVPS did it for free due to the change in policy. Now, this is what I call a great company!

Thanks again ashokjp and PowerVPS!

Z-

Anyways happy to see one guy happy on this ... ;)

sgriffin
06-30-2007, 12:24 AM
SLM is bad, very bad.
0 Problems until SLM.
Now its "you gotta buy the new monitoring service cause you'll need to be restarted every two weeks."
Is this linux or windows again?

Daniel
06-30-2007, 09:54 AM
SLM is bad, very bad.
0 Problems until SLM.
Now its "you gotta buy the new monitoring service cause you'll need to be restarted every two weeks."

Actually, SLM is very beneficial. Instead of customers being able to take advantage of abusing their resource limits (sitting in the burst RAM forever), they are actually kept inside the limits they pay for. It also keeps the nodes healthier and everything overall gets better.