enh(plugin): notification::microsoft::office365::teams::mode::alert - add workflow (#5124)

Co-authored-by: omercier <32134301+omercier@users.noreply.github.com>
This commit is contained in:
sdepassio 2024-08-07 15:13:15 +02:00 committed by GitHub
parent 2696c3c37f
commit d635b1c04e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 397 additions and 84 deletions

View File

@ -134,8 +134,8 @@ runs:
path: ./*.${{ inputs.package_extension }} path: ./*.${{ inputs.package_extension }}
key: ${{ inputs.cache_key }} key: ${{ inputs.cache_key }}
# Update if condition to true to get packages as artifacts # Add to your PR the label upload-artifacts to get packages as artifacts
- if: ${{ false }} - if: ${{ contains(github.event.pull_request.labels.*.name, 'upload-artifacts') }}
name: Upload package artifacts name: Upload package artifacts
uses: actions/upload-artifact@26f96dfa697d77e81fd5907df203aa23a56210a8 # v4.3.0 uses: actions/upload-artifact@26f96dfa697d77e81fd5907df203aa23a56210a8 # v4.3.0
with: with:

View File

@ -0,0 +1,155 @@
#
# Copyright 2024 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package notification::microsoft::office365::teams::custom::workflowapi;
use strict;
use warnings;
use centreon::plugins::http;
use centreon::plugins::statefile;
use JSON::XS;
sub new {
my ($class, %options) = @_;
my $self = {};
bless $self, $class;
if (!defined($options{output})) {
print "Class Custom: Need to specify 'output' argument.\n";
exit 3;
}
if (!defined($options{options})) {
$options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
$options{output}->option_exit();
}
if (!defined($options{noptions})) {
$options{options}->add_options(arguments => {
'teams-workflow:s' => { name => 'teams_workflow' },
'port:s' => { name => 'port' },
'proto:s' => { name => 'proto' },
'timeout:s' => { name => 'timeout' }
});
}
$options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
$self->{output} = $options{output};
$self->{http} = centreon::plugins::http->new(%options);
return $self;
}
sub set_defaults {}
sub set_options {
my ($self, %options) = @_;
$self->{option_results} = $options{option_results};
$self->{http}->add_header(key => 'Accept', value => 'application/json');
$self->{http}->add_header(key => 'Content-Type', value => 'application/json');
}
sub check_options {
my ($self, %options) = @_;
$self->{teams_workflow} = (defined($self->{option_results}->{teams_workflow})) ? $self->{option_results}->{teams_workflow} : '';
$self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
$self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443;
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 30;
if ($self->{teams_workflow} eq '') {
$self->{output}->add_option_msg(short_msg => 'Need to specify the --teams-workflow option.');
$self->{output}->option_exit();
}
$self->{http}->set_options(%{$self->{option_results}}, hostname => 'dummy');
return 0;
}
sub json_decode {
my ($self, %options) = @_;
$options{content} =~ s/\r//mg;
my $decoded;
eval {
$decoded = JSON::XS->new->allow_nonref(1)->utf8->decode($options{content});
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
$self->{output}->option_exit();
}
return $decoded;
}
sub teams_post_notification {
my ($self, %options) = @_;
my $encoded_data = JSON::XS->new->utf8->encode($options{json_request});
my $content = $self->{http}->request(
method => 'POST',
full_url => $self->{teams_workflow},
query_form_post => $encoded_data
);
return $content;
}
1;
__END__
=head1 NAME
O365 Teams Workflows API
=head1 SYNOPSIS
O365 Teams Workflows API
=head1 REST API OPTIONS
=over 8
=item B<--teams-workflow>
Define the Workflow full URI (required).
=item B<--port>
Define the API port (default: 443).
=item B<--proto>
Define the protocol if needed (default: 'https').
=item B<--timeout>
Define the HTTP timeout.
=back
=head1 DESCRIPTION
B<custom>.
=cut

View File

@ -38,13 +38,13 @@ sub new {
'centreon-url:s' => { name => 'centreon_url' }, 'centreon-url:s' => { name => 'centreon_url' },
'channel-id:s' => { name => 'channel_id' }, 'channel-id:s' => { name => 'channel_id' },
'date:s' => { name => 'date' }, 'date:s' => { name => 'date' },
'extra-info-format:s' => { name => 'extra_info_format', default => 'Author: %s, Comment: %s'}, 'extra-info-format:s' => { name => 'extra_info_format', default => 'Author: %s, Comment: %s' },
'extra-info:s' => { name => 'extra_info'}, 'extra-info:s' => { name => 'extra_info' },
'host-name:s' => { name => 'host_name' }, 'host-name:s' => { name => 'host_name' },
'host-output:s' => { name => 'host_output', default => '' }, 'host-output:s' => { name => 'host_output', default => '' },
'host-state:s' => { name => 'host_state' }, 'host-state:s' => { name => 'host_state' },
'legacy:s' => { name => 'legacy' }, 'legacy:s' => { name => 'legacy' },
'notification-type:s' => { name => 'notif_type'}, 'notification-type:s' => { name => 'notif_type' },
'service-description:s' => { name => 'service_name' }, 'service-description:s' => { name => 'service_name' },
'service-output:s' => { name => 'service_output', default => '' }, 'service-output:s' => { name => 'service_output', default => '' },
'service-state:s' => { name => 'service_state' }, 'service-state:s' => { name => 'service_state' },
@ -72,7 +72,7 @@ sub check_options {
sub build_resource_status_filters { sub build_resource_status_filters {
my ($self, %options) = @_; my ($self, %options) = @_;
my $data_format = URI::Encode->new({encode_reserved => 1}); my $data_format = URI::Encode->new({ encode_reserved => 1 });
my $raw_resource_status_filters = { my $raw_resource_status_filters = {
"id" => "", "id" => "",
"name" => "New+filter", "name" => "New+filter",
@ -166,17 +166,17 @@ sub build_resource_status_filters {
return $link_url_path; return $link_url_path;
} }
sub build_payload { sub build_webhook_payload {
my ($self, %options) = @_; my ($self, %options) = @_;
my $message = $self->build_message(); my $message = $self->build_webhook_message();
$self->{json_payload} = { $self->{json_payload} = {
'@type' => 'MessageCard', '@type' => 'MessageCard',
'@context' => 'https://schema.org/extensions', '@context' => 'https://schema.org/extensions',
potentialAction => $message->{potentialAction}, potentialAction => $message->{potentialAction},
sections => $message->{sections}, sections => $message->{sections},
summary => 'Centreon ' . $message->{notif_type}, summary => 'Centreon ' . $message->{notif_type},
themecolor => $message->{themecolor} themeColor => $message->{themecolor}
}; };
if ($@) { if ($@) {
@ -187,7 +187,7 @@ sub build_payload {
return $self; return $self;
} }
sub build_message { sub build_webhook_message {
my ($self, %options) = @_; my ($self, %options) = @_;
my $teams_colors = { my $teams_colors = {
@ -217,11 +217,13 @@ sub build_message {
$formatted_resource = 'BAM' if defined($self->{option_results}->{bam}); $formatted_resource = 'BAM' if defined($self->{option_results}->{bam});
push @{$self->{sections}}, { push @{$self->{sections}}, {
activityTitle => $self->{notif_type} . ': ' . $formatted_resource . ' "' . $self->{option_results}->{$resource_type . '_name'} . '" is ' . $self->{option_results}->{$resource_type . '_state'}, activityTitle =>
activitySubtitle => $resource_type eq 'service' ? 'Host ' . $self->{option_results}->{host_name} : '' $self->{notif_type} . ': ' . $formatted_resource . ' "' . $self->{option_results}->{$resource_type . '_name'} . '" is ' . $self->{option_results}->{$resource_type . '_state'},
activitySubtitle =>
$resource_type eq 'service' ? 'Host ' . $self->{option_results}->{host_name} : ''
}; };
$self->{themecolor} = $teams_colors->{$self->{notif_type}}; $self->{themecolor} = $teams_colors->{$self->{notif_type}};
if ($self->{option_results}->{notif_type} eq 'PROBLEM') { if ($self->{notif_type} eq 'PROBLEM') {
$self->{themecolor} = $teams_colors->{PROBLEM}->{$resource_type}->{lc($self->{option_results}->{$resource_type . '_state'})}; $self->{themecolor} = $teams_colors->{PROBLEM}->{$resource_type}->{lc($self->{option_results}->{$resource_type . '_state'})};
} }
@ -243,14 +245,14 @@ sub build_message {
} }
if (defined($self->{option_results}->{action_links})) { if (defined($self->{option_results}->{action_links})) {
if (!defined($self->{option_results}->{centreon_url}) || $self->{option_results}->{centreon_url} eq ''){ if (!defined($self->{option_results}->{centreon_url}) || $self->{option_results}->{centreon_url} eq '') {
$self->{output}->add_option_msg(short_msg => 'Please set --centreon-url option'); $self->{output}->add_option_msg(short_msg => 'Please set --centreon-url option');
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
my $uri = URI::Encode->new({encode_reserved => 0}); my $uri = URI::Encode->new({ encode_reserved => 0 });
my $link_url_path; my $link_url_path;
if (defined($self->{option_results}->{legacy})){ if (defined($self->{option_results}->{legacy})) {
$link_url_path = '/main.php?p=2020'; # deprecated pages $link_url_path = '/main.php?p=2020'; # deprecated pages
$link_url_path .= ($resource_type eq 'service') ? $link_url_path .= ($resource_type eq 'service') ?
'1&o=svc&host_search=' . $self->{option_results}->{host_name} . '&search=' . $self->{option_results}->{service_name} : '1&o=svc&host_search=' . $self->{option_results}->{host_name} . '&search=' . $self->{option_results}->{service_name} :
@ -291,11 +293,166 @@ sub build_message {
return $self; return $self;
} }
sub build_workflow_payload {
my ($self, %options) = @_;
my $message = $self->build_workflow_message();
$self->{json_payload} = {
type => "message",
attachments => [
{
contentType => "application/vnd.microsoft.card.adaptive",
content => {
'$schema' => "http://adaptivecards.io/schemas/adaptive-card.json",
type => "AdaptiveCard",
version => "1.0",
body => $message->{body},
actions => $message->{actions}
}
}
]
};
if ($@) {
$self->{output}->add_option_msg(short_msg => 'Cannot decode json response');
$self->{output}->option_exit();
}
return $self;
}
sub build_workflow_message {
my ($self, %options) = @_;
my $teams_colors = {
ACKNOWLEDGEMENT => 'accent',
DOWNTIMEEND => 'accent',
DOWNTIMESTART => 'accent',
RECOVERY => 'good',
PROBLEM => {
host => {
up => 'good',
down => 'attention',
unreachable => 'attention'
},
service => {
ok => 'good',
warning => 'warning',
critical => 'attention',
unknown => 'light'
}
}
};
$self->{body} = [];
$self->{notif_type} = $self->{option_results}->{notif_type};
my $resource_type = defined($self->{option_results}->{host_state}) ? 'host' : 'service';
my $formatted_resource = ucfirst($resource_type);
$formatted_resource = 'BAM' if defined($self->{option_results}->{bam});
my $themecolor = $teams_colors->{$self->{notif_type}};
if ($self->{notif_type} eq 'PROBLEM') {
$themecolor = $teams_colors->{PROBLEM}->{$resource_type}->{lc($self->{option_results}->{$resource_type . '_state'})};
}
if (!defined($themecolor)) {
$themecolor = 'default';
}
push @{$self->{body}}, {
type => "TextBlock",
text => $self->{notif_type} . ': ' . $formatted_resource . ' "' . $self->{option_results}->{$resource_type . '_name'} . '" is ' . $self->{option_results}->{$resource_type . '_state'},
"size" => "Large",
"weight" => "Bolder",
"style" => "heading",
"color" => $themecolor
};
push @{$self->{body}}, {
type => "TextBlock",
text => $resource_type eq 'service' ? 'Host ' . $self->{option_results}->{host_name} : '',
"size" => "Medium",
"weight" => "Bolder",
"style" => "heading",
"color" => $themecolor
};
if (defined($self->{option_results}->{$resource_type . '_output'}) && $self->{option_results}->{$resource_type . '_output'} ne '') {
push @{$self->{body}}, {
type => "TextBlock",
text => "Status: " . $self->{option_results}->{$resource_type . '_output'}
};
}
if (defined($self->{option_results}->{date}) && $self->{option_results}->{date} ne '') {
push @{$self->{body}}, {
type => "TextBlock",
text => "Event date: " . $self->{option_results}->{date}
};
}
if (defined($self->{option_results}->{extra_info}) && $self->{option_results}->{extra_info} !~ m/^\/\/$/) {
if ($self->{option_results}->{extra_info} =~ m/^(.*)\/\/(.*)$/) {
push @{$self->{body}}, {
type => "TextBlock",
text => "Additional Information: \n" . sprintf($self->{option_results}->{extra_info_format}, $1, $2),
"wrap" => "true"
};
}
}
if (defined($self->{option_results}->{action_links})) {
if (!defined($self->{option_results}->{centreon_url}) || $self->{option_results}->{centreon_url} eq '') {
$self->{output}->add_option_msg(short_msg => 'Please set --centreon-url option');
$self->{output}->option_exit();
}
my $uri = URI::Encode->new({ encode_reserved => 0 });
my $link_url_path;
if (defined($self->{option_results}->{legacy})) {
$link_url_path = '/main.php?p=2020'; # deprecated pages
$link_url_path .= ($resource_type eq 'service') ?
'1&o=svc&host_search=' . $self->{option_results}->{host_name} . '&search=' . $self->{option_results}->{service_name} :
'2&o=svc&host_search=' . $self->{option_results}->{host_name};
my $link_uri_encoded = $uri->encode($self->{option_results}->{centreon_url} . $link_url_path);
} else {
$link_url_path = $self->build_resource_status_filters();
}
my $link_uri_encoded = $uri->encode($self->{option_results}->{centreon_url}) . $link_url_path;
push @{$self->{actions}}, {
"type" => "Action.OpenUrl",
"title" => "Details",
"url" => "$link_uri_encoded",
"role" => "button"
};
if ($resource_type eq 'service') {
my $graph_url_path = '/main.php?p=204&mode=0&svc_id=';
$graph_url_path .= $self->{option_results}->{host_name} . ';' . $self->{option_results}->{service_name};
my $graph_uri_encoded = $uri->encode($self->{option_results}->{centreon_url} . $graph_url_path);
push @{$self->{actions}}, {
"type" => "Action.OpenUrl",
"title" => "Graph",
"url" => $graph_uri_encoded,
"role" => "button"
};
}
}
return $self;
}
sub run { sub run {
my ($self, %options) = @_; my ($self, %options) = @_;
my $json_request = $self->build_payload(); my $json_request;
my $response = $options{custom}->teams_post_notification( if (!centreon::plugins::misc::is_empty($options{custom}->{teams_webhook})) {
$json_request = $self->build_webhook_payload();
} else {
$json_request = $self->build_workflow_payload();
}
$options{custom}->teams_post_notification(
channel_id => $self->{teams}->{channel_id}, channel_id => $self->{teams}->{channel_id},
json_request => $self->{json_payload}, json_request => $self->{json_payload},
team_id => $self->{teams}->{team_id} team_id => $self->{teams}->{team_id}

View File

@ -36,6 +36,7 @@ sub new {
$self->{custom_modes}->{graphapi} = 'cloud::microsoft::office365::custom::graphapi'; $self->{custom_modes}->{graphapi} = 'cloud::microsoft::office365::custom::graphapi';
$self->{custom_modes}->{webhookapi} = 'notification::microsoft::office365::teams::custom::webhookapi'; $self->{custom_modes}->{webhookapi} = 'notification::microsoft::office365::teams::custom::webhookapi';
$self->{custom_modes}->{workflowapi} = 'notification::microsoft::office365::teams::custom::workflowapi';
return $self; return $self;
} }