Monday, November 16, 2009

WebSite Monitoring Simple .!

First of all create one /opt/monitoring.rb and /home/sites.txt
-------------------------------------------------------------
#!/usr/bin/ruby

require 'net/http'
require 'net/smtp'


File.open("/home/sites.txt").each { |line|
# get rid of CRLF
line.chomp!

next if(line[0..0] == '#' || line.empty?)

# url, emails = line.split(' ')
# emails = emails.split(",")

url= line.split(' ')

# check if http:// was in the url if not add it in there
# url.insert(0, "http://") unless(url.match(/^http\:\/\//))

# Get the HTTP_RESPONSE from the site we are checking
res = Net::HTTP.get_response(URI.parse(url.to_s))

# Check the response code and send an email if the code is bad
unless(res.code =~ /2|3\d{2}/ ) then
from = "shrikant.lokhande@gmail.com"
message = "From: shrikant.lokhande@gmail.com\nSubject: #{url} Unavailable\n\n#{url} - #{res.code} - #{res.message}\nHTTP Version - #{res.http_version}\n\n"
begin
Net::SMTP.start("smtp server",25,"domain name","username","passwd","login") do |smtp|
smtp.send_message(message, from, 'youremail@gmail.com')
end
rescue Exception => e
print "Exception occured: " + e
end
end
}
-------------------------------------------

vi /home/sites.txt

http://www.website.com
-------------------------------------------

Add script in cron like :
* * * * * ruby /opt/monitoring.rb

from another server or same server. but prefer from another server. configure SMTP and add your email id in the script for alert.

Thats it.!

2 comments:

Unknown said...

Good Just Keep on writing by which a novice like us can also moulded into expert.

digital signage said...

Grateful to check out your website, I seem to be ahead to more excellent sites and I wish that you wrote more informative post for us. Well done work.