add purestorage restapi: wip

This commit is contained in:
qgarnier 2017-12-19 11:15:11 +01:00
parent 1f5a01a035
commit f23b3b03cb
3 changed files with 467 additions and 0 deletions

View File

@ -0,0 +1,269 @@
#
# 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::purestorage::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', },
});
}
$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} : undef;
$self->{username} = (defined($self->{option_results}->{username})) ? $self->{option_results}->{username} : undef;
$self->{password} = (defined($self->{option_results}->{password})) ? $self->{option_results}->{password} : undef;
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
$self->{proxyurl} = (defined($self->{option_results}->{proxyurl})) ? $self->{option_results}->{proxyurl} : undef;
if (!defined($self->{hostname})) {
$self->{output}->add_option_msg(short_msg => "Need to specify hostname option.");
$self->{output}->option_exit();
}
if (!defined($self->{username})) {
$self->{output}->add_option_msg(short_msg => "Need to specify username option.");
$self->{output}->option_exit();
}
if (!defined($self->{password})) {
$self->{output}->add_option_msg(short_msg => "Need to specify password option.");
$self->{output}->option_exit();
}
return 0;
}
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};
}
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/json');
if (defined($self->{session_id})) {
$self->{http}->add_header(key => 'Cookie', value => 'session=' . $self->{session_id});
}
$self->{http}->set_options(%{$self->{option_results}});
}
sub request_api {
my ($self, %options) = @_;
my $response = $self->{http}->request(method => $options{method}, url_path => $options{url},
critical_status => '', warning_status => '', unknown_status => '');
my $decoded;
eval {
$decoded = decode_json($response);
};
if ($@) {
$self->{output}->output_add(long_msg => $response, debug => 1);
$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->{msg});
$self->{output}->option_exit();
}
return $decoded;
}
sub get_api_token {
my ($self, %options) = @_;
my $json_request = { username => $self->{username}, password => $self->{password} };
my $encoded;
eval {
$encoded = encode_json($json_request);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot encode json request");
$self->{output}->option_exit();
}
$self->settings();
my $decoded = $self->request_api(method => 'POST', url_path => '/api/1.11/auth/apitoken', query_form_post => $encoded);
if (!defined($decoded->{api_token})) {
$self->{output}->add_option_msg(short_msg => "Cannot get api token");
$self->{output}->option_exit();
}
return $decoded->{api_token};
}
sub get_session {
my ($self, %options) = @_;
my $json_request = { api_token => $options{api_token} };
my $encoded;
eval {
$encoded = encode_json($json_request);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot encode json request");
$self->{output}->option_exit();
}
$self->settings();
my $decoded = $self->request_api(method => 'POST', url_path => '/api/1.11/auth/session', query_form_post => $encoded);
my $headers = $self->{http}->get_header();
my $cookie = $headers->header('Set-Cookie');
if (!defined($cookie)) {
$self->{output}->add_option_msg(short_msg => "Cannot get session");
$self->{output}->option_exit();
}
$cookie =~ /session=(.*);/;
return $1;
}
sub connect {
my ($self, %options) = @_;
my $api_token = $self->get_api_token();
$self->{session_id} = $self->get_session(api_token => $api_token);
}
sub get_object {
my ($self, %options) = @_;
if (!defined($self->{api_token})) {
$self->connect();
}
$self->settings();
return $self->request_api(method => 'GET', url_path => '/api/1.11' . $options{path});
}
sub DESTROY {
my $self = shift;
if (defined($self->{session_id})) {
$self->request_api(method => 'DELETE', url_path => '/api/1.11/auth/session');
}
}
1;
__END__
=head1 NAME
Pure Storage REST API
=head1 SYNOPSIS
Pure Storage Rest API custom mode
=head1 REST API OPTIONS
=over 8
=item B<--hostname>
PureStorage hostname.
=item B<--username>
PureStorage username.
=item B<--password>
v password.
=item B<--proxyurl>
Proxy URL if any.
=item B<--timeout>
Set HTTP timeout in seconds (Default: '10').
=back
=head1 DESCRIPTION
B<custom>.
=cut

View File

@ -0,0 +1,149 @@
#
# 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::purestorage::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 : %.6fms',
perfdatas => [
{ label => 'latency_inner', value => 'latency_inner_absolute', template => '%.6f',
min => 0, unit => 'ms', label_extra_instance => 1, instance_use => 'display_absolute' },
],
}
},
{ label => 'latency-outer', set => {
key_values => [ { name => 'latency_outer' }, { name => 'display' } ],
output_template => 'Latency Outer : %.6fms',
perfdatas => [
{ label => 'latency_outer', value => 'latency_outer_absolute', template => '%.6f',
min => 0, unit => 'ms', 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_object(path => '/volume?space=true');
use Data::Dumper;
print Data::Dumper::Dumper($result);
exit(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,49 @@
#
# 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::purestorage::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} = '0.1';
%{$self->{modes}} = (
'volume-usage' => 'storage::purestorage::restapi::mode::volumeusage',
);
$self->{custom_modes}{api} = 'storage::purestorage::restapi::custom::api';
return $self;
}
1;
__END__
=head1 PLUGIN DESCRIPTION
Check Pure Storage through HTTP/REST API.
=cut