sensu-plugins-http 0.4.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MGFlZjZlODVjOGQxY2QxZDE5MGU3ZDg2Y2EzZTIwMDA4MDc0MDE1NQ==
4
+ YmE4MDg5NjNjNGJlYzEyZDgxNDJhNTc1NjM0YjAxYWViMzEyYTEzZQ==
5
5
  data.tar.gz: !binary |-
6
- NDZiNWRiOWM5YjM0MWQ4MzJmMzQ5OTVlODA5MDU4NTczMTE0MzM1ZQ==
6
+ ODFhOTk2M2JmYTdlZmIzNWViNDY4ZmViNDM3ZTQxNzE4ZDM4OGM2MA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NGQzZTIyNDY2Zjg1MDcxNTFhYTBjZjc1NzIyNzFjMGZiMzg1ZTYzN2YzODI5
10
- NzBiZDBkZDAzMzFiMjc0ZjMwYWYxYmNmZDAzZWVlOTMyNWI4MTViNjUyNjdl
11
- OWIyZTNhNDIzYTY0OTk4MWZkNmJhY2RhY2IyZjBhM2Y2MjQ1ODQ=
9
+ NDEzYjgzNDJhNTYxMDhjMDMzZThmNjllZGY2ZWZiNTUxNGE4YzZjYmZiMWUy
10
+ MjE5OWMwMGI1M2IwZGQxYTM5MzIyYWFhNzQ4OTdjMzc3YWRhMjY0NDlmOTQ3
11
+ YmY3MGY3YzkyZGNjZDk0ZmU5ZTNiNTE3Yjc1MjEwOGQ3NWIyZGQ=
12
12
  data.tar.gz: !binary |-
13
- ZTgxOGViMTYzZmNiOTcyMGQ1MjIxMjIxZDc3NjhiYzM4OGY3Zjg4NzMwOTJj
14
- NTYyN2FmZjQ5MWVjY2ZiMDZjYzg5OGM2MDdjMDg4NTcyMzliOWM0NTNkNmY5
15
- YjI2OWE4NWRlMzBiNGY3NDYyMDgwOGFlMzIxMTEzYWNiNThhMWI=
13
+ ZTNkZjgwNjcwOTVhNzk2Y2U4YzcwNzU2ZjNhOGEyYmE0M2FjZWI1YjYzNWU0
14
+ ODc3NDVlYzdjYTIzZTFhNWQ3MDI0MWFjNzdkNGQyMzMyZTljOTk4ZGJmYmU2
15
+ ZmY1YThjNGI5YWU2NDc4NGM3ODg1NWVlMjY3YjJmMjA1YjA5YjY=
@@ -5,9 +5,29 @@ This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachang
5
5
 
6
6
  ## [Unreleased]
7
7
 
8
+ ## [1.0.0] - 2016-01-27
9
+ ### Fixed
10
+ - Don't send basic auth when no password is supplied
11
+ - Add default thresholds to check-https-cert.rb
12
+ - check-http: fix default port selection for https
13
+
14
+ ### Added
15
+ - Add `rest-client` dependency for `metrics-http-json`
16
+ - Add `check-head-redirect` that checks that redirection links can be followed in a set number of requests
17
+ - Add `check-http-cors` that checks CORS headers
18
+ - check-http-json: add `cert-key` parameter to allow specifying a separate cert file
19
+ - Add `--negquery` to `check-http.rb` for query text that should not exist
20
+
21
+ ### Removed
22
+ - Support for Ruby 1.9.3
23
+
24
+ ### Changed
25
+ - Upgrade to Rubocop 0.40 and cleanup
26
+ - Pin to `json < 2.0.0` to workaround test failures on Ruby 2.3.0
27
+
8
28
  ## [0.4.0] - 2016-04-26
9
29
  ### Changed
10
- - Rename http-json-graphite -> metrics-json-graphite
30
+ - Rename http-json-graphite -> metrics-json-graphite
11
31
 
12
32
  ## [0.3.0] - 2016-04-08
13
33
  ### Added
@@ -68,7 +88,8 @@ This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachang
68
88
  ### Added
69
89
  - Initial release
70
90
 
71
- [Unreleased]: https://github.com/sensu-plugins/sensu-plugins-http/compare/0.4.0...HEAD
91
+ [Unreleased]: https://github.com/sensu-plugins/sensu-plugins-http/compare/1.0.0...HEAD
92
+ [1.0.0]: https://github.com/sensu-plugins/sensu-plugins-http/compare/0.4.0...1.0.0
72
93
  [0.4.0]: https://github.com/sensu-plugins/sensu-plugins-http/compare/0.3.0...0.4.0
73
94
  [0.3.0]: https://github.com/sensu-plugins/sensu-plugins-http/compare/0.2.1...0.3.0
74
95
  [0.2.1]: https://github.com/sensu-plugins/sensu-plugins-http/compare/0.2.0...0.2.1
data/README.md CHANGED
@@ -14,10 +14,17 @@
14
14
  * bin/check-https-cert.rb
15
15
  * bin/check-last-modified.rb
16
16
  * bin/metrics-curl.rb
17
+ * bin/metrics-http-json.rb
17
18
  * bin/metrics-http-json-deep.rb
19
+ * bin/check-head-redirect.rb
20
+ * bin/check-http-cors.rb
18
21
 
19
22
  ## Usage
20
23
 
24
+ check-head-redirect.rb and check-last-modified.rb can be used in conjunction with AWS to pull configuration from a specific bucket and file.
25
+
26
+ This is helpful if you do not want to configure connection information as an argument to the sensu checks. If a bucket and key are specified that the environment the sensu check executes in has access to, or you provide an AWS key and token, the checks will pull the specified JSON file from S3 and merge the JSON config in to the current check configuration.
27
+
21
28
  ## Installation
22
29
 
23
30
  [Installation and Setup](http://sensu-plugins.io/docs/installation_instructions.html)
@@ -0,0 +1,149 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # check-head-redirect
4
+ #
5
+ # DESCRIPTION:
6
+ #
7
+ # OUTPUT:
8
+ # plain text
9
+ #
10
+ # PLATFORMS:
11
+ # Linux
12
+ #
13
+ # DEPENDENCIES:
14
+ # gem: sensu-plugin
15
+ #
16
+ # USAGE:
17
+ #
18
+ # NOTES:
19
+ #
20
+ # LICENSE:
21
+ # Leon Gibat <brendan.gibat@gmail.com>
22
+ # Released under the same terms as Sensu (the MIT license); see LICENSE
23
+ # for details.
24
+ #
25
+ require 'sensu-plugin/check/cli'
26
+ require 'net/https'
27
+ require 'time'
28
+ require 'aws-sdk'
29
+ require 'json'
30
+ require 'sensu-plugins-http'
31
+
32
+ #
33
+ # Checks that redirection links can be followed in a set number of requests.
34
+ #
35
+ class CheckHeadRedirect < Sensu::Plugin::Check::CLI
36
+ include Common
37
+ option :aws_access_key_id,
38
+ short: '-a AWS_ACCESS_KEY_ID',
39
+ long: '--aws-access-key-id AWS_ACCESS_KEY_ID',
40
+ description: 'AWS Access Key. Either set ENV["AWS_ACCESS_KEY_ID"] or provide it as an option',
41
+ default: ENV['AWS_ACCESS_KEY_ID']
42
+
43
+ option :aws_secret_access_key,
44
+ short: '-k AWS_SECRET_ACCESS_KEY',
45
+ long: '--aws-secret-access-key AWS_SECRET_ACCESS_KEY',
46
+ description: 'AWS Secret Access Key. Either set ENV["AWS_SECRET_ACCESS_KEY"] or provide it as an option',
47
+ default: ENV['AWS_SECRET_ACCESS_KEY']
48
+
49
+ option :aws_region,
50
+ short: '-r AWS_REGION',
51
+ long: '--aws-region REGION',
52
+ description: 'AWS Region (defaults to us-east-1)',
53
+ default: 'us-east-1'
54
+
55
+ option :s3_config_bucket,
56
+ short: '-s S3_CONFIG_BUCKET',
57
+ long: '--s3-config-bucket S3_CONFIG_BUCKET',
58
+ description: 'S3 config bucket'
59
+
60
+ option :s3_config_key,
61
+ short: '-k S3_CONFIG_KEY',
62
+ long: '--s3-config-key S3_CONFIG_KEY',
63
+ description: 'S3 config key'
64
+
65
+ option :url,
66
+ short: '-u URL',
67
+ long: '--url URL',
68
+ description: 'The URL of the file to be checked'
69
+
70
+ option :user,
71
+ short: '-U USER',
72
+ long: '--username USER',
73
+ description: 'A username to connect as'
74
+
75
+ option :password,
76
+ short: '-a PASS',
77
+ long: '--password PASS',
78
+ description: 'A password to use for the username'
79
+
80
+ option :follow_redirects,
81
+ short: '-R FOLLOW_REDIRECTS',
82
+ long: '--redirect FOLLOW_REDIRECTS',
83
+ proc: proc(&:to_i),
84
+ default: 0,
85
+ description: 'Follow first <N> redirects'
86
+
87
+ option :follow_redirects_with_get,
88
+ short: '-g GET_REDIRECTS',
89
+ long: '--get-redirects GET_REDIRECTS',
90
+ proc: proc(&:to_i),
91
+ default: 0,
92
+ description: 'Follow first <N> redirects with GET requests'
93
+
94
+ option :auth_first_only,
95
+ short: '-A',
96
+ long: '--auth-first-only',
97
+ default: true,
98
+ description: 'Use basic auth on first request only'
99
+
100
+ def follow_uri(uri, total_redirects, get_redirects, auth_count)
101
+ location = URI(uri)
102
+ http = Net::HTTP.new(location.host, location.port)
103
+
104
+ if location.port == 443
105
+ http.use_ssl = true
106
+ end
107
+
108
+ request = if get_redirects > 0
109
+ Net::HTTP::Get.new(location.request_uri)
110
+ else
111
+ Net::HTTP::Head.new(location.request_uri)
112
+ end
113
+
114
+ if auth_count > 0 && config[:user] && config[:password] && total_redirects == config[:follow_redirects]
115
+ http.use_ssl = true
116
+ request.basic_auth(config[:user], config[:password])
117
+ auth_count -= 1
118
+ end
119
+
120
+ response = http.request(request)
121
+ if total_redirects > 0
122
+ case response
123
+ when Net::HTTPSuccess then ok
124
+ when Net::HTTPRedirection then follow_uri(response['location'], total_redirects - 1, get_redirects - 1, auth_count)
125
+ else
126
+ critical 'Http Error'
127
+ end
128
+ else
129
+ case response
130
+ when Net::HTTPSuccess then ok
131
+ else
132
+ critical 'Http Error'
133
+ end
134
+ end
135
+ end
136
+
137
+ def run
138
+ merge_s3_config
139
+
140
+ url = config[:url]
141
+
142
+ # Validate arguments
143
+ unless url
144
+ unknown 'No URL specified'
145
+ end
146
+
147
+ follow_uri(url, config[:follow_redirects], config[:follow_redirects_with_get], config[:auth_first_only] ? 1 : config[:follow_redirects])
148
+ end
149
+ end
@@ -0,0 +1,143 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # check-http-cors
4
+ #
5
+ # DESCRIPTION:
6
+ # Takes either a URL or a combination of host/path/query/port/ssl, and checks
7
+ # for valid JSON output in the response. Can also optionally validate simple
8
+ # string key/value pairs.
9
+ #
10
+ # OUTPUT:
11
+ # plain text
12
+ #
13
+ # PLATFORMS:
14
+ # Linux
15
+ #
16
+ # DEPENDENCIES:
17
+ # gem: sensu-plugin
18
+ # gem: json
19
+ # gem: net/http
20
+ #
21
+ # USAGE:
22
+ # #YELLOW
23
+ #
24
+ # EXAMPLE:
25
+ # # simple key access
26
+ # $ ruby plugins/http/check-http-json.rb -u https://example.com/cors_resource -O "Origin:http://dummy"
27
+ #
28
+ # NOTES:
29
+ # Based on Check HTTP by Sonian Inc.
30
+ #
31
+ # LICENSE:
32
+ # Copyright 2015 Alexander Paz <alexjpaz@gmail.com>
33
+ # Released under the same terms as Sensu (the MIT license); see LICENSE
34
+ # for details.
35
+ #
36
+
37
+ require 'rubygems' if RUBY_VERSION < '1.9.0'
38
+ require 'sensu-plugin/check/cli'
39
+ require 'json'
40
+ require 'net/http'
41
+ require 'net/https'
42
+
43
+ class CheckCORS < Sensu::Plugin::Check::CLI
44
+ option :url, short: '-u URL'
45
+ option :host, short: '-h HOST'
46
+ option :path, short: '-p PATH'
47
+ option :query, short: '-q QUERY'
48
+ option :port, short: '-P PORT', proc: proc(&:to_i)
49
+ option :header, short: '-H HEADER', long: '--header HEADER'
50
+ option :ssl, short: '-s', boolean: true, default: false
51
+ option :insecure, short: '-k', boolean: true, default: false
52
+ option :user, short: '-U', long: '--username USER'
53
+ option :password, short: '-a', long: '--password PASS'
54
+ option :cert, short: '-c FILE'
55
+ option :cacert, short: '-C FILE'
56
+ option :timeout, short: '-t SECS', proc: proc(&:to_i), default: 15
57
+ option :key, short: '-K KEY', long: '--key KEY'
58
+ option :value, short: '-v VALUE', long: '--value VALUE'
59
+
60
+ def run
61
+ if config[:url]
62
+ uri = URI.parse(config[:url])
63
+ config[:host] = uri.host
64
+ config[:path] = uri.path
65
+ config[:query] = uri.query
66
+ config[:port] = uri.port
67
+ config[:ssl] = uri.scheme == 'https'
68
+ else
69
+ # #YELLOW
70
+ unless config[:host] && config[:path]
71
+ unknown 'No URL specified'
72
+ end
73
+ config[:port] ||= config[:ssl] ? 443 : 80
74
+ end
75
+
76
+ begin
77
+ timeout(config[:timeout]) do
78
+ acquire_resource
79
+ end
80
+ rescue Timeout::Error
81
+ critical 'Connection timed out'
82
+ rescue => e
83
+ critical "Connection error: #{e.message}"
84
+ end
85
+ end
86
+
87
+ def cors?(res)
88
+ headers = {}
89
+
90
+ if config[:header]
91
+ config[:header].split(',').each do |header|
92
+ h, v = header.split(':', 2)
93
+ headers[h] = v.strip
94
+ end
95
+ end
96
+
97
+ res['Access-Control-Allow-Origin'] == headers['Origin']
98
+ end
99
+
100
+ def acquire_resource
101
+ res = request_http
102
+
103
+ case res.code
104
+ when /^2/
105
+ if cors?(res)
106
+ ok 'Request has matching CORS headers'
107
+ else
108
+ critical 'Response does not have valid CORS headers'
109
+ end
110
+ else
111
+ critical res.code
112
+ end
113
+ end
114
+
115
+ def request_http
116
+ http = Net::HTTP.new(config[:host], config[:port])
117
+
118
+ if config[:ssl]
119
+ http.use_ssl = true
120
+ if config[:cert]
121
+ cert_data = File.read(config[:cert])
122
+ http.cert = OpenSSL::X509::Certificate.new(cert_data)
123
+ http.key = OpenSSL::PKey::RSA.new(cert_data, nil)
124
+ end
125
+ http.ca_file = config[:cacert] if config[:cacert]
126
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE if config[:insecure]
127
+ end
128
+
129
+ req = Net::HTTP::Get.new([config[:path], config[:query]].compact.join('?'))
130
+ unless config[:user].nil? && config[:password].nil?
131
+ req.basic_auth config[:user], config[:password]
132
+ end
133
+
134
+ if config[:header]
135
+ config[:header].split(',').each do |header|
136
+ h, v = header.split(':', 2)
137
+ req[h] = v.strip
138
+ end
139
+ end
140
+
141
+ http.request(req)
142
+ end
143
+ end
@@ -1,4 +1,4 @@
1
- #! /usr/bin/env ruby
1
+ #!/usr/bin/env ruby
2
2
  #
3
3
  # check-http-json
4
4
  #
@@ -50,8 +50,9 @@ class CheckJson < Sensu::Plugin::Check::CLI
50
50
  option :insecure, short: '-k', boolean: true, default: false
51
51
  option :user, short: '-U', long: '--username USER'
52
52
  option :password, short: '-a', long: '--password PASS'
53
- option :cert, short: '-c FILE'
54
- option :cacert, short: '-C FILE'
53
+ option :cert, short: '-c FILE', long: '--cert FILE'
54
+ option :certkey, long: '--cert-key FILE'
55
+ option :cacert, short: '-C FILE', long: '--cacert FILE'
55
56
  option :timeout, short: '-t SECS', proc: proc(&:to_i), default: 15
56
57
  option :key, short: '-K KEY', long: '--key KEY'
57
58
  option :value, short: '-v VALUE', long: '--value VALUE'
@@ -98,6 +99,9 @@ class CheckJson < Sensu::Plugin::Check::CLI
98
99
  if config[:cert]
99
100
  cert_data = File.read(config[:cert])
100
101
  http.cert = OpenSSL::X509::Certificate.new(cert_data)
102
+ if config[:certkey]
103
+ cert_data = File.read(config[:certkey])
104
+ end
101
105
  http.key = OpenSSL::PKey::RSA.new(cert_data, nil)
102
106
  end
103
107
  http.ca_file = config[:cacert] if config[:cacert]
@@ -113,7 +117,7 @@ class CheckJson < Sensu::Plugin::Check::CLI
113
117
  post_body = IO.readlines(config[:postbody])
114
118
  req.body = post_body.join
115
119
  end
116
- if !config[:user].nil? && !config[:password].nil?
120
+ unless config[:user].nil? && config[:password].nil?
117
121
  req.basic_auth config[:user], config[:password]
118
122
  end
119
123
  if config[:header]
@@ -1,4 +1,4 @@
1
- #! /usr/bin/env ruby
1
+ #!/usr/bin/env ruby
2
2
  #
3
3
  # check-http
4
4
  #
@@ -67,8 +67,7 @@ class CheckHttp < Sensu::Plugin::Check::CLI
67
67
  short: '-P PORT',
68
68
  long: '--port PORT',
69
69
  proc: proc(&:to_i),
70
- description: 'Select another port',
71
- default: 80
70
+ description: 'Select another port'
72
71
 
73
72
  option :request_uri,
74
73
  short: '-p PATH',
@@ -133,7 +132,12 @@ class CheckHttp < Sensu::Plugin::Check::CLI
133
132
  option :pattern,
134
133
  short: '-q PAT',
135
134
  long: '--query PAT',
136
- description: 'Query for a specific pattern'
135
+ description: 'Query for a specific pattern that must exist'
136
+
137
+ option :negpattern,
138
+ short: '-n PAT',
139
+ long: '--negquery PAT',
140
+ description: 'Query for a specific pattern that must be absent'
137
141
 
138
142
  option :timeout,
139
143
  short: '-t SECS',
@@ -229,6 +233,8 @@ class CheckHttp < Sensu::Plugin::Check::CLI
229
233
  http.read_timeout = config[:timeout]
230
234
  http.open_timeout = config[:timeout]
231
235
  http.ssl_timeout = config[:timeout]
236
+ http.continue_timeout = config[:timeout]
237
+ http.keep_alive_timeout = config[:timeout]
232
238
 
233
239
  warn_cert_expire = nil
234
240
  if config[:ssl]
@@ -261,7 +267,7 @@ class CheckHttp < Sensu::Plugin::Check::CLI
261
267
  Net::HTTP::Post.new(config[:request_uri], 'User-Agent' => config[:ua])
262
268
  end
263
269
 
264
- if !config[:user].nil? && !config[:password].nil?
270
+ unless config[:user].nil? && config[:password].nil?
265
271
  req.basic_auth config[:user], config[:password]
266
272
  end
267
273
  if config[:header]
@@ -294,6 +300,10 @@ class CheckHttp < Sensu::Plugin::Check::CLI
294
300
 
295
301
  size = res.body.nil? ? '0' : res.body.size
296
302
 
303
+ handle_response(res, size, body)
304
+ end
305
+
306
+ def handle_response(res, size, body)
297
307
  case res.code
298
308
  when /^2/
299
309
  if config[:redirectto]
@@ -304,6 +314,12 @@ class CheckHttp < Sensu::Plugin::Check::CLI
304
314
  else
305
315
  critical "#{res.code}, did not find /#{config[:pattern]}/ in #{size} bytes: #{res.body[0...200]}..."
306
316
  end
317
+ elsif config[:negpattern]
318
+ if res.body =~ /#{config[:negpattern]}/
319
+ critical "#{res.code}, found /#{config[:negpattern]}/ in #{size} bytes: #{res.body[0...200]}..."
320
+ else
321
+ ok "#{res.code}, did not find /#{config[:negpattern]}/ in #{size} bytes" + body
322
+ end
307
323
  else
308
324
  ok("#{res.code}, #{size} bytes" + body) unless config[:response_code]
309
325
  end
@@ -1,4 +1,4 @@
1
- #! /usr/bin/env ruby
1
+ #!/usr/bin/env ruby
2
2
  #
3
3
  # check-https-cert
4
4
  #
@@ -44,12 +44,14 @@ class CheckHttpCert < Sensu::Plugin::Check::CLI
44
44
  short: '-w',
45
45
  long: '--warning DAYS',
46
46
  proc: proc(&:to_i),
47
+ default: 50,
47
48
  description: 'Warn EXPIRE days before cert expires'
48
49
 
49
50
  option :critical,
50
51
  short: '-c',
51
52
  long: '--critical DAYS',
52
53
  proc: proc(&:to_i),
54
+ default: 25,
53
55
  description: 'Critical EXPIRE days before cert expires'
54
56
 
55
57
  option :insecure,
@@ -1,4 +1,4 @@
1
- #! /usr/bin/env ruby
1
+ #!/usr/bin/env ruby
2
2
  #
3
3
  # check-fleet-units
4
4
  #
@@ -25,12 +25,44 @@
25
25
  require 'sensu-plugin/check/cli'
26
26
  require 'net/https'
27
27
  require 'time'
28
+ require 'aws-sdk'
29
+ require 'json'
30
+ require 'sensu-plugins-http'
28
31
 
29
32
  #
30
- # Checks the last modified time of a file to verify it has been updated within a
33
+ # Checks the last modified time of a file to verify it has been updated with a
31
34
  # specified threshold.
32
35
  #
33
36
  class CheckLastModified < Sensu::Plugin::Check::CLI
37
+ include Common
38
+ option :aws_access_key_id,
39
+ short: '-a AWS_ACCESS_KEY_ID',
40
+ long: '--aws-access-key-id AWS_ACCESS_KEY_ID',
41
+ description: 'AWS Access Key. Either set ENV["AWS_ACCESS_KEY_ID"] or provide it as an option',
42
+ default: ENV['AWS_ACCESS_KEY_ID']
43
+
44
+ option :aws_secret_access_key,
45
+ short: '-k AWS_SECRET_KEY',
46
+ long: '--aws-secret-access-key AWS_SECRET_ACCESS_KEY',
47
+ description: 'AWS Secret Access Key. Either set ENV["AWS_SECRET_ACCESS_KEY"] or provide it as an option',
48
+ default: ENV['AWS_SECRET_ACCESS_KEY']
49
+
50
+ option :aws_region,
51
+ short: '-r AWS_REGION',
52
+ long: '--aws-region REGION',
53
+ description: 'AWS Region (defaults to us-east-1).',
54
+ default: 'us-east-1'
55
+
56
+ option :s3_config_bucket,
57
+ short: '-s S3_CONFIG_BUCKET',
58
+ long: '--s3-config-bucket S3_CONFIG_BUCKET',
59
+ description: 'S3 config bucket'
60
+
61
+ option :s3_config_key,
62
+ short: '-k S3_CONFIG_KEY',
63
+ long: '--s3-config-key S3_CONFIG_KEY',
64
+ description: 'S3 config key'
65
+
34
66
  option :url,
35
67
  short: '-u URL',
36
68
  long: '--url URL',
@@ -51,11 +83,69 @@ class CheckLastModified < Sensu::Plugin::Check::CLI
51
83
  long: '--time TIME',
52
84
  description: 'The time in seconds the file should be updated by'
53
85
 
86
+ option :follow_redirects,
87
+ short: '-R FOLLOW_REDIRECTS',
88
+ long: '--redirect FOLLOW_REDIRECTS',
89
+ proc: proc(&:to_i),
90
+ default: 0,
91
+ description: 'Follow first <N> redirects'
92
+
93
+ option :follow_redirects_with_get,
94
+ short: '-g GET_REDIRECTS',
95
+ long: '--get-redirects GET_REDIRECTS',
96
+ proc: proc(&:to_i),
97
+ default: 0,
98
+ description: 'Follow first <N> redirects with GET requests'
99
+
100
+ option :auth_first_only,
101
+ short: '-A',
102
+ long: '--auth-first-only',
103
+ default: true,
104
+ description: 'Use basic auth on first request only'
105
+
106
+ def follow_uri(uri, total_redirects, get_redirects, auth_count)
107
+ location = URI(uri)
108
+ http = Net::HTTP.new(location.host, location.port)
109
+
110
+ if location.port == 443
111
+ http.use_ssl = true
112
+ end
113
+
114
+ request = if get_redirects > 0
115
+ Net::HTTP::Get.new(location.request_uri)
116
+ else
117
+ Net::HTTP::Head.new(location.request_uri)
118
+ end
119
+
120
+ if auth_count > 0 && config[:user] && config[:password] && total_redirects == config[:follow_redirects]
121
+ http.use_ssl = true
122
+ request.basic_auth(config[:user], config[:password])
123
+ auth_count -= 1
124
+ end
125
+
126
+ response = http.request(request)
127
+
128
+ if total_redirects > 0
129
+ case response
130
+ when Net::HTTPSuccess then response
131
+ when Net::HTTPRedirection then follow_uri(response['location'], total_redirects - 1, get_redirects - 1, auth_count)
132
+ else
133
+ critical 'Http Error'
134
+ end
135
+ else
136
+ case response
137
+ when Net::HTTPSuccess then response
138
+ else
139
+ critical 'Http Error'
140
+ end
141
+ end
142
+ end
143
+
54
144
  def run
55
- cli = CheckLastModified.new
56
- cli.parse_options
57
- url = cli.config[:url]
58
- threshold = cli.config[:threshold]
145
+ merge_s3_config
146
+
147
+ url = config[:url]
148
+ threshold = config[:threshold]
59
149
 
60
150
  # Validate arguments
61
151
  unless url
@@ -66,18 +156,9 @@ class CheckLastModified < Sensu::Plugin::Check::CLI
66
156
  unknown 'No threshold specified'
67
157
  end
68
158
 
69
- # Build a request from user options and then request it
70
- uri = URI(url)
71
- http = Net::HTTP.new(uri.host, uri.port)
72
- request = Net::HTTP::Head.new(uri.request_uri)
73
-
74
- if cli.config[:user] && cli.config[:password]
75
- http.use_ssl = true
76
- request.basic_auth(cli.config[:user], cli.config[:password])
77
- end
78
-
79
- response = http.request(request)
159
+ response = follow_uri(url, config[:follow_redirects], config[:follow_redirects_with_get], config[:auth_first_only] ? 1 : config[:follow_redirects])
80
160
 
161
+ # Build a request from user options and then request it
81
162
  if response.header['last-modified'].nil?
82
163
  critical 'Http Error'
83
164
  end
@@ -1,4 +1,4 @@
1
- #! /usr/bin/env ruby
1
+ #!/usr/bin/env ruby
2
2
  #
3
3
  # metrics-curl
4
4
  #
@@ -1 +1,2 @@
1
1
  require 'sensu-plugins-http/version'
2
+ require 'sensu-plugins-http/common'
@@ -0,0 +1,32 @@
1
+
2
+ module Common
3
+ def initialize
4
+ super()
5
+ aws_config
6
+ end
7
+
8
+ def aws_config
9
+ Aws.config.update(
10
+ credentials: Aws::Credentials.new(config[:aws_access_key_id], config[:aws_secret_access_key])
11
+ ) if config[:aws_access_key_id] && config[:aws_secret_access_key]
12
+
13
+ Aws.config.update(
14
+ region: config[:aws_region]
15
+ )
16
+ end
17
+
18
+ def merge_s3_config
19
+ return if config[:s3_config_bucket].nil? || config[:s3_config_key].nil?
20
+
21
+ aws_config
22
+
23
+ s3 = Aws::S3::Client.new
24
+ begin
25
+ resp = s3.get_object(bucket: config[:s3_config_bucket], key: config[:s3_config_key])
26
+ s3_config = JSON.parse(resp.body.read, symbolize_names: true)
27
+ config.merge!(s3_config)
28
+ rescue
29
+ critical 'Error getting config file from s3'
30
+ end
31
+ end
32
+ end
@@ -1,7 +1,7 @@
1
1
  module SensuPluginsHttp
2
2
  module Version
3
- MAJOR = 0
4
- MINOR = 4
3
+ MAJOR = 1
4
+ MINOR = 0
5
5
  PATCH = 0
6
6
 
7
7
  VER_STRING = [MAJOR, MINOR, PATCH].compact.join('.')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sensu-plugins-http
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sensu-Plugins and contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-27 00:00:00.000000000 Z
11
+ date: 2016-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sensu-plugin
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '2.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '2.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rest-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: bundler
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -128,14 +156,14 @@ dependencies:
128
156
  requirements:
129
157
  - - ~>
130
158
  - !ruby/object:Gem::Version
131
- version: '0.37'
159
+ version: 0.40.0
132
160
  type: :development
133
161
  prerelease: false
134
162
  version_requirements: !ruby/object:Gem::Requirement
135
163
  requirements:
136
164
  - - ~>
137
165
  - !ruby/object:Gem::Version
138
- version: '0.37'
166
+ version: 0.40.0
139
167
  - !ruby/object:Gem::Dependency
140
168
  name: yard
141
169
  requirement: !ruby/object:Gem::Requirement
@@ -206,6 +234,20 @@ dependencies:
206
234
  - - ~>
207
235
  - !ruby/object:Gem::Version
208
236
  version: '2.9'
237
+ - !ruby/object:Gem::Dependency
238
+ name: json
239
+ requirement: !ruby/object:Gem::Requirement
240
+ requirements:
241
+ - - <
242
+ - !ruby/object:Gem::Version
243
+ version: 2.0.0
244
+ type: :development
245
+ prerelease: false
246
+ version_requirements: !ruby/object:Gem::Requirement
247
+ requirements:
248
+ - - <
249
+ - !ruby/object:Gem::Version
250
+ version: 2.0.0
209
251
  description: ! "This plugin provides native HTTP instrumentation\n for
210
252
  monitoring and metrics collection, including:\n response
211
253
  code, JSON response, HTTP last modified,\n SSL expiry,
@@ -217,6 +259,8 @@ executables:
217
259
  - metrics-curl.rb
218
260
  - check-last-modified.rb
219
261
  - check-https-cert.rb
262
+ - check-http-cors.rb
263
+ - check-head-redirect.rb
220
264
  - metrics-http-json.rb
221
265
  - check-http.rb
222
266
  extensions: []
@@ -225,6 +269,8 @@ files:
225
269
  - CHANGELOG.md
226
270
  - LICENSE
227
271
  - README.md
272
+ - bin/check-head-redirect.rb
273
+ - bin/check-http-cors.rb
228
274
  - bin/check-http-json.rb
229
275
  - bin/check-http.rb
230
276
  - bin/check-https-cert.rb
@@ -233,6 +279,7 @@ files:
233
279
  - bin/metrics-http-json-deep.rb
234
280
  - bin/metrics-http-json.rb
235
281
  - lib/sensu-plugins-http.rb
282
+ - lib/sensu-plugins-http/common.rb
236
283
  - lib/sensu-plugins-http/version.rb
237
284
  homepage: https://github.com/sensu-plugins/sensu-plugins-http
238
285
  licenses:
@@ -252,7 +299,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
252
299
  requirements:
253
300
  - - ! '>='
254
301
  - !ruby/object:Gem::Version
255
- version: 1.9.3
302
+ version: 2.0.0
256
303
  required_rubygems_version: !ruby/object:Gem::Requirement
257
304
  requirements:
258
305
  - - ! '>='