sensu-plugins-matrix 0.0.2

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: 68a839404cf4da8c493f6c179605554be5500d90
4
+ data.tar.gz: c19e3900384c06849290ac9d73243a31d59da5fb
5
+ SHA512:
6
+ metadata.gz: 158dc782d2e4ddd9409d422edd18d752fd2fd2e71af3f48a8750001acece55757552c1547f58c8843be761922e36a176db260336794c52f6f3b277978da85d97
7
+ data.tar.gz: 7bcc16c2bda25d4bda78667d490ff6afdfd7852989902fc277d1a3a780faa727a08d5d1c0590615dfe391c731ba786900184b37172afdb109c8ffd7fca2a24ea
@@ -0,0 +1,8 @@
1
+ # Change Log
2
+ This project adheres to [Semantic Versioning](http://semver.org/).
3
+
4
+ This CHANGELOG follows the format listed [here](https://github.com/sensu-plugins/community/blob/master/HOW_WE_CHANGELOG.md)
5
+
6
+ ## 0.0.1 - 2019-07-24
7
+ ### Added
8
+ - initial release
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Sensu-Plugins
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,85 @@
1
+ ## Sensu-Plugins-Matrix
2
+
3
+ ## Functionality
4
+
5
+ This plugin provides a handler to send notifications to a Matrix chat.
6
+
7
+ ## Files
8
+ * bin/handler-matrix.rb
9
+
10
+ ## Usage
11
+
12
+ After installation, you have to set up a `pipe` type handler, like so:
13
+
14
+ ```json
15
+ {
16
+ "handlers": {
17
+ "matrix": {
18
+ "type": "pipe",
19
+ "command": "/opt/sensu/embedded/bin/handler-matrix.rb",
20
+ "filter": "occurrences"
21
+ }
22
+ }
23
+ }
24
+ ```
25
+
26
+ This gem also expects a JSON configuration file with the following contents:
27
+
28
+ ```json
29
+ {
30
+ "matrix": {
31
+ "matrix_user": "sensu",
32
+ "matrix_access_token": "MDYOURTOKEN_HERE",
33
+ "matrix_server": "matrix.example.com",
34
+ "matrix_room_id": "!TBDXXXXXXXXX:matrix.example.com"
35
+ }
36
+ }
37
+ ```
38
+
39
+ ### Parameters:
40
+ - `matrix_server`: The URL of the Matrix (synapse) instance
41
+ - `matrix_user`: The username of the Matrix account that will be used to send
42
+ the messages. You probably want to use a special "bot" account for this.
43
+ - `password`: The password of the Matrix account that will be used to send
44
+ the messages.
45
+ - `matrix_access_token`: You can also specify a access token instead of the password
46
+ - `matrix_room_id`: The room to which the error message is to be sent.
47
+ - `message_template` (optional): An ERB template in Markdown to use to format
48
+ messages instead of the default. Supports the following variables:
49
+ - `action_to_string`
50
+ - `action_symbol`
51
+ - `client_name`
52
+ - `check_name`
53
+ - `status_to_string`
54
+ - `status_symbol`
55
+ - `occurrences`
56
+ - `output`
57
+ - `message_template_file` (optional): A file to read an ERB template in Markdown
58
+ from to format messages. Supports the same variables as `message_template`.
59
+
60
+
61
+ ### Advanced configuration
62
+
63
+ By default, the handler assumes that the config parameters are specified in the
64
+ `matrix` top-level key of the JSON, as shown above. You also have the option
65
+ to make the handler fetch the config from a different key. To do this, pass the
66
+ `-j` option to the handler with the name of the desired key You can define
67
+ multiple handlers, and each handler can send notifications to a different room
68
+ and from a different bot. You could, for example, have critical and non-critical
69
+ Matrix rooms, and send the notifications to one or the other depending on the
70
+ check. For example:
71
+
72
+ ```json
73
+ {
74
+ "handlers": {
75
+ "critical_matrix": {
76
+ "type": "pipe",
77
+ "command": "/opt/sensu/embedded/bin/handler-matrix.rb -j critical_matrix_options"
78
+ },
79
+ "non_critical_matrix": {
80
+ "type": "pipe",
81
+ "command": "/opt/sensu/embedded/bin/handler-matrix.rb -j non_critical_matrix_options"
82
+ }
83
+ }
84
+ }
85
+ ```
@@ -0,0 +1,182 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Sensu Handler: mailer
4
+ #
5
+ # This handler formats alerts as mails and sends them off to a pre-defined recipient.
6
+ #
7
+ # Copyright 2019 Seji64
8
+ #
9
+ # Released under the same terms as Sensu (the MIT license); see LICENSE
10
+ # for details.
11
+
12
+ # Note: The default matrix config is fetched from the predefined json config file which is "matrix.json" or any other
13
+ # file defined using the "json_config" command line option.
14
+
15
+ require 'sensu-handler'
16
+ require 'matrix_sdk'
17
+ require 'erb'
18
+
19
+ class Mailer < Sensu::Handler
20
+ option :json_config,
21
+ description: 'Name of the JSON Configuration block in Sensu (default: matrix)',
22
+ short: '-j JsonConfig',
23
+ long: '--json_config JsonConfigKey',
24
+ required: false,
25
+ default: 'matrix'
26
+
27
+ def fetch_setting(setting_key)
28
+ config_key = config[:json_config]
29
+ value = @event[setting_key]
30
+ value ||= settings[config_key][setting_key] if config_key
31
+ end
32
+
33
+ def json_config
34
+ @json_config ||= config[:json_config]
35
+ end
36
+
37
+ def action_to_string
38
+ actions = {
39
+ 'create' => 'Created',
40
+ 'resolve' => 'Resolved',
41
+ 'flapping' => 'Flapping'
42
+ }
43
+ actions[@event['action']]
44
+ end
45
+
46
+ def action_symbol
47
+ icons = {
48
+ 'create' => "\xF0\x9F\x98\xB1",
49
+ 'resolve' => "\xF0\x9F\x98\x8D",
50
+ 'flapping' => "\xF0\x9F\x90\x9D"
51
+ }
52
+ icons[@event['action']]
53
+ end
54
+
55
+ def check_status
56
+ @event['check']['status']
57
+ end
58
+
59
+ def occurrences
60
+ @event['occurrences']
61
+ end
62
+ def status_symbol
63
+ symbol = {
64
+ 0 => '✔',
65
+ 1 => '⚠',
66
+ 2 => '❌',
67
+ 3 => '❓'
68
+ }
69
+ symbol[check_status.to_i]
70
+ end
71
+
72
+ def status_to_string
73
+ status = {
74
+ 0 => 'OK',
75
+ 1 => 'Warning',
76
+ 2 => 'Critical',
77
+ 3 => 'Unknown'
78
+ }
79
+ status[check_status.to_i]
80
+ end
81
+
82
+ def short_name
83
+ client_name + '/' + check_name
84
+ end
85
+
86
+ def client_name
87
+ @event['client']['name']
88
+ end
89
+
90
+ def check_name
91
+ @event['check']['name']
92
+ end
93
+
94
+ def output
95
+ (@event['check']['output']).to_s.gsub(/(\s-p|\s-P|\s--password)(\s*\S+)/, '\1 <password omitted>')
96
+ end
97
+
98
+ def command
99
+ (@event['check']['command']).to_s.gsub(/(\s-p|\s-P|\s--password)(\s*\S+)/, '\1 <password omitted>')
100
+ end
101
+
102
+ def room_id
103
+ fetch_setting 'matrix_room_id'
104
+ end
105
+
106
+ def username
107
+ fetch_setting 'matrix_user'
108
+ end
109
+
110
+ def token
111
+ fetch_setting 'matrix_access_token'
112
+ end
113
+
114
+ def password
115
+ fetch_setting 'matrix_password'
116
+ end
117
+
118
+ def server
119
+ fetch_setting 'matrix_server'
120
+ end
121
+
122
+ def timeout
123
+ fetch_setting 'timeout' || 10
124
+ end
125
+
126
+ def default_message
127
+ [
128
+ '<b>Alert <%= action_to_string %></b> <%= action_symbol %>',
129
+ '<b>Host:</b> <%= client_name %>',
130
+ '<b>Check:</b> <%= check_name %>',
131
+ '<b>Status:</b> <%= status_to_string %> <%= status_symbol %>',
132
+ '<b>Occurrences:</b> <%= occurrences %>',
133
+ '<b>Output:<b> <pre><%= output %></pre>'
134
+ ].join("<br>")
135
+ end
136
+
137
+ def build_message
138
+ template_file = fetch_setting 'message_template_file'
139
+ if !template_file.nil?
140
+ template = File.read(template_file)
141
+ else
142
+ template = fetch_setting 'message_template'
143
+ template ||= default_message
144
+ end
145
+ message = ERB.new(template).result(binding)
146
+
147
+ message
148
+ end
149
+
150
+ def handle
151
+
152
+ ENV['SSL_CERT_FILE'] = '/etc/pki/tls/certs/ca-bundle.crt'
153
+
154
+ if settings[json_config].nil?
155
+ p "invalid config: #{options[:json_config]} you need to pass a key and not a file"
156
+ exit 3 # unknown
157
+ end
158
+
159
+ begin
160
+ Timeout.timeout timeout do
161
+ client = MatrixSdk::Client.new "https://#{server}"
162
+ if !token.nil?
163
+ client.api.access_token = token
164
+ client.sync
165
+ else
166
+ client.login username, password
167
+ end
168
+ room = client.find_room room_id
169
+ room.send_html build_message
170
+ # Logout if we logged in before with username and password
171
+ if token.nil?
172
+ client.logout
173
+ end
174
+ end
175
+ puts 'matrix -- sent alert for #{short_name} to room with id #{room_id}'
176
+ rescue Timeout::Error
177
+ puts 'matrix -- timed out while attempting to ' + @event['action'] + ' an incident -- #{short_name}'
178
+ rescue StandardError => e
179
+ puts "Matrix handler error '#{e.inspect}' while attempting to report an incident: #{short_name} #{action_to_string}"
180
+ end
181
+ end
182
+ end
@@ -0,0 +1 @@
1
+ require 'sensu-plugins-matrix/version'
@@ -0,0 +1,9 @@
1
+ module SensuPluginsMatrix
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ PATCH = 2
6
+
7
+ VER_STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,225 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sensu-plugins-matrix
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Seji64
8
+ - Sensu-Plugins and contributors
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2019-07-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: matrix_sdk
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: 1.2.1
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: 1.2.1
28
+ - !ruby/object:Gem::Dependency
29
+ name: sensu-plugin
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: '4.0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ version: '4.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: erubis
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '='
47
+ - !ruby/object:Gem::Version
48
+ version: 2.7.0
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '='
54
+ - !ruby/object:Gem::Version
55
+ version: 2.7.0
56
+ - !ruby/object:Gem::Dependency
57
+ name: bundler
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: '2.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '2.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: codeclimate-test-reporter
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: '1.0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ~>
82
+ - !ruby/object:Gem::Version
83
+ version: '1.0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: github-markup
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ~>
89
+ - !ruby/object:Gem::Version
90
+ version: '3.0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ~>
96
+ - !ruby/object:Gem::Version
97
+ version: '3.0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: pry
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ~>
103
+ - !ruby/object:Gem::Version
104
+ version: '0.10'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ~>
110
+ - !ruby/object:Gem::Version
111
+ version: '0.10'
112
+ - !ruby/object:Gem::Dependency
113
+ name: rake
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ~>
117
+ - !ruby/object:Gem::Version
118
+ version: '12.3'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: '12.3'
126
+ - !ruby/object:Gem::Dependency
127
+ name: redcarpet
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ~>
131
+ - !ruby/object:Gem::Version
132
+ version: '3.2'
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ~>
138
+ - !ruby/object:Gem::Version
139
+ version: '3.2'
140
+ - !ruby/object:Gem::Dependency
141
+ name: rubocop
142
+ requirement: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ~>
145
+ - !ruby/object:Gem::Version
146
+ version: 0.73.0
147
+ type: :development
148
+ prerelease: false
149
+ version_requirements: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ~>
152
+ - !ruby/object:Gem::Version
153
+ version: 0.73.0
154
+ - !ruby/object:Gem::Dependency
155
+ name: rspec
156
+ requirement: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ~>
159
+ - !ruby/object:Gem::Version
160
+ version: '3.4'
161
+ type: :development
162
+ prerelease: false
163
+ version_requirements: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ~>
166
+ - !ruby/object:Gem::Version
167
+ version: '3.4'
168
+ - !ruby/object:Gem::Dependency
169
+ name: yard
170
+ requirement: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ~>
173
+ - !ruby/object:Gem::Version
174
+ version: 0.9.11
175
+ type: :development
176
+ prerelease: false
177
+ version_requirements: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ~>
180
+ - !ruby/object:Gem::Version
181
+ version: 0.9.11
182
+ description: Sensu plugins for interfacing with Matrix Chat
183
+ email: <sensu-users@googlegroups.com>
184
+ executables:
185
+ - handler-matrix.rb
186
+ extensions: []
187
+ extra_rdoc_files: []
188
+ files:
189
+ - bin/handler-matrix.rb
190
+ - lib/sensu-plugins-matrix.rb
191
+ - lib/sensu-plugins-matrix/version.rb
192
+ - LICENSE
193
+ - README.md
194
+ - CHANGELOG.md
195
+ homepage: https://github.com/Seji64/sensu-plugins-matrix
196
+ licenses:
197
+ - MIT
198
+ metadata:
199
+ maintainer: Seji64
200
+ development_status: active
201
+ production_status: unstable - testing recommended
202
+ release_draft: 'false'
203
+ release_prerelease: 'false'
204
+ post_install_message: You can use the embedded Ruby by setting EMBEDDED_RUBY=true
205
+ in /etc/default/sensu
206
+ rdoc_options: []
207
+ require_paths:
208
+ - lib
209
+ required_ruby_version: !ruby/object:Gem::Requirement
210
+ requirements:
211
+ - - '>='
212
+ - !ruby/object:Gem::Version
213
+ version: 2.3.0
214
+ required_rubygems_version: !ruby/object:Gem::Requirement
215
+ requirements:
216
+ - - '>='
217
+ - !ruby/object:Gem::Version
218
+ version: '0'
219
+ requirements: []
220
+ rubyforge_project:
221
+ rubygems_version: 2.0.14.1
222
+ signing_key:
223
+ specification_version: 4
224
+ summary: Sensu handler for Matrix Chat
225
+ test_files: []