add slack plugin (#1076)

This commit is contained in:
Colin Gagnaire 2018-07-23 18:23:07 +02:00 committed by GitHub
parent e2e39da6b9
commit 988eb3b19f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 612 additions and 0 deletions

View File

@ -0,0 +1,240 @@
#
# Copyright 2018 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::custom::api;
use strict;
use warnings;
use centreon::plugins::http;
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 =>
{
"hostname:s" => { name => 'hostname' },
"port:s" => { name => 'port' },
"proto:s" => { name => 'proto' },
"proxyurl:s" => { name => 'proxyurl' },
"timeout:s" => { name => 'timeout' },
"ssl-opt:s@" => { name => 'ssl_opt' },
"api-path:s" => { name => 'api_path' },
"api-token:s" => { name => 'api_token' },
});
}
$options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
$self->{output} = $options{output};
$self->{mode} = $options{mode};
$self->{http} = centreon::plugins::http->new(output => $self->{output});
return $self;
}
sub set_options {
my ($self, %options) = @_;
$self->{option_results} = $options{option_results};
}
sub set_defaults {
my ($self, %options) = @_;
foreach (keys %{$options{default}}) {
if ($_ eq $self->{mode}) {
for (my $i = 0; $i < scalar(@{$options{default}->{$_}}); $i++) {
foreach my $opt (keys %{$options{default}->{$_}[$i]}) {
if (!defined($self->{option_results}->{$opt}[$i])) {
$self->{option_results}->{$opt}[$i] = $options{default}->{$_}[$i]->{$opt};
}
}
}
}
}
}
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->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
$self->{proxyurl} = (defined($self->{option_results}->{proxyurl})) ? $self->{option_results}->{proxyurl} : undef;
$self->{ssl_opt} = (defined($self->{option_results}->{ssl_opt})) ? $self->{option_results}->{ssl_opt} : undef;
$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;
if (!defined($self->{hostname})) {
$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 build_options_for_httplib {
my ($self, %options) = @_;
$self->{option_results}->{hostname} = $self->{hostname};
$self->{option_results}->{port} = $self->{port};
$self->{option_results}->{proto} = $self->{proto};
$self->{option_results}->{timeout} = $self->{timeout};
$self->{option_results}->{proxyurl} = $self->{proxyurl};
$self->{option_results}->{ssl_opt} = $self->{ssl_opt};
$self->{option_results}->{warning_status} = '';
$self->{option_results}->{critical_status} = '';
}
sub settings {
my ($self, %options) = @_;
$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 {
my ($self, %options) = @_;
$self->settings();
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 => '');
my $response = $self->{http}->get_response();
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}->option_exit();
}
if ($decoded->{ok} != 1) {
$self->{output}->add_option_msg(short_msg => "Error: " . $decoded->{error});
$self->{output}->option_exit();
}
return $decoded;
}
sub get_api_token {
my ($self, %options) = @_;
if (defined($self->{api_token}) && $self->{api_token} ne '') {
return $self->{api_token};
}
# OAuth2 not handled
return;
}
sub get_object {
my ($self, %options) = @_;
if (!defined($self->{token})) {
$self->{token} = $self->get_api_token();
}
my $result = $self->request_api(%options);
return $result;
}
1;
__END__
=head1 NAME
Slack REST API
=head1 SYNOPSIS
Slack Rest API custom mode
=head1 REST API OPTIONS
=over 8
=item B<--hostname>
Slack API hostname (Default: 'slack.com').
=item B<--api-path>
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'.
=item B<--port>
Slack API port
=item B<--proto>
Specify https if needed (Default: 'https')
=item B<--proxyurl>
Proxy URL if any
=item B<--timeout>
Set HTTP timeout
=item B<--ssl-opt>
Set SSL Options (Examples: --ssl-opt="SSL_version => TLSv1"
--ssl-opt="SSL_verify_mode => SSL_VERIFY_NONE").
=back
=head1 DESCRIPTION
B<custom>.
=cut

View File

@ -0,0 +1,181 @@
#
# Copyright 2018 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::countchannels;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
sub prefix_output {
my ($self, %options) = @_;
return "Channel '" . $options{instance_value}->{name} . "' ";
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0 },
{ name => 'channels', type => 1, cb_prefix_output => 'prefix_output' },
];
$self->{maps_counters}->{global} = [
{ label => 'count', set => {
key_values => [ { name => 'count' } ],
output_template => 'Number of channels : %d',
perfdatas => [
{ label => 'count', value => 'count_absolute', 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'),
}
},
];
}
sub custom_info_perfdata {
my ($self, %options) = @_;
my $extra_label = '';
$extra_label = '_' . $self->{result_values}->{name} if (!defined($options{extra_instance}) || $options{extra_instance} != 0);
$self->{output}->perfdata_add(label => 'members' . $extra_label,
value => $self->{result_values}->{num_members},
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{label}),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{label}),
min => 0);
}
sub custom_info_threshold {
my ($self, %options) = @_;
my $exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{num_members},
threshold => [ { label => 'critical-' . $self->{label}, exit_litteral => 'critical' },
{ label => 'warning-' . $self->{label}, 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 new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$self->{version} = '1.0';
$options{options}->add_options(arguments =>
{
"filter-channel:s" => { name => 'filter_channel' },
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
}
sub manage_selection {
my ($self, %options) = @_;
my $result = $options{custom}->get_object(url_path => '/channels.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}/) {
$self->{output}->output_add(long_msg => "skipping '" . $channel->{name_normalized} . "': no matching filter name.", debug => 1);
next;
}
$self->{channels}->{$channel->{id}} = {
id => $channel->{id},
name => $channel->{name_normalized},
num_members => $channel->{num_members},
};
$self->{global}->{count}++;
}
if (scalar(keys %{$self->{channels}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No channels found.");
$self->{output}->option_exit();
}
}
1;
__END__
=head1 MODE
Check channels count.
=over 8
=item B<--warning-count>
Threshold warning for channels count.
=item B<--critical-count>
Threshold critical for channels count.
=item B<--warning-members>
Threshold warning for members count per channel.
=item B<--critical-members>
Threshold critical for members count per channel.
=back
=cut

View File

@ -0,0 +1,142 @@
#
# Copyright 2018 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::countmembers;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
sub prefix_output {
my ($self, %options) = @_;
return "User '" . $options{instance_value}->{real_name} . "' ";
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0 },
{ name => 'members', type => 1, cb_prefix_output => 'prefix_output' },
];
$self->{maps_counters}->{global} = [
{ label => 'count', set => {
key_values => [ { name => 'count' } ],
output_template => 'Number of members : %d',
perfdatas => [
{ label => 'count', value => 'count_absolute', template => '%d',
min => 0 },
],
}
},
];
$self->{maps_counters}->{members} = [
{ label => 'info', 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; },
}
},
];
}
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);
bless $self, $class;
$self->{version} = '1.0';
$options{options}->add_options(arguments =>
{
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
}
sub manage_selection {
my ($self, %options) = @_;
my $result = $options{custom}->get_object(url_path => '/users.list');
$self->{global}->{count} = 0;
foreach my $member (@{$result->{members}}) {
$self->{members}->{$member->{id}} = {
id => $member->{id},
real_name => $member->{profile}->{real_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();
}
}
1;
__END__
=head1 MODE
Check members count.
=over 8
=item B<--warning-count>
Threshold warning for members count.
=item B<--critical-count>
Threshold critical for members count.
=back
=cut

View File

@ -0,0 +1,49 @@
#
# Copyright 2018 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::plugin;
use strict;
use warnings;
use base qw(centreon::plugins::script_custom);
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$self->{version} = '1.0';
%{$self->{modes}} = (
'count-channels' => 'apps::slack::restapi::mode::countchannels',
'count-members' => 'apps::slack::restapi::mode::countmembers',
);
$self->{custom_modes}{restapi} = 'apps::slack::restapi::custom::api';
return $self;
}
1;
__END__
=head1 PLUGIN DESCRIPTION
Check Slack using API.
=cut