enh(slack): add services mode (#3034)

This commit is contained in:
qgarnier 2021-08-10 17:19:09 +02:00 committed by GitHub
parent 0628f8b4ac
commit 0b6302f4ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 390 additions and 135 deletions

View File

@ -24,6 +24,7 @@ use strict;
use warnings;
use centreon::plugins::http;
use JSON::XS;
use Digest::MD5 qw(md5_hex);
sub new {
my ($class, %options) = @_;
@ -38,7 +39,7 @@ sub new {
$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 => {
'hostname:s' => { name => 'hostname' },
@ -69,24 +70,26 @@ sub check_options {
my ($self, %options) = @_;
$self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : 'slack.com';
$self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : undef;
$self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443;
$self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
$self->{api_path} = (defined($self->{option_results}->{api_path})) ? $self->{option_results}->{api_path} : '/api';
$self->{api_token} = (defined($self->{option_results}->{api_token})) ? $self->{option_results}->{api_token} : undef;
$self->{api_token} = (defined($self->{option_results}->{api_token})) ? $self->{option_results}->{api_token} : '';
if (!defined($self->{hostname})) {
if ($self->{hostname} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify hostname option.");
$self->{output}->option_exit();
}
if (!defined($self->{api_token})) {
$self->{output}->add_option_msg(short_msg => "Need to specify api-token option.");
$self->{output}->option_exit();
}
return 0;
}
sub get_connection_infos {
my ($self, %options) = @_;
return md5_hex($self->{option_results}->{api_token});
}
sub build_options_for_httplib {
my ($self, %options) = @_;
@ -94,8 +97,6 @@ sub build_options_for_httplib {
$self->{option_results}->{port} = $self->{port};
$self->{option_results}->{proto} = $self->{proto};
$self->{option_results}->{timeout} = $self->{timeout};
$self->{option_results}->{warning_status} = '';
$self->{option_results}->{critical_status} = '';
}
sub settings {
@ -103,30 +104,38 @@ sub settings {
$self->build_options_for_httplib();
$self->{http}->add_header(key => 'Accept', value => 'application/json');
$self->{http}->add_header(key => 'Content-Type', value => 'application/x-www-form-urlencoded');
if (defined($self->{token})) {
$self->{http}->add_header(key => 'Authorization', value => 'Bearer ' . $self->{token});
}
$self->{http}->set_options(%{$self->{option_results}});
}
sub request_api {
sub request_web_api {
my ($self, %options) = @_;
$self->settings();
if ($self->{api_token} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify api-token option.");
$self->{output}->option_exit();
}
my $content = $self->{http}->request(method => $options{method}, url_path => $self->{api_path} . $options{url_path},
query_form_post => $options{query_form_post}, critical_status => '', warning_status => '', unknown_status => '');
$self->settings();
my $content = $self->{http}->request(
method => defined($options{method}) ? $options{method} : 'GET',
url_path => $self->{api_path} . $options{endpoint},
post_param => $options{post_param},
header => [
'Authorization: Bearer ' . $self->{api_token}
],
critical_status => '',
warning_status => '',
unknown_status => ''
);
my $decoded;
eval {
$decoded = decode_json($content);
};
if ($@) {
$self->{output}->output_add(long_msg => $content, debug => 1);
$self->{output}->add_option_msg(short_msg => "Cannot decode json response");
$self->{output}->add_option_msg(short_msg => 'Cannot decode json response');
$self->{output}->option_exit();
}
if ($decoded->{ok} != 1) {
if ($decoded->{ok} !~ /1|true/i) {
$self->{output}->add_option_msg(short_msg => "Error: " . $decoded->{error});
$self->{output}->option_exit();
}
@ -134,27 +143,45 @@ sub request_api {
return $decoded;
}
sub get_api_token {
sub get_services {
my ($self, %options) = @_;
if (defined($self->{api_token}) && $self->{api_token} ne '') {
return $self->{api_token};
}
# OAuth2 not handled
return;
my $services = {
'Login/SSO' => 1,
'Messaging' => 1,
'Posts/Files' => 1,
'Calls' => 1,
'Apps/Integrations/APIs' => 1,
'Connections' => 1,
'Link Previews' => 1,
'Notifications' => 1,
'Search' => 1,
'Workspace/Org Administration' => 1
};
return $services;
}
sub get_object {
sub request_status_api {
my ($self, %options) = @_;
if (!defined($self->{token})) {
$self->{token} = $self->get_api_token();
$self->settings();
my $content = $self->{http}->request(
full_url => 'https://status.slack.com/api/v2.0.0/current',
hostname => '',
critical_status => '',
warning_status => '',
unknown_status => ''
);
my $decoded;
eval {
$decoded = decode_json($content);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => 'Cannot decode json response');
$self->{output}->option_exit();
}
my $result = $self->request_api(%options);
return $result;
return $decoded;
}
1;
@ -183,8 +210,7 @@ Slack API url path (Default: '/api').
=item B<--api-token>
Slack API token of a user or app with following
permissions : 'users:read', 'channels:read'.
Slack API token of app.
=item B<--port>

View File

@ -28,53 +28,13 @@ use warnings;
sub prefix_output {
my ($self, %options) = @_;
return "Channel '" . $options{instance_value}->{name} . "' ";
}
sub custom_info_perfdata {
my ($self, %options) = @_;
$self->{output}->perfdata_add(
label => 'members',
nlabel => 'channels.members.count',
instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{name} : undef,
value => $self->{result_values}->{num_members},
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
min => 0
return sprintf(
"Channel '%s' [id: %s] ",
$options{instance_value}->{name},
$options{instance_value}->{id}
);
}
sub custom_info_threshold {
my ($self, %options) = @_;
my $exit = $self->{perfdata}->threshold_check(
value => $self->{result_values}->{num_members},
threshold => [
{ label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' },
{ label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' }
]
);
return $exit;
}
sub custom_info_output {
my ($self, %options) = @_;
my $msg = sprintf("[id: %s] [members: %s]", $self->{result_values}->{id}, $self->{result_values}->{num_members});
return $msg;
}
sub custom_info_calc {
my ($self, %options) = @_;
$self->{result_values}->{id} = $options{new_datas}->{$self->{instance} . '_id'};
$self->{result_values}->{name} = $options{new_datas}->{$self->{instance} . '_name'};
$self->{result_values}->{num_members} = $options{new_datas}->{$self->{instance} . '_num_members'};
return 0;
}
sub set_counters {
my ($self, %options) = @_;
@ -84,23 +44,23 @@ sub set_counters {
];
$self->{maps_counters}->{global} = [
{ label => 'count', nlabel => 'channels.count', set => {
{ label => 'count', nlabel => 'channels.total.count', set => {
key_values => [ { name => 'count' } ],
output_template => 'Number of channels: %d',
perfdatas => [
{ label => 'count', template => '%d', min => 0 }
{ template => '%d', min => 0 }
]
}
}
];
$self->{maps_counters}->{channels} = [
{ label => 'members', set => {
key_values => [ { name => 'id' }, { name => 'name' }, { name => 'num_members' } ],
closure_custom_calc => $self->can('custom_info_calc'),
closure_custom_output => $self->can('custom_info_output'),
closure_custom_perfdata => $self->can('custom_info_perfdata'),
closure_custom_threshold_check => $self->can('custom_info_threshold')
{ label => 'members', nlabel => 'channel.members.count', set => {
key_values => [ { name => 'num_members' }, { name => 'id' }, { name => 'name' } ],
output_template => 'members: %s',
perfdatas => [
{ template => '%d', min => 0, label_extra_instance => 1, instance_use => 'name' }
]
}
}
];
@ -108,28 +68,22 @@ sub set_counters {
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class;
$options{options}->add_options(arguments => {
'filter-channel:s' => { name => 'filter_channel' }
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
return $self;
}
sub manage_selection {
my ($self, %options) = @_;
my $result = $options{custom}->get_object(url_path => '/channels.list');
my $result = $options{custom}->request_web_api(endpoint => '/conversations.list');
$self->{global}->{count} = 0;
foreach my $channel (@{$result->{channels}}) {
if (defined($self->{option_results}->{filter_channel}) && $self->{option_results}->{filter_channel} ne '' &&
$channel->{name_normalized} !~ /$self->{option_results}->{filter_channel}/) {
@ -137,7 +91,7 @@ sub manage_selection {
next;
}
$self->{channels}->{$channel->{id}} = {
$self->{channels}->{ $channel->{id} } = {
id => $channel->{id},
name => $channel->{name_normalized},
num_members => $channel->{num_members},
@ -145,7 +99,7 @@ sub manage_selection {
$self->{global}->{count}++;
}
if (scalar(keys %{$self->{channels}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No channels found.");
$self->{output}->option_exit();
@ -158,10 +112,16 @@ __END__
=head1 MODE
Check channels count.
Check channels.
Scope: 'channels.read'.
=over 8
=item B<--filter-channel>
Filter channels by channel name (can be a regexp).
=item B<--warning-count>
Threshold warning for channels count.

View File

@ -31,6 +31,12 @@ sub prefix_output {
return "User '" . $options{instance_value}->{real_name} . "' ";
}
sub custom_info_output {
my ($self, %options) = @_;
return sprintf("[id: %s] [display name: %s]", $self->{result_values}->{id}, $self->{result_values}->{display_name});
}
sub set_counters {
my ($self, %options) = @_;
@ -40,20 +46,19 @@ sub set_counters {
];
$self->{maps_counters}->{global} = [
{ label => 'count', nlabel => 'members.count', set => {
{ label => 'count', nlabel => 'members.total.count', set => {
key_values => [ { name => 'count' } ],
output_template => 'Number of members: %d',
perfdatas => [
{ label => 'count', template => '%d', min => 0 }
{ template => '%d', min => 0 }
]
}
}
];
$self->{maps_counters}->{members} = [
{ label => 'info', set => {
{ label => 'info', display_ok => 0, threshold => 0, set => {
key_values => [ { name => 'id' }, { name => 'real_name' }, { name => 'display_name' } ],
closure_custom_calc => $self->can('custom_info_calc'),
closure_custom_output => $self->can('custom_info_output'),
closure_custom_perfdata => sub { return 0; }
}
@ -61,55 +66,33 @@ sub set_counters {
];
}
sub custom_info_output {
my ($self, %options) = @_;
my $msg = sprintf("[id: %s] [display name: %s]", $self->{result_values}->{id}, $self->{result_values}->{display_name});
return $msg;
}
sub custom_info_calc {
my ($self, %options) = @_;
$self->{result_values}->{id} = $options{new_datas}->{$self->{instance} . '_id'};
$self->{result_values}->{real_name} = $options{new_datas}->{$self->{instance} . '_real_name'};
$self->{result_values}->{display_name} = $options{new_datas}->{$self->{instance} . '_display_name'};
return 0;
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class;
$options{options}->add_options(arguments => {});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
$options{options}->add_options(arguments => {});
return $self;
}
sub manage_selection {
my ($self, %options) = @_;
my $result = $options{custom}->get_object(url_path => '/users.list');
my $result = $options{custom}->request_web_api(endpoint => '/users.list');
$self->{global}->{count} = 0;
foreach my $member (@{$result->{members}}) {
$self->{members}->{$member->{id}} = {
$self->{members}->{ $member->{id} } = {
id => $member->{id},
real_name => $member->{profile}->{real_name_normalized},
display_name => $member->{profile}->{display_name_normalized},
display_name => $member->{profile}->{display_name_normalized}
};
$self->{global}->{count}++;
}
if (scalar(keys %{$self->{members}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No members found.");
$self->{output}->option_exit();
@ -124,6 +107,8 @@ __END__
Check members count.
Scope: 'users.read'
=over 8
=item B<--warning-count>

View File

@ -0,0 +1,107 @@
#
# Copyright 2021 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 apps::slack::restapi::mode::listservices;
use base qw(centreon::plugins::mode);
use strict;
use warnings;
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class;
$options{options}->add_options(arguments => {
'filter-name:s' => { name => 'filter_name' }
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
}
sub manage_selection {
my ($self, %options) = @_;
my $results = $options{custom}->get_services();
my $services = {};
foreach my $name (keys %$results) {
next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne ''
&& $name !~ /$self->{option_results}->{filter_name}/);
$services->{$name} = $name;
}
return $services;
}
sub run {
my ($self, %options) = @_;
my $services = $self->manage_selection(%options);
foreach (sort keys %$services) {
$self->{output}->output_add(
long_msg => sprintf("[name = %s]", $services->{$_})
);
}
$self->{output}->output_add(severity => 'OK', short_msg => 'Slack services:');
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
$self->{output}->exit();
}
sub disco_format {
my ($self, %options) = @_;
$self->{output}->add_disco_format(elements => ['name']);
}
sub disco_show {
my ($self, %options) = @_;
my $services = $self->manage_selection(%options);
foreach (sort keys %$services) {
$self->{output}->add_disco_entry(
name => $services->{$_}
);
}
}
1;
__END__
=head1 MODE
List slack services.
=over 8
=item B<--filter-name>
Filter service name (Can be a regexp).
=back
=cut

View File

@ -0,0 +1,175 @@
#
# Copyright 2021 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 apps::slack::restapi::mode::services;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
use DateTime;
use centreon::plugins::misc;
sub custom_status_output {
my ($self, %options) = @_;
my $msg = 'status is ' . $self->{result_values}->{status};
if ($self->{result_values}->{since} ne '') {
$msg .= sprintf(
' [type: %s][since: %s][message: %s]',
$self->{result_values}->{type},
$self->{result_values}->{since},
$self->{result_values}->{message}
);
}
return $msg;
}
sub prefix_service_output {
my ($self, %options) = @_;
return "Service '" . $options{instance_value}->{name} . "' ";
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0 },
{ name => 'services', type => 1, cb_prefix_output => 'prefix_service_output', display_long => 1, cb_long_output => 'long_output', message_multiple => 'All slack services are ok' }
];
$self->{maps_counters}->{global} = [
{ label => 'services', nlabel => 'slack.services.count', display_ok => 0, set => {
key_values => [ { name => 'total' } ],
output_template => '%s Slack services',
perfdatas => [
{ template => '%s', min => 0 }
]
}
}
];
$self->{maps_counters}->{services} = [
{
label => 'status',
type => 2,
warning_default => '%{status} eq "active" and %{type} eq "incident"',
critical_default => '%{status} eq "active" and %{type} eq "outage"',
set => {
key_values => [ { name => 'status' }, { name => 'type' }, { name => 'name' }, { name => 'message' }, { name => 'since' } ],
closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold_ng
}
}
];
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class;
$options{options}->add_options(arguments => {
'filter-name:s' => { name => 'filter_name' }
});
return $self;
}
sub manage_selection {
my ($self, %options) = @_;
my $services = $options{custom}->get_services();
my $results = $options{custom}->request_status_api();
$self->{services} = {};
foreach my $name (keys %$services) {
next if (
defined($self->{option_results}->{filter_name})
&& $self->{option_results}->{filter_name} ne ''
&& $name !~ /$self->{option_results}->{filter_name}/i
);
$self->{services}->{lc($name)} = {
name => $name,
status => 'ok',
type => '-',
message => '-',
since => ''
};
}
foreach my $entry (@{$results->{active_incidents}}) {
next if ($entry->{status} eq 'ok');
next if (!defined($entry->{date_created}) || $entry->{date_created} !~ /^(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)(.*)$/);
my $dt = DateTime->new(
year => $1,
month => $2,
day => $3,
hour => $4,
minute => $5,
second => $6,
time_zone => $7
);
my $diff_time = time() - $dt->epoch();
foreach (@{$entry->{services}}) {
next if (!defined($self->{services}->{ lc($_->{title}) }));
$self->{services}->{ lc($_->{title}) }->{status} = $entry->{status};
$self->{services}->{ lc($_->{title}) }->{type} = $entry->{type};
$self->{services}->{ lc($_->{title}) }->{message} = $entry->{title};
$self->{services}->{ lc($_->{title}) }->{since} = centreon::plugins::misc::change_seconds(value => $diff_time);
}
}
$self->{global} = { total => scalar(keys %{$self->{services}}) };
}
1;
__END__
=head1 MODE
Check Slack services status.
=over 8
=item B<--filter-name>
Only display the status for a specific servie
(Example: --filter-service='connections')
=item B<--warning-status>
Set warning threshold for the service status (Default: '%{status} eq "active" and %{type} eq "incident"').
=item B<--critical-status>
Set warning threshold for the service status (Default: '%{status} eq "active" and %{type} eq "outage"').
=back
=cut

View File

@ -32,7 +32,9 @@ sub new {
$self->{version} = '1.0';
$self->{modes} = {
'count-channels' => 'apps::slack::restapi::mode::countchannels',
'count-members' => 'apps::slack::restapi::mode::countmembers'
'count-members' => 'apps::slack::restapi::mode::countmembers',
'list-services' => 'apps::slack::restapi::mode::listservices',
'services' => 'apps::slack::restapi::mode::services'
};
$self->{custom_modes}->{restapi} = 'apps::slack::restapi::custom::api';