Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 66 lines (56 sloc) 1.542 kb
f61f2d7 @stevenwilkin load config from file
authored
1 #!/usr/bin/env ruby
2
3 require 'rubygems'
4 require 'yaml'
9654543 @stevenwilkin add message method
authored
5 require 'xmpp4r-simple'
ee3179d @stevenwilkin add main loop
authored
6 require 'open-uri'
7 require 'timeout'
f61f2d7 @stevenwilkin load config from file
authored
8
ef9d754 @stevenwilkin add domain checking method
authored
9 TIMEOUT = 10 # max time to attempt to access each domain
10
5ee681e @stevenwilkin add a few more comments
authored
11 $user = nil #
12 $pass = nil # credentials for sending XMPP account
13 $recipient = nil # recieving XMPP account
14 $domains = [] # list of domains to check
f61f2d7 @stevenwilkin load config from file
authored
15
5ee681e @stevenwilkin add a few more comments
authored
16 # load configuration settings from YAML
eea6c01 @stevenwilkin put config loading into method and call from new main method
authored
17 def load_config
18 config_file = File.expand_path(File.join(File.dirname(__FILE__), 'minder.yaml'))
19 config = YAML::load(File.read(config_file))
20 # populate globals
21 $user = config['xmpp_from']['user']
22 $pass = config['xmpp_from']['pass']
23 $recipient = config['xmpp_to']
24 $domains = config['domains']
25 end
f61f2d7 @stevenwilkin load config from file
authored
26
5ee681e @stevenwilkin add a few more comments
authored
27 # send an XMPP message to the recipient address
9654543 @stevenwilkin add message method
authored
28 def message(msg)
29 jabber = Jabber::Simple.new($user, $pass)
30 jabber.deliver($recipient, msg)
31 sleep 1
32 end
33
5ee681e @stevenwilkin add a few more comments
authored
34 # can the domain be read within the maximum time allowed?
ef9d754 @stevenwilkin add domain checking method
authored
35 def can_read_domain?(domain)
36 print "Checking #{domain}: "
37 url = "http://#{domain}/"
38 begin
39 Timeout::timeout(TIMEOUT) do
40 open(url, 'Cache-Control' => 'no-cache').read
41 end
22bce9e @stevenwilkin catch http related exceptions
authored
42 rescue Timeout::Error # couldn't access domain within timeout period
43 puts 'down!'
44 false
45 rescue OpenURI::HTTPError # all other http exceptions
ef9d754 @stevenwilkin add domain checking method
authored
46 puts 'down!'
47 false
48 else
49 puts 'ok'
50 true
51 end
52 end
53
ee3179d @stevenwilkin add main loop
authored
54 # attempt to access each domain, send XMPP message if can't
eea6c01 @stevenwilkin put config loading into method and call from new main method
authored
55 def main
56 load_config
ee3179d @stevenwilkin add main loop
authored
57 $domains.each do |domain|
58 unless can_read_domain? domain
59 message "#{domain} is down!"
60 end
61 end
eea6c01 @stevenwilkin put config loading into method and call from new main method
authored
62 end
63
64
65 main if __FILE__ == $0
Something went wrong with that request. Please try again.