sensu-extensions-deregistration 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d0df0b359379e91b13898784b2ab34a9cc9ac87a
4
+ data.tar.gz: 19e0a6566a2c5b5688f87b1e074823648b21387b
5
+ SHA512:
6
+ metadata.gz: 4f7c4ef2ef5ace144f06fa833fc83fd0ce8bd75a171575f0e47c89418d80365bda2731ce5e48d54863f1ec34122c2c9229a00a24e3ac994da9866e24ade31de1
7
+ data.tar.gz: 7363a3997373f54d82c3dcb4e7e5a945c6cdd3fe1776c9b7a068935b993b2c3e0f6590a7e2eacc7c2cf3becb51a0ada0dcf0384770c3877eba45009315be57fe
@@ -0,0 +1,30 @@
1
+ # Change Log
2
+
3
+ This project adheres to [Semantic Versioning](http://semver.org/).
4
+
5
+ This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachangelog.com/).
6
+
7
+ ## [Unreleased]
8
+
9
+ ### Fixed
10
+
11
+ ### Added
12
+
13
+ ### Changed
14
+
15
+ ## [1.0.0] - 2016-01-31
16
+
17
+ ### Fixed
18
+
19
+ ### Added
20
+
21
+ ### Changed
22
+
23
+ ## 0.0.1 - 2016-01-31
24
+
25
+ ### Added
26
+
27
+ - Initial release.
28
+
29
+ [Unreleased]: https://github.com/sensu-extensions/sensu-extensions-template/compare/v1.0.0...HEAD
30
+ [1.0.0]: https://github.com/sensu-extensions/sensu-extensions-template/compare/v0.0.1...v1.0.0
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2018 Sensu, Inc.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,48 @@
1
+ # Sensu::Extensions::Deregistration
2
+
3
+ **NOTE: This extension is considered experimental and needs testing!**
4
+
5
+ For each event recieved, this handler extension deletes the corresponding client via the /clients API
6
+
7
+ ## Installation
8
+
9
+ 1. Install this gem using sensu-install
10
+
11
+ ``` bash
12
+ $ sensu-install -e sensu-extensions-deregistration
13
+ ```
14
+
15
+ 2. Configure Sensu to load the extension, as [documented here][0].
16
+
17
+ ``` json
18
+ {
19
+ "extensions": {
20
+ "deregistration": {
21
+ "gem": "sensu-extensions-deregistration"
22
+ }
23
+ }
24
+ }
25
+ ```
26
+
27
+ ## Configuration
28
+
29
+ With the extension loaded, the "deregistration" handler is now available for use with [client deregistration attributes][1]:
30
+
31
+ ```
32
+ {
33
+ "client": {
34
+ "name": "1-424242",
35
+ "...": "...",
36
+ "deregister": true,
37
+ "deregistration": {
38
+ "handler": "deregistration"
39
+ }
40
+ }
41
+ }
42
+
43
+ ```
44
+
45
+ With this configuration in place, clients will be automatically deregistered when sensu-client process recieves a SIGTERM.
46
+
47
+ [1]: https://sensuapp.org/docs/latest/reference/clients.html#deregistration-attributes
48
+ [0]: https://sensuapp.org/docs/latest/reference/extensions.html
@@ -0,0 +1,73 @@
1
+ require 'sensu/extension'
2
+ require 'net/http'
3
+
4
+ module Sensu
5
+ module Extension
6
+ class Deregistration < Handler
7
+ def name
8
+ 'deregistration'
9
+ end
10
+
11
+ def description
12
+ 'deregisters (deletes) clients'
13
+ end
14
+
15
+ def options
16
+ return @options if @options
17
+ @options = {
18
+ timeout: 1
19
+ }
20
+ @options.merge!(@settings[:deregistration]) if @settings[:deregistration].is_a?(Hash)
21
+ @options
22
+ end
23
+
24
+ # Returns configuration for an API
25
+ def api
26
+ return @api if @api
27
+ @api = @settings[:api] || {}
28
+ if @api.key?(:endpoints) && @api[:endpoints].is_a?(Array)
29
+ @api = @api[:endpoints].sample
30
+ else
31
+ @api
32
+ end
33
+ end
34
+
35
+ # Remove the Sensu client from the registry, using the Sensu
36
+ # API. This method returns `true` if the Sensu client deletion
37
+ # was successful.
38
+ #
39
+ # @param event [Hash]
40
+ # @return [TrueClass, FalseClass]
41
+ def remove_sensu_client!(event)
42
+ http = Net::HTTP.new(api.fetch(:host, '127.0.0.1'), api.fetch(:port, 4567))
43
+ client_name = event[:client][:name]
44
+ request = Net::HTTP::Delete.new("/clients/#{client_name}")
45
+ if api[:user] && api[:password]
46
+ request.basic_auth(api[:user], api[:password])
47
+ end
48
+ response = http.request(request)
49
+ response.code == '202'
50
+ end
51
+
52
+ def run(event, &callback)
53
+ handle = proc do
54
+ event = Sensu::JSON.load(event)
55
+ client_name = event[:client][:name]
56
+ begin
57
+ Timeout.timeout(options[:timeout]) do
58
+ if remove_sensu_client!(event)
59
+ ["deleted client #{client_name}", 0]
60
+ else
61
+ ["error deleting client #{client_name}", 2]
62
+ end
63
+ end
64
+ rescue => error
65
+ @logger.error('client deregistration error', error: error.to_s)
66
+ ["error deleting client #{client_name}: #{error}", 2]
67
+ end
68
+ end
69
+ EM.defer(handle, callback)
70
+ end
71
+ end
72
+ end
73
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sensu-extensions-deregistration
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Sensu Inc
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sensu-extension
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
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: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sensu-logger
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sensu-settings
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: The Sensu Core built-in deregistration handler
112
+ email:
113
+ - "<support@sensu.io>"
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - CHANGELOG.md
119
+ - LICENSE
120
+ - README.md
121
+ - lib/sensu/extensions/deregistration.rb
122
+ homepage: https://github.com/sensu/sensu-extensions-deregistration
123
+ licenses: []
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.6.11
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: The Sensu Core built-in deregistration handler
145
+ test_files: []