sr71 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +4 -0
- data/.rspec +1 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/LICENSE +19 -0
- data/Rakefile +7 -0
- data/bin/sr71 +6 -0
- data/file +26 -0
- data/lib/sr71.rb +31 -0
- data/lib/sr71/cli.rb +38 -0
- data/lib/sr71/configuration.rb +9 -0
- data/lib/sr71/http_check.rb +58 -0
- data/lib/sr71/logger.rb +9 -0
- data/lib/sr71/service.rb +18 -0
- data/lib/sr71/ticker.rb +13 -0
- data/lib/sr71/version.rb +3 -0
- data/spec/spec_helper.rb +35 -0
- data/spec/sr71/configuration_spec.rb +17 -0
- data/spec/sr71/http_check_spec.rb +12 -0
- data/spec/sr71/logger_spec.rb +17 -0
- data/spec/sr71/service_spec.rb +21 -0
- data/spec/sr71/ticker_spec.rb +29 -0
- data/spec/support/stub_server.rb +28 -0
- data/spec/support/test_config.yml +7 -0
- data/sr71.gemspec +25 -0
- metadata +112 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color --format documentation
|
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.3-p194@sr71 --create
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2012 Michael Pilat
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
7
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
8
|
+
so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
data/Rakefile
ADDED
data/bin/sr71
ADDED
data/file
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Sr71::Service do
|
4
|
+
describe "needs_check?" do
|
5
|
+
it "is true when the check interval has elapsed" do
|
6
|
+
check = Sr71::Service.new("url" => "http://www.example.com", "interval" => 30)
|
7
|
+
check.needs_check?.should be_true
|
8
|
+
|
9
|
+
Time.stub(:now).and_return(15)
|
10
|
+
check.needs_check?.should be_false
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "start_check" do
|
15
|
+
it "makes an HTTP request to the url" do
|
16
|
+
server = nil
|
17
|
+
EM.run do
|
18
|
+
server = StubServer.new(http_response("200 OK")) { EM.stop }
|
19
|
+
|
20
|
+
check = Sr71::Service.new("url" => "http://127.0.0.1:8081", "interval" => 30)
|
21
|
+
check.start_check
|
22
|
+
end
|
23
|
+
server.request_count.should == 1
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/sr71.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require "eventmachine"
|
2
|
+
require "em-http-request"
|
3
|
+
|
4
|
+
require "sr71/configuration"
|
5
|
+
require "sr71/http_check"
|
6
|
+
require "sr71/logger"
|
7
|
+
require "sr71/service"
|
8
|
+
require "sr71/ticker"
|
9
|
+
require "sr71/version"
|
10
|
+
|
11
|
+
module Sr71
|
12
|
+
class << self
|
13
|
+
attr_accessor :services
|
14
|
+
end
|
15
|
+
|
16
|
+
self.services = []
|
17
|
+
|
18
|
+
def self.configure(options)
|
19
|
+
@config_file = options[:sr71_config]
|
20
|
+
@config = Configuration.from_file(@config_file)
|
21
|
+
@services = @config.map do | (name, attributes) |
|
22
|
+
Sr71::Service.new(attributes)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.start!
|
27
|
+
EM.run do
|
28
|
+
Ticker.start!
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/sr71/cli.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
|
3
|
+
module Sr71
|
4
|
+
class CLI
|
5
|
+
class Options
|
6
|
+
def parse!(args)
|
7
|
+
args, options = args.dup, {}
|
8
|
+
|
9
|
+
options[:sr71_config] = '/etc/sr71.yml'
|
10
|
+
|
11
|
+
opt_parser = OptionParser.new do |opts|
|
12
|
+
opts.banner = "Usage: sr71 [options]"
|
13
|
+
opts.on("-c", "--config=file", String, "SR71 configuration file") { |v| options[:sr71_config] = v }
|
14
|
+
|
15
|
+
opts.separator ""
|
16
|
+
|
17
|
+
opts.on("-h", "--help", "Show this help message.") { puts opts; exit }
|
18
|
+
end
|
19
|
+
|
20
|
+
opt_parser.parse! args
|
21
|
+
|
22
|
+
options
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def start
|
27
|
+
options = Options.new.parse!(ARGV)
|
28
|
+
|
29
|
+
unless File.exists?(options[:sr71_config])
|
30
|
+
puts "Could not find #{options[:sr71_config]}. Specify correct location with -c file"
|
31
|
+
exit 1
|
32
|
+
end
|
33
|
+
|
34
|
+
Sr71.configure(options)
|
35
|
+
Sr71.start!
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Sr71
|
2
|
+
class HttpCheck
|
3
|
+
class ConnectionTimer
|
4
|
+
attr_reader :connect_time
|
5
|
+
def initialize(&block)
|
6
|
+
@callback = block
|
7
|
+
end
|
8
|
+
|
9
|
+
def request(client, head, body)
|
10
|
+
@callback.call
|
11
|
+
[head, body]
|
12
|
+
end
|
13
|
+
|
14
|
+
def response(resp)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.check(url)
|
19
|
+
request = EventMachine::HttpRequest.new(url, {
|
20
|
+
:connect_timeout => 5,
|
21
|
+
:inactivity_timeout => 30
|
22
|
+
})
|
23
|
+
|
24
|
+
connect_time = nil
|
25
|
+
request.use ConnectionTimer do
|
26
|
+
connect_time = Time.now
|
27
|
+
end
|
28
|
+
|
29
|
+
start_time = Time.now
|
30
|
+
http = request.get
|
31
|
+
|
32
|
+
http.callback do
|
33
|
+
end_time = Time.now
|
34
|
+
Sr71::Logger.log({
|
35
|
+
:type => "http_check",
|
36
|
+
:start_time => start_time,
|
37
|
+
:connect_time => connect_time - start_time,
|
38
|
+
:elapsed_time => end_time - start_time,
|
39
|
+
:end_time => end_time.to_f,
|
40
|
+
:url => url,
|
41
|
+
:status_code => http.response_header.status
|
42
|
+
})
|
43
|
+
end
|
44
|
+
|
45
|
+
http.errback do
|
46
|
+
end_time = Time.now
|
47
|
+
Sr71::Logger.log({
|
48
|
+
:type => "http_check",
|
49
|
+
:start_time => start_time,
|
50
|
+
:elapsed_time => end_time - start_time,
|
51
|
+
:end_time => end_time.to_f,
|
52
|
+
:url => url,
|
53
|
+
:error => http.error
|
54
|
+
})
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/lib/sr71/logger.rb
ADDED
data/lib/sr71/service.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Sr71
|
2
|
+
class Service
|
3
|
+
def initialize(attrs = {})
|
4
|
+
@url = attrs["url"]
|
5
|
+
@interval = attrs["interval"]
|
6
|
+
@last_check = 0
|
7
|
+
end
|
8
|
+
|
9
|
+
def needs_check?
|
10
|
+
(@last_check + @interval) <= Time.now.to_f
|
11
|
+
end
|
12
|
+
|
13
|
+
def start_check
|
14
|
+
@last_check = Time.now.to_f
|
15
|
+
HttpCheck.check(@url)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/sr71/ticker.rb
ADDED
data/lib/sr71/version.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'sr71'
|
2
|
+
require 'rspec'
|
3
|
+
|
4
|
+
Dir.glob("#{File.expand_path('support', File.dirname(__FILE__))}/**/*.rb").each { |f| require f }
|
5
|
+
|
6
|
+
TEST_CONFIG = File.expand_path('support/test_config.yml', File.dirname(__FILE__))
|
7
|
+
|
8
|
+
def http_response(string)
|
9
|
+
return <<-RESPONSE
|
10
|
+
HTTP/1.1 #{string}
|
11
|
+
Connection: close
|
12
|
+
|
13
|
+
OK
|
14
|
+
RESPONSE
|
15
|
+
end
|
16
|
+
|
17
|
+
def run_in_reactor(timeout = 1)
|
18
|
+
Timeout::timeout(timeout) do
|
19
|
+
EM.epoll
|
20
|
+
EM.run do
|
21
|
+
yield
|
22
|
+
end
|
23
|
+
end
|
24
|
+
rescue Timeout::Error
|
25
|
+
fail 'Eventmachine was not stopped before the timeout expired'
|
26
|
+
end
|
27
|
+
|
28
|
+
def capturing_stdout(&block)
|
29
|
+
old_stdout = $stdout
|
30
|
+
$stdout = StringIO.new
|
31
|
+
yield
|
32
|
+
result = $stdout.string
|
33
|
+
$stdout = old_stdout
|
34
|
+
result
|
35
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Sr71::Configuration do
|
4
|
+
describe "from_file" do
|
5
|
+
it "reads configuration from a file" do
|
6
|
+
config = Sr71::Configuration.from_file(TEST_CONFIG)
|
7
|
+
|
8
|
+
config["google"].should_not be_nil
|
9
|
+
config["google"]["url"].should == "http://www.google.com"
|
10
|
+
config["google"]["interval"].should == 30
|
11
|
+
|
12
|
+
config["example"].should_not be_nil
|
13
|
+
config["example"]["url"].should == "http://www.example.com"
|
14
|
+
config["example"]["interval"].should == 60
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Sr71::HttpCheck do
|
4
|
+
describe "check" do
|
5
|
+
it "makes a get request to the URL" do
|
6
|
+
run_in_reactor(5) do
|
7
|
+
server = StubServer.new(http_response("200 OK"), 0.25) { EM.stop }
|
8
|
+
Sr71::HttpCheck.check("http://127.0.0.1:8081")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Sr71::Logger do
|
4
|
+
describe "log" do
|
5
|
+
it "logs a request as JSON" do
|
6
|
+
output = capturing_stdout do
|
7
|
+
Sr71::Logger.log({
|
8
|
+
:response => "200 OK",
|
9
|
+
:foo => "bar",
|
10
|
+
:baz => "blah"
|
11
|
+
})
|
12
|
+
end
|
13
|
+
|
14
|
+
output.should == "{ \"response\": \"200 OK\", \"foo\": \"bar\", \"baz\": \"blah\" }\n"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Sr71::Service do
|
4
|
+
describe "needs_check?" do
|
5
|
+
it "is true when the check interval has elapsed" do
|
6
|
+
check = Sr71::Service.new("url" => "http://www.example.com", "interval" => 30)
|
7
|
+
check.needs_check?.should be_true
|
8
|
+
|
9
|
+
Time.stub(:now).and_return(15)
|
10
|
+
check.needs_check?.should be_false
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "start_check" do
|
15
|
+
it "makes an HTTP request to the url" do
|
16
|
+
service = Sr71::Service.new("url" => "http://127.0.0.1:8081", "interval" => 30)
|
17
|
+
Sr71::HttpCheck.should_receive(:check).with("http://127.0.0.1:8081")
|
18
|
+
service.start_check
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Sr71::Ticker do
|
4
|
+
describe "start!" do
|
5
|
+
it "schedules to run every tenth of a second" do
|
6
|
+
EventMachine::PeriodicTimer.should_receive(:new).with(0.1, kind_of(Method))
|
7
|
+
Sr71::Ticker.start!
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "tick" do
|
12
|
+
it "calls start_service for each service" do
|
13
|
+
service = Sr71::Service.new("url" => "http://www.example.com", "interval" => 30)
|
14
|
+
Sr71.services = [service]
|
15
|
+
|
16
|
+
service.should_receive(:start_check)
|
17
|
+
Sr71::Ticker.tick
|
18
|
+
end
|
19
|
+
|
20
|
+
it "does not call start_service if a service is not needed" do
|
21
|
+
service = Sr71::Service.new("url" => "http://www.example.com", "interval" => 30)
|
22
|
+
service.stub(:needs_check?).and_return(false)
|
23
|
+
Sr71.services = [service]
|
24
|
+
|
25
|
+
service.should_receive(:start_check).never
|
26
|
+
Sr71::Ticker.tick
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class StubServer
|
2
|
+
attr_reader :request_count
|
3
|
+
|
4
|
+
module Server
|
5
|
+
attr_accessor :response, :delay, :callback
|
6
|
+
|
7
|
+
def receive_data(data)
|
8
|
+
EM.add_timer(@delay) do
|
9
|
+
send_data @response
|
10
|
+
close_connection_after_writing
|
11
|
+
callback.call
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize (response, delay = 0, port = 8081, host = "127.0.0.1", &block)
|
17
|
+
@sig = EventMachine::start_server(host, port, Server) do |s|
|
18
|
+
s.response = response
|
19
|
+
s.delay = delay
|
20
|
+
s.callback = block
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def stop
|
25
|
+
EventMachine.stop_server @sig
|
26
|
+
EM.stop
|
27
|
+
end
|
28
|
+
end
|
data/sr71.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "sr71/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "sr71"
|
7
|
+
s.version = Sr71::VERSION
|
8
|
+
s.authors = ["Michael Pilat"]
|
9
|
+
s.email = ["mike@mikepilat.com"]
|
10
|
+
s.homepage = "http://mikepilat.com/"
|
11
|
+
s.summary = %q{A remote service monitor}
|
12
|
+
s.description = %q{A remote service monitor}
|
13
|
+
|
14
|
+
s.rubyforge_project = "sr71"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency "eventmachine", "~> 1.0.0"
|
22
|
+
s.add_dependency "em-http-request", "~> 1.0"
|
23
|
+
|
24
|
+
s.add_development_dependency "rspec", "~> 2.11.0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sr71
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Michael Pilat
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: eventmachine
|
16
|
+
requirement: &70343863968620 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70343863968620
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: em-http-request
|
27
|
+
requirement: &70343863968120 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '1.0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70343863968120
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
38
|
+
requirement: &70343863967620 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 2.11.0
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70343863967620
|
47
|
+
description: A remote service monitor
|
48
|
+
email:
|
49
|
+
- mike@mikepilat.com
|
50
|
+
executables:
|
51
|
+
- sr71
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- .gitignore
|
56
|
+
- .rspec
|
57
|
+
- .rvmrc
|
58
|
+
- Gemfile
|
59
|
+
- LICENSE
|
60
|
+
- Rakefile
|
61
|
+
- bin/sr71
|
62
|
+
- file
|
63
|
+
- lib/sr71.rb
|
64
|
+
- lib/sr71/cli.rb
|
65
|
+
- lib/sr71/configuration.rb
|
66
|
+
- lib/sr71/http_check.rb
|
67
|
+
- lib/sr71/logger.rb
|
68
|
+
- lib/sr71/service.rb
|
69
|
+
- lib/sr71/ticker.rb
|
70
|
+
- lib/sr71/version.rb
|
71
|
+
- spec/spec_helper.rb
|
72
|
+
- spec/sr71/configuration_spec.rb
|
73
|
+
- spec/sr71/http_check_spec.rb
|
74
|
+
- spec/sr71/logger_spec.rb
|
75
|
+
- spec/sr71/service_spec.rb
|
76
|
+
- spec/sr71/ticker_spec.rb
|
77
|
+
- spec/support/stub_server.rb
|
78
|
+
- spec/support/test_config.yml
|
79
|
+
- sr71.gemspec
|
80
|
+
homepage: http://mikepilat.com/
|
81
|
+
licenses: []
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ! '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project: sr71
|
100
|
+
rubygems_version: 1.8.15
|
101
|
+
signing_key:
|
102
|
+
specification_version: 3
|
103
|
+
summary: A remote service monitor
|
104
|
+
test_files:
|
105
|
+
- spec/spec_helper.rb
|
106
|
+
- spec/sr71/configuration_spec.rb
|
107
|
+
- spec/sr71/http_check_spec.rb
|
108
|
+
- spec/sr71/logger_spec.rb
|
109
|
+
- spec/sr71/service_spec.rb
|
110
|
+
- spec/sr71/ticker_spec.rb
|
111
|
+
- spec/support/stub_server.rb
|
112
|
+
- spec/support/test_config.yml
|