enh(notification/teams): handle all types of notification (#2982)

This commit is contained in:
Thibault S 2021-07-23 13:25:49 +02:00 committed by GitHub
parent 7dcad24541
commit d35e3c4262
1 changed files with 60 additions and 22 deletions

View File

@ -1,5 +1,5 @@
# #
# Copyright 2018 Centreon (http://www.centreon.com/) # Copyright 2021 Centreon (http://www.centreon.com/)
# #
# Centreon is a full-fledged industry-strength solution that meets # Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for # the needs in IT infrastructure and application monitoring for
@ -34,16 +34,19 @@ sub new {
$options{options}->add_options(arguments => { $options{options}->add_options(arguments => {
'action-links' => { name => 'action_links' }, 'action-links' => { name => 'action_links' },
'bam' => { name => 'bam' }, 'bam' => { name => 'bam' },
'centreon-url:s' => { name => 'centreon_url' },
'channel-id:s' => { name => 'channel_id' },
'date:s' => { name => 'date' },
'extra-info-format:s' => { name => 'extra_info_format', default => 'Author: %s, Comment: %s'},
'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' },
'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' },
'centreon-url:s' => { name => 'centreon_url' }, 'team-id:s' => { name => 'team_id' }
'channel-id:s' => { name => 'channel_id' },
'team-id:s' => { name => 'team_id' },
'date:s' => { name => 'date' }
}); });
return $self; return $self;
@ -57,6 +60,11 @@ sub check_options {
$self->{option_results}->{channel_id} : undef; $self->{option_results}->{channel_id} : undef;
$self->{teams}->{team_id} = defined($self->{option_results}->{team_id}) && $self->{option_results}->{channel_id} ne '' $self->{teams}->{team_id} = defined($self->{option_results}->{team_id}) && $self->{option_results}->{channel_id} ne ''
? $self->{option_results}->{team_id} : undef; ? $self->{option_results}->{team_id} : undef;
if (!defined($self->{option_results}->{notif_type}) || $self->{option_results}->{notif_type} eq '') {
$self->{output}->add_option_msg(short_msg => "You need to specify the --notification-type option.");
$self->{output}->option_exit();
}
} }
sub build_payload { sub build_payload {
@ -68,7 +76,7 @@ sub build_payload {
'@context' => 'https://schema.org/extensions', '@context' => 'https://schema.org/extensions',
potentialAction => $message->{potentialAction}, potentialAction => $message->{potentialAction},
sections => $message->{sections}, sections => $message->{sections},
summary => 'Centreon Alert', summary => 'Centreon ' . $message->{notif_type},
themecolor => $message->{themecolor} themecolor => $message->{themecolor}
}; };
@ -83,31 +91,40 @@ sub build_payload {
sub build_message { sub build_message {
my ($self, %options) = @_; my ($self, %options) = @_;
my %teams_colors = ( my $teams_colors = {
host => { ACKNOWLEDGEMENT => 'fefc8e',
up => '42f56f', DOWNTIMEEND => 'f1dfff',
down => 'f21616', DOWNTIMESTART => 'f1dfff',
unreachable => 'f21616' RECOVERY => '42f56f',
}, PROBLEM => {
service => { host => {
ok => '42f56f', up => '42f56f',
warning => 'f59042', down => 'f21616',
critical => 'f21616', unreachable => 'f21616'
unknown => '757575' },
service => {
ok => '42f56f',
warning => 'f59042',
critical => 'f21616',
unknown => '757575'
}
} }
); };
$self->{sections} = []; $self->{sections} = [];
$self->{notif_type} = $self->{option_results}->{notif_type};
my $resource_type = defined($self->{option_results}->{host_state}) ? 'host' : 'service'; my $resource_type = defined($self->{option_results}->{host_state}) ? 'host' : 'service';
my $formatted_resource = ucfirst($resource_type); my $formatted_resource = ucfirst($resource_type);
$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 => $formatted_resource . ' "' . $self->{option_results}->{$resource_type . '_name'} . '" is ' . $self->{option_results}->{$resource_type . '_state'}, activityTitle => $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} : '' activitySubtitle => $resource_type eq 'service' ? 'Host ' . $self->{option_results}->{host_name} : ''
}; };
$self->{themecolor} = $teams_colors->{$self->{notif_type}};
$self->{themecolor} = $teams_colors{$resource_type}->{lc($self->{option_results}->{$resource_type . '_state'})}; if ($self->{option_results}->{notif_type} eq 'PROBLEM') {
$self->{themecolor} = $teams_colors->{PROBLEM}->{$resource_type}->{lc($self->{option_results}->{$resource_type . '_state'})};
}
if (defined($self->{option_results}->{$resource_type . '_output'}) && $self->{option_results}->{$resource_type . '_output'} ne '') { if (defined($self->{option_results}->{$resource_type . '_output'}) && $self->{option_results}->{$resource_type . '_output'} ne '') {
push @{$self->{sections}[0]->{facts}}, { name => 'Status', 'value' => $self->{option_results}->{$resource_type . '_output'} }; push @{$self->{sections}[0]->{facts}}, { name => 'Status', 'value' => $self->{option_results}->{$resource_type . '_output'} };
@ -117,6 +134,15 @@ sub build_message {
push @{$self->{sections}[0]->{facts}}, { name => 'Event date', 'value' => $self->{option_results}->{date} }; push @{$self->{sections}[0]->{facts}}, { name => 'Event date', 'value' => $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->{sections}[0]->{facts}}, {
name => 'Additional Information',
value => sprintf($self->{option_results}->{extra_info_format}, $1, $2)
};
}
}
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');
@ -185,6 +211,10 @@ centreon_plugins.pl --plugin=notification::microsoft::office365::teams::plugin -
=over 8 =over 8
=item B<--notification-type>
Specify the notification type (Required).
=item B<--host-name> =item B<--host-name>
Specify Host server name for the alert (Required). Specify Host server name for the alert (Required).
@ -226,6 +256,14 @@ Compatibility with Centreon BAM notifications.
Specify the date & time of the event. Specify the date & time of the event.
=item B<--extra-info>
Specify extra information about author and comment (only for ACK and DOWNTIME types).
=item B<--extra-info-format>
Specify the extra info display format (Default: 'Author: %s, Comment: %s').
=back =back
=cut =cut