sensu-plugins-telegram 0.1.1 → 0.2.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 +4 -4
- data/README.md +69 -5
- data/bin/handler-telegram.rb +71 -44
- data/lib/sensu-plugins-telegram/version.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b27c66b68f1ee795b6aa43157a5fd6f41d215582
|
4
|
+
data.tar.gz: d5819eb9b5e000459f20265a4e4e60461ccee9da
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a27771ac2da30b8a25a711777c6450d605876c73c8cd586af2ed7b99d469f433744fa5caf98cc78ed6b40d866e337357f93e4ead52937c328b53242aa19b36a
|
7
|
+
data.tar.gz: c104cab7aa44ec827173e55fe3f087a8820637519824f88973057da4dc576a8f3a51a9798ff67037b8c16a6b1918ccf20376b968e2d889e1a18ca9d03e9c9c42
|
data/README.md
CHANGED
@@ -15,7 +15,20 @@ This plugin provides a handler to send notifications to a Telegram chat.
|
|
15
15
|
|
16
16
|
## Usage
|
17
17
|
|
18
|
-
|
18
|
+
After installation, you have to set up a `pipe` type handler, like so:
|
19
|
+
|
20
|
+
```json
|
21
|
+
{
|
22
|
+
"handlers": {
|
23
|
+
"telegram": {
|
24
|
+
"type": "pipe",
|
25
|
+
"command": "handler-telegram.rb"
|
26
|
+
}
|
27
|
+
}
|
28
|
+
}
|
29
|
+
```
|
30
|
+
|
31
|
+
This gem also expects a JSON configuration file with the following contents:
|
19
32
|
|
20
33
|
```json
|
21
34
|
{
|
@@ -39,10 +52,53 @@ This gem requires a JSON configuration file with the following contents:
|
|
39
52
|
be written to a file in this location. You can then monitor this
|
40
53
|
location to detect any errors with the Telegram handler.
|
41
54
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
55
|
+
### Advanced configuration
|
56
|
+
|
57
|
+
By default, the handler assumes that the config parameters are specified in the
|
58
|
+
`telegram` top-level key of the JSON, as shown above. You also have the option
|
59
|
+
to make the handler fetch the config from a different key. To do this, pass the
|
60
|
+
`-j` option to the handler with the name of the desired key You can define
|
61
|
+
multiple handlers, and each handler can send notifications to a different chat
|
62
|
+
and from a different bot. You could, for example, have critical and non-critical
|
63
|
+
Telegram groups, and send the notifications to one or the other depending on the
|
64
|
+
check. For example:
|
65
|
+
|
66
|
+
```json
|
67
|
+
{
|
68
|
+
"handlers": {
|
69
|
+
"critical_telegram": {
|
70
|
+
"type": "pipe",
|
71
|
+
"command": "handler-telegram.rb -j critical_telegram_options"
|
72
|
+
},
|
73
|
+
"non_critical_telegram": {
|
74
|
+
"type": "pipe",
|
75
|
+
"command": "handler-telegram.rb -j non_critical_telegram_options"
|
76
|
+
}
|
77
|
+
}
|
78
|
+
}
|
79
|
+
```
|
80
|
+
|
81
|
+
This example will fetch the options from a JSON like this:
|
82
|
+
|
83
|
+
```json
|
84
|
+
{
|
85
|
+
"telegram": {
|
86
|
+
"bot_token": "YOUR_BOT_TOKEN"
|
87
|
+
},
|
88
|
+
"critical_telegram_options": {
|
89
|
+
"chat_id": -123123
|
90
|
+
},
|
91
|
+
"non_critical_telegram_options": {
|
92
|
+
"chat_id": -456456
|
93
|
+
}
|
94
|
+
}
|
95
|
+
```
|
96
|
+
|
97
|
+
As you can see, you can specify the default config in the `telegram` key, and
|
98
|
+
the rest of the config in their own custom keys.
|
99
|
+
|
100
|
+
You can also directly add the configuration parameters to the event data using a
|
101
|
+
mutator. For example:
|
46
102
|
|
47
103
|
```ruby
|
48
104
|
#!/usr/bin/env ruby
|
@@ -53,6 +109,14 @@ event.merge!(chat_id: -456456)
|
|
53
109
|
puts JSON.dump(event)
|
54
110
|
```
|
55
111
|
|
112
|
+
### Configuration precedence
|
113
|
+
|
114
|
+
The handler will load the config as follows (from least to most priority):
|
115
|
+
|
116
|
+
* Default `telegram` key
|
117
|
+
* Custom config keys
|
118
|
+
* Event data
|
119
|
+
|
56
120
|
## Installation
|
57
121
|
|
58
122
|
[Installation and Setup](http://sensu-plugins.io/docs/installation_instructions.html)
|
data/bin/handler-telegram.rb
CHANGED
@@ -25,27 +25,7 @@
|
|
25
25
|
# "error_file_location": "/tmp/telegram_handler_error"
|
26
26
|
# }
|
27
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)
|
28
|
+
# For more details, please see the README.
|
49
29
|
#
|
50
30
|
# NOTES:
|
51
31
|
#
|
@@ -58,23 +38,37 @@
|
|
58
38
|
require 'rubygems' if RUBY_VERSION < '1.9.0'
|
59
39
|
require 'sensu-handler'
|
60
40
|
require 'restclient'
|
41
|
+
require 'cgi'
|
61
42
|
|
62
43
|
class TelegramHandler < Sensu::Handler
|
44
|
+
option :json_config,
|
45
|
+
description: 'Config name',
|
46
|
+
short: '-j config_key',
|
47
|
+
long: '--json_config config_key',
|
48
|
+
required: false
|
63
49
|
|
64
50
|
def chat_id
|
65
|
-
|
51
|
+
fetch_setting 'chat_id'
|
66
52
|
end
|
67
53
|
|
68
54
|
def bot_token
|
69
|
-
|
55
|
+
fetch_setting 'bot_token'
|
70
56
|
end
|
71
57
|
|
72
58
|
def error_file
|
73
|
-
|
59
|
+
fetch_setting 'error_file_location'
|
60
|
+
end
|
61
|
+
|
62
|
+
def fetch_setting(setting_key)
|
63
|
+
config_key = config[:json_config]
|
64
|
+
|
65
|
+
value = @event[setting_key]
|
66
|
+
value ||= settings[config_key][setting_key] if config_key
|
67
|
+
value || settings['telegram'][setting_key]
|
74
68
|
end
|
75
69
|
|
76
70
|
def event_name
|
77
|
-
|
71
|
+
client_name + '/' + check_name
|
78
72
|
end
|
79
73
|
|
80
74
|
def action_name
|
@@ -86,17 +80,42 @@ class TelegramHandler < Sensu::Handler
|
|
86
80
|
actions[@event['action']]
|
87
81
|
end
|
88
82
|
|
83
|
+
def action_icon
|
84
|
+
icons = {
|
85
|
+
'create' => "\xF0\x9F\x98\xB1",
|
86
|
+
'resolve' => "\xF0\x9F\x98\x8D",
|
87
|
+
'flapping' => "\xF0\x9F\x90\x9D"
|
88
|
+
}
|
89
|
+
icons[@event['action']]
|
90
|
+
end
|
91
|
+
|
92
|
+
def client_name
|
93
|
+
escape_html @event['client']['name']
|
94
|
+
end
|
95
|
+
|
96
|
+
def check_name
|
97
|
+
escape_html @event['check']['name']
|
98
|
+
end
|
99
|
+
|
100
|
+
def output
|
101
|
+
escape_html @event['check']['output']
|
102
|
+
end
|
103
|
+
|
104
|
+
def escape_html(string)
|
105
|
+
CGI.escapeHTML(string)
|
106
|
+
end
|
107
|
+
|
89
108
|
def telegram_url
|
90
109
|
"https://api.telegram.org/bot#{bot_token}/sendMessage"
|
91
110
|
end
|
92
111
|
|
93
112
|
def build_message
|
94
113
|
[
|
95
|
-
"<b>Alert #{action_name}</b>",
|
96
|
-
"<b>Host:</b> #{
|
97
|
-
"<b>Check:</b> #{
|
98
|
-
"<b>Status:</b> #{
|
99
|
-
"<b>Output:</b>
|
114
|
+
"<b>Alert #{action_name}</b> #{action_icon}",
|
115
|
+
"<b>Host:</b> #{client_name}",
|
116
|
+
"<b>Check:</b> #{check_name}",
|
117
|
+
"<b>Status:</b> #{status} #{status_icon}",
|
118
|
+
"<b>Output:</b> <code>#{output}</code>"
|
100
119
|
].join("\n")
|
101
120
|
end
|
102
121
|
|
@@ -111,33 +130,31 @@ class TelegramHandler < Sensu::Handler
|
|
111
130
|
|
112
131
|
def handle_error(exception)
|
113
132
|
open(error_file, 'w') do |f|
|
114
|
-
f.puts
|
115
|
-
f.puts
|
116
|
-
f.puts
|
133
|
+
f.puts 'URL: ' + telegram_url
|
134
|
+
f.puts 'Params: ' + params.inspect
|
135
|
+
f.puts 'Exception: ' + exception.inspect
|
117
136
|
end if error_file
|
118
137
|
end
|
119
138
|
|
120
139
|
def clear_error
|
121
|
-
File.delete(error_file) if File.exist?(error_file)
|
140
|
+
File.delete(error_file) if error_file && File.exist?(error_file)
|
122
141
|
end
|
123
142
|
|
124
143
|
def handle
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
puts "... Telegram handler error '#{e.inspect}' while attempting to report an incident: #{event_name} #{action_name}"
|
133
|
-
end
|
144
|
+
print 'Sending to Telegram: ' + params.inspect
|
145
|
+
RestClient.post(telegram_url, params, format: :json)
|
146
|
+
puts '... done.'
|
147
|
+
clear_error
|
148
|
+
rescue SocketError, RestClient::Exception => e
|
149
|
+
handle_error(e)
|
150
|
+
puts "... Telegram handler error '#{e.inspect}' while attempting to report an incident: #{event_name} #{action_name}"
|
134
151
|
end
|
135
152
|
|
136
153
|
def check_status
|
137
154
|
@event['check']['status']
|
138
155
|
end
|
139
156
|
|
140
|
-
def
|
157
|
+
def status
|
141
158
|
status = {
|
142
159
|
0 => 'OK',
|
143
160
|
1 => 'Warning',
|
@@ -146,4 +163,14 @@ class TelegramHandler < Sensu::Handler
|
|
146
163
|
}
|
147
164
|
status[check_status.to_i]
|
148
165
|
end
|
166
|
+
|
167
|
+
def status_icon
|
168
|
+
icons = {
|
169
|
+
0 => "\xF0\x9F\x91\x8D",
|
170
|
+
1 => "\xE2\x9A\xA1",
|
171
|
+
2 => "\xF0\x9F\x9A\xA8",
|
172
|
+
3 => "\xF0\x9F\x8C\x80"
|
173
|
+
}
|
174
|
+
icons[check_status.to_i]
|
175
|
+
end
|
149
176
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sensu-plugins-telegram
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hernan Schmidt
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sensu-plugin
|