sensu-plugins-rocket-chat 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 907a6317875acb87e033e8c46fe88f61ba8de161
4
+ data.tar.gz: 85b93fbb5d86fbbeea387370b8dbfbe7664e6054
5
+ SHA512:
6
+ metadata.gz: 8d1eeae7a405e018fab67e1e5f5aed984d2c30c4609e491b3c0ba753e3f80a5ef08c05f6da746452823577bf76319e52460974b794001787e5b96e75c6151c4f
7
+ data.tar.gz: ff42064a664074cd28709558911c57d0735fd35d41e4bc07fde163c11e9f59733d1567319a63e1a37fe36d2f84517eed114a9f2474d7f0b6f474b83dbe62b947
@@ -0,0 +1,10 @@
1
+ # Change Log
2
+ This project adheres to [Semantic Versioning](http://semver.org/).
3
+
4
+ This CHANGELOG follows [this format](https://github.com/sensu-plugins/community/blob/master/HOW_WE_CHANGELOG.md).
5
+
6
+ ## [Unreleased]
7
+
8
+ ## 1.0.1 - 2018-11-20
9
+ ### Added
10
+ - Initial release
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Hernan Schmidt
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,146 @@
1
+ ## Sensu-Plugins-RocketChat
2
+
3
+ [![Build Status](https://travis-ci.org/sensu-plugins/sensu-plugins-rocket-chat.svg?branch=master)](https://travis-ci.org/sensu-plugins/sensu-plugins-rocket-chat)
4
+ [![Gem Version](https://badge.fury.io/rb/sensu-plugins-rocket-chat.svg)](http://badge.fury.io/rb/sensu-plugins-rocket-chat)
5
+
6
+ ## Functionality
7
+
8
+ This plugin provides a handler to send notifications to a RocketChat chat.
9
+
10
+ ## Files
11
+ * bin/handler-rocketchat.rb
12
+
13
+ ## Usage
14
+
15
+ After installation, you have to set up a `pipe` type handler, like so:
16
+
17
+ ```json
18
+ {
19
+ "handlers": {
20
+ "rocketchat": {
21
+ "type": "pipe",
22
+ "command": "handler-rocketchat.rb",
23
+ "filter": "occurrences"
24
+ }
25
+ }
26
+ }
27
+ ```
28
+
29
+ This gem also expects a JSON configuration file with the following contents:
30
+
31
+ ```json
32
+ {
33
+ "rocketchat": {
34
+ "server_url": "SERVER_URL",
35
+ "username": "YOUR_USERNAME",
36
+ "password": "YOUR_PASSWORD",
37
+ "room_id": "ROOM_ID",
38
+ "error_file_location": "/tmp/rocketchat_handler_error"
39
+ }
40
+ }
41
+ ```
42
+
43
+ ### Parameters:
44
+ - `server_url`: The URL of the RocketChat instance (e.g.
45
+ `https://chat.yourcompany.com`).
46
+ - `ssl_ca_file` (optional): The path to the certificate file used by the
47
+ RocketChat server.
48
+ - `ssl_verify_mode` (optional): Value `1` will try to verify the SSL certificate
49
+ of the RocketChat server (default), while `0` will skip verification (not
50
+ recommended).
51
+ - `username`: The username of the RocketChat account that will be used to send
52
+ the messages. You probably want to use a special "bot" account for this.
53
+ - `password`: The password of the RocketChat account that will be used to send
54
+ the messages.
55
+ - `room_id`: The room to which the error message is to be sent. To send the
56
+ message to a user, `room_id` should look like `@username`. For a channel,
57
+ `channel-name`.
58
+ - `error_file_location` (optional): in case there is a failure sending the
59
+ message to RocketChat (ie. connectivity issues), the exception message will
60
+ be written to a file in this location. You can then monitor this
61
+ location to detect any errors with the RocketChat handler.
62
+ - `message_template` (optional): An ERB template in Markdown to use to format
63
+ messages instead of the default. Supports the following variables:
64
+ - `action_name`
65
+ - `action_icon`
66
+ - `client_name`
67
+ - `check_name`
68
+ - `status`
69
+ - `status_icon`
70
+ - `output`
71
+ - `message_template_file` (optional): A file to read an ERB template in Markdown
72
+ from to format messages. Supports the same variables as `message_template`.
73
+
74
+
75
+ ### Advanced configuration
76
+
77
+ By default, the handler assumes that the config parameters are specified in the
78
+ `rocketchat` top-level key of the JSON, as shown above. You also have the option
79
+ to make the handler fetch the config from a different key. To do this, pass the
80
+ `-j` option to the handler with the name of the desired key You can define
81
+ multiple handlers, and each handler can send notifications to a different room
82
+ and from a different bot. You could, for example, have critical and non-critical
83
+ RocketChat rooms, and send the notifications to one or the other depending on the
84
+ check. For example:
85
+
86
+ ```json
87
+ {
88
+ "handlers": {
89
+ "critical_rocketchat": {
90
+ "type": "pipe",
91
+ "command": "handler-rocketchat.rb -j critical_rocketchat_options"
92
+ },
93
+ "non_critical_rocketchat": {
94
+ "type": "pipe",
95
+ "command": "handler-rocketchat.rb -j non_critical_rocketchat_options"
96
+ }
97
+ }
98
+ }
99
+ ```
100
+
101
+ This example will fetch the options from a JSON like this:
102
+
103
+ ```json
104
+ {
105
+ "rocketchat": {
106
+ "server_url": "SERVER_URL",
107
+ "username": "YOUR_USERNAME",
108
+ "password": "YOUR_PASSWORD"
109
+ },
110
+ "critical_rocketchat_options": {
111
+ "room_id": "emergency-alerts"
112
+ },
113
+ "non_critical_rocketchat_options": {
114
+ "room_id": "non-critical-alerts"
115
+ }
116
+ }
117
+ ```
118
+
119
+ As you can see, you can specify the default config in the `rocketchat` key, and
120
+ the rest of the config in their own custom keys.
121
+
122
+ You can also directly add the configuration parameters to the event data using a
123
+ mutator. For example:
124
+
125
+ ```ruby
126
+ #!/usr/bin/env ruby
127
+ require 'rubygems'
128
+ require 'json'
129
+ event = JSON.parse(STDIN.read, symbolize_names: true)
130
+ event.merge!(room_id: 'emergency-alerts')
131
+ puts JSON.dump(event)
132
+ ```
133
+
134
+ ### Configuration precedence
135
+
136
+ The handler will load the config as follows (from least to most priority):
137
+
138
+ * Default `rocketchat` key
139
+ * Custom config keys
140
+ * Event data
141
+
142
+ ## Installation
143
+
144
+ [Installation and Setup](http://sensu-plugins.io/docs/installation_instructions.html)
145
+
146
+ ## Notes
@@ -0,0 +1,219 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # handler-rocketchat
6
+ #
7
+ # DESCRIPTION:
8
+ # This handler sends messages to a given RocketChat room (user or channel).
9
+ #
10
+ # OUTPUT:
11
+ # Plain text
12
+ #
13
+ # PLATFORMS:
14
+ # Linux, BSD, Windows, OS X
15
+ #
16
+ # DEPENDENCIES:
17
+ # gem: sensu-plugin
18
+ # gem: rocketchat
19
+ #
20
+ # USAGE:
21
+ # This gem requires a JSON configuration file with the following contents:
22
+ # {
23
+ # "rocketchat": {
24
+ # "server_url": "SERVER_URL"
25
+ # "username": "YOUR_USERNAME",
26
+ # "password": "YOUR_PASSWORD",
27
+ # "room_id": "ROOM_ID",
28
+ # "error_file_location": "/tmp/rocketchat_handler_error"
29
+ # }
30
+ # }
31
+ # For more details, please see the README.
32
+ #
33
+ # NOTES:
34
+ #
35
+ # LICENSE:
36
+ #
37
+ # Copyright 2018 Hernan Schmidt <hschmidt@suse.de>
38
+ # Released under the same terms as Sensu (the MIT license); see LICENSE
39
+ # for details.
40
+
41
+ require 'sensu-handler'
42
+ require 'rocketchat'
43
+ require 'cgi'
44
+ require 'erb'
45
+
46
+ class RocketChatHandler < Sensu::Handler
47
+ option :json_config,
48
+ description: 'Config name',
49
+ short: '-j config_key',
50
+ long: '--json_config config_key',
51
+ required: false
52
+
53
+ def handle
54
+ print 'Sending to RocketChat: ' + params.inspect
55
+
56
+ rocket_server = RocketChat::Server.new(server_url, server_settings)
57
+ session = rocket_server.login(username, password)
58
+ session.chat.post_message(room_id: room_id,
59
+ text: build_message)
60
+ session.logout
61
+
62
+ puts '... done.'
63
+ clear_error
64
+ rescue StandardError => e
65
+ handle_error(e)
66
+ puts "... RocketChat handler error '#{e.inspect}' while attempting to report an incident: #{event_name} #{action_name}"
67
+ end
68
+
69
+ def room_id
70
+ fetch_setting 'room_id'
71
+ end
72
+
73
+ def username
74
+ fetch_setting 'username'
75
+ end
76
+
77
+ def password
78
+ fetch_setting 'password'
79
+ end
80
+
81
+ def error_file
82
+ fetch_setting 'error_file_location'
83
+ end
84
+
85
+ def server_url
86
+ fetch_setting 'server_url'
87
+ end
88
+
89
+ def ssl_ca_file
90
+ fetch_setting('ssl_ca_file')
91
+ end
92
+
93
+ def ssl_verify_mode
94
+ fetch_setting('ssl_verify_mode')
95
+ end
96
+
97
+ def server_settings
98
+ settings = {}
99
+ settings[:ssl_ca_file] = ssl_ca_file if ssl_ca_file
100
+ settings[:ssl_verify_mode] = ssl_verify_mode if ssl_verify_mode
101
+ settings
102
+ end
103
+
104
+ def fetch_setting(setting_key)
105
+ config_key = config[:json_config]
106
+
107
+ value = @event[setting_key]
108
+ value ||= settings[config_key][setting_key] if config_key
109
+ value || settings['rocketchat'][setting_key]
110
+ end
111
+
112
+ def event_name
113
+ client_name + '/' + check_name
114
+ end
115
+
116
+ def action_name
117
+ actions = {
118
+ 'create' => 'Created',
119
+ 'resolve' => 'Resolved',
120
+ 'flapping' => 'Flapping'
121
+ }
122
+ actions[@event['action']]
123
+ end
124
+
125
+ def action_icon
126
+ icons = {
127
+ 'create' => "\xF0\x9F\x98\xB1",
128
+ 'resolve' => "\xF0\x9F\x98\x8D",
129
+ 'flapping' => "\xF0\x9F\x90\x9D"
130
+ }
131
+ icons[@event['action']]
132
+ end
133
+
134
+ def client_name
135
+ escape_html @event['client']['name']
136
+ end
137
+
138
+ def check_name
139
+ escape_html @event['check']['name']
140
+ end
141
+
142
+ def output
143
+ escape_html @event['check']['output']
144
+ end
145
+
146
+ def escape_html(string)
147
+ CGI.escapeHTML(string)
148
+ end
149
+
150
+ def build_message
151
+ template_file = fetch_setting 'message_template_file'
152
+ if !template_file.nil?
153
+ template = File.read(template_file)
154
+ else
155
+ template = fetch_setting 'message_template'
156
+ template ||= default_message
157
+ end
158
+
159
+ message = ERB.new(template).result(binding)
160
+
161
+ message
162
+ end
163
+
164
+ def default_message
165
+ [
166
+ '**Alert <%= action_name %>** <%= action_icon %>',
167
+ '**Host:** <%= client_name %>',
168
+ '**Check:** <%= check_name %>',
169
+ '**Status:** <%= status %> <%= status_icon %>',
170
+ '**Output:** `<%= output %>`'
171
+ ].join("\n")
172
+ end
173
+
174
+ def params
175
+ {
176
+ server_url: server_url,
177
+ server_settings: server_settings,
178
+ room_id: room_id,
179
+ text: build_message
180
+ }
181
+ end
182
+
183
+ def handle_error(exception)
184
+ return unless error_file
185
+
186
+ File.open(error_file, 'w') do |f|
187
+ f.puts 'Params: ' + params.inspect
188
+ f.puts 'Exception: ' + exception.inspect
189
+ end
190
+ end
191
+
192
+ def clear_error
193
+ File.delete(error_file) if error_file && File.exist?(error_file)
194
+ end
195
+
196
+ def check_status
197
+ @event['check']['status']
198
+ end
199
+
200
+ def status
201
+ status = {
202
+ 0 => 'OK',
203
+ 1 => 'Warning',
204
+ 2 => 'Critical',
205
+ 3 => 'Unknown'
206
+ }
207
+ status[check_status.to_i]
208
+ end
209
+
210
+ def status_icon
211
+ icons = {
212
+ 0 => "\xF0\x9F\x91\x8D",
213
+ 1 => "\xE2\x9A\xA1",
214
+ 2 => "\xF0\x9F\x9A\xA8",
215
+ 3 => "\xF0\x9F\x8C\x80"
216
+ }
217
+ icons[check_status.to_i]
218
+ end
219
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sensu-plugins-rocketchat/version'
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SensuPluginsRocketChat
4
+ module Version
5
+ MAJOR = 1
6
+ MINOR = 0
7
+ PATCH = 1
8
+
9
+ VER_STRING = [MAJOR, MINOR, PATCH].compact.join('.')
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,213 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sensu-plugins-rocket-chat
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Hernan Schmidt
8
+ - Sensu-Plugins and contributors
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2018-11-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rocketchat
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 0.1.16
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 0.1.16
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: '2.7'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '2.7'
42
+ - !ruby/object:Gem::Dependency
43
+ name: bundler
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '1.7'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '1.7'
56
+ - !ruby/object:Gem::Dependency
57
+ name: codeclimate-test-reporter
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '0.4'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '0.4'
70
+ - !ruby/object:Gem::Dependency
71
+ name: github-markup
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '1.3'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '1.3'
84
+ - !ruby/object:Gem::Dependency
85
+ name: pry
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '0.10'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '0.10'
98
+ - !ruby/object:Gem::Dependency
99
+ name: rake
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - "~>"
103
+ - !ruby/object:Gem::Version
104
+ version: '10.5'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: '10.5'
112
+ - !ruby/object:Gem::Dependency
113
+ name: redcarpet
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - "~>"
117
+ - !ruby/object:Gem::Version
118
+ version: '3.2'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - "~>"
124
+ - !ruby/object:Gem::Version
125
+ version: '3.2'
126
+ - !ruby/object:Gem::Dependency
127
+ name: rspec
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - "~>"
131
+ - !ruby/object:Gem::Version
132
+ version: '3.4'
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: '3.4'
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.60.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.60.0
154
+ - !ruby/object:Gem::Dependency
155
+ name: yard
156
+ requirement: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - "~>"
159
+ - !ruby/object:Gem::Version
160
+ version: 0.9.11
161
+ type: :development
162
+ prerelease: false
163
+ version_requirements: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - "~>"
166
+ - !ruby/object:Gem::Version
167
+ version: 0.9.11
168
+ description: Sensu plugin for interfacing with RocketChat
169
+ email:
170
+ - hschmidt@suse.de
171
+ - sensu-users@googlegroups.com
172
+ executables:
173
+ - handler-rocketchat.rb
174
+ extensions: []
175
+ extra_rdoc_files: []
176
+ files:
177
+ - CHANGELOG.md
178
+ - LICENSE
179
+ - README.md
180
+ - bin/handler-rocketchat.rb
181
+ - lib/sensu-plugins-rocketchat.rb
182
+ - lib/sensu-plugins-rocketchat/version.rb
183
+ homepage: https://github.com/lagartoflojo/sensu-plugins-rocket-chat
184
+ licenses:
185
+ - MIT
186
+ metadata:
187
+ maintainer: lagartoflojo
188
+ development_status: active
189
+ production_status: unstable - testing recommended
190
+ release_draft: 'false'
191
+ release_prerelease: 'false'
192
+ post_install_message: You can use the embedded Ruby by setting EMBEDDED_RUBY=true
193
+ in /etc/default/sensu
194
+ rdoc_options: []
195
+ require_paths:
196
+ - lib
197
+ required_ruby_version: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: 2.3.0
202
+ required_rubygems_version: !ruby/object:Gem::Requirement
203
+ requirements:
204
+ - - ">="
205
+ - !ruby/object:Gem::Version
206
+ version: '0'
207
+ requirements: []
208
+ rubyforge_project:
209
+ rubygems_version: 2.6.14.1
210
+ signing_key:
211
+ specification_version: 4
212
+ summary: Sensu handler for RocketChat
213
+ test_files: []