You are not logged in.

#1 2008-04-15 21:03:22

seanf
Administrator
Registered: 2008-02-01
Posts: 1899
Website

Zope memory watchdog

Here's a simple shell script I use to keep tabs on my Zope's memory usage:

Code:

#!/bin/bash
RAMUSAGE=$(ps -u $LOGNAME -o rss,command | grep -v peruser | awk '{sum+=$1} END {print sum}')
if [ $RAMUSAGE -gt 163840 ]; then
    echo "$( date +'%Y-%m-%d %H:%M:%S') Memory usage at $RAMUSAGE - restarting Zope" >> ~/logs/user/mem.log
    ~/webapps/zope/Zope/bin/zopectl restart
    # you can run other stuff here also
fi

It does the following:

1. Calculates my total memory usage
2. Compares that to a preset value of 163840 (160MB)
3. If it is greater than 160MB, make an entry in ~/logs/user/mem.log, then restart the Zope app located in ~/webapps/zope.

I've got it saved as ~/bin/zopewatch, and scheduled to run every 5 minutes with the following cron job:

Code:

*/5 * * * * /home/my_username/bin/zopewatch > /dev/null 2>&1

I know not everyone would want their Zope restarting automatically, but mine is just a personal site, and running like this ensures that I'll never receive a high memory warning from WebFaction.

You could use this for any long running app, not just Zope - just change the line that restarts Zope to whatever you need (call another script, fire off an email, whatever).

Last edited by seanf (2008-10-16 14:41:26)

Offline

 

#2 2008-05-14 13:48:46

insmalls
Member
From: USA
Registered: 2007-09-19
Posts: 24
Website

Re: Zope memory watchdog

Great script. I am adding a bit of info to help people do this who don't know cron.
1. You can use the nano editor to create the zopewatch.  <ctrl>o saves, <ctrl>x exits
nano zopewatch
2. You will probably have to set the permissions on the zopewatch script to allow it to execute. Do so with chmod command, like the following:
chmod 744 zopewatch
3. You set the cron job using crontab. This will pop you into an editor (vi) which is very cryptic. If you use it, soem quick commands are found on this link: http://www.cs.colostate.edu/helpdocs/vi.html
4. If you hate using vi, set your editor to default to nano:
export EDITOR=nano
Note that you can look at all sorts of system variables using the following:
export | more
Good way to learn...
5. More info on crontab can be found here: http://www.adminschoice.com/docs/crontab.htm#Commands

This was helpful to me, as I just migrated my plone 2.5.3 sites to plone 3.0.6 and my memory usage is way up. It goes over 200MB with in 18 hours, same traffic, same sites.

Jamie

Offline

 

#3 2008-05-14 18:47:18

seanf
Administrator
Registered: 2008-02-01
Posts: 1899
Website

Re: Zope memory watchdog

Thanks, Jamie!

Offline

 

#4 2008-09-26 08:52:33

urbanian
Member
Registered: 2007-09-21
Posts: 53

Re: Zope memory watchdog

I'm really confused by crontab / cron jobs and was wondering if anyone could help.

I went to edit my crontab after reading this post - by using 'crontab -e' but there are too many fields displayed in my crontab and it doesn't match up to the info in that link about crontabs either... 

here is my crontab :
1,21,41 * * * * /home/urbanian/webapps/myzope/Zope/bin/zopectl start > /dev/null 2>&1
0,20,40 * * * * /home/urbanian/webapps/newzope/Zope/bin/zopectl start > /dev/null 2>&1

I've read somewhere else that these default cron job are set to run every ten minutes but I just can't work out what I'm looking at here...  what are these fields?

Many thanks,
Ian.

Offline

 

#5 2008-09-26 08:54:23

seanf
Administrator
Registered: 2008-02-01
Posts: 1899
Website

Re: Zope memory watchdog

There are no default cron jobs that run every 10 minutes, but http://www.linuxhelp.net/guides/cron/ explains the fields in crontab nicely. Hope that helps!

Last edited by seanf (2008-09-26 08:56:31)

Offline

 

#6 2008-09-26 09:07:09

urbanian
Member
Registered: 2007-09-21
Posts: 53

Re: Zope memory watchdog

When you add a Zope instance in the applications section you are presented with information and in the Doc : field it says that :
"...this installer sets up a cronjob that runs every 10 minutes and restarts your Zope instance if it is down"

Looking at that linuxhelp.net guide hasn't helped me understand my default cron job as mine appear to have 8 fields rather than the 7 in this guide and if I use the guide it would mean that this cron job is running on the 20th hour of the 40th day of the month which makes no sense. 

Apologies if I'm being dense but it's about as clear as mud.
Many thanks for your help.
Ian.

Last edited by urbanian (2008-09-26 09:07:55)

Offline

 

#7 2008-09-26 09:16:27

seanf
Administrator
Registered: 2008-02-01
Posts: 1899
Website

Re: Zope memory watchdog

urbanian wrote:

When you add a Zope instance in the applications section your are presented with information and in the Doc : field it says that :
"...this installer sets up a cronjob that runs every 10 minutes and restarts your Zope instance if it is down"

Oh, THAT default cron job! smile It's actually every 20 minutes.. I'll look into correcting that info for newly-installed apps.

urbanian wrote:

Looking at that linuxhelp.net guide hasn't helped me understand why my default cron job as mine appear to have 8 fields rather than the 7 in this guide and if I use the guide it would mean that this cron job is running on the 20th hour of the 40th day of the month which makes no sense. 

Apologies if I'm being dense but it's about as clear as mud.
Many thanks for your help.

No worries... crontab is an ancient format, and it's true that the 'fields' aren't clearly delimited.

here's one of your crontab entries:

Code:

0,20,40 * * * * /home/urbanian/webapps/newzope/Zope/bin/zopectl start > /dev/null 2>&1

The first five "fields" are the schedule, and are delimited by spaces. Since the job is in your personal crontab, there's no user argument, so everything after the schedule is the command. So:

0,20,40 -  At 0,20, and 40 minutes after the hour,
* - On every hour,
* - On every day of the month,
* - Of every month,
* - On every day of the week,
Run the command "/home/urbanian/webapps/newzope/Zope/bin/zopectl start > /dev/null 2>&1"

More clear?

Offline

 

#8 2008-09-26 09:23:34

urbanian
Member
Registered: 2007-09-21
Posts: 53

Re: Zope memory watchdog

Oh right... I was being caught out by those multiple minutes in what I now see is just the first field. 

Many thanks for clearing that up for me!
Cheers,
Ian.

Offline

 

#9 2008-10-16 14:42:21

seanf
Administrator
Registered: 2008-02-01
Posts: 1899
Website

Re: Zope memory watchdog

I've updated the script to ignore per-user Apache processes, since we're not counting them against your RAM usage for now.

Offline

 

#10 2008-10-20 15:09:01

insmalls
Member
From: USA
Registered: 2007-09-19
Posts: 24
Website

Re: Zope memory watchdog

Hi Sean,
I commend you on thinking to update the script. As soon as you guys responded to me about the weird ram / restarts I was seeing, I thought of this too. For my two cents, I did the same basic thing using the grep -v option:

ps -u insmalls -o pid,rss,command | grep -e mpm -v | awk '{print $0}{sum+=$2} END {print "Total", sum}'

Have a great day!
Jamie

Offline

 

#11 2008-10-25 22:58:38

insmalls
Member
From: USA
Registered: 2007-09-19
Posts: 24
Website

Re: Zope memory watchdog

Hi Sean,
Just had an idea that might help Plone users out.  How about modifying your script to be general enough that we could all just run that one single script. That way, if there were future changes in the ram, etc, we would automatically benefit from those changed on the central script...

If this is an big enough issue (Plone ram usage in general), then the script and cron job could be set as a default when we use the auto installer for Plone (with instructions for advanced user to turn it off if they wanted too).  That way, you know everyone is probably running it and therefore reducing ram usage.  Your central restart script could even look up and measure against our particular ram thresholds for various plans.

BTW, for some reason my "fix" above messed up my version of the restart script and I went with your change :-)

Well, that is my feedback. Have a great night,
Jamie

Offline

 

#12 2009-03-26 02:43:50

edtaa
Member
From: Camberley, UK
Registered: 2009-03-26
Posts: 30
Website

Re: Zope memory watchdog

Hi guys
I just signed up with Webfaction and so far so great. On previous host, I had a cron job to restart zope daily but the zopewatch script seems like a better option. I have just set it up and was wondering what change to make for 240mb ram accounts. I've just put 240000 initially. Many thanks to seanf for the script and to insmalls for the how-to on cron / nano.
Terry

Offline

 

#13 2009-03-26 08:12:38

seanf
Administrator
Registered: 2008-02-01
Posts: 1899
Website

Re: Zope memory watchdog

Terry - you can go a tiny bit higher. 240 * 1024kb = 245760

Offline

 

#14 2009-03-26 12:45:08

edtaa
Member
From: Camberley, UK
Registered: 2009-03-26
Posts: 30
Website

Re: Zope memory watchdog

Thanks Sean. I haven't seen a restart as yet but will increase it anyway.

Offline

 

#15 2009-03-28 03:58:57

edtaa
Member
From: Camberley, UK
Registered: 2009-03-26
Posts: 30
Website

Re: Zope memory watchdog

Hi again Sean
Can you help me debug the zopewatch script? I've gone back to an exact copy of your original script at the start of this post and have checked the syntax in detail but have a couple of errors. I'm not a coder so apologise if this is very basic stuff. Errors are both in line 4:
{sum+=$1} END {print sum} : command not found
and
[ -gt: unary operator expected
I really need this script to work! Thanks for any help.

Offline

 

#16 2009-03-28 08:16:01

seanf
Administrator
Registered: 2008-02-01
Posts: 1899
Website

Re: Zope memory watchdog

You've edited your script in a text editor with word wrap enabled, so you've broken some of the statements into multiple lines. So, re-edit your script and ensure that it matches the one posted here, line-for-line.

Offline

 

#17 2009-03-28 11:53:05

edtaa
Member
From: Camberley, UK
Registered: 2009-03-26
Posts: 30
Website

Re: Zope memory watchdog

Thanks Sean. I got rid of word wrap and found a couple of errors in my code. The script works OK now.

Offline

 

#18 2010-01-07 12:24:13

edtaa
Member
From: Camberley, UK
Registered: 2009-03-26
Posts: 30
Website

Re: Zope memory watchdog

Hi again Sean. Hope you can advise me again! I'm finding that my website is becoming inaccessible a couple of times each day. I've got a feeling that this is linked to memory going over 200mb so I was wondering if a solution might be to restart my zope at a lower level than the maximum that I currently have - 245760. I've tried many other things such as reducing the ZODB (down from 900mb to 700mb), amending my robots.txt file to limit robot access. Getting a bit desperate here so any help gratefully received.

Offline

 

#19 2010-01-07 15:52:47

seanf
Administrator
Registered: 2008-02-01
Posts: 1899
Website

Re: Zope memory watchdog

I've checked our monitoring logs, and from what I see there we have not killed your processes recently.

I've checked your account services, and you have 240MB, so 245760 is the correct value. Using a lower value will mean that your Zope will restart more often, which is the opposite of what you want.

I can see in your ~/logs/user/mem.log files that the watchdog script *is* restarting your Zope a couple of times per day, which is what it's supposed to do when your memory usage goes too high. Having your script restart your Zope is better than having our monitors kill your processes, because in the latter case it means your Zope will not restart immediately.

Offline

 

#20 2010-01-07 17:02:05

edtaa
Member
From: Camberley, UK
Registered: 2009-03-26
Posts: 30
Website

Re: Zope memory watchdog

OK Sean. Thanks for looking at this. I'm running out of ideas for why the site becomes inaccessible a couple of times or so per day and runs normally for the rest of the time. If you have any ideas, I'd love to hear them.

Offline

 

#21 2010-01-07 17:12:50

seanf
Administrator
Registered: 2008-02-01
Posts: 1899
Website

Re: Zope memory watchdog

The site becomes inaccessible a couple of times per day because your memory watchdog script is restarting it a couple of times per day. Your site will not be accessible while Zope is restarting.

If you're trying to figure out why your memory usage is increasing, then you should try using a tool like http://code.google.com/p/zope-memory-readings/

Offline

 

#22 2010-01-08 02:30:49

edtaa
Member
From: Camberley, UK
Registered: 2009-03-26
Posts: 30
Website

Re: Zope memory watchdog

Hi again Sean and thanks for this. I'll give zope-memory-readings a go - where do I need to put the package? By the way, my website's down times are not especially linked to the restarting of Zope.

Offline

 

#23 2010-01-08 10:45:38

seanf
Administrator
Registered: 2008-02-01
Posts: 1899
Website

Re: Zope memory watchdog

zope-memory-readings is not a package - it's just  a script, so you can just download it and run it as described at http://code.google.com/p/zope-memory-readings/

If your website is down and you don't think it's happening due to a Zope restart, then please open a support ticket the next time it happens so we can take a closer look.

Offline

 

#24 2010-01-08 12:18:42

edtaa
Member
From: Camberley, UK
Registered: 2009-03-26
Posts: 30
Website

Re: Zope memory watchdog

Thanks Sean. The site is down again now. I have submitted two tickets about this over the past month. They tickets are Ticket ID: JDE-308562 and XEF-693664. I had some interesting advice from various people and have followed it all up in detail but the problem is still here. Any ideas?

Offline

 

#25 2010-01-08 12:27:57

seanf
Administrator
Registered: 2008-02-01
Posts: 1899
Website

Re: Zope memory watchdog

I've just checked the site you mentioned in those support tickets and it seems to be working for me. If you're still having issues with the site, then please either open a new ticket, or reply to one of your old tickets (since support issues like this are off-topic for this forum). When you do that, please let us know exactly what you mean when you say the site is down, eg any error messages, etc. Thanks!

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson