riemann_check_http 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +52 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/riemann_check_http.rb +6 -0
- data/lib/riemann_check_http/net_https.rb +73 -0
- data/lib/riemann_check_http/riemann.rb +92 -0
- data/lib/riemann_check_http/version.rb +3 -0
- data/riemann_check_http.gemspec +37 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b0574c03c515ce22111e5b82111da82b32cc7325
|
4
|
+
data.tar.gz: 75b6541fa5d3a9f77f45b644fb492a70868a40d1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 91025da20e8de87b310f843a07143a6032bf7c4242cdae07357ed78ceded3cd7e87281926ca1a91fcffc5fe37998212357896ccfeb9133b4f34266368b3a9a0a
|
7
|
+
data.tar.gz: ec42da9d2a3492cb24d20f09a9553011e0d4158fac1bcbb5185d81ffbe60fea9bc13aaa809db091113255d3e905d9653bee659fe92a571e7b125851a11cfcdfd
|
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) 2015 TODO: Write your name
|
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,52 @@
|
|
1
|
+
# RiemannCheckHttp
|
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/riemann_check_http`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'riemann_check_http'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install riemann_check_http
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'riemann_check_http'
|
27
|
+
$riemann_host = RIEMANN_SERVER
|
28
|
+
$riemann_port = '5555'
|
29
|
+
con = RiemannCheckHttp::Riemann.new host: $riemann_host, port: $riemann_port
|
30
|
+
con.riemann_ttl = 500
|
31
|
+
con.http_open_timeout = 5
|
32
|
+
con.http_read_timeout = 30
|
33
|
+
con.use_ssl = true
|
34
|
+
con.use_pem = true
|
35
|
+
con.check_http 'sample.url.com', '501..598', '302..305', '301'
|
36
|
+
```
|
37
|
+
|
38
|
+
## Development
|
39
|
+
|
40
|
+
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.
|
41
|
+
|
42
|
+
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).
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/riemann_check_http.
|
47
|
+
|
48
|
+
|
49
|
+
## License
|
50
|
+
|
51
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
52
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "riemann_check_http"
|
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,73 @@
|
|
1
|
+
|
2
|
+
require 'net/http'
|
3
|
+
require "net/https"
|
4
|
+
require 'cacert'
|
5
|
+
require "uri"
|
6
|
+
|
7
|
+
module RiemannCheckHttp
|
8
|
+
class NetHttp
|
9
|
+
|
10
|
+
attr_accessor :open_timeout
|
11
|
+
attr_accessor :read_timeout
|
12
|
+
attr_accessor :use_ssl
|
13
|
+
attr_accessor :use_pem
|
14
|
+
attr_accessor :pem_file
|
15
|
+
|
16
|
+
def initialize(opts = {})
|
17
|
+
@use_ssl = opts[:use_ssl] || false
|
18
|
+
@use_pem = opts[:use_pem] || false
|
19
|
+
@pem_file = opts[:pem_file] || Cacert.pem
|
20
|
+
@read_timeout = opts[:read_timeout] || 30
|
21
|
+
@open_timeout = opts[:open_timeout] || 10
|
22
|
+
end
|
23
|
+
|
24
|
+
def get(uri_str)
|
25
|
+
begin
|
26
|
+
res = fetch(uri_str)
|
27
|
+
[res.code, res.message]
|
28
|
+
rescue Exception => e
|
29
|
+
['598', e.message]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def fetch(uri_str, limit = 10)
|
34
|
+
raise ArgumentError, 'too many HTTP redirects' if limit == 0
|
35
|
+
if uri_str =~ /^(http:\/\/|https:\/\/)?(.*)$/
|
36
|
+
prot = $1 || 'http://'
|
37
|
+
uri_str = "#{prot}#{$2}"
|
38
|
+
end
|
39
|
+
uri = URI.parse uri_str
|
40
|
+
http = Net::HTTP.new uri.host, uri.port
|
41
|
+
http.open_timeout = open_timeout
|
42
|
+
http.read_timeout = read_timeout
|
43
|
+
if use_ssl and (prot == 'https://')
|
44
|
+
if use_pem
|
45
|
+
http.use_ssl = true
|
46
|
+
http.ca_file = pem_file
|
47
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
48
|
+
else
|
49
|
+
http.use_ssl = true
|
50
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
51
|
+
end
|
52
|
+
else
|
53
|
+
http.use_ssl = false
|
54
|
+
end
|
55
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
56
|
+
response = http.request(request)
|
57
|
+
case response
|
58
|
+
when Net::HTTPSuccess then
|
59
|
+
response
|
60
|
+
when Net::HTTPRedirection then
|
61
|
+
location = response['location']
|
62
|
+
#warn "redirected to #{location}"
|
63
|
+
fetch(location, limit - 1)
|
64
|
+
else
|
65
|
+
response
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
#http = RiemannCheckHttp::NetHttp.new use_ssl: true, use_pem: true, pem_file: '/root/url_monitoring/cacert.pem', open_timeout: 30
|
73
|
+
#puts http.get('test.example.com').inspect
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'riemann/client'
|
2
|
+
require 'cacert'
|
3
|
+
require_relative 'net_https'
|
4
|
+
|
5
|
+
module RiemannCheckHttp
|
6
|
+
class Riemann
|
7
|
+
|
8
|
+
attr_accessor :http_open_timeout
|
9
|
+
attr_accessor :http_read_timeout
|
10
|
+
attr_accessor :riemann_ttl
|
11
|
+
attr_accessor :riemann_con
|
12
|
+
attr_accessor :use_ssl
|
13
|
+
attr_accessor :use_pem #http.verify_mode = OpenSSL::SSL::VERIFY_NONE if use_pem = false
|
14
|
+
attr_accessor :pem_file #if not defined takes Cacert.pem file coming through Cacert ruby gem
|
15
|
+
attr_accessor :domain
|
16
|
+
|
17
|
+
def initialize(opts = {})
|
18
|
+
raise "host: param is missing" if not opts[:host]
|
19
|
+
raise "domain: param is missing" if not opts[:domain]
|
20
|
+
opts[:port] ||= 5555
|
21
|
+
opts[:riemann_timeout] ||= 5
|
22
|
+
@domain = opts[:domain]
|
23
|
+
@http_open_timeout = 10
|
24
|
+
@http_read_timeout = 30
|
25
|
+
@use_ssl = opts[:use_ssl] || false
|
26
|
+
@use_pem = opts[:use_pem] || false
|
27
|
+
@pem_file = opts[:pem_file] || Cacert.pem
|
28
|
+
@riemann_ttl = 120
|
29
|
+
@riemann_con = ::Riemann::Client.new host: opts[:host], port: opts[:port], riemann_timeout: opts[:riemann_timeout]
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
def send_riemann(app, state, type, dsc)
|
34
|
+
metric = (state == 'critical') ? 0 : 1
|
35
|
+
tuple = {
|
36
|
+
service: "#{app}.#{type}",
|
37
|
+
state: state,
|
38
|
+
tags: ["#{type}_url_monitoring","influxdb"],
|
39
|
+
metric: metric,
|
40
|
+
description: dsc,
|
41
|
+
ttl: riemann_ttl
|
42
|
+
}
|
43
|
+
riemann_con << tuple
|
44
|
+
end
|
45
|
+
|
46
|
+
def check_http(url, *sc)
|
47
|
+
code = '501'
|
48
|
+
msg = 'Unknown'
|
49
|
+
if url =~ /^(http:\/\/|https:\/\/)?(.*?)(\.internal)?\.#{domain}\.com/
|
50
|
+
app = $2 if $2
|
51
|
+
type = $3 ? 'internal' : 'external'
|
52
|
+
else
|
53
|
+
raise "Improper url: #{url}"
|
54
|
+
end
|
55
|
+
con = RiemannCheckHttp::NetHttp.new use_pem: use_pem, use_ssl: use_ssl, read_timeout: 30, open_timeout: 5, pem_file: pem_file
|
56
|
+
(code, msg) = con.get url
|
57
|
+
state = check_app_state code, sc
|
58
|
+
dsc = msg + ", status code #{code}"
|
59
|
+
send_riemann app, state, type, dsc
|
60
|
+
[code, msg]
|
61
|
+
end
|
62
|
+
|
63
|
+
def check_app_state(code, sc)
|
64
|
+
state = 'okay'
|
65
|
+
sc.each do |c|
|
66
|
+
if c =~ /^[0-9]{3}(\.\.[0-9]{3})?$/
|
67
|
+
if $1
|
68
|
+
if eval(c) === code.to_i
|
69
|
+
state = 'critical'
|
70
|
+
break
|
71
|
+
end
|
72
|
+
else
|
73
|
+
if code.to_s == c
|
74
|
+
state = 'critical'
|
75
|
+
break
|
76
|
+
end
|
77
|
+
end
|
78
|
+
else
|
79
|
+
raise "#{c.inspect} status code is not in valid format"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
state
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
#con = RiemannCheckHttp::Riemann.new host:'x.x.x.x', port: 5555, use_ssl: true, use_pem: true
|
91
|
+
#res = con.check_http 'test.example.com', '501..598', '302..305', '301'
|
92
|
+
#puts res.inspect
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'riemann_check_http/version'
|
5
|
+
require 'riemann_check_http/net_https.rb'
|
6
|
+
require 'riemann_check_http/riemann.rb'
|
7
|
+
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.name = "riemann_check_http"
|
10
|
+
spec.version = RiemannCheckHttp::VERSION
|
11
|
+
spec.authors = ["pawan pandey"]
|
12
|
+
spec.email = ["pandey.p1987@gmail.com"]
|
13
|
+
|
14
|
+
spec.summary = %q{http health check plugin for riemann}
|
15
|
+
spec.description = %q{Monitor the http url for status code and send the status to riemann}
|
16
|
+
spec.homepage = ""
|
17
|
+
spec.license = "MIT"
|
18
|
+
|
19
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
20
|
+
# delete this section to allow pushing this gem to any host.
|
21
|
+
# if spec.respond_to?(:metadata)
|
22
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
23
|
+
# else
|
24
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
25
|
+
# end
|
26
|
+
|
27
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
spec.add_development_dependency "rspec"
|
35
|
+
spec.add_dependency "riemann-client", "~>0.2.5"
|
36
|
+
spec.add_dependency "cacert", "~>0.2.0"
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: riemann_check_http
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- pawan pandey
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-03 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.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
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: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: riemann-client
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.2.5
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.2.5
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: cacert
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.2.0
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.2.0
|
83
|
+
description: Monitor the http url for status code and send the status to riemann
|
84
|
+
email:
|
85
|
+
- pandey.p1987@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- lib/riemann_check_http.rb
|
100
|
+
- lib/riemann_check_http/net_https.rb
|
101
|
+
- lib/riemann_check_http/riemann.rb
|
102
|
+
- lib/riemann_check_http/version.rb
|
103
|
+
- riemann_check_http.gemspec
|
104
|
+
homepage: ''
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata: {}
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.4.8
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: http health check plugin for riemann
|
128
|
+
test_files: []
|