Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: WebSite Monitoring made Simple !!!  (Read 1984 times)

0 Members and 1 Guest are viewing this topic.

Aby

  • Guest
WebSite Monitoring made Simple !!!
« on: January 09, 2014, 05:06:39 am »
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 = "xxxx@gmail.com"
message = "From: xxxx@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.!
« Last Edit: January 09, 2014, 05:19:43 am by Aby »