sensu-plugins-microsoft-teams 0.0.5 → 0.0.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 260a88dbeb260219a3bd8c3e94938538ef4c0278
4
- data.tar.gz: ce1058ccc6bb7a057ce6f777111168090a111a56
3
+ metadata.gz: 760e0374a37eb151039fdde0fd61324fbfbfed94
4
+ data.tar.gz: 37532f42c0eaa461ad723bb75e694d575b307dc6
5
5
  SHA512:
6
- metadata.gz: be81ed9abb2a282f6dcb29095b630fbc4cb9230c04c87e2df942787b65b025f254e012151a627cab44be610f6b20866b40e0e6400d607bf120bb9898e6b973a4
7
- data.tar.gz: 530a8a6bedd3e3aedebe4f0e461c9f086e246938751cfbdf5e71d1701ac21857754004807eb7b8be8861a027cf05f59c255546850e9ff89b2ae3e4130f6d92d0
6
+ metadata.gz: a27faa41bcaf3c36c9f64b6830eb842ea80ad1c65247f3e8c8afabe5a885ab98236fc27473a0a352c99d5a448bcc34b79e85be0e19e16bc355973ba3f1dcf4e3
7
+ data.tar.gz: 00439e3e84748bd6a652839d39cfda5de02a86f3e299c7ea0dd944e20e86a8e5f3a73f17fce718cf491e2fb3a08aab2079f68aa084089fe090b7f9e7876e770b
@@ -6,13 +6,15 @@
6
6
  # for details.
7
7
  #
8
8
  # In order to use this plugin, you must first configure an incoming webhook
9
- # integration in Microsoft Teams. You can create the required webhook by visiting
10
- # https://outlook.office.com/connectors/publish
9
+ # integration in Microsoft Teams. You can create the required webhook by
10
+ # visiting
11
+ # https://docs.microsoft.com/en-us/outlook/actionable-messages/actionable-messages-via-connectors#sending-actionable-messages-via-office-365-connectors
11
12
  #
12
- # After you configure your webhook, you'll need the webhook URL from the connector.
13
+ # After you configure your webhook, you'll need the webhook URL from the integration.
13
14
 
14
15
  require 'sensu-handler'
15
16
  require 'json'
17
+ require 'erubis'
16
18
 
17
19
  class MicrosoftTeams < Sensu::Handler
18
20
  option :json_config,
@@ -21,8 +23,48 @@ class MicrosoftTeams < Sensu::Handler
21
23
  long: '--json JSONCONFIG',
22
24
  default: 'microsoft-teams'
23
25
 
24
- def microsoft_teams_webhook_url
25
- get_setting('teams_url')
26
+ def payload_template
27
+ get_setting('payload_template')
28
+ end
29
+
30
+ def teams_webhook_url
31
+ get_setting('webhook_url')
32
+ end
33
+
34
+ def teams_icon_emoji
35
+ get_setting('icon_emoji')
36
+ end
37
+
38
+ def teams_icon_url
39
+ get_setting('icon_url')
40
+ end
41
+
42
+ def teams_channel
43
+ @event['client']['teams_channel'] || @event['check']['teams_channel'] || get_setting('channel')
44
+ end
45
+
46
+ def teams_message_prefix
47
+ get_setting('message_prefix')
48
+ end
49
+
50
+ def teams_bot_name
51
+ get_setting('bot_name')
52
+ end
53
+
54
+ def teams_surround
55
+ get_setting('surround')
56
+ end
57
+
58
+ def teams_link_names
59
+ get_setting('link_names')
60
+ end
61
+
62
+ def message_template
63
+ get_setting('template') || get_setting('message_template')
64
+ end
65
+
66
+ def fields
67
+ get_setting('fields')
26
68
  end
27
69
 
28
70
  def proxy_address
@@ -45,20 +87,71 @@ class MicrosoftTeams < Sensu::Handler
45
87
  get_setting('dashboard')
46
88
  end
47
89
 
90
+ def incident_key
91
+ if dashboard_uri.nil?
92
+ @event['client']['name'] + '/' + @event['check']['name']
93
+ else
94
+ "<#{dashboard_uri}#{@event['client']['name']}?check=#{@event['check']['name']}|#{@event['client']['name']}/#{@event['check']['name']}>"
95
+ end
96
+ end
97
+
48
98
  def get_setting(name)
49
99
  settings[config[:json_config]][name]
50
100
  end
51
101
 
52
102
  def handle
53
- uri = URI(microsoft_teams_webhook_url)
54
- http = Net::HTTP.new(uri.host, uri.port)
103
+ if payload_template.nil?
104
+ description = @event['check']['notification'] || build_description
105
+ post_data("#{incident_key}: #{description}")
106
+ else
107
+ post_data(render_payload_template(teams_channel))
108
+ end
109
+ end
110
+
111
+ def render_payload_template(channel)
112
+ return unless payload_template && File.readable?(payload_template)
113
+ template = File.read(payload_template)
114
+ eruby = Erubis::Eruby.new(template)
115
+ eruby.result(binding)
116
+ end
117
+
118
+ def build_description
119
+ template = if message_template && File.readable?(message_template)
120
+ File.read(message_template)
121
+ else
122
+ '''<%=
123
+ [
124
+ @event["check"]["output"].gsub(\'"\', \'\\"\'),
125
+ @event["client"]["address"],
126
+ @event["client"]["subscriptions"].join(",")
127
+ ].join(" : ")
128
+ %>
129
+ '''
130
+ end
131
+ eruby = Erubis::Eruby.new(template)
132
+ eruby.result(binding)
133
+ end
134
+
135
+ def post_data(body)
136
+ uri = URI(teams_webhook_url)
137
+ http = if proxy_address.nil?
138
+ Net::HTTP.new(uri.host, uri.port)
139
+ else
140
+ Net::HTTP::Proxy(proxy_address, proxy_port, proxy_username, proxy_password).new(uri.host, uri.port)
141
+ end
55
142
  http.use_ssl = true
56
- req = Net::HTTP::Post.new("{uri.path}?#{uri.query}", 'Content-Type' => 'application/json')
57
- text = '{"text": "Hello World from Sensu"}'
58
- req.body = JSON.parse(text)
59
-
143
+
144
+ req = Net::HTTP::Post.new("#{uri.path}?#{uri.query}", 'Content-Type' => 'application/json')
145
+
146
+ if payload_template.nil?
147
+ text = teams_surround ? teams_surround + body + teams_surround : body
148
+ req.body = payload(text).to_json
149
+ else
150
+ req.body = body
151
+ end
152
+
60
153
  response = http.request(req)
61
- verifiy_response(response)
154
+ verify_response(response)
62
155
  end
63
156
 
64
157
  def verify_response(response)
@@ -70,6 +163,38 @@ class MicrosoftTeams < Sensu::Handler
70
163
  end
71
164
  end
72
165
 
166
+ def payload(notice)
167
+ client_fields = []
168
+
169
+ unless fields.nil?
170
+ fields.each do |field|
171
+ # arbritary based on what I feel like
172
+ # -vjanelle
173
+ is_short = true unless @event['client'].key?(field) && @event['client'][field].length > 50
174
+ client_fields << {
175
+ title: field,
176
+ value: @event['client'][field],
177
+ short: is_short
178
+ }
179
+ end
180
+ end
181
+
182
+ {
183
+ icon_url: teams_icon_url ? teams_icon_url : 'https://raw.githubusercontent.com/sensu/sensu-logo/master/sensu1_flat%20white%20bg_png.png',
184
+ attachments: [{
185
+ title: "#{@event['client']['address']} - #{translate_status}",
186
+ text: [teams_message_prefix, notice].compact.join(' '),
187
+ color: color,
188
+ fields: client_fields
189
+ }]
190
+ }.tap do |payload|
191
+ payload[:channel] = teams_channel if teams_channel
192
+ payload[:username] = teams_bot_name if teams_bot_name
193
+ payload[:icon_emoji] = teams_icon_emoji if teams_icon_emoji
194
+ payload[:link_names] = teams_link_names if teams_link_names
195
+ end
196
+ end
197
+
73
198
  def color
74
199
  color = {
75
200
  0 => '#36a64f',
@@ -93,4 +218,4 @@ class MicrosoftTeams < Sensu::Handler
93
218
  }
94
219
  status[check_status.to_i]
95
220
  end
96
- end
221
+ end
@@ -2,7 +2,7 @@ module SensuPluginsMicrosoftTeams
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- PATCH = 5
5
+ PATCH = 6
6
6
 
7
7
  VER_STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sensu-plugins-microsoft-teams
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sensu-Plugins and contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-13 00:00:00.000000000 Z
11
+ date: 2017-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sensu-plugin