add kaminario plugin restapi

This commit is contained in:
qgarnier 2017-10-09 14:50:04 +02:00
parent c6381dd0be
commit b8ddf99a01
4 changed files with 591 additions and 0 deletions

View File

@ -0,0 +1,208 @@
#
# Copyright 2017 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 storage::kaminario::restapi::custom::api;
use strict;
use warnings;
use centreon::plugins::http;
use JSON;
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', },
"username:s@" => { name => 'username', },
"password:s@" => { name => 'password', },
"proxyurl:s@" => { name => 'proxyurl', },
"timeout:s@" => { name => 'timeout', },
"resolution:s@" => { name => 'resolution', },
});
}
$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})) ? shift(@{$self->{option_results}->{hostname}}) : undef;
$self->{username} = (defined($self->{option_results}->{username})) ? shift(@{$self->{option_results}->{username}}) : '';
$self->{password} = (defined($self->{option_results}->{password})) ? shift(@{$self->{option_results}->{password}}) : '';
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? shift(@{$self->{option_results}->{timeout}}) : 10;
$self->{proxyurl} = (defined($self->{option_results}->{proxyurl})) ? shift(@{$self->{option_results}->{proxyurl}}) : undef;
$self->{resolution} = (defined($self->{option_results}->{resolution})) ? shift(@{$self->{option_results}->{resolution}}) : '5m';
if (!defined($self->{hostname})) {
$self->{output}->add_option_msg(short_msg => "Need to specify hostname option.");
$self->{output}->option_exit();
}
if (!defined($self->{hostname}) ||
scalar(@{$self->{option_results}->{hostname}}) == 0) {
return 0;
}
return 1;
}
sub build_options_for_httplib {
my ($self, %options) = @_;
$self->{option_results}->{hostname} = $self->{hostname};
$self->{option_results}->{timeout} = $self->{timeout};
$self->{option_results}->{port} = 443;
$self->{option_results}->{proto} = 'https';
$self->{option_results}->{proxyurl} = $self->{proxyurl};
$self->{option_results}->{credentials} = 1;
$self->{option_results}->{username} = $self->{username};
$self->{option_results}->{password} = $self->{password};
}
sub settings {
my ($self, %options) = @_;
$self->build_options_for_httplib();
$self->{http}->add_header(key => 'Accept', value => 'application/json');
$self->{http}->set_options(%{$self->{option_results}});
}
sub get_performance {
my ($self, %options) = @_;
$self->settings();
#my $content = $self->{http}->request(url_path => '/api/v2' . $options{path} . '&__resolution=' . $self->{resolution},
# critical_status => '', warning_status => '');
my $content = do {
local $/ = undef;
if (!open my $fh, "<", '/tmp/4.json') {
$self->{output}->add_option_msg(short_msg => "Could not open file toto : $!");
$self->{output}->option_exit();
}
<$fh>;
};
#my $response = $self->{http}->get_response();
my $decoded;
eval {
$decoded = decode_json($content);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response");
$self->{output}->option_exit();
}
#if ($response->code() != 200) {
# $self->{output}->add_option_msg(short_msg => "Connection issue: " . $decoded->{message});
# $self->{output}->option_exit();
#}
return $decoded;
}
1;
__END__
=head1 NAME
KAMINARIO REST API
=head1 SYNOPSIS
Kaminario Rest API custom mode
=head1 REST API OPTIONS
=over 8
=item B<--hostname>
Kaminario hostname.
=item B<--username>
Kaminario username.
=item B<--password>
Kaminario password.
=item B<--proxyurl>
Proxy URL if any.
=item B<--timeout>
Set HTTP timeout in seconds (Default: '10').
=item B<--resolution>
Selected data performance resolution (Default: '5m').
=back
=head1 DESCRIPTION
B<custom>.
=cut

View File

@ -0,0 +1,187 @@
#
# Copyright 2017 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 storage::kaminario::restapi::mode::systemusage;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' },
{ name => 'bs', type => 1, cb_prefix_output => 'prefix_bs_output', message_multiple => 'All block sizes are ok' }
];
$self->{maps_counters}->{global} = [
{ label => 'iops', set => {
key_values => [ { name => 'iops_avg' } ],
output_template => 'Average IOPs : %s',
perfdatas => [
{ label => 'iops', value => 'iops_avg_absolute', template => '%s',
min => 0, unit => 'iops' },
],
}
},
{ label => 'throughput', set => {
key_values => [ { name => 'throughput_avg' } ],
output_template => 'Average Throughput : %s %s',
output_change_bytes => 1,
perfdatas => [
{ label => 'throughput', value => 'throughput_avg_absolute', template => '%s',
min => 0, unit => 'B' },
],
}
},
{ label => 'latency-inner', set => {
key_values => [ { name => 'latency_inner' } ],
output_template => 'Latency Inner : %.6fs',
perfdatas => [
{ label => 'latency_inner', value => 'latency_inner_absolute', template => '%.6f',
min => 0, unit => 's' },
],
}
},
{ label => 'latency-outer', set => {
key_values => [ { name => 'latency_outer' } ],
output_template => 'Latency Outer : %.6fs',
perfdatas => [
{ label => 'latency_outer', value => 'latency_outer_absolute', template => '%.6f',
min => 0, unit => 's' },
],
}
},
];
$self->{maps_counters}->{bs} = [
{ label => 'bs-iops', set => {
key_values => [ { name => 'iops_avg' }, { name => 'display' } ],
output_template => 'Average IOPs : %s',
perfdatas => [
{ label => 'iops', value => 'iops_avg_absolute', template => '%s',
min => 0, unit => 'iops', label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'bs-throughput', set => {
key_values => [ { name => 'throughput_avg' }, { name => 'display' } ],
output_template => 'Average Throughput : %s %s',
output_change_bytes => 1,
perfdatas => [
{ label => 'throughput', value => 'throughput_avg_absolute', template => '%s',
min => 0, unit => 'B', label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'bs-latency-inner', set => {
key_values => [ { name => 'latency_inner' }, { name => 'display' } ],
output_template => 'Latency Inner : %.6fs',
perfdatas => [
{ label => 'latency_inner', value => 'latency_inner_absolute', template => '%.6f',
min => 0, unit => 's', label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'bs-latency-outer', set => {
key_values => [ { name => 'latency_outer' }, { name => 'display' } ],
output_template => 'Latency Outer : %.6fs',
perfdatas => [
{ label => 'latency_outer', value => 'latency_outer_absolute', template => '%.6f',
min => 0, unit => 's', label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
];
}
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 prefix_global_output {
my ($self, %options) = @_;
return "System ";
}
sub prefix_bs_output {
my ($self, %options) = @_;
return "Block size '" . $options{instance_value}->{display} . "' ";
}
sub manage_selection {
my ($self, %options) = @_;
$self->{bs} = {};
my $result = $options{custom}->get_performance(path => '/stats/system?__datapoints=1&__bs_breakdown=true');
foreach my $entry (@{$result->{hits}}) {
$self->{bs}->{$entry->{bs}} = {
display => $entry->{bs},
%{$entry},
};
}
$result = $options{custom}->get_performance(path => '/stats/system?__datapoints=1');
$self->{global} = { %{$result->{hits}->[0]} };
}
1;
__END__
=head1 MODE
Check system usage.
=over 8
=item B<--filter-counters>
Only display some counters (regexp can be used).
Example: --filter-counters='^iops$'
=item B<--warning-*>
Threshold warning.
Can be: 'latency-inner', 'latency-outer', 'iops', 'throughput',
'bs-latency-inner', 'bs-latency-outer', 'bs-iops', 'bs-throughput',
=item B<--critical-*>
Threshold critical.
Can be: 'latency-inner', 'latency-outer', 'iops', 'throughput',
'bs-latency-inner', 'bs-latency-outer', 'bs-iops', 'bs-throughput',
=back
=cut

View File

@ -0,0 +1,146 @@
#
# Copyright 2017 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 storage::kaminario::restapi::mode::volumeusage;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'volume', type => 1, cb_prefix_output => 'prefix_volume_output', message_multiple => 'All volumes are ok' },
];
$self->{maps_counters}->{volume} = [
{ label => 'iops', set => {
key_values => [ { name => 'iops_avg' }, { name => 'display' } ],
output_template => 'Average IOPs : %s',
perfdatas => [
{ label => 'iops', value => 'iops_avg_absolute', template => '%s',
min => 0, unit => 'iops', label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'throughput', set => {
key_values => [ { name => 'throughput_avg' }, { name => 'display' } ],
output_template => 'Average Throughput : %s %s',
output_change_bytes => 1,
perfdatas => [
{ label => 'throughput', value => 'throughput_avg_absolute', template => '%s',
min => 0, unit => 'B', label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'latency-inner', set => {
key_values => [ { name => 'latency_inner' }, { name => 'display' } ],
output_template => 'Latency Inner : %.6fs',
perfdatas => [
{ label => 'latency_inner', value => 'latency_inner_absolute', template => '%.6f',
min => 0, unit => 's', label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'latency-outer', set => {
key_values => [ { name => 'latency_outer' }, { name => 'display' } ],
output_template => 'Latency Outer : %.6fs',
perfdatas => [
{ label => 'latency_outer', value => 'latency_outer_absolute', template => '%.6f',
min => 0, unit => 's', label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
];
}
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-name:s" => { name => 'filter_name' },
});
return $self;
}
sub prefix_volume_output {
my ($self, %options) = @_;
return "Volume '" . $options{instance_value}->{display} . "' ";
}
sub manage_selection {
my ($self, %options) = @_;
$self->{volume} = {};
my $result = $options{custom}->get_performance(path => '/stats/volumes?__datapoints=1');
foreach my $entry (@{$result->{hits}}) {
if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
$entry->{volume_name} !~ /$self->{option_results}->{filter_name}/) {
$self->{output}->output_add(long_msg => "skipping '" . $entry->{volume_name} . "': no matching filter.", debug => 1);
next;
}
$self->{volume}->{$entry->{volume_name}} = {
display => $entry->{volume_name},
%{$entry},
};
}
}
1;
__END__
=head1 MODE
Check volume usage.
=over 8
=item B<--filter-counters>
Only display some counters (regexp can be used).
Example: --filter-counters='^iops$'
=item B<--filter-name>
Filter volume name (can be a regexp).
=item B<--warning-*>
Threshold warning.
Can be: 'latency-inner', 'latency-outer', 'iops', 'throughput'
=item B<--critical-*>
Threshold critical.
Can be: 'latency-inner', 'latency-outer', 'iops', 'throughput'
=back
=cut

View File

@ -0,0 +1,50 @@
#
# Copyright 2017 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 storage::kaminario::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}} = (
'system-usage' => 'storage::kaminario::restapi::mode::systemusage',
'volume-usage' => 'storage::kaminario::restapi::mode::volumeusage',
);
$self->{custom_modes}{api} = 'storage::kaminario::restapi::custom::api';
return $self;
}
1;
__END__
=head1 PLUGIN DESCRIPTION
Check Kaminario through HTTP/REST API.
=cut