Sunday, February 13, 2011

Monitor rails instances of passenger in Nginx


A month back, we have faced an issue of memory out of space which leads the server crash. We have solved this issue with help of following script. This script is sending an kill signal  rails instance which is taking a more memory. Due to this crash will be get notified at client side.

This script is use full for monitoring rails instance of passenger. As other available tools are not able to monitor rails instance which is maintained by passenger.

It will monitor rails instance such that, it will kill rails instance which is taking more than 500MB and if rails instance has processed 200 requests.

After killing rails instance, passenger will automatically fork another rails instance if required.

The reason of restarting instance after certain requests, to keep memory available for other rails instances. Since I have found an articles which are saying "Rails expands the Ruby process so much that additional memory allocation grows much larger than we actually need, due to the exponential growth factor. And since MRI never gives back unused memory"

I have saw passenger has PassengerMaxRequests and PassengerMaxMemory (Not sure) for Apache server but not available for nginx.

My script will do the same thing for nginx :).


Open any file,
e.g
vi monitor_rails_instance
and paste following code in it.
 1 #!/bin/sh
 2 
 3 while true; do
 4 
 5   passenger-memory-stats | grep Rails:\ /home  | awk ' { if($2 > 500) print "kill -9 " $1}' | bash
 6   # above line will kill all the rails instances which are using memory more than 500MB
 7 
 8   passenger-status | grep Processed:  | awk ' { if($7 > 200) print "kill -6 " $3}' | bash
 9   # above line will abort all rails instance who have processed 200 request. 
10 
11   sleep 2
12 done
Then give execute permission to the file
sudo chmod +x monitor_rails_instance
And then run this script as super user
sudo ./monitor_rails_instance

Note:

If you want to run this script as background process on server
sudo nohup ./monitor_rails_instance

No comments:

Post a Comment