Monday, November 16, 2009

WebSite Monitoring Simple .!

First of all create one /opt/monitoring.rb and /home/sites.txt

require 'net/http'
require 'net/smtp'"/home/sites.txt").each { |line|
# get rid of CRLF

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 = ""
message = "From:\nSubject: #{url} Unavailable\n\n#{url} - #{res.code} - #{res.message}\nHTTP Version - #{res.http_version}\n\n"
Net::SMTP.start("smtp server",25,"domain name","username","passwd","login") do |smtp|
smtp.send_message(message, from, '')
rescue Exception => e
print "Exception occured: " + e

vi /home/sites.txt

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.!


Amitkumar 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.