Showing posts with label ruby rails. Show all posts
Showing posts with label ruby rails. Show all posts

Thursday, January 29, 2009

Monitoring thins with monit...

Short story:
Download and install the latest code from http://mmonit.org/monit/download/ (configure, make, make install.. done! )

edit /etc/monitrc

# ========================================================
# Monit global settings:
# ========================================================
set daemon 60
set logfile syslog
set mailserver localhost
set mail-format { from: monit@cfandersen.com }
# ========================================================
# Monit http server settings:
# ========================================================
set httpd port 2812
allow localhost
INCLUDE "/etc/monit/thins.monitrc"

/etc/monit/thins.monitrc


check process thin-8000 with pidfile /app/shared/pids/thin.8000.pid
group rails
start program = "/usr/local/bin/thin -C /app/current/config/thin_cluster.yml start --only 8000"
stop program = "/usr/local/bin/thin -C /app/current/config/thin_cluster.yml stop --only 8000"
if failed host localhost port 8000 protocol HTTP
request "/token/monit" with timeout 60 seconds then restart
if cpu is greater than 60% for 2 cycles then alert
if cpu > 80% for 5 cycles then restart
if totalmem > 100.0 MB for 5 cycles then restart
if loadavg(5min) greater than 10 for 8 cycles then stop
if 5 restarts within 5 cycles then timeout
depends on thin_bin

check file thin_bin with path /usr/local/bin/thin
group rails
if failed checksum then unmonitor
if failed permission 755 then unmonitor
if failed uid root then unmonitor
if failed gid root then unmonitor

Thats the setup now the ISSUE!

Monit would NOT start thin, and the error I was looking at in the thin log was:

/usr/local/lib/ruby/gems/1.8/gems/rails-2.1.0/lib/rails_generator/lookup.rb:35:in `expand_path': couldn't find HOME environment -- expanding `~' (ArgumentError)
from /usr/local/lib/ruby/gems/1.8/gems/rails-2.1.0/lib/rails_generator/lookup.rb:35:in `user_home'
from /usr/local/lib/ruby/gems/1.8/gems/rails-2.1.0/lib/rails_generator/lookup.rb:114:in `use_component_sources!'
from /usr/local/lib/ruby/gems/1.8/gems/rails-2.1.0/lib/rails_generator/lookup.rb:55:in `included'
from /usr/local/lib/ruby/gems/1.8/gems/rails-2.1.0/lib/rails_generator.rb:38:in `include'
from /usr/local/lib/ruby/gems/1.8/gems/rails-2.1.0/lib/rails_generator.rb:38:in `send'
from /usr/local/lib/ruby/gems/1.8/gems/rails-2.1.0/lib/rails_generator.rb:38
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
... 26 levels...
from /usr/local/lib/ruby/gems/1.8/gems/thin-1.0.0/lib/thin/runner.rb:139:in `run!'
from /usr/local/lib/ruby/gems/1.8/gems/thin-1.0.0/bin/thin:6
from /usr/local/bin/thin:20:in `load'
from /usr/local/bin/thin:20

I did not want to have a "startup" script hanging around SO I added this in my /usr/local/bin/thin file:

ENV["HOME"] = "/tmp"

Problem solved!

Friday, September 26, 2008

Building a site map in Ruby on Rails...

I needed an automated sitemap process for the www.cadechristian.com website and here is what I managed to create...
route.rb:
map.sitemap 'sitemap.xml', :controller => 'sitemap', :action => 'sitemap'
robots.txt:
Sitemap: http://www.cadechristian.com/sitemap.xml

sitemap controller:
def sitemap
@model1 = Page.find(:all)
@model2 = Page.find(:all)
render :layout => false
end

/app/view/sitemap/sitemap.builder:
@home_url = 'http://www.cadechristian.com'

xml.instruct!
xml.urlset :xmlns => 'http://www.sitemaps.org/schemas/sitemap/0.9' do

# Add static Urls
%w( / /url1 /url2 /url3 ).each do |url|
xml.url do
xml.loc @home_url + url
xml.lastmod Time.today.xmlschema
xml.priority 1
end
end

@model1.each do |model|
xml.url do
xml.loc @home_url + model_path(model)
xml.lastmod product.updated_at.xmlschema
xml.priority 0.9
end
end

@model2.each do |model|
xml.url do
xml.loc @home_url + model_path(model)
xml.lastmod product.updated_at.xmlschema
xml.priority 0.9
end
end
end



That removes the need to update my sitemap.xml file when we add products and other static resources :)

You could also create a ruby script or some hook that runs this code:

require 'net/http'
require 'uri'
include ActionController::UrlWriter

sitemap_uri = 'www.cadechristian.com/sitemap.xml'
escaped_sitemap_uri = URI.escape(sitemap_uri)

%w( submissions.ask.com www.google.com ).each do |engine|
Net::HTTP.get(engine, '/ping?sitemap=' + escaped_sitemap_uri)
end
Net::HTTP.get('webmaster.live.com', '/ping.aspx?siteMap=' + escaped_sitemap_uri)

This would ping the search engines and you are done...