Debian Codegraph @

src/notification/microsoft/office365/teams/mode/alert.pm

raw
1 #
2 # Copyright 2024 Centreon (http://www.centreon.com/)
3 #
4 # Centreon is a full-fledged industry-strength solution that meets
5 # the needs in IT infrastructure and application monitoring for
6 # service performance.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 #
20
21 package notification::microsoft::office365::teams::mode::alert;
22
23 use strict;
24 use warnings;
25 use base qw(centreon::plugins::mode);
26 use URI::Encode;
27 use JSON::XS;
28
29 sub new {
30 my ($class, %options) = @_;
31 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
32 bless $self, $class;
33
34 $self->{version} = '1.0';
35 $options{options}->add_options(arguments => {
36 'action-links' => { name => 'action_links' },
37 'bam' => { name => 'bam' },
38 'centreon-url:s' => { name => 'centreon_url' },
39 'channel-id:s' => { name => 'channel_id' },
40 'date:s' => { name => 'date' },
41 'extra-info-format:s' => { name => 'extra_info_format', default => 'Author: %s, Comment: %s' },
42 'extra-info:s' => { name => 'extra_info' },
43 'host-name:s' => { name => 'host_name' },
44 'host-output:s' => { name => 'host_output', default => '' },
45 'host-state:s' => { name => 'host_state' },
46 'legacy:s' => { name => 'legacy' },
47 'notification-type:s' => { name => 'notif_type' },
48 'service-description:s' => { name => 'service_name' },
49 'service-output:s' => { name => 'service_output', default => '' },
50 'service-state:s' => { name => 'service_state' },
51 'team-id:s' => { name => 'team_id' }
52 });
53
54 return $self;
55 }
56
57 sub check_options {
58 my ($self, %options) = @_;
59 $self->SUPER::init(%options);
60
61 $self->{teams}->{channel_id} = defined($self->{option_results}->{channel_id}) && $self->{option_results}->{channel_id} ne '' ?
62 $self->{option_results}->{channel_id} : undef;
63 $self->{teams}->{team_id} = defined($self->{option_results}->{team_id}) && $self->{option_results}->{channel_id} ne ''
64 ? $self->{option_results}->{team_id} : undef;
65
66 if (!defined($self->{option_results}->{notif_type}) || $self->{option_results}->{notif_type} eq '') {
67 $self->{output}->add_option_msg(short_msg => "You need to specify the --notification-type option.");
68 $self->{output}->option_exit();
69 }
70 }
71
72 sub build_resource_status_filters {
73 my ($self, %options) = @_;
74
75 my $data_format = URI::Encode->new({ encode_reserved => 1 });
76 my $raw_resource_status_filters = {
77 "id" => "",
78 "name" => "New+filter",
79 "criterias" => [
80 {
81 "name" => "resource_types",
82 "object_type" => undef,
83 "type" => "multi_select",
84 "value" => [
85 {
86 "id" => "service",
87 "name" => "Service"
88 }
89 ]
90 },
91 {
92 "name" => "states",
93 "object_type" => undef,
94 "type" => "multi_select",
95 "value" => [
96
97 ]
98 },
99 {
100 "name" => "statuses",
101 "object_type" => undef,
102 "type" => "multi_select",
103 "value" => [
104
105 ]
106 },
107 {
108 "name" => "status_types",
109 "object_type" => undef,
110 "type" => "multi_select",
111 "value" => [
112
113 ]
114 },
115 {
116 "name" => "host_groups",
117 "object_type" => "host_groups",
118 "type" => "multi_select",
119 "value" => [
120
121 ]
122 },
123 {
124 "name" => "service_groups",
125 "object_type" => "service_groups",
126 "type" => "multi_select",
127 "value" => [
128
129 ]
130 },
131 {
132 "name" => "monitoring_servers",
133 "object_type" => "monitoring_servers",
134 "type" => "multi_select",
135 "value" => [
136
137 ]
138 },
139 {
140 "name" => "search",
141 "object_type" => undef,
142 "type" => "text",
143 "value" => sprintf(
144 's.description:%s h.name:%s',
145 defined($self->{option_results}->{service_name}) ? $self->{option_results}->{service_name} : '',
146 defined($self->{option_results}->{host_name}) ? $self->{option_results}->{host_name} : ''
147 )
148 },
149 {
150 "name" => "sort",
151 "object_type" => undef,
152 "type" => "array",
153 "value" => [
154 "status_severity_code",
155 "asc"
156 ]
157 }
158 ]
159 };
160
161 my $link_url_path = '/monitoring/resources?filter=';
162 my $encoded_resource_status_filters = JSON::XS->new->utf8->encode($raw_resource_status_filters);
163 my $encoded_data_for_uri = $data_format->encode($encoded_resource_status_filters);
164 $link_url_path .= $encoded_data_for_uri;
165
166 return $link_url_path;
167 }
168
169 sub build_webhook_payload {
170 my ($self, %options) = @_;
171
172 my $message = $self->build_webhook_message();
173 $self->{json_payload} = {
174 '@type' => 'MessageCard',
175 '@context' => 'https://schema.org/extensions',
176 potentialAction => $message->{potentialAction},
177 sections => $message->{sections},
178 summary => 'Centreon ' . $message->{notif_type},
179 themeColor => $message->{themecolor}
180 };
181
182 if ($@) {
183 $self->{output}->add_option_msg(short_msg => 'Cannot decode json response');
184 $self->{output}->option_exit();
185 }
186
187 return $self;
188 }
189
190 sub build_webhook_message {
191 my ($self, %options) = @_;
192
193 my $teams_colors = {
194 ACKNOWLEDGEMENT => 'fefc8e',
195 DOWNTIMEEND => 'f1dfff',
196 DOWNTIMESTART => 'f1dfff',
197 RECOVERY => '42f56f',
198 PROBLEM => {
199 host => {
200 up => '42f56f',
201 down => 'f21616',
202 unreachable => 'f21616'
203 },
204 service => {
205 ok => '42f56f',
206 warning => 'f59042',
207 critical => 'f21616',
208 unknown => '757575'
209 }
210 }
211 };
212
213 $self->{sections} = [];
214 $self->{notif_type} = $self->{option_results}->{notif_type};
215 my $resource_type = defined($self->{option_results}->{host_state}) ? 'host' : 'service';
216 my $formatted_resource = ucfirst($resource_type);
217 $formatted_resource = 'BAM' if defined($self->{option_results}->{bam});
218
219 push @{$self->{sections}}, {
220 activityTitle =>
221 $self->{notif_type} . ': ' . $formatted_resource . ' "' . $self->{option_results}->{$resource_type . '_name'} . '" is ' . $self->{option_results}->{$resource_type . '_state'},
222 activitySubtitle =>
223 $resource_type eq 'service' ? 'Host ' . $self->{option_results}->{host_name} : ''
224 };
225 $self->{themecolor} = $teams_colors->{$self->{notif_type}};
226 if ($self->{notif_type} eq 'PROBLEM') {
227 $self->{themecolor} = $teams_colors->{PROBLEM}->{$resource_type}->{lc($self->{option_results}->{$resource_type . '_state'})};
228 }
229
230 if (defined($self->{option_results}->{$resource_type . '_output'}) && $self->{option_results}->{$resource_type . '_output'} ne '') {
231 push @{$self->{sections}[0]->{facts}}, { name => 'Status', 'value' => $self->{option_results}->{$resource_type . '_output'} };
232 }
233
234 if (defined($self->{option_results}->{date}) && $self->{option_results}->{date} ne '') {
235 push @{$self->{sections}[0]->{facts}}, { name => 'Event date', 'value' => $self->{option_results}->{date} };
236 }
237
238 if (defined($self->{option_results}->{extra_info}) && $self->{option_results}->{extra_info} !~ m/^\/\/$/) {
239 if ($self->{option_results}->{extra_info} =~ m/^(.*)\/\/(.*)$/) {
240 push @{$self->{sections}[0]->{facts}}, {
241 name => 'Additional Information',
242 value => sprintf($self->{option_results}->{extra_info_format}, $1, $2)
243 };
244 }
245 }
246
247 if (defined($self->{option_results}->{action_links})) {
248 if (!defined($self->{option_results}->{centreon_url}) || $self->{option_results}->{centreon_url} eq '') {
249 $self->{output}->add_option_msg(short_msg => 'Please set --centreon-url option');
250 $self->{output}->option_exit();
251 }
252 my $uri = URI::Encode->new({ encode_reserved => 0 });
253 my $link_url_path;
254
255 if (defined($self->{option_results}->{legacy})) {
256 $link_url_path = '/main.php?p=2020'; # deprecated pages
257 $link_url_path .= ($resource_type eq 'service') ?
258 '1&o=svc&host_search=' . $self->{option_results}->{host_name} . '&search=' . $self->{option_results}->{service_name} :
259 '2&o=svc&host_search=' . $self->{option_results}->{host_name};
260
261 my $link_uri_encoded = $uri->encode($self->{option_results}->{centreon_url} . $link_url_path);
262 } else {
263 $link_url_path = $self->build_resource_status_filters();
264 }
265
266 my $link_uri_encoded = $uri->encode($self->{option_results}->{centreon_url}) . $link_url_path;
267
268 push @{$self->{potentialAction}}, {
269 '@type' => 'OpenUri',
270 name => 'Details',
271 targets => [{
272 'os' => 'default',
273 'uri' => $link_uri_encoded
274 }]
275 };
276
277 if ($resource_type eq 'service') {
278 my $graph_url_path = '/main.php?p=204&mode=0&svc_id=';
279
280 $graph_url_path .= $self->{option_results}->{host_name} . ';' . $self->{option_results}->{service_name};
281 my $graph_uri_encoded = $uri->encode($self->{option_results}->{centreon_url} . $graph_url_path);
282 push @{$self->{potentialAction}}, {
283 '@type' => 'OpenUri',
284 name => 'Graph',
285 targets => [{
286 'os' => 'default',
287 'uri' => $graph_uri_encoded
288 }]
289 };
290 }
291
292 }
293 return $self;
294 }
295
296 sub build_workflow_payload {
297 my ($self, %options) = @_;
298
299 my $message = $self->build_workflow_message();
300 $self->{json_payload} = {
301 type => "message",
302 attachments => [
303 {
304 contentType => "application/vnd.microsoft.card.adaptive",
305 content => {
306 '$schema' => "http://adaptivecards.io/schemas/adaptive-card.json",
307 type => "AdaptiveCard",
308 version => "1.0",
309 body => $message->{body},
310 actions => $message->{actions}
311 }
312 }
313 ]
314 };
315
316 if ($@) {
317 $self->{output}->add_option_msg(short_msg => 'Cannot decode json response');
318 $self->{output}->option_exit();
319 }
320
321 return $self;
322 }
323
324 sub build_workflow_message {
325 my ($self, %options) = @_;
326
327 my $teams_colors = {
328 ACKNOWLEDGEMENT => 'accent',
329 DOWNTIMEEND => 'accent',
330 DOWNTIMESTART => 'accent',
331 RECOVERY => 'good',
332 PROBLEM => {
333 host => {
334 up => 'good',
335 down => 'attention',
336 unreachable => 'attention'
337 },
338 service => {
339 ok => 'good',
340 warning => 'warning',
341 critical => 'attention',
342 unknown => 'light'
343 }
344 }
345 };
346
347 $self->{body} = [];
348 $self->{notif_type} = $self->{option_results}->{notif_type};
349 my $resource_type = defined($self->{option_results}->{host_state}) ? 'host' : 'service';
350 my $formatted_resource = ucfirst($resource_type);
351 $formatted_resource = 'BAM' if defined($self->{option_results}->{bam});
352 my $themecolor = $teams_colors->{$self->{notif_type}};
353 if ($self->{notif_type} eq 'PROBLEM') {
354 $themecolor = $teams_colors->{PROBLEM}->{$resource_type}->{lc($self->{option_results}->{$resource_type . '_state'})};
355 }
356 if (!defined($themecolor)) {
357 $themecolor = 'default';
358 }
359
360 push @{$self->{body}}, {
361 type => "TextBlock",
362 text => $self->{notif_type} . ': ' . $formatted_resource . ' "' . $self->{option_results}->{$resource_type . '_name'} . '" is ' . $self->{option_results}->{$resource_type . '_state'},
363 "size" => "Large",
364 "weight" => "Bolder",
365 "style" => "heading",
366 "color" => $themecolor
367 };
368 push @{$self->{body}}, {
369 type => "TextBlock",
370 text => $resource_type eq 'service' ? 'Host ' . $self->{option_results}->{host_name} : '',
371 "size" => "Medium",
372 "weight" => "Bolder",
373 "style" => "heading",
374 "color" => $themecolor
375 };
376
377 if (defined($self->{option_results}->{$resource_type . '_output'}) && $self->{option_results}->{$resource_type . '_output'} ne '') {
378 push @{$self->{body}}, {
379 type => "TextBlock",
380 text => "Status: " . $self->{option_results}->{$resource_type . '_output'}
381 };
382 }
383
384 if (defined($self->{option_results}->{date}) && $self->{option_results}->{date} ne '') {
385 push @{$self->{body}}, {
386 type => "TextBlock",
387 text => "Event date: " . $self->{option_results}->{date}
388 };
389 }
390
391 if (defined($self->{option_results}->{extra_info}) && $self->{option_results}->{extra_info} !~ m/^\/\/$/) {
392 if ($self->{option_results}->{extra_info} =~ m/^(.*)\/\/(.*)$/) {
393 push @{$self->{body}}, {
394 type => "TextBlock",
395 text => "Additional Information: \n" . sprintf($self->{option_results}->{extra_info_format}, $1, $2),
396 "wrap" => "true"
397 };
398 }
399 }
400
401 if (defined($self->{option_results}->{action_links})) {
402 if (!defined($self->{option_results}->{centreon_url}) || $self->{option_results}->{centreon_url} eq '') {
403 $self->{output}->add_option_msg(short_msg => 'Please set --centreon-url option');
404 $self->{output}->option_exit();
405 }
406 my $uri = URI::Encode->new({ encode_reserved => 0 });
407 my $link_url_path;
408
409 if (defined($self->{option_results}->{legacy})) {
410 $link_url_path = '/main.php?p=2020'; # deprecated pages
411 $link_url_path .= ($resource_type eq 'service') ?
412 '1&o=svc&host_search=' . $self->{option_results}->{host_name} . '&search=' . $self->{option_results}->{service_name} :
413 '2&o=svc&host_search=' . $self->{option_results}->{host_name};
414
415 my $link_uri_encoded = $uri->encode($self->{option_results}->{centreon_url} . $link_url_path);
416 } else {
417 $link_url_path = $self->build_resource_status_filters();
418 }
419
420 my $link_uri_encoded = $uri->encode($self->{option_results}->{centreon_url}) . $link_url_path;
421
422 push @{$self->{actions}}, {
423 "type" => "Action.OpenUrl",
424 "title" => "Details",
425 "url" => "$link_uri_encoded",
426 "role" => "button"
427 };
428
429 if ($resource_type eq 'service') {
430 my $graph_url_path = '/main.php?p=204&mode=0&svc_id=';
431
432 $graph_url_path .= $self->{option_results}->{host_name} . ';' . $self->{option_results}->{service_name};
433 my $graph_uri_encoded = $uri->encode($self->{option_results}->{centreon_url} . $graph_url_path);
434 push @{$self->{actions}}, {
435 "type" => "Action.OpenUrl",
436 "title" => "Graph",
437 "url" => $graph_uri_encoded,
438 "role" => "button"
439 };
440 }
441
442 }
443 return $self;
444 }
445
446 sub run {
447 my ($self, %options) = @_;
448
449 my $json_request;
450 if (!centreon::plugins::misc::is_empty($options{custom}->{teams_webhook})) {
451 $json_request = $self->build_webhook_payload();
452 } else {
453 $json_request = $self->build_workflow_payload();
454 }
455 $options{custom}->teams_post_notification(
456 channel_id => $self->{teams}->{channel_id},
457 json_request => $self->{json_payload},
458 team_id => $self->{teams}->{team_id}
459 );
460 $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
461 $self->{output}->exit();
462 }
463
464 1;
465
466 __END__
467
468 =head1 MODE
469
470 Send notifications to a Microsoft Teams Channel.
471
472 Example for a Host:
473 centreon_plugins.pl --plugin=notification::microsoft::office365::teams::plugin --mode=alert --teams-webhook='https:/teams.microsoft.com/1/channel/...'
474 --host-name='my_host_1' --host-state='DOWN' --host-output='CRITICAL - my_host_1: rta nan, lost 100%'
475 --centreon-url='https://127.0.0.1/centreon' --action-links'
476
477 =over 8
478
479 =item B<--notification-type>
480
481 Specify the notification type (required).
482
483 =item B<--host-name>
484
485 Specify Host server name for the alert (required).
486
487 =item B<--host-state>
488
489 Specify Host server state for the alert.
490
491 =item B<--host-output>
492
493 Specify Host server output message for the alert.
494
495 =item B<--service-desc>
496
497 Specify Service description name for the alert.
498
499 =item B<--service-state>
500
501 Specify Service state for the alert.
502
503 =item B<--service-output>
504
505 Specify Service output message for the alert.
506
507 =item B<--action-links>
508
509 Only to be used with Centreon.
510 Add actions links buttons to the notification card (resource status & graph page).
511
512 =item B<--centreon-url>
513
514 Specify the Centreon interface URL (to be used with the action links).
515 Syntax: --centreon-url='https://mycentreon.mydomain.local/centreon'
516
517 =item B<--bam>
518
519 Compatibility with Centreon BAM notifications.
520
521 =item B<--date>
522
523 Specify the date & time of the event.
524
525 =item B<--extra-info>
526
527 Specify extra information about author and comment (only for ACK and DOWNTIME types).
528
529 =item B<--extra-info-format>
530
531 Specify the extra info display format (default: 'Author: %s, Comment: %s').
532
533 =item B<--legacy>
534
535 Only to be used with Centreon.
536 Permit redirection to Centreon legacy resource status pages.
537 To be used with --action-links.
538
539 =back
540
541 =cut