Skip to content

Instantly share code, notes, and snippets.

@amirkdv
Last active November 20, 2015 19:33
Embed
What would you like to do?
Simple HTTP monitor

POC simple HTTP monitoring script. First build the docker images:

git clone [this-gist] monitor
cd monitor
docker build -t amirkdv/monitor .

Now start the container, and all sites under sites.yaml will be pinged one after another:

docker run -i -t amirkdv/monitor
# Example 1 (immediate 200):
#   connecting to http://www.bbc.co.uk/
#   final HTTP code: 200
#   ---> UP
# Example 2 (DNS resolves, but site is down):
#   connecting to http://muhc-dev.tree.ewdev.ca/
#   Timeout
#   ---> DOWN: sending e-mail to admin@example.com
# Example 3 (DNS does not resolve):
#   connecting to http://nonexiting.muhc.ca
#   Timeout
#   ---> DOWN: sending e-mail to admin@example.com
# Example 4 (requires redirection to get to 200):
#   connecting to http://google.ca
#   redirecting (HTTP code: 301)
#   final HTTP code: 200
#   ---> UP
# Example 5 (URL contains query):
#   connecting to http://reuters-demo.tree.ewdev.ca:9090/reuters/select?q=a
#   final HTTP code: 200
#   ---> UP
# Example 6 (URL contains illegal query resulting in 403):
#   connecting to http://reuters-demo.tree.ewdev.ca:9090/reuters/
#   final HTTP code: 403
#   ---> DOWN: sending e-mail to admin@example.com
# Example 7 (HTTP basic auth):
#   connecting to http://sync-dev.tree.ewdev.ca
#   final HTTP code: 200
#   ---> UP
# Example 8 (HTTP basic auth, invalid credentials):
#   connecting to http://sync-dev.tree.ewdev.ca
#   final HTTP code: 401
#   ---> DOWN: sending e-mail to admin@example.com
FROM ubuntu:12.04
ENV DEBIAN_FRONTEND noninteractive
# RUN echo 'Acquire::http::Proxy "http://172.17.42.1:8000";' > /etc/apt/apt.conf.d/30squid-deb-proxy
# Install Ruby
RUN apt-get update
RUN apt-get install -y ruby-full build-essential libxml2-dev libxslt-dev
# Install Exim
RUN echo "exim4-config exim4/dc_eximconfig_configtype select internet site; mail is sent and received directly using SMTP" | debconf-set-selections
RUN apt-get install -y exim4
# configure container
RUN mkdir -p /var/monitor
ADD . /var/monitor/
WORKDIR /var/monitor
CMD ["ruby", "/var/monitor/monitor.rb"]
#!/usr/bin/env ruby
require 'yaml'
require 'net/http'
require 'timeout'
SITES_DEF = './sites.yaml'
INTERVAL = 5*60
ADMIN = 'someone@example.com'
def ping(site)
uri = URI.parse(site['URI'])
http = Net::HTTP.new(uri.host, uri.port)
req = Net::HTTP::Get.new(uri.request_uri)
if credentials = site['credentials']
req.basic_auth(credentials['username'], credentials['password'])
end
http.request(req)
end
def monitor
sites = YAML.load_file(SITES_DEF)
sites.each do |site|
puts site['name'] + ':'
available = false
begin
Timeout.timeout(10) do
puts " connecting to #{site['URI']}"
res = ping(site)
while (res.code == '302' || res.code == '301')
puts " redirecting (HTTP code: #{res.code})"
site['URI'] = res.header['location']
res = ping(site)
end
available = (res.code =='200')
puts " final HTTP code: #{res.code}"
end
rescue Timeout::Error
puts " Timeout"
rescue SocketError => e
puts " Uri not resolved"
end
if available
puts " ---> UP"
else
puts " ---> DOWN: sending e-mail to #{ADMIN}"
mail_log = site['name'].gsub(/[^A-Za-z0-9-]/, '_').downcase + '_mail.log'
cmd = "echo 'Subject: #{site['name']} is down' | sendmail -v #{ADMIN} >> #{mail_log} 2>&1"
system(cmd)
end
end
end
loop do
monitor
sleep INTERVAL
end
- name: Example 1 (immediate 200)
URI: http://www.bbc.co.uk/
- name: Example 2 (DNS resolves, but site is down)
URI: http://muhc-dev.tree.ewdev.ca/
- name: Example 3 (DNS does not resolve)
URI: http://nonexiting.muhc.ca
- name: Example 4 (requires redirection to get to 200)
URI: http://google.ca
- name: Example 5 (URL contains query)
URI: http://reuters-demo.tree.ewdev.ca:9090/reuters/select?q=a
- name: Example 6 (URL contains illegal query resulting in 403)
URI: http://reuters-demo.tree.ewdev.ca:9090/reuters/
- name: Example 7 (HTTP basic auth)
URI: http://sync-dev.tree.ewdev.ca
credentials:
username: evolvingweb
password: evolvingweb
- name: Example 8 (HTTP basic auth, invalid credentials)
URI: http://sync-dev.tree.ewdev.ca
credentials:
username: incorrect
password: incorrect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment