status_checker 0.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +61 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/status_checker.rb +3 -0
- data/lib/status_checker/check.rb +76 -0
- data/lib/status_checker/email.rb +43 -0
- data/lib/status_checker/timer.rb +38 -0
- data/lib/status_checker/version.rb +3 -0
- data/status_checker.gemspec +39 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 248481e6ef92adc986114ee52bddb33711ff9356
|
4
|
+
data.tar.gz: 1ffd716b0f03bb52bcc304afadee31e69b065b39
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b6842ffa75bd092ffb91fc819edc1556c5568a6dfb7d8834cf0bbffbf432581ec56f8372cc008eaf629f04c2af951e7f16a7eb0238ba7b40fbdcc0f5b8a8c7d4
|
7
|
+
data.tar.gz: b5e2d3bd0991a52aa9022fe1b53bc32ca12c1d5bb251b6dbab7000d4b8c67fe08e51d46e792e7e12cde1f068ad9ddff93aaa9707f89e2925df7d02cb9e845a83
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 andgursky
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# StatusChecker
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/status_checker`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
StatusChecker gem is allows you to check http status of any web site and get the alert message to your e-mail box if it's response code not 200.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'status_checker'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install status_checker
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
In console:
|
26
|
+
|
27
|
+
$ bundle exec bin/console
|
28
|
+
$ checker = StatusChecker::Check.new(["youremail@mail.com"], 60, ["https://site1.com", "http://site2.com"])
|
29
|
+
|
30
|
+
All arguments has default values, but if you want to change them, go ahead :)
|
31
|
+
|
32
|
+
$ checker.start
|
33
|
+
|
34
|
+
This method runs loop with checking response codes process. You'll receive message to youremail@mail.com if status code of any site you provided (site1 or site2) will be different from 200.
|
35
|
+
If you want, you can stop the loop:
|
36
|
+
|
37
|
+
$ checker.stop
|
38
|
+
|
39
|
+
By the way, you dont have to run loop for checking web site every time. You can check it at onse:
|
40
|
+
|
41
|
+
$ checker.check_url("https://example.com")
|
42
|
+
|
43
|
+
and it returns an array with url, response code and response message:
|
44
|
+
|
45
|
+
$ ["https://example.com", "200", "OK"]
|
46
|
+
|
47
|
+
## Development
|
48
|
+
|
49
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
50
|
+
|
51
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
52
|
+
|
53
|
+
## Contributing
|
54
|
+
|
55
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/status_checker.
|
56
|
+
|
57
|
+
|
58
|
+
## License
|
59
|
+
|
60
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
61
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "status_checker"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require "status_checker/timer"
|
2
|
+
require "status_checker/email"
|
3
|
+
require "net/https"
|
4
|
+
require "uri"
|
5
|
+
|
6
|
+
module StatusChecker
|
7
|
+
class Check
|
8
|
+
|
9
|
+
def initialize(email=["technical_root@mail.ru"], per=10,
|
10
|
+
domain=["https://ukr.net", "https://yandex.ru"])
|
11
|
+
# initialize all instanse variables
|
12
|
+
extend MonitorMixin
|
13
|
+
@email = email
|
14
|
+
@delay = per
|
15
|
+
@domain = domain
|
16
|
+
@timer = StatusChecker::Timer.new(@delay)
|
17
|
+
@resp_stack = Hash.new
|
18
|
+
end
|
19
|
+
|
20
|
+
def start
|
21
|
+
# run checker loop
|
22
|
+
threads = []
|
23
|
+
@timer.start do
|
24
|
+
@domain.each do |dom|
|
25
|
+
threads << Thread.new(dom) do |url|
|
26
|
+
check_url(url)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
threads.each { |thr| thr.join }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def stop
|
34
|
+
# stop checker loop
|
35
|
+
@timer.stop
|
36
|
+
end
|
37
|
+
|
38
|
+
def check_url(url)
|
39
|
+
response = check_http_status_of(url)
|
40
|
+
if response.code != 200.to_s && !recurrent_code?(url, response.code)
|
41
|
+
add_code_to_resp_stack(response.code, url)
|
42
|
+
send_email(url, response)
|
43
|
+
end
|
44
|
+
[url, response.code, response.message]
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def check_http_status_of(url)
|
50
|
+
uri = URI.parse(url)
|
51
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
52
|
+
http.use_ssl = true if uri.scheme == "https"
|
53
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
54
|
+
http.request(request)
|
55
|
+
end
|
56
|
+
|
57
|
+
def recurrent_code?(url, resp_code)
|
58
|
+
synchronize do
|
59
|
+
@resp_stack.each_pair do |key, val|
|
60
|
+
return true if key==resp_code && val==url
|
61
|
+
end
|
62
|
+
end
|
63
|
+
return false
|
64
|
+
end
|
65
|
+
|
66
|
+
def add_code_to_resp_stack(code, url)
|
67
|
+
synchronize do
|
68
|
+
@resp_stack.merge!("#{code}" => url)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def send_email(url, resp)
|
73
|
+
StatusChecker::Email.new(@email).send(url, resp)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'net/smtp'
|
2
|
+
require 'pry'
|
3
|
+
|
4
|
+
module StatusChecker
|
5
|
+
class Email
|
6
|
+
def initialize(receivers)
|
7
|
+
raise ArgumentError, "receivers can't be empty" if receivers.empty?
|
8
|
+
@receivers = receivers
|
9
|
+
@addres = 'smtp.mail.ru'
|
10
|
+
@port = 465
|
11
|
+
@domain = 'mail.ru'
|
12
|
+
@authentication = :plain
|
13
|
+
@user_name = 'technical_root@mail.ru'
|
14
|
+
@password = 'qwerty123456'
|
15
|
+
@smtp = Net::SMTP.new(@addres, @port)
|
16
|
+
@smtp.enable_tls
|
17
|
+
end
|
18
|
+
|
19
|
+
def send(url, response)
|
20
|
+
begin
|
21
|
+
@smtp.start(@domain, @user_name, @password, @authentication)
|
22
|
+
@receivers.each do |email|
|
23
|
+
@smtp.send_message message(url, response, email), @user_name, email
|
24
|
+
end
|
25
|
+
ensure
|
26
|
+
@smtp.finish
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def message(url, response, email)
|
33
|
+
"From:<#{@user_name}>\n" \
|
34
|
+
"To:<#{email}>\n" \
|
35
|
+
"MIME-Version: 1.0\n" \
|
36
|
+
"Content-type: text/html\n" \
|
37
|
+
"Subject: Attention!!!\n" \
|
38
|
+
"<h1 style='color:red'><b>!!!ATTENTION!!!</b></h1>\n" \
|
39
|
+
"<p>The host #{url} #{response.message} and has " \
|
40
|
+
"<b>code: #{response.code}</b></p>"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module StatusChecker
|
2
|
+
class Timer
|
3
|
+
def initialize(interval)
|
4
|
+
raise ArgumentError, "Illegal interval" if interval < 0
|
5
|
+
extend MonitorMixin
|
6
|
+
@run = nil
|
7
|
+
@th = nil
|
8
|
+
@delay = interval
|
9
|
+
end
|
10
|
+
|
11
|
+
def start(&handler)
|
12
|
+
@run = true
|
13
|
+
@th = Thread.new do
|
14
|
+
t = Time.now
|
15
|
+
while run?
|
16
|
+
t += @delay
|
17
|
+
(handler.call rescue nil) and
|
18
|
+
sleep(t - Time.now) rescue nil
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def stop
|
24
|
+
synchronize do
|
25
|
+
@run = false
|
26
|
+
end
|
27
|
+
@th.join
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def run?
|
33
|
+
synchronize do
|
34
|
+
@run
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'status_checker/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "status_checker"
|
8
|
+
spec.version = StatusChecker::VERSION
|
9
|
+
spec.authors = ["andgursky"]
|
10
|
+
spec.email = ["andgursky@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{ HTTP status checker }
|
13
|
+
spec.description = <<-DESCRIPTION
|
14
|
+
It checks http status of domain and send email if it's not 200.
|
15
|
+
DESCRIPTION
|
16
|
+
|
17
|
+
spec.homepage = "https://github.com/andgursky/status_checker"
|
18
|
+
spec.license = "MIT"
|
19
|
+
|
20
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
21
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
22
|
+
#if spec.respond_to?(:metadata)
|
23
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
24
|
+
#else
|
25
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
26
|
+
# "public gem pushes."
|
27
|
+
#end
|
28
|
+
|
29
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
30
|
+
f.match(%r{^(test|spec|features)/})
|
31
|
+
end
|
32
|
+
spec.bindir = "exe"
|
33
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
34
|
+
spec.require_paths = ["lib"]
|
35
|
+
|
36
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
37
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
38
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: status_checker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- andgursky
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-03-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: " It checks http status of domain and send email if it's not 200.\n"
|
56
|
+
email:
|
57
|
+
- andgursky@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/status_checker.rb
|
72
|
+
- lib/status_checker/check.rb
|
73
|
+
- lib/status_checker/email.rb
|
74
|
+
- lib/status_checker/timer.rb
|
75
|
+
- lib/status_checker/version.rb
|
76
|
+
- status_checker.gemspec
|
77
|
+
homepage: https://github.com/andgursky/status_checker
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.5.1
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: HTTP status checker
|
101
|
+
test_files: []
|