sensu-plugins-telegram 0.1.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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +9 -0
- data/LICENSE +22 -0
- data/README.md +60 -0
- data/bin/handler-telegram.rb +149 -0
- data/lib/sensu-plugins-telegram.rb +1 -0
- data/lib/sensu-plugins-telegram/version.rb +9 -0
- metadata +211 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a0456677ce532f35d78c76b88a2282b43ed592d2
|
4
|
+
data.tar.gz: 98aef9c764e82803cfdd9f845c563a55442c3014
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 76f5090947b9621d748bd9a95f15f6992ed188026442f39aba2aede7043e9c3cd17fa8e3b80966e82a722c623fc74e85a7838fa27d6f327bb8b656b8b43491dd
|
7
|
+
data.tar.gz: 27804ba3d44b433704bb4ca708d4986a0b01fc6fd4c3360f69c653f897dc2a906b2bfb1bdd59224059cb025e616a485d438436e6f4d933409c362cf19cb8b047
|
data/CHANGELOG.md
ADDED
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.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
## Sensu-Plugins-telegram
|
2
|
+
|
3
|
+
<!-- [ ](https://travis-ci.org/sensu-plugins/sensu-plugins-campfire)
|
4
|
+
[](http://badge.fury.io/rb/sensu-plugins-campfire)
|
5
|
+
[](https://codeclimate.com/github/sensu-plugins/sensu-plugins-campfire)
|
6
|
+
[](https://codeclimate.com/github/sensu-plugins/sensu-plugins-campfire)
|
7
|
+
[](https://gemnasium.com/sensu-plugins/sensu-plugins-campfire) -->
|
8
|
+
|
9
|
+
## Functionality
|
10
|
+
|
11
|
+
This plugin provides a handler to send notifications to a Telegram chat.
|
12
|
+
|
13
|
+
## Files
|
14
|
+
* bin/handler-telegram.rb
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
This gem requires a JSON configuration file with the following contents:
|
19
|
+
|
20
|
+
```json
|
21
|
+
{
|
22
|
+
"telegram": {
|
23
|
+
"bot_token": "YOUR_BOT_TOKEN",
|
24
|
+
"chat_id": -123123,
|
25
|
+
"error_file_location": "/tmp/telegram_handler_error"
|
26
|
+
}
|
27
|
+
}
|
28
|
+
```
|
29
|
+
|
30
|
+
### Parameters:
|
31
|
+
- `bot_token`: your bot's token, as provided by
|
32
|
+
[@BotFather](https://telegram.me/botfather).
|
33
|
+
- `chat_id`: the chat to which the error message is to be sent.
|
34
|
+
The bot must be a member of this channel or group.
|
35
|
+
You can get this chat_id by adding the bot to the corresponding group
|
36
|
+
and then accessing `https://api.telegram.org/bot<TOKEN>/getUpdates`.
|
37
|
+
- `error_file_location` (optional): in case there is a failure sending the
|
38
|
+
message to Telegram (ie. connectivity issues), the exception mesage will
|
39
|
+
be written to a file in this location. You can then monitor this
|
40
|
+
location to detect any errors with the Telegram handler.
|
41
|
+
|
42
|
+
If you want to send some events to one chat, and other events to another
|
43
|
+
chat, you can directly add the chat_id to the event data (the `@event` hash)
|
44
|
+
using a mutator. Then, create one handler specification for each channel,
|
45
|
+
specifying the corresponding mutator. For example:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
#!/usr/bin/env ruby
|
49
|
+
require 'rubygems'
|
50
|
+
require 'json'
|
51
|
+
event = JSON.parse(STDIN.read, :symbolize_names => true)
|
52
|
+
event.merge!(chat_id: -456456)
|
53
|
+
puts JSON.dump(event)
|
54
|
+
```
|
55
|
+
|
56
|
+
## Installation
|
57
|
+
|
58
|
+
[Installation and Setup](http://sensu-plugins.io/docs/installation_instructions.html)
|
59
|
+
|
60
|
+
## Notes
|
@@ -0,0 +1,149 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# handler-telegram
|
4
|
+
#
|
5
|
+
# DESCRIPTION:
|
6
|
+
# This handler sends messages to a given Telegram chat (person, group
|
7
|
+
# or channel).
|
8
|
+
#
|
9
|
+
# OUTPUT:
|
10
|
+
# Plain text
|
11
|
+
#
|
12
|
+
# PLATFORMS:
|
13
|
+
# Linux, BSD, Windows, OS X
|
14
|
+
#
|
15
|
+
# DEPENDENCIES:
|
16
|
+
# gem: sensu-plugin
|
17
|
+
# gem: rest-client
|
18
|
+
#
|
19
|
+
# USAGE:
|
20
|
+
# This gem requires a JSON configuration file with the following contents:
|
21
|
+
# {
|
22
|
+
# "telegram": {
|
23
|
+
# "bot_token": "YOUR_BOT_TOKEN",
|
24
|
+
# "chat_id": -123123,
|
25
|
+
# "error_file_location": "/tmp/telegram_handler_error"
|
26
|
+
# }
|
27
|
+
# }
|
28
|
+
#
|
29
|
+
# Parameters:
|
30
|
+
# - bot_token: your bot's token, as provided by @BotFather
|
31
|
+
# - chat_id: the chat to which the error message is to be sent.
|
32
|
+
# The bot must be a member of this channel or group.
|
33
|
+
# You can get this chat_id by adding the bot to the corresponding group
|
34
|
+
# and then accessing https://api.telegram.org/bot<TOKEN>/getUpdates
|
35
|
+
# - error_file_location (optional): in case there is a failure sending the
|
36
|
+
# message to Telegram (ie. connectivity issues), the exception mesage will
|
37
|
+
# be written to a file in this location. You can then monitor this
|
38
|
+
# location to detect any errors with the Telegram handler.
|
39
|
+
#
|
40
|
+
# If you want to send some events to one chat, and other events to another
|
41
|
+
# chat, you can directly add the chat_id to the event data (the `@event` hash)
|
42
|
+
# using a mutator. For example:
|
43
|
+
# #!/usr/bin/env ruby
|
44
|
+
# require 'rubygems'
|
45
|
+
# require 'json'
|
46
|
+
# event = JSON.parse(STDIN.read, :symbolize_names => true)
|
47
|
+
# event.merge!(chat_id: -456456)
|
48
|
+
# puts JSON.dump(event)
|
49
|
+
#
|
50
|
+
# NOTES:
|
51
|
+
#
|
52
|
+
# LICENSE:
|
53
|
+
#
|
54
|
+
# Copyright 2016 Hernan Schmidt <hschmidt@suse.de>
|
55
|
+
# Released under the same terms as Sensu (the MIT license); see LICENSE
|
56
|
+
# for details.
|
57
|
+
|
58
|
+
require 'rubygems' if RUBY_VERSION < '1.9.0'
|
59
|
+
require 'sensu-handler'
|
60
|
+
require 'restclient'
|
61
|
+
|
62
|
+
class TelegramHandler < Sensu::Handler
|
63
|
+
|
64
|
+
def chat_id
|
65
|
+
@event['chat_id'] || settings['telegram']['chat_id']
|
66
|
+
end
|
67
|
+
|
68
|
+
def bot_token
|
69
|
+
settings['telegram']['bot_token']
|
70
|
+
end
|
71
|
+
|
72
|
+
def error_file
|
73
|
+
settings['telegram']['error_file_location']
|
74
|
+
end
|
75
|
+
|
76
|
+
def event_name
|
77
|
+
@event['client']['name'] + '/' + @event['check']['name']
|
78
|
+
end
|
79
|
+
|
80
|
+
def action_name
|
81
|
+
actions = {
|
82
|
+
'create' => 'Created',
|
83
|
+
'resolve' => 'Resolved',
|
84
|
+
'flapping' => 'Flapping'
|
85
|
+
}
|
86
|
+
actions[@event['action']]
|
87
|
+
end
|
88
|
+
|
89
|
+
def telegram_url
|
90
|
+
"https://api.telegram.org/bot#{bot_token}/sendMessage"
|
91
|
+
end
|
92
|
+
|
93
|
+
def build_message
|
94
|
+
[
|
95
|
+
"<b>Alert #{action_name}</b>",
|
96
|
+
"<b>Host:</b> #{@event['client']['name']}",
|
97
|
+
"<b>Check:</b> #{@event['check']['name']}",
|
98
|
+
"<b>Status:</b> #{translate_status}",
|
99
|
+
"<b>Output:</b> #{@event['check']['output']}"
|
100
|
+
].join("\n")
|
101
|
+
end
|
102
|
+
|
103
|
+
def params
|
104
|
+
{
|
105
|
+
chat_id: chat_id,
|
106
|
+
text: build_message,
|
107
|
+
parse_mode: 'HTML',
|
108
|
+
disable_web_page_preview: true
|
109
|
+
}
|
110
|
+
end
|
111
|
+
|
112
|
+
def handle_error(exception)
|
113
|
+
open(error_file, 'w') do |f|
|
114
|
+
f.puts "URL: " + telegram_url
|
115
|
+
f.puts "Params: " + params.inspect
|
116
|
+
f.puts "Exception: " + exception.inspect
|
117
|
+
end if error_file
|
118
|
+
end
|
119
|
+
|
120
|
+
def clear_error
|
121
|
+
File.delete(error_file) if File.exist?(error_file)
|
122
|
+
end
|
123
|
+
|
124
|
+
def handle
|
125
|
+
begin
|
126
|
+
print "Sending to Telegram: " + params.inspect
|
127
|
+
RestClient.post(telegram_url, params, format: :json)
|
128
|
+
puts "... done."
|
129
|
+
clear_error
|
130
|
+
rescue SocketError, RestClient::Exception => e
|
131
|
+
handle_error(e)
|
132
|
+
puts "... Telegram handler error '#{e.inspect}' while attempting to report an incident: #{event_name} #{action_name}"
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def check_status
|
137
|
+
@event['check']['status']
|
138
|
+
end
|
139
|
+
|
140
|
+
def translate_status
|
141
|
+
status = {
|
142
|
+
0 => 'OK',
|
143
|
+
1 => 'Warning',
|
144
|
+
2 => 'Critical',
|
145
|
+
3 => 'Unknown'
|
146
|
+
}
|
147
|
+
status[check_status.to_i]
|
148
|
+
end
|
149
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'sensu-plugins-telegram/version'
|
metadata
ADDED
@@ -0,0 +1,211 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sensu-plugins-telegram
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hernan Schmidt
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sensu-plugin
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rest-client
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.8'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.8'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: codeclimate-test-reporter
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.4'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.4'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: github-markup
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.3'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.3'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.10'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.10'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rake
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '10.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '10.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: redcarpet
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '3.2'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '3.2'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rubocop
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - '='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 0.37.0
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - '='
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 0.37.0
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: rspec
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '3.1'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '3.1'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: yard
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - "~>"
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0.8'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - "~>"
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0.8'
|
167
|
+
description: Sensu plugins for interfacing with Telegram messenger
|
168
|
+
email: "<hschmidt@suse.de>"
|
169
|
+
executables:
|
170
|
+
- handler-telegram.rb
|
171
|
+
extensions: []
|
172
|
+
extra_rdoc_files: []
|
173
|
+
files:
|
174
|
+
- CHANGELOG.md
|
175
|
+
- LICENSE
|
176
|
+
- README.md
|
177
|
+
- bin/handler-telegram.rb
|
178
|
+
- lib/sensu-plugins-telegram.rb
|
179
|
+
- lib/sensu-plugins-telegram/version.rb
|
180
|
+
homepage: https://github.com/lagartoflojo/sensu-plugins-telegram
|
181
|
+
licenses:
|
182
|
+
- MIT
|
183
|
+
metadata:
|
184
|
+
maintainer: sensu-plugin
|
185
|
+
development_status: active
|
186
|
+
production_status: unstable - testing recommended
|
187
|
+
release_draft: 'false'
|
188
|
+
release_prerelease: 'false'
|
189
|
+
post_install_message: You can use the embedded Ruby by setting EMBEDDED_RUBY=true
|
190
|
+
in /etc/default/sensu
|
191
|
+
rdoc_options: []
|
192
|
+
require_paths:
|
193
|
+
- lib
|
194
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
195
|
+
requirements:
|
196
|
+
- - ">="
|
197
|
+
- !ruby/object:Gem::Version
|
198
|
+
version: 1.9.3
|
199
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
200
|
+
requirements:
|
201
|
+
- - ">="
|
202
|
+
- !ruby/object:Gem::Version
|
203
|
+
version: '0'
|
204
|
+
requirements: []
|
205
|
+
rubyforge_project:
|
206
|
+
rubygems_version: 2.4.8
|
207
|
+
signing_key:
|
208
|
+
specification_version: 4
|
209
|
+
summary: Sensu plugins for interfacing with Telegram messenger
|
210
|
+
test_files: []
|
211
|
+
has_rdoc:
|