flying 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ flying.rb
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/README.markdown ADDED
@@ -0,0 +1,65 @@
1
+ Flying is a tool for monitoring websites and apps. It was designed to be as simple as Gemfiles.
2
+
3
+ ###Getting started
4
+
5
+ Install the gem:
6
+
7
+ gem install flying
8
+
9
+ Create a Ruby file anywhere with the following content:
10
+
11
+ ```ruby
12
+ # flying.rb
13
+ require "flying"
14
+
15
+ Flying.start do
16
+ site "http://www.google.com"
17
+ site "http://www.nytimes.com"
18
+ end
19
+ ```
20
+
21
+ Now, just run in your terminal:
22
+
23
+ ruby flying.rb
24
+
25
+ Whenever one of those two sites gets offline, Flying will warn you and stop monitoring.
26
+
27
+ ###Goal of the project
28
+
29
+ We want to be able to use the following DSL:
30
+
31
+ ```ruby
32
+ require "flying"
33
+
34
+ Flying.start do
35
+ # Defines the interval between verifications
36
+ timer 1
37
+
38
+ # Send an email if any error occured
39
+ notify_me :email => "email@gmail.com"
40
+
41
+ # When an error happens, monitoring stops. :on_error_continue
42
+ # makes Flying ignore the error and continue monitoring.
43
+ site "http://www.google.com", :as => :google, :on_error_continue
44
+
45
+ # If :google is down (above, :as => :google), Flying will notify the related services,
46
+ # e.g. 'www.mysite.com is probably not working properly because :google is offline'
47
+ site "http://www.mysite.com", :depends_on => :google
48
+
49
+ # Besides checking site availability, makes sure the response.body is a valid XML,
50
+ # ideal for making sure 3rd-party webservices are working.
51
+ site "http://www.fedex.com/webservice", :format => :xml
52
+
53
+ # Logs to the server via SSH, goes to a Rails app directory and do the following:
54
+ #
55
+ # - Reads production.log file and checks if any error happened (e.g. Errno... )
56
+ # - Looks for config/database.yml and tries connecting to all possible DBs, except
57
+ # 'development' and 'test'
58
+ rails_app "ssh://kurko@google.com", :path => "/var/rails/google"
59
+ end
60
+ ```
61
+
62
+ ###License
63
+
64
+ License is MIT. If you're gonna improve this project, please collaborate and send
65
+ a pull request.
data/lib/flying.rb CHANGED
@@ -1,5 +1,37 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
1
2
  require "flying/version"
3
+ require "flying/bots/up"
4
+
5
+ require "flying/aliases/aliases"
2
6
 
3
7
  module Flying
4
- # Your code goes here...
8
+ @an_error_ocurred = false
9
+ @first_attempt = true
10
+ @total_attempts = 0
11
+
12
+ def self.start(&block)
13
+ begin
14
+ puts "Running..."
15
+ looping = true
16
+ while(looping) do
17
+ yield
18
+ if @an_error_ocurred
19
+ puts "Stopped assessing targets."
20
+ break
21
+ end
22
+ if @first_attempt
23
+ puts "Everything's working fine. I'll continue monitoring the targets."
24
+ @first_attempt = false
25
+ end
26
+ @total_attempts += 1
27
+ sleep(5)
28
+ end
29
+ rescue Interrupt => e
30
+ end
31
+ end
32
+
33
+ def self.an_error_ocurred(value = '')
34
+ return @an_error_ocurred if value == ''
35
+ @an_error_ocurred = value
36
+ end
5
37
  end
@@ -0,0 +1,7 @@
1
+ module Kernel
2
+ def site(referer, *options)
3
+ process = Flying::Bot::Up.new
4
+ assessment = process.assess(referer, options)
5
+ puts process.message unless assessment
6
+ end
7
+ end
@@ -0,0 +1,62 @@
1
+ require "net/http"
2
+
3
+ module Flying
4
+ module Bot
5
+ class Up
6
+ attr_reader :message
7
+
8
+ def initialize
9
+ @message = "Ok."
10
+ end
11
+
12
+ def assess(referer, *options)
13
+ begin
14
+ response_code = get_http_response_code(referer)
15
+ rescue
16
+ Flying.an_error_ocurred(true)
17
+ set_error_message(referer, false, $!)
18
+ return false
19
+ end
20
+ return true if ["200", "302"].include? response_code
21
+ Flying.an_error_ocurred(true)
22
+ set_error_message(referer, response_code.to_s)
23
+ false
24
+ end
25
+
26
+ def set_error_message(referer, response_code, error_details = '')
27
+ case response_code
28
+ when false
29
+ @message = message_unreachable
30
+ when "404"
31
+ @message = message_not_found
32
+ when "501"
33
+ @message = message_server_error
34
+ else
35
+ @message = message_unknown_error + "(#{error_details})"
36
+ end
37
+ @message = referer + ": " + @message
38
+ end
39
+
40
+ def get_http_response_code referer
41
+ Net::HTTP.get_response(URI(referer)).code
42
+ end
43
+
44
+ # Messages
45
+ def message_unknown_error
46
+ "An unknown error ocurred."
47
+ end
48
+
49
+ def message_unreachable
50
+ "It is unreachable (is the url correct?)."
51
+ end
52
+
53
+ def message_not_found
54
+ "The target was simply not found (404)."
55
+ end
56
+
57
+ def message_server_error
58
+ "We got a response saying there's a server error (501)"
59
+ end
60
+ end
61
+ end
62
+ end
@@ -1,3 +1,3 @@
1
1
  module Flying
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,20 @@
1
+ require "spec_helper"
2
+
3
+ describe Flying::Bot::Up do
4
+ subject { Flying::Bot::Up.new }
5
+
6
+ context "up" do
7
+ it "checks if site is up" do
8
+ subject.stub(:get_http_response_code).and_return("302")
9
+ subject.assess("http://www.google.com").should be_true
10
+ end
11
+ end
12
+
13
+ context "down" do
14
+ it "returns false if 404 and saves a message" do
15
+ subject.stub(:get_http_response_code).and_return("404")
16
+ subject.assess("http://www.google.com").should be_false
17
+ subject.message.should == "http://www.google.com: The target was simply not found (404)."
18
+ end
19
+ end
20
+ end
@@ -0,0 +1 @@
1
+ require File.expand_path("../../lib/flying.rb", __FILE__)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flying
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-25 00:00:00.000000000 Z
12
+ date: 2011-11-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70243805079340 !ruby/object:Gem::Requirement
16
+ requirement: &70101949261140 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70243805079340
24
+ version_requirements: *70101949261140
25
25
  description: Verifies if each given server is not down.
26
26
  email:
27
27
  - chavedomundo@gmail.com
@@ -30,11 +30,17 @@ extensions: []
30
30
  extra_rdoc_files: []
31
31
  files:
32
32
  - .gitignore
33
+ - .rspec
33
34
  - Gemfile
35
+ - README.markdown
34
36
  - Rakefile
35
37
  - flying.gemspec
36
38
  - lib/flying.rb
39
+ - lib/flying/aliases/aliases.rb
40
+ - lib/flying/bots/up.rb
37
41
  - lib/flying/version.rb
42
+ - spec/bots/up_spec.rb
43
+ - spec/spec_helper.rb
38
44
  homepage: https://github.com/kurko/flying
39
45
  licenses: []
40
46
  post_install_message:
@@ -59,4 +65,6 @@ rubygems_version: 1.8.11
59
65
  signing_key:
60
66
  specification_version: 3
61
67
  summary: Verifies if services are up and running.
62
- test_files: []
68
+ test_files:
69
+ - spec/bots/up_spec.rb
70
+ - spec/spec_helper.rb