diff --git a/cloud/openstack/restapi/mode/hypervisor.pm b/cloud/openstack/restapi/mode/hypervisor.pm new file mode 100644 index 000000000..7655a153e --- /dev/null +++ b/cloud/openstack/restapi/mode/hypervisor.pm @@ -0,0 +1,303 @@ +# +# Copyright 2015 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 cloud::openstack::restapi::mode::hypervisor; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::http; +use JSON; + +my $thresholds = { + status => [ + ['up', 'OK'], + ['down', 'CRITICAL'], + ], +}; + +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 => + { + "data:s" => { name => 'data' }, + "hostname:s" => { name => 'hostname' }, + "http-peer-addr:s" => { name => 'http_peer_addr' }, + "port:s" => { name => 'port', default => '5000' }, + "proto:s" => { name => 'proto' }, + "urlpath:s" => { name => 'url_path', default => '/v3/auth/tokens' }, + "proxyurl:s" => { name => 'proxyurl' }, + "proxypac:s" => { name => 'proxypac' }, + "credentials" => { name => 'credentials' }, + "username:s" => { name => 'username' }, + "password:s" => { name => 'password' }, + "ssl:s" => { name => 'ssl', }, + "header:s@" => { name => 'header' }, + "timeout:s" => { name => 'timeout' }, + "tenant-id:s" => { name => 'tenant_id' }, + "hypervisor-id:s" => { name => 'hypervisor_id' }, + "threshold-overload:s@" => { name => 'threshold_overload' }, + }); + + $self->{http} = centreon::plugins::http->new(output => $self->{output}); + $self->{hypervisor_infos} = (); + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + $self->{overload_th} = {}; + foreach my $val (@{$self->{option_results}->{threshold_overload}}) { + if ($val !~ /^(.*?),(.*?),(.*)$/) { + $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload option '" . $val . "'."); + $self->{output}->option_exit(); + } + my ($section, $status, $filter) = ($1, $2, $3); + if ($self->{output}->is_litteral_status(status => $status) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload status '" . $val . "'."); + $self->{output}->option_exit(); + } + $self->{overload_th}->{$section} = [] if (!defined($self->{overload_th}->{$section})); + push @{$self->{overload_th}->{$section}}, {filter => $filter, status => $status}; + } + + if (!defined($self->{option_results}->{header}) || $self->{option_results}->{header} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --header option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{data}) || $self->{option_results}->{data} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --data option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{hostname}) || $self->{option_results}->{hostname} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --hostname option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{tenant_id}) || $self->{option_results}->{tenant_id} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --tenant-id option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{hypervisor_id}) || $self->{option_results}->{hypervisor_id} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --hypervisor-id option."); + $self->{output}->option_exit(); + } + + $self->{http}->set_options(%{$self->{option_results}}) +} + +sub token_request { + my ($self, %options) = @_; + + $self->{method} = 'GET'; + if (defined($self->{option_results}->{data})) { + local $/ = undef; + if (!open(FILE, "<", $self->{option_results}->{data})) { + $self->{output}->output_add(severity => 'UNKNOWN', + short_msg => sprintf("Could not read file '%s': %s", $self->{option_results}->{data}, $!)); + $self->{output}->display(); + $self->{output}->exit(); + } + $self->{json_request} = ; + close FILE; + $self->{method} = 'POST'; + } + + my $response = $self->{http}->request(method => $self->{method}, query_form_post => $self->{json_request}); + my $headers = $self->{http}->get_header(); + + eval { + $self->{header} = $headers->header('X-Subject-Token'); + }; + + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot retrieve API Token"); + $self->{output}->option_exit(); + } +} + +sub api_request { + my ($self, %options) = @_; + + $self->{method} = 'GET'; + $self->{option_results}->{url_path} = "/v2/".$self->{option_results}->{tenant_id}."/os-hypervisors/".$self->{option_results}->{hypervisor_id}; + $self->{option_results}->{port} = '8774'; + @{$self->{option_results}->{header}} = ('X-Auth-Token:' . $self->{header}, 'Accept:application/json'); + $self->{http}->set_options(%{$self->{option_results}}); + + my $webcontent; + my $jsoncontent = $self->{http}->request(method => $self->{method}); + + my $json = JSON->new; + + eval { + $webcontent = $json->decode($jsoncontent); + }; + + $self->{hypervisor_infos}->{name} = $webcontent->{hypervisor}->{hypervisor_hostname}; + $self->{hypervisor_infos}->{state} = $webcontent->{hypervisor}->{state}; + $self->{hypervisor_infos}->{status} = $webcontent->{hypervisor}->{status}; +} + + +sub get_severity { + my ($self, %options) = @_; + my $status = 'UNKNOWN'; # default + + if (defined($self->{overload_th}->{$options{section}})) { + foreach (@{$self->{overload_th}->{$options{section}}}) { + if ($options{value} =~ /$_->{filter}/i) { + $status = $_->{status}; + return $status; + } + } + } + foreach (@{$thresholds->{$options{section}}}) { + if ($options{value} =~ /$$_[0]/i) { + $status = $$_[1]; + return $status; + } + } + + return $status; +} + +sub run { + my ($self, %options) = @_; + + $self->token_request(); + $self->api_request(); + + my $exit = $self->get_severity(section => 'status', value => $self->{hypervisor_infos}->{state}); + $self->{output}->output_add(severity => $exit, + short_msg => sprintf("Hypervisor %s is %s (status: %s)", + $self->{hypervisor_infos}->{name}, + $self->{hypervisor_infos}->{state}, + $self->{hypervisor_infos}->{status})); + + $self->{output}->display(); + $self->{output}->exit(); + + exit 0; +} + +1; + +__END__ + +=head1 MODE + +List OpenStack instances through Compute API V2 + +JSON OPTIONS: + +=over 8 + +=item B<--data> + +Set file with JSON request + +=back + +HTTP OPTIONS: + +=over 8 + +=item B<--hostname> + +IP Addr/FQDN of OpenStack Compute's API + +=item B<--http-peer-addr> + +Set the address you want to connect (Useful if hostname is only a vhost. no ip resolve) + +=item B<--port> + +Port used by OpenStack Keystone's API (Default: '5000') + +=item B<--proto> + +Specify https if needed (Default: 'http') + +=item B<--urlpath> + +Set path to get API's Token (Default: '/v3/auth/tokens') + +=item B<--proxyurl> + +Proxy URL + +=item B<--proxypac> + +Proxy pac file (can be an url or local file) + +=item B<--credentials> + +Specify this option if you access webpage over basic authentification + +=item B<--username> + +Specify username + +=item B<--password> + +Specify password + +=item B<--ssl> + +Specify SSL version (example : 'sslv3', 'tlsv1'...) + +=item B<--header> + +Set HTTP headers (Multiple option. Example: --header='Content-Type: xxxxx') + +=item B<--timeout> + +Threshold for HTTP timeout (Default: 3) + +=item B<--threshold-overload> + +Set to overload default threshold values (syntax: section,status,regexp) +It used before default thresholds (order stays). +Example: --threshold-overload='status,WARNING,^down$)' + +=back + +OPENSTACK OPTIONS: + +=over 8 + +=item B<--tenant-id> + +Set Tenant's ID + +=item B<--hypervisor-id> + +Set Hypervisor's ID + +=back + +=cut diff --git a/cloud/openstack/restapi/mode/instance.pm b/cloud/openstack/restapi/mode/instance.pm new file mode 100644 index 000000000..6cd5b5bc6 --- /dev/null +++ b/cloud/openstack/restapi/mode/instance.pm @@ -0,0 +1,311 @@ +# +# Copyright 2015 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 cloud::openstack::restapi::mode::instance; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::http; +use JSON; + +my $thresholds = { + status => [ + ['ACTIVE', 'OK'], + ['PAUSED', 'WARNING'], + ['SUSPENDED', 'WARNING'], + ['SHUTOFF', 'CRITICAL'], + ['REBUILD', 'WARNING'], + ['HARD_REBOOT', 'WARNING'], + ['ERROR', 'CRITCAL'], + ['BUILDING', 'OK'], + ['STOPPED', 'WARNING'], + ['DELETED', 'OK'], + ], +}; + +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 => + { + "data:s" => { name => 'data' }, + "hostname:s" => { name => 'hostname' }, + "http-peer-addr:s" => { name => 'http_peer_addr' }, + "port:s" => { name => 'port', default => '5000' }, + "proto:s" => { name => 'proto' }, + "urlpath:s" => { name => 'url_path', default => '/v3/auth/tokens' }, + "proxyurl:s" => { name => 'proxyurl' }, + "proxypac:s" => { name => 'proxypac' }, + "credentials" => { name => 'credentials' }, + "username:s" => { name => 'username' }, + "password:s" => { name => 'password' }, + "ssl:s" => { name => 'ssl', }, + "header:s@" => { name => 'header' }, + "timeout:s" => { name => 'timeout' }, + "tenant-id:s" => { name => 'tenant_id' }, + "instance-id:s" => { name => 'instance_id' }, + "threshold-overload:s@" => { name => 'threshold_overload' }, + }); + + $self->{http} = centreon::plugins::http->new(output => $self->{output}); + $self->{instance_infos} = (); + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + $self->{overload_th} = {}; + foreach my $val (@{$self->{option_results}->{threshold_overload}}) { + if ($val !~ /^(.*?),(.*?),(.*)$/) { + $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload option '" . $val . "'."); + $self->{output}->option_exit(); + } + my ($section, $status, $filter) = ($1, $2, $3); + if ($self->{output}->is_litteral_status(status => $status) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload status '" . $val . "'."); + $self->{output}->option_exit(); + } + $self->{overload_th}->{$section} = [] if (!defined($self->{overload_th}->{$section})); + push @{$self->{overload_th}->{$section}}, {filter => $filter, status => $status}; + } + + if (!defined($self->{option_results}->{header}) || $self->{option_results}->{header} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --header option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{data}) || $self->{option_results}->{data} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --data option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{hostname}) || $self->{option_results}->{hostname} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --hostname option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{tenant_id}) || $self->{option_results}->{tenant_id} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --tenant-id option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{instance_id}) || $self->{option_results}->{instance_id} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --instance-id option."); + $self->{output}->option_exit(); + } + + $self->{http}->set_options(%{$self->{option_results}}) +} + +sub token_request { + my ($self, %options) = @_; + + $self->{method} = 'GET'; + if (defined($self->{option_results}->{data})) { + local $/ = undef; + if (!open(FILE, "<", $self->{option_results}->{data})) { + $self->{output}->output_add(severity => 'UNKNOWN', + short_msg => sprintf("Could not read file '%s': %s", $self->{option_results}->{data}, $!)); + $self->{output}->display(); + $self->{output}->exit(); + } + $self->{json_request} = ; + close FILE; + $self->{method} = 'POST'; + } + + my $response = $self->{http}->request(method => $self->{method}, query_form_post => $self->{json_request}); + my $headers = $self->{http}->get_header(); + + eval { + $self->{header} = $headers->header('X-Subject-Token'); + }; + + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot retrieve API Token"); + $self->{output}->option_exit(); + } +} + +sub api_request { + my ($self, %options) = @_; + + $self->{method} = 'GET'; + $self->{option_results}->{url_path} = "/v2/".$self->{option_results}->{tenant_id}."/servers/".$self->{option_results}->{instance_id}; + $self->{option_results}->{port} = '8774'; + @{$self->{option_results}->{header}} = ('X-Auth-Token:' . $self->{header}, 'Accept:application/json'); + $self->{http}->set_options(%{$self->{option_results}}); + + my $webcontent; + my $jsoncontent = $self->{http}->request(method => $self->{method}); + + my $json = JSON->new; + + eval { + $webcontent = $json->decode($jsoncontent); + }; + + $self->{instance_infos}->{name} = $webcontent->{server}->{name}; + $self->{instance_infos}->{vm_state} = $webcontent->{server}->{'OS-EXT-STS:vm_state'}; + $self->{instance_infos}->{state} = $webcontent->{server}->{status}; +} + + +sub get_severity { + my ($self, %options) = @_; + my $status = 'UNKNOWN'; # default + + if (defined($self->{overload_th}->{$options{section}})) { + foreach (@{$self->{overload_th}->{$options{section}}}) { + if ($options{value} =~ /$_->{filter}/i) { + $status = $_->{status}; + return $status; + } + } + } + foreach (@{$thresholds->{$options{section}}}) { + if ($options{value} =~ /$$_[0]/i) { + $status = $$_[1]; + return $status; + } + } + + return $status; +} + +sub run { + my ($self, %options) = @_; + + $self->token_request(); + $self->api_request(); + + my $exit = $self->get_severity(section => 'status', value => $self->{instance_infos}->{state}); + $self->{output}->output_add(severity => $exit, + short_msg => sprintf("Instance %s is in %s state (vm_state: %s)", + $self->{instance_infos}->{name}, + $self->{instance_infos}->{state}, + $self->{instance_infos}->{vm_state})); + + $self->{output}->display(); + $self->{output}->exit(); + + exit 0; +} + +1; + +__END__ + +=head1 MODE + +Monitor instance status through Compute API V2 + +JSON OPTIONS: + +=over 8 + +=item B<--data> + +Set file with JSON request + +=back + +HTTP OPTIONS: + +=over 8 + +=item B<--hostname> + +IP Addr/FQDN of OpenStack Compute's API + +=item B<--http-peer-addr> + +Set the address you want to connect (Useful if hostname is only a vhost. no ip resolve) + +=item B<--port> + +Port used by OpenStack Keystone's API (Default: '5000') + +=item B<--proto> + +Specify https if needed (Default: 'http') + +=item B<--urlpath> + +Set path to get API's Token (Default: '/v3/auth/tokens') + +=item B<--proxyurl> + +Proxy URL + +=item B<--proxypac> + +Proxy pac file (can be an url or local file) + +=item B<--credentials> + +Specify this option if you access webpage over basic authentification + +=item B<--username> + +Specify username + +=item B<--password> + +Specify password + +=item B<--ssl> + +Specify SSL version (example : 'sslv3', 'tlsv1'...) + +=item B<--header> + +Set HTTP headers (Multiple option. Example: --header='Content-Type: xxxxx') + +=item B<--timeout> + +Threshold for HTTP timeout (Default: 3) + +=item B<--threshold-overload> + +Set to overload default threshold values (syntax: section,status,regexp) +It used before default thresholds (order stays). +Example: --threshold-overload='status,CRITICAL,^STOPPED$)' + +=back + +OPENSTACK OPTIONS: + +=over 8 + +=item B<--tenant-id> + +Set Tenant's ID + +=item B<--instance-id> + +Set Instance's ID + +=back + +=cut diff --git a/cloud/openstack/restapi/mode/listhypervisors.pm b/cloud/openstack/restapi/mode/listhypervisors.pm new file mode 100644 index 000000000..f6763f998 --- /dev/null +++ b/cloud/openstack/restapi/mode/listhypervisors.pm @@ -0,0 +1,292 @@ +# +# Copyright 2015 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 cloud::openstack::restapi::mode::listhypervisors; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::http; +use JSON; + +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 => + { + "data:s" => { name => 'data' }, + "hostname:s" => { name => 'hostname' }, + "http-peer-addr:s" => { name => 'http_peer_addr' }, + "port:s" => { name => 'port', default => '5000' }, + "proto:s" => { name => 'proto' }, + "urlpath:s" => { name => 'url_path', default => '/v3/auth/tokens' }, + "proxyurl:s" => { name => 'proxyurl' }, + "proxypac:s" => { name => 'proxypac' }, + "credentials" => { name => 'credentials' }, + "username:s" => { name => 'username' }, + "password:s" => { name => 'password' }, + "ssl:s" => { name => 'ssl', }, + "header:s@" => { name => 'header' }, + "exclude:s" => { name => 'exclude' }, + "timeout:s" => { name => 'timeout' }, + "tenant-id:s" => { name => 'tenant_id' }, + }); + + $self->{http} = centreon::plugins::http->new(output => $self->{output}); + $self->{hypervisor_infos} = (); + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + if (!defined($self->{option_results}->{header}) || $self->{option_results}->{header} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --header option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{data}) || $self->{option_results}->{data} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --data option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{hostname}) || $self->{option_results}->{hostname} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --hostname option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{tenant_id}) || $self->{option_results}->{tenant_id} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --tenant-id option."); + $self->{output}->option_exit(); + } + + $self->{http}->set_options(%{$self->{option_results}}) +} + +sub check_exclude { + my ($self, %options) = @_; + + if (defined($self->{option_results}->{exclude}) && $self->{option_results}->{exclude} =~ /(^|\s|,)${options{status}}(\s|,|$)/) { + $self->{output}->output_add(long_msg => sprintf("Skipping ${options{status}} instance.")); + return 1; + } + return 0; +} + +sub token_request { + my ($self, %options) = @_; + + $self->{method} = 'GET'; + if (defined($self->{option_results}->{data})) { + local $/ = undef; + if (!open(FILE, "<", $self->{option_results}->{data})) { + $self->{output}->output_add(severity => 'UNKNOWN', + short_msg => sprintf("Could not read file '%s': %s", $self->{option_results}->{data}, $!)); + $self->{output}->display(); + $self->{output}->exit(); + } + $self->{json_request} = ; + close FILE; + $self->{method} = 'POST'; + } + + my $response = $self->{http}->request(method => $self->{method}, query_form_post => $self->{json_request}); + my $headers = $self->{http}->get_header(); + + eval { + $self->{header} = $headers->header('X-Subject-Token'); + }; + + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot retrieve API Token"); + $self->{output}->option_exit(); + } +} + +sub api_request { + my ($self, %options) = @_; + + $self->{method} = 'GET'; + $self->{option_results}->{url_path} = "/v2/".$self->{option_results}->{tenant_id}."/os-hypervisors/detail"; + $self->{option_results}->{port} = '8774'; + @{$self->{option_results}->{header}} = ('X-Auth-Token:' . $self->{header}, 'Accept:application/json'); + $self->{http}->set_options(%{$self->{option_results}}); + + my $webcontent; + my $jsoncontent = $self->{http}->request(method => $self->{method}); + + my $json = JSON->new; + + eval { + $webcontent = $json->decode($jsoncontent); + }; + + foreach my $val (@{$webcontent->{hypervisors}}) { + my $hypervisorname = $val->{hypervisor_hostname}; + $self->{hypervisor_infos}->{$hypervisorname}->{id} = $val->{id}; + $self->{hypervisor_infos}->{$hypervisorname}->{ipaddress} = $val->{host_ip}; + $self->{hypervisor_infos}->{$hypervisorname}->{type} = $val->{hypervisor_type}; + $self->{hypervisor_infos}->{$hypervisorname}->{status} = $val->{status}; + $self->{hypervisor_infos}->{$hypervisorname}->{state} = $val->{state}; + } +} + +sub disco_format { + my ($self, %options) = @_; + + my $names = ['name', 'ip', 'type', 'status', 'state']; + $self->{output}->add_disco_format(elements => $names); +} + +sub disco_show { + my ($self, %options) = @_; + + $self->token_request(); + $self->api_request(); + + foreach my $hypervisorname (keys %{$self->{hypervisor_infos}}) { + $self->{output}->add_disco_entry(name => $hypervisorname, + id => $self->{hypervisor_infos}->{$hypervisorname}->{id}, + ip => $self->{hypervisor_infos}->{$hypervisorname}->{ipaddress}, + type => $self->{hypervisor_infos}->{$hypervisorname}->{type}, + status => $self->{hypervisor_infos}->{$hypervisorname}->{status}, + state => $self->{hypervisor_infos}->{$hypervisorname}->{state}, + ); + } +} + +sub run { + my ($self, %options) = @_; + + $self->token_request(); + $self->api_request(); + + foreach my $hypervisorname (keys %{$self->{hypervisor_infos}}) { + $self->{output}->output_add(long_msg => sprintf("%s [id = %s, ip = %s, type = %s, status = %s, state = %s]", + $hypervisorname, + $self->{hypervisor_infos}->{$hypervisorname}->{id}, + $self->{hypervisor_infos}->{$hypervisorname}->{ipaddress}, + $self->{hypervisor_infos}->{$hypervisorname}->{type}, + $self->{hypervisor_infos}->{$hypervisorname}->{status}, + $self->{hypervisor_infos}->{$hypervisorname}->{state},)); + } + + $self->{output}->output_add(severity => 'OK', + short_msg => 'List hypervisors:'); + + $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); + $self->{output}->exit(); + + exit 0; +} + +1; + +__END__ + +=head1 MODE + +List OpenStack hypervisors through Compute API V2 + +JSON OPTIONS: + +=over 8 + +=item B<--data> + +Set file with JSON request + +=back + +HTTP OPTIONS: + +=over 8 + +=item B<--hostname> + +IP Addr/FQDN of OpenStack Compute's API + +=item B<--http-peer-addr> + +Set the address you want to connect (Useful if hostname is only a vhost. no ip resolve) + +=item B<--port> + +Port used by OpenStack Keystone's API (Default: '5000') + +=item B<--proto> + +Specify https if needed (Default: 'http') + +=item B<--urlpath> + +Set path to get API's Token (Default: '/v3/auth/tokens') + +=item B<--proxyurl> + +Proxy URL + +=item B<--proxypac> + +Proxy pac file (can be an url or local file) + +=item B<--credentials> + +Specify this option if you access webpage over basic authentification + +=item B<--username> + +Specify username + +=item B<--password> + +Specify password + +=item B<--ssl> + +Specify SSL version (example : 'sslv3', 'tlsv1'...) + +=item B<--header> + +Set HTTP headers (Multiple option. Example: --header='Content-Type: xxxxx') + +=item B<--exlude> + +Exclude specific instance's state (comma seperated list) (Example: --exclude=disabled) + +=item B<--timeout> + +Threshold for HTTP timeout (Default: 3) + +=back + +OPENSTACK OPTIONS: + +=over 8 + +=item B<--tenant-id> + +Set Tenant's ID + +=back + +=cut diff --git a/cloud/openstack/restapi/mode/listinstances.pm b/cloud/openstack/restapi/mode/listinstances.pm new file mode 100644 index 000000000..323589ad7 --- /dev/null +++ b/cloud/openstack/restapi/mode/listinstances.pm @@ -0,0 +1,305 @@ +# +# Copyright 2015 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 cloud::openstack::restapi::mode::listinstances; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::http; +use JSON; + +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 => + { + "data:s" => { name => 'data' }, + "hostname:s" => { name => 'hostname' }, + "http-peer-addr:s" => { name => 'http_peer_addr' }, + "port:s" => { name => 'port', default => '5000' }, + "proto:s" => { name => 'proto' }, + "urlpath:s" => { name => 'url_path', default => '/v3/auth/tokens' }, + "proxyurl:s" => { name => 'proxyurl' }, + "proxypac:s" => { name => 'proxypac' }, + "credentials" => { name => 'credentials' }, + "username:s" => { name => 'username' }, + "password:s" => { name => 'password' }, + "ssl:s" => { name => 'ssl', }, + "header:s@" => { name => 'header' }, + "exclude:s" => { name => 'exclude' }, + "timeout:s" => { name => 'timeout' }, + "tenant-id:s" => { name => 'tenant_id' }, + }); + + $self->{http} = centreon::plugins::http->new(output => $self->{output}); + $self->{instance_infos} = (); + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + if (!defined($self->{option_results}->{header}) || $self->{option_results}->{header} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --header option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{data}) || $self->{option_results}->{data} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --data option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{hostname}) || $self->{option_results}->{hostname} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --hostname option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{tenant_id}) || $self->{option_results}->{tenant_id} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --tenant-id option."); + $self->{output}->option_exit(); + } + + $self->{http}->set_options(%{$self->{option_results}}) +} + +sub check_exclude { + my ($self, %options) = @_; + + if (defined($self->{option_results}->{exclude}) && $self->{option_results}->{exclude} =~ /(^|\s|,)${options{status}}(\s|,|$)/) { + $self->{output}->output_add(long_msg => sprintf("Skipping ${options{status}} instance.")); + return 1; + } + return 0; +} + +sub token_request { + my ($self, %options) = @_; + + $self->{method} = 'GET'; + if (defined($self->{option_results}->{data})) { + local $/ = undef; + if (!open(FILE, "<", $self->{option_results}->{data})) { + $self->{output}->output_add(severity => 'UNKNOWN', + short_msg => sprintf("Could not read file '%s': %s", $self->{option_results}->{data}, $!)); + $self->{output}->display(); + $self->{output}->exit(); + } + $self->{json_request} = ; + close FILE; + $self->{method} = 'POST'; + } + + my $response = $self->{http}->request(method => $self->{method}, query_form_post => $self->{json_request}); + my $headers = $self->{http}->get_header(); + eval { + $self->{header} = $headers->header('X-Subject-Token'); + }; + + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot retrieve API Token"); + $self->{output}->option_exit(); + } +} + +sub api_request { + my ($self, %options) = @_; + + $self->{method} = 'GET'; + $self->{option_results}->{url_path} = "/v2/".$self->{option_results}->{tenant_id}."/servers/detail"; + $self->{option_results}->{port} = '8774'; + @{$self->{option_results}->{header}} = ('X-Auth-Token:' . $self->{header}, 'Accept:application/json'); + $self->{http}->set_options(%{$self->{option_results}}); + + my $webcontent; + my $jsoncontent = $self->{http}->request(method => $self->{method}); + + my $json = JSON->new; + + eval { + $webcontent = $json->decode($jsoncontent); + }; + + foreach my $val (@{$webcontent->{servers}}) { + my $instancestate; + if ($val->{status} eq "ACTIVE") { + next if ($self->check_exclude(status => 'Running')); + $instancestate = $val->{status}; + } elsif ($val->{status} eq "SUSPENDED" || $val->{status} eq "PAUSED") { + next if ($self->check_exclude(status => 'Paused')); + $instancestate = $val->{status}; + } elsif ($val->{status} eq "SHUTOFF") { + next if ($self->check_exclude(status => 'Off')); + $instancestate = $val->{status}; + } elsif ($val->{status} eq "REBUILD" || $val->{status} eq "HARD_REBOOT") { + next if ($self->check_exclude(status => 'Reboot')); + $instancestate = $val->{status}; + } + my $instancename = $val->{name}; + $self->{instance_infos}->{$instancename}->{id} = $val->{id}; + $self->{instance_infos}->{$instancename}->{zone} = $val->{'OS-EXT-AZ:availability_zone'}; + $self->{instance_infos}->{$instancename}->{compute} = $val->{'OS-EXT-SRV-ATTR:host'}; + $self->{instance_infos}->{$instancename}->{osname} = $val->{'OS-EXT-SRV-ATTR:instance_name'}; + $self->{instance_infos}->{$instancename}->{state} = $instancestate; + } +} + +sub disco_format { + my ($self, %options) = @_; + + my $names = ['name', 'id', 'zone', 'compute', 'osname', 'state']; + $self->{output}->add_disco_format(elements => $names); +} + +sub disco_show { + my ($self, %options) = @_; + + $self->token_request(); + $self->api_request(); + + foreach my $instancename (keys %{$self->{instance_infos}}) { + $self->{output}->add_disco_entry(name => $instancename, + id => $self->{instance_infos}->{$instancename}->{id}, + zone => $self->{instance_infos}->{$instancename}->{zone}, + compute => $self->{instance_infos}->{$instancename}->{compute}, + osname => $self->{instance_infos}->{$instancename}->{osname}, + state => $self->{instance_infos}->{$instancename}->{state}, + ); + } +} + +sub run { + my ($self, %options) = @_; + + $self->token_request(); + $self->api_request(); + + foreach my $instancename (keys %{$self->{instance_infos}}) { + $self->{output}->output_add(long_msg => sprintf("%s [id = %s, zone = %s, compute = %s, osname = %s, state = %s]", + $instancename, + $self->{instance_infos}->{$instancename}->{id}, + $self->{instance_infos}->{$instancename}->{zone}, + $self->{instance_infos}->{$instancename}->{compute}, + $self->{instance_infos}->{$instancename}->{osname}, + $self->{instance_infos}->{$instancename}->{state})); + } + + $self->{output}->output_add(severity => 'OK', + short_msg => 'List instances:'); + + $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); + $self->{output}->exit(); + + exit 0; +} + +1; + +__END__ + +=head1 MODE + +List OpenStack instances through Compute API V2 + +JSON OPTIONS: + +=over 8 + +=item B<--data> + +Set file with JSON request + +=back + +HTTP OPTIONS: + +=over 8 + +=item B<--hostname> + +IP Addr/FQDN of OpenStack Compute's API + +=item B<--http-peer-addr> + +Set the address you want to connect (Useful if hostname is only a vhost. no ip resolve) + +=item B<--port> + +Port used by OpenStack Keystone's API (Default: '5000') + +=item B<--proto> + +Specify https if needed (Default: 'http') + +=item B<--urlpath> + +Set path to get API's Token (Default: '/v3/auth/tokens') + +=item B<--proxyurl> + +Proxy URL + +=item B<--proxypac> + +Proxy pac file (can be an url or local file) + +=item B<--credentials> + +Specify this option if you access webpage over basic authentification + +=item B<--username> + +Specify username + +=item B<--password> + +Specify password + +=item B<--ssl> + +Specify SSL version (example : 'sslv3', 'tlsv1'...) + +=item B<--header> + +Set HTTP headers (Multiple option. Example: --header='Content-Type: xxxxx') + +=item B<--exlude> + +Exclude specific instance's state (comma seperated list) (Example: --exclude=Paused,Running,Off,Exited) + +=item B<--timeout> + +Threshold for HTTP timeout (Default: 3) + +=back + +OPENSTACK OPTIONS: + +=over 8 + +=item B<--tenant-id> + +Set Tenant's ID + +=back + +=cut diff --git a/cloud/openstack/restapi/mode/listnetworks.pm b/cloud/openstack/restapi/mode/listnetworks.pm new file mode 100644 index 000000000..162f0f6b1 --- /dev/null +++ b/cloud/openstack/restapi/mode/listnetworks.pm @@ -0,0 +1,273 @@ +# +# Copyright 2015 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 cloud::openstack::restapi::mode::listnetworks; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::http; +use JSON; + +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 => + { + "data:s" => { name => 'data' }, + "hostname:s" => { name => 'hostname' }, + "http-peer-addr:s" => { name => 'http_peer_addr' }, + "port:s" => { name => 'port', default => '5000' }, + "proto:s" => { name => 'proto' }, + "urlpath:s" => { name => 'url_path', default => '/v3/auth/tokens' }, + "proxyurl:s" => { name => 'proxyurl' }, + "proxypac:s" => { name => 'proxypac' }, + "credentials" => { name => 'credentials' }, + "username:s" => { name => 'username' }, + "password:s" => { name => 'password' }, + "ssl:s" => { name => 'ssl', }, + "header:s@" => { name => 'header' }, + "exclude:s" => { name => 'exclude' }, + "timeout:s" => { name => 'timeout' }, + }); + + $self->{http} = centreon::plugins::http->new(output => $self->{output}); + $self->{networks_infos} = (); + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + if (!defined($self->{option_results}->{header}) || $self->{option_results}->{header} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --header option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{data}) || $self->{option_results}->{data} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --data option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{hostname}) || $self->{option_results}->{hostname} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --hostname option."); + $self->{output}->option_exit(); + } + + $self->{http}->set_options(%{$self->{option_results}}) +} + +sub check_exclude { + my ($self, %options) = @_; + + if (defined($self->{option_results}->{exclude}) && $self->{option_results}->{exclude} =~ /(^|\s|,)${options{status}}(\s|,|$)/) { + $self->{output}->output_add(long_msg => sprintf("Skipping ${options{status}} instance.")); + return 1; + } + return 0; +} + +sub token_request { + my ($self, %options) = @_; + + $self->{method} = 'GET'; + if (defined($self->{option_results}->{data})) { + local $/ = undef; + if (!open(FILE, "<", $self->{option_results}->{data})) { + $self->{output}->output_add(severity => 'UNKNOWN', + short_msg => sprintf("Could not read file '%s': %s", $self->{option_results}->{data}, $!)); + $self->{output}->display(); + $self->{output}->exit(); + } + $self->{json_request} = ; + close FILE; + $self->{method} = 'POST'; + } + + my $response = $self->{http}->request(method => $self->{method}, query_form_post => $self->{json_request}); + my $headers = $self->{http}->get_header(); + eval { + $self->{header} = $headers->header('X-Subject-Token'); + }; + + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot retrieve API Token"); + $self->{output}->option_exit(); + } +} + +sub api_request { + my ($self, %options) = @_; + + $self->{method} = 'GET'; + $self->{option_results}->{url_path} = "/v2.0/networks"; + $self->{option_results}->{port} = '9696'; + @{$self->{option_results}->{header}} = ('X-Auth-Token:' . $self->{header}, 'Accept:application/json'); + $self->{http}->set_options(%{$self->{option_results}}); + + my $webcontent; + my $jsoncontent = $self->{http}->request(method => $self->{method}); + + my $json = JSON->new; + + eval { + $webcontent = $json->decode($jsoncontent); + }; + + foreach my $val (@{$webcontent->{networks}}) { + my $networkname = $val->{name}; + $self->{networks_infos}->{$networkname}->{id} = $val->{id}; + $self->{networks_infos}->{$networkname}->{tenant} = $val->{tenant_id}; + $self->{networks_infos}->{$networkname}->{state} = $val->{status}; + $self->{networks_infos}->{$networkname}->{admin_state} = $val->{admin_state_up}; + } +} + +sub disco_format { + my ($self, %options) = @_; + + my $names = ['name', 'id', 'tenant', 'state', 'admin_state']; + $self->{output}->add_disco_format(elements => $names); +} + +sub disco_show { + my ($self, %options) = @_; + + $self->token_request(); + $self->api_request(); + + foreach my $networkname (keys %{$self->{networks_infos}}) { + $self->{output}->add_disco_entry(name => $networkname, + id => $self->{networks_infos}->{$networkname}->{id}, + tenant => $self->{networks_infos}->{$networkname}->{tenant}, + state => $self->{networks_infos}->{$networkname}->{state}, + admin_state => $self->{networks_infos}->{$networkname}->{admin_state}, + ); + } +} + +sub run { + my ($self, %options) = @_; + + $self->token_request(); + $self->api_request(); + + foreach my $networkname (keys %{$self->{networks_infos}}) { + $self->{output}->output_add(long_msg => sprintf("%s [id = %s, tenant = %s, state = %s, admin_state = %s]", + $networkname, + $self->{networks_infos}->{$networkname}->{id}, + $self->{networks_infos}->{$networkname}->{tenant}, + $self->{networks_infos}->{$networkname}->{state}, + $self->{networks_infos}->{$networkname}->{admin_state})); + } + + $self->{output}->output_add(severity => 'OK', + short_msg => 'List networks:'); + + $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); + $self->{output}->exit(); + + exit 0; +} + +1; + +__END__ + +=head1 MODE + +List OpenStack networks through Networking API V2.0 + +JSON OPTIONS: + +=over 8 + +=item B<--data> + +Set file with JSON request + +=back + +HTTP OPTIONS: + +=over 8 + +=item B<--hostname> + +IP Addr/FQDN of OpenStack Compute's API + +=item B<--http-peer-addr> + +Set the address you want to connect (Useful if hostname is only a vhost. no ip resolve) + +=item B<--port> + +Port used by OpenStack Keystone's API (Default: '5000') + +=item B<--proto> + +Specify https if needed (Default: 'http') + +=item B<--urlpath> + +Set path to get API's Token (Default: '/v3/auth/tokens') + +=item B<--proxyurl> + +Proxy URL + +=item B<--proxypac> + +Proxy pac file (can be an url or local file) + +=item B<--credentials> + +Specify this option if you access webpage over basic authentification + +=item B<--username> + +Specify username + +=item B<--password> + +Specify password + +=item B<--ssl> + +Specify SSL version (example : 'sslv3', 'tlsv1'...) + +=item B<--header> + +Set HTTP headers (Multiple option. Example: --header='Content-Type: xxxxx') + +=item B<--exlude> + +Exclude specific instance's state (comma seperated list) (Example: --exclude=ACTIVE) + +=item B<--timeout> + +Threshold for HTTP timeout (Default: 3) + +=back + +=cut diff --git a/cloud/openstack/restapi/mode/listvolumes.pm b/cloud/openstack/restapi/mode/listvolumes.pm new file mode 100644 index 000000000..1ad82ef90 --- /dev/null +++ b/cloud/openstack/restapi/mode/listvolumes.pm @@ -0,0 +1,291 @@ +# +# Copyright 2015 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 cloud::openstack::restapi::mode::listvolumes; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::http; +use JSON; + +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 => + { + "data:s" => { name => 'data' }, + "hostname:s" => { name => 'hostname' }, + "http-peer-addr:s" => { name => 'http_peer_addr' }, + "port:s" => { name => 'port', default => '5000' }, + "proto:s" => { name => 'proto' }, + "urlpath:s" => { name => 'url_path', default => '/v3/auth/tokens' }, + "proxyurl:s" => { name => 'proxyurl' }, + "proxypac:s" => { name => 'proxypac' }, + "credentials" => { name => 'credentials' }, + "username:s" => { name => 'username' }, + "password:s" => { name => 'password' }, + "ssl:s" => { name => 'ssl', }, + "header:s@" => { name => 'header' }, + "exclude:s" => { name => 'exclude' }, + "timeout:s" => { name => 'timeout' }, + "tenant-id:s" => { name => 'tenant_id' }, + }); + + $self->{http} = centreon::plugins::http->new(output => $self->{output}); + $self->{volumes_infos} = (); + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + if (!defined($self->{option_results}->{header}) || $self->{option_results}->{header} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --header option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{data}) || $self->{option_results}->{data} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --data option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{hostname}) || $self->{option_results}->{hostname} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --hostname option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{tenant_id}) || $self->{option_results}->{tenant_id} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --tenant-id option."); + $self->{output}->option_exit(); + } + + $self->{http}->set_options(%{$self->{option_results}}) +} + +sub check_exclude { + my ($self, %options) = @_; + + if (defined($self->{option_results}->{exclude}) && $self->{option_results}->{exclude} =~ /(^|\s|,)${options{status}}(\s|,|$)/) { + $self->{output}->output_add(long_msg => sprintf("Skipping ${options{status}} instance.")); + return 1; + } + return 0; +} + +sub token_request { + my ($self, %options) = @_; + + $self->{method} = 'GET'; + if (defined($self->{option_results}->{data})) { + local $/ = undef; + if (!open(FILE, "<", $self->{option_results}->{data})) { + $self->{output}->output_add(severity => 'UNKNOWN', + short_msg => sprintf("Could not read file '%s': %s", $self->{option_results}->{data}, $!)); + $self->{output}->display(); + $self->{output}->exit(); + } + $self->{json_request} = ; + close FILE; + $self->{method} = 'POST'; + } + + my $response = $self->{http}->request(method => $self->{method}, query_form_post => $self->{json_request}); + my $headers = $self->{http}->get_header(); + eval { + $self->{header} = $headers->header('X-Subject-Token'); + }; + + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot retrieve API Token"); + $self->{output}->option_exit(); + } +} + +sub api_request { + my ($self, %options) = @_; + + $self->{method} = 'GET'; + $self->{option_results}->{url_path} = "/v2/".$self->{option_results}->{tenant_id}."/volumes/detail"; + $self->{option_results}->{port} = '8776'; + @{$self->{option_results}->{header}} = ('X-Auth-Token:' . $self->{header}, 'Accept:application/json'); + $self->{http}->set_options(%{$self->{option_results}}); + + my $webcontent; + my $jsoncontent = $self->{http}->request(method => $self->{method}); + + my $json = JSON->new; + + eval { + $webcontent = $json->decode($jsoncontent); + }; + + foreach my $val (@{$webcontent->{volumes}}) { + my $volumename = $val->{name}; + $self->{volumes_infos}->{$volumename}->{id} = $val->{id}; + $self->{volumes_infos}->{$volumename}->{zone} = $val->{availability_zone}; + $self->{volumes_infos}->{$volumename}->{size} = $val->{size}; + $self->{volumes_infos}->{$volumename}->{type} = $val->{volume_type}; + $self->{volumes_infos}->{$volumename}->{state} = $val->{status}; + } +} + +sub disco_format { + my ($self, %options) = @_; + + my $names = ['name', 'id', 'zone', 'type', 'size', 'state']; + $self->{output}->add_disco_format(elements => $names); +} + +sub disco_show { + my ($self, %options) = @_; + + $self->token_request(); + $self->api_request(); + + foreach my $volumename (keys %{$self->{volumes_infos}}) { + $self->{output}->add_disco_entry(name => $volumename, + id => $self->{volumes_infos}->{$volumename}->{id}, + zone => $self->{volumes_infos}->{$volumename}->{zone}, + size => $self->{volumes_infos}->{$volumename}->{size}."Gb", + type => $self->{volumes_infos}->{$volumename}->{type}, + state => $self->{volumes_infos}->{$volumename}->{state}, + ); + } +} + +sub run { + my ($self, %options) = @_; + + $self->token_request(); + $self->api_request(); + + foreach my $volumename (keys %{$self->{volumes_infos}}) { + $self->{output}->output_add(long_msg => sprintf("%s [id = %s, zone = %s, size = %sGb, type = %s, state = %s]", + $volumename, + $self->{volumes_infos}->{$volumename}->{id}, + $self->{volumes_infos}->{$volumename}->{zone}, + $self->{volumes_infos}->{$volumename}->{size}, + $self->{volumes_infos}->{$volumename}->{type}, + $self->{volumes_infos}->{$volumename}->{state})); + } + + $self->{output}->output_add(severity => 'OK', + short_msg => 'List volumes:'); + + $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); + $self->{output}->exit(); + + exit 0; +} + +1; + +__END__ + +=head1 MODE + +List OpenStack volumes through Block Storage API V2 + +JSON OPTIONS: + +=over 8 + +=item B<--data> + +Set file with JSON request + +=back + +HTTP OPTIONS: + +=over 8 + +=item B<--hostname> + +IP Addr/FQDN of OpenStack Compute's API + +=item B<--http-peer-addr> + +Set the address you want to connect (Useful if hostname is only a vhost. no ip resolve) + +=item B<--port> + +Port used by OpenStack Keystone's API (Default: '5000') + +=item B<--proto> + +Specify https if needed (Default: 'http') + +=item B<--urlpath> + +Set path to get API's Token (Default: '/v3/auth/tokens') + +=item B<--proxyurl> + +Proxy URL + +=item B<--proxypac> + +Proxy pac file (can be an url or local file) + +=item B<--credentials> + +Specify this option if you access webpage over basic authentification + +=item B<--username> + +Specify username + +=item B<--password> + +Specify password + +=item B<--ssl> + +Specify SSL version (example : 'sslv3', 'tlsv1'...) + +=item B<--header> + +Set HTTP headers (Multiple option. Example: --header='Content-Type: xxxxx') + +=item B<--exlude> + +Exclude specific instance's state (comma seperated list) (Example: --exclude=Paused,Running,Off,Exited) + +=item B<--timeout> + +Threshold for HTTP timeout (Default: 3) + +=back + +OPENSTACK OPTIONS: + +=over 8 + +=item B<--tenant-id> + +Set Tenant's ID + +=back + +=cut diff --git a/cloud/openstack/restapi/mode/network.pm b/cloud/openstack/restapi/mode/network.pm new file mode 100644 index 000000000..f1b4ba91c --- /dev/null +++ b/cloud/openstack/restapi/mode/network.pm @@ -0,0 +1,296 @@ +# +# Copyright 2015 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 cloud::openstack::restapi::mode::network; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::http; +use JSON; + +my $thresholds = { + status => [ + ['ACTIVE', 'OK'], + ['BUILD', 'OK'], + ['DOWN', 'CRITICAL'], + ['ERROR', 'CRITICAL'], + ], +}; + +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 => + { + "data:s" => { name => 'data' }, + "hostname:s" => { name => 'hostname' }, + "http-peer-addr:s" => { name => 'http_peer_addr' }, + "port:s" => { name => 'port', default => '5000' }, + "proto:s" => { name => 'proto' }, + "urlpath:s" => { name => 'url_path', default => '/v3/auth/tokens' }, + "proxyurl:s" => { name => 'proxyurl' }, + "proxypac:s" => { name => 'proxypac' }, + "credentials" => { name => 'credentials' }, + "username:s" => { name => 'username' }, + "password:s" => { name => 'password' }, + "ssl:s" => { name => 'ssl', }, + "header:s@" => { name => 'header' }, + "timeout:s" => { name => 'timeout' }, + "network-id:s" => { name => 'network_id' }, + "threshold-overload:s@" => { name => 'threshold_overload' }, + }); + + $self->{http} = centreon::plugins::http->new(output => $self->{output}); + $self->{network_infos} = (); + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + $self->{overload_th} = {}; + foreach my $val (@{$self->{option_results}->{threshold_overload}}) { + if ($val !~ /^(.*?),(.*?),(.*)$/) { + $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload option '" . $val . "'."); + $self->{output}->option_exit(); + } + my ($section, $status, $filter) = ($1, $2, $3); + if ($self->{output}->is_litteral_status(status => $status) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload status '" . $val . "'."); + $self->{output}->option_exit(); + } + $self->{overload_th}->{$section} = [] if (!defined($self->{overload_th}->{$section})); + push @{$self->{overload_th}->{$section}}, {filter => $filter, status => $status}; + } + + if (!defined($self->{option_results}->{header}) || $self->{option_results}->{header} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --header option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{data}) || $self->{option_results}->{data} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --data option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{hostname}) || $self->{option_results}->{hostname} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --hostname option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{network_id}) || $self->{option_results}->{network_id} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --network-id option."); + $self->{output}->option_exit(); + } + + $self->{http}->set_options(%{$self->{option_results}}) +} + +sub token_request { + my ($self, %options) = @_; + + $self->{method} = 'GET'; + if (defined($self->{option_results}->{data})) { + local $/ = undef; + if (!open(FILE, "<", $self->{option_results}->{data})) { + $self->{output}->output_add(severity => 'UNKNOWN', + short_msg => sprintf("Could not read file '%s': %s", $self->{option_results}->{data}, $!)); + $self->{output}->display(); + $self->{output}->exit(); + } + $self->{json_request} = ; + close FILE; + $self->{method} = 'POST'; + } + + my $response = $self->{http}->request(method => $self->{method}, query_form_post => $self->{json_request}); + my $headers = $self->{http}->get_header(); + + eval { + $self->{header} = $headers->header('X-Subject-Token'); + }; + + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot retrieve API Token"); + $self->{output}->option_exit(); + } +} + +sub api_request { + my ($self, %options) = @_; + + $self->{method} = 'GET'; + $self->{option_results}->{url_path} = "/v2.0/networks/".$self->{option_results}->{network_id}; + $self->{option_results}->{port} = '9696'; + @{$self->{option_results}->{header}} = ('X-Auth-Token:' . $self->{header}, 'Accept:application/json'); + $self->{http}->set_options(%{$self->{option_results}}); + + my $webcontent; + my $jsoncontent = $self->{http}->request(method => $self->{method}); + + my $json = JSON->new; + + eval { + $webcontent = $json->decode($jsoncontent); + }; + + $self->{network_infos}->{name} = $webcontent->{network}->{name}; + $self->{network_infos}->{admin_state} = $webcontent->{network}->{admin_state_up}; + $self->{network_infos}->{status} = $webcontent->{network}->{status}; +} + + +sub get_severity { + my ($self, %options) = @_; + my $status = 'UNKNOWN'; # default + + if (defined($self->{overload_th}->{$options{section}})) { + foreach (@{$self->{overload_th}->{$options{section}}}) { + if ($options{value} =~ /$_->{filter}/i) { + $status = $_->{status}; + return $status; + } + } + } + foreach (@{$thresholds->{$options{section}}}) { + if ($options{value} =~ /$$_[0]/i) { + $status = $$_[1]; + return $status; + } + } + + return $status; +} + +sub run { + my ($self, %options) = @_; + + $self->token_request(); + $self->api_request(); + + my $exit = $self->get_severity(section => 'status', value => $self->{network_infos}->{status}); + $self->{output}->output_add(severity => $exit, + short_msg => sprintf("Network %s is in %s state (admin_state: %s)", + $self->{network_infos}->{name}, + $self->{network_infos}->{status}, + $self->{network_infos}->{admin_state})); + + $self->{output}->display(); + $self->{output}->exit(); + + exit 0; +} + +1; + +__END__ + +=head1 MODE + +List OpenStack instances through Compute API V2 + +JSON OPTIONS: + +=over 8 + +=item B<--data> + +Set file with JSON request + +=back + +HTTP OPTIONS: + +=over 8 + +=item B<--hostname> + +IP Addr/FQDN of OpenStack Compute's API + +=item B<--http-peer-addr> + +Set the address you want to connect (Useful if hostname is only a vhost. no ip resolve) + +=item B<--port> + +Port used by OpenStack Keystone's API (Default: '5000') + +=item B<--proto> + +Specify https if needed (Default: 'http') + +=item B<--urlpath> + +Set path to get API's Token (Default: '/v3/auth/tokens') + +=item B<--proxyurl> + +Proxy URL + +=item B<--proxypac> + +Proxy pac file (can be an url or local file) + +=item B<--credentials> + +Specify this option if you access webpage over basic authentification + +=item B<--username> + +Specify username + +=item B<--password> + +Specify password + +=item B<--ssl> + +Specify SSL version (example : 'sslv3', 'tlsv1'...) + +=item B<--header> + +Set HTTP headers (Multiple option. Example: --header='Content-Type: xxxxx') + +=item B<--timeout> + +Threshold for HTTP timeout (Default: 3) + +=item B<--threshold-overload> + +Set to overload default threshold values (syntax: section,status,regexp) +It used before default thresholds (order stays). +Example: --threshold-overload='status,CRITICAL,^BUILD$)' + +=back + +OPENSTACK OPTIONS: + +=over 8 + +=item B<--network-id> + +Set Network's ID + +=back + +=cut diff --git a/cloud/openstack/restapi/mode/port.pm b/cloud/openstack/restapi/mode/port.pm new file mode 100644 index 000000000..51c51e703 --- /dev/null +++ b/cloud/openstack/restapi/mode/port.pm @@ -0,0 +1,300 @@ +# +# Copyright 2015 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 cloud::openstack::restapi::mode::port; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::http; +use JSON; + +my $thresholds = { + status => [ + ['ACTIVE', 'OK'], + ['DOWN', 'CRITICAL'], + ['N/A', 'UNKNOWN'], + ], +}; + +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 => + { + "data:s" => { name => 'data' }, + "hostname:s" => { name => 'hostname' }, + "http-peer-addr:s" => { name => 'http_peer_addr' }, + "port:s" => { name => 'port', default => '5000' }, + "proto:s" => { name => 'proto' }, + "urlpath:s" => { name => 'url_path', default => '/v3/auth/tokens' }, + "proxyurl:s" => { name => 'proxyurl' }, + "proxypac:s" => { name => 'proxypac' }, + "credentials" => { name => 'credentials' }, + "username:s" => { name => 'username' }, + "password:s" => { name => 'password' }, + "ssl:s" => { name => 'ssl', }, + "header:s@" => { name => 'header' }, + "exclude:s" => { name => 'exclude' }, + "timeout:s" => { name => 'timeout' }, + "port-id:s" => { name => 'port_id' }, + "threshold-overload:s@" => { name => 'threshold_overload' }, + }); + + $self->{http} = centreon::plugins::http->new(output => $self->{output}); + $self->{port_infos} = (); + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + $self->{overload_th} = {}; + foreach my $val (@{$self->{option_results}->{threshold_overload}}) { + if ($val !~ /^(.*?),(.*?),(.*)$/) { + $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload option '" . $val . "'."); + $self->{output}->option_exit(); + } + my ($section, $status, $filter) = ($1, $2, $3); + if ($self->{output}->is_litteral_status(status => $status) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload status '" . $val . "'."); + $self->{output}->option_exit(); + } + $self->{overload_th}->{$section} = [] if (!defined($self->{overload_th}->{$section})); + push @{$self->{overload_th}->{$section}}, {filter => $filter, status => $status}; + } + + if (!defined($self->{option_results}->{header}) || $self->{option_results}->{header} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --header option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{data}) || $self->{option_results}->{data} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --data option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{hostname}) || $self->{option_results}->{hostname} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --hostname option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{port_id}) || $self->{option_results}->{port_id} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --port-id option."); + $self->{output}->option_exit(); + } + + $self->{http}->set_options(%{$self->{option_results}}) +} + +sub token_request { + my ($self, %options) = @_; + + $self->{method} = 'GET'; + if (defined($self->{option_results}->{data})) { + local $/ = undef; + if (!open(FILE, "<", $self->{option_results}->{data})) { + $self->{output}->output_add(severity => 'UNKNOWN', + short_msg => sprintf("Could not read file '%s': %s", $self->{option_results}->{data}, $!)); + $self->{output}->display(); + $self->{output}->exit(); + } + $self->{json_request} = ; + close FILE; + $self->{method} = 'POST'; + } + + my $response = $self->{http}->request(method => $self->{method}, query_form_post => $self->{json_request}); + my $headers = $self->{http}->get_header(); + + eval { + $self->{header} = $headers->header('X-Subject-Token'); + }; + + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot retrieve API Token"); + $self->{output}->option_exit(); + } +} + +sub api_request { + my ($self, %options) = @_; + + $self->{method} = 'GET'; + $self->{option_results}->{url_path} = "/v2.0/ports/".$self->{option_results}->{port_id}; + $self->{option_results}->{port} = '9696'; + @{$self->{option_results}->{header}} = ('X-Auth-Token:' . $self->{header}, 'Accept:application/json'); + $self->{http}->set_options(%{$self->{option_results}}); + + my $webcontent; + my $jsoncontent = $self->{http}->request(method => $self->{method}); + + my $json = JSON->new; + + eval { + $webcontent = $json->decode($jsoncontent); + }; + if ($webcontent->{port}->{name} eq '') { + $self->{port_infos}->{name} = $webcontent->{port}->{id}; + } else { + $self->{port_infos}->{name} = $webcontent->{port}->{name}; + } + $self->{port_infos}->{admin_state} = $webcontent->{port}->{admin_state_up}; + $self->{port_infos}->{status} = $webcontent->{port}->{status}; +} + + +sub get_severity { + my ($self, %options) = @_; + my $status = 'UNKNOWN'; # default + + if (defined($self->{overload_th}->{$options{section}})) { + foreach (@{$self->{overload_th}->{$options{section}}}) { + if ($options{value} =~ /$_->{filter}/i) { + $status = $_->{status}; + return $status; + } + } + } + foreach (@{$thresholds->{$options{section}}}) { + if ($options{value} =~ /$$_[0]/i) { + $status = $$_[1]; + return $status; + } + } + + return $status; +} + +sub run { + my ($self, %options) = @_; + + $self->token_request(); + $self->api_request(); + + my $exit = $self->get_severity(section => 'status', value => $self->{port_infos}->{status}); + $self->{output}->output_add(severity => $exit, + short_msg => sprintf("Port %s is in %s state (admin_state: %s)", + $self->{port_infos}->{name}, + $self->{port_infos}->{status}, + $self->{port_infos}->{admin_state})); + + $self->{output}->display(); + $self->{output}->exit(); + + exit 0; +} + +1; + +__END__ + +=head1 MODE + +List OpenStack instances through Compute API V2 + +JSON OPTIONS: + +=over 8 + +=item B<--data> + +Set file with JSON request + +=back + +HTTP OPTIONS: + +=over 8 + +=item B<--hostname> + +IP Addr/FQDN of OpenStack Compute's API + +=item B<--http-peer-addr> + +Set the address you want to connect (Useful if hostname is only a vhost. no ip resolve) + +=item B<--port> + +Port used by OpenStack Keystone's API (Default: '5000') + +=item B<--proto> + +Specify https if needed (Default: 'http') + +=item B<--urlpath> + +Set path to get API's Token (Default: '/v3/auth/tokens') + +=item B<--proxyurl> + +Proxy URL + +=item B<--proxypac> + +Proxy pac file (can be an url or local file) + +=item B<--credentials> + +Specify this option if you access webpage over basic authentification + +=item B<--username> + +Specify username + +=item B<--password> + +Specify password + +=item B<--ssl> + +Specify SSL version (example : 'sslv3', 'tlsv1'...) + +=item B<--header> + +Set HTTP headers (Multiple option. Example: --header='Content-Type: xxxxx') + +=item B<--timeout> + +Threshold for HTTP timeout (Default: 3) + +=back + +=item B<--threshold-overload> + +Set to overload default threshold values (syntax: section,status,regexp) +It used before default thresholds (order stays). +Example: --threshold-overload='status,CRITICAL,^N\/A$)' + +=back + +OPENSTACK OPTIONS: + +=over 8 + +=item B<--port-id> + +Set Port's ID + +=back + diff --git a/cloud/openstack/restapi/mode/volume.pm b/cloud/openstack/restapi/mode/volume.pm new file mode 100644 index 000000000..3ca349c5d --- /dev/null +++ b/cloud/openstack/restapi/mode/volume.pm @@ -0,0 +1,313 @@ +# +# Copyright 2015 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 cloud::openstack::restapi::mode::volume; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::http; +use JSON; + +my $thresholds = { + status => [ + ['creating', 'OK'], + ['available', 'OK'], + ['attaching', 'OK'], + ['in-use', 'OK'], + ['deleting', 'OK'], + ['backing-up', 'WARNING'], + ['restoring-backup', 'WARNING'], + ['error', 'CRITICAL'], + ['error_deleting', 'CRITICAL'], + ['error_restoring', 'CRITICAL'], + ['error_extending', 'CRITICAL'], + ], +}; + +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 => + { + "data:s" => { name => 'data' }, + "hostname:s" => { name => 'hostname' }, + "http-peer-addr:s" => { name => 'http_peer_addr' }, + "port:s" => { name => 'port', default => '5000' }, + "proto:s" => { name => 'proto' }, + "urlpath:s" => { name => 'url_path', default => '/v3/auth/tokens' }, + "proxyurl:s" => { name => 'proxyurl' }, + "proxypac:s" => { name => 'proxypac' }, + "credentials" => { name => 'credentials' }, + "username:s" => { name => 'username' }, + "password:s" => { name => 'password' }, + "ssl:s" => { name => 'ssl', }, + "header:s@" => { name => 'header' }, + "timeout:s" => { name => 'timeout' }, + "tenant-id:s" => { name => 'tenant_id' }, + "volume-id:s" => { name => 'volume_id' }, + "threshold-overload:s@" => { name => 'threshold_overload' }, + }); + + $self->{http} = centreon::plugins::http->new(output => $self->{output}); + $self->{volume_infos} = (); + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + $self->{overload_th} = {}; + foreach my $val (@{$self->{option_results}->{threshold_overload}}) { + if ($val !~ /^(.*?),(.*?),(.*)$/) { + $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload option '" . $val . "'."); + $self->{output}->option_exit(); + } + my ($section, $status, $filter) = ($1, $2, $3); + if ($self->{output}->is_litteral_status(status => $status) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload status '" . $val . "'."); + $self->{output}->option_exit(); + } + $self->{overload_th}->{$section} = [] if (!defined($self->{overload_th}->{$section})); + push @{$self->{overload_th}->{$section}}, {filter => $filter, status => $status}; + } + + if (!defined($self->{option_results}->{header}) || $self->{option_results}->{header} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --header option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{data}) || $self->{option_results}->{data} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --data option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{hostname}) || $self->{option_results}->{hostname} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --hostname option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{tenant_id}) || $self->{option_results}->{tenant_id} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --tenant-id option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{volume_id}) || $self->{option_results}->{volume_id} eq '') { + $self->{output}->add_option_msg(short_msg => "You need to specify --volume-id option."); + $self->{output}->option_exit(); + } + + $self->{http}->set_options(%{$self->{option_results}}) +} + +sub token_request { + my ($self, %options) = @_; + + $self->{method} = 'GET'; + if (defined($self->{option_results}->{data})) { + local $/ = undef; + if (!open(FILE, "<", $self->{option_results}->{data})) { + $self->{output}->output_add(severity => 'UNKNOWN', + short_msg => sprintf("Could not read file '%s': %s", $self->{option_results}->{data}, $!)); + $self->{output}->display(); + $self->{output}->exit(); + } + $self->{json_request} = ; + close FILE; + $self->{method} = 'POST'; + } + + my $response = $self->{http}->request(method => $self->{method}, query_form_post => $self->{json_request}); + my $headers = $self->{http}->get_header(); + + eval { + $self->{header} = $headers->header('X-Subject-Token'); + }; + + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot retrieve API Token"); + $self->{output}->option_exit(); + } +} + +sub api_request { + my ($self, %options) = @_; + + $self->{method} = 'GET'; + $self->{option_results}->{url_path} = "/v2/".$self->{option_results}->{tenant_id}."/volumes/".$self->{option_results}->{volume_id}; + $self->{option_results}->{port} = '8776'; + @{$self->{option_results}->{header}} = ('X-Auth-Token:' . $self->{header}, 'Accept:application/json'); + $self->{http}->set_options(%{$self->{option_results}}); + + my $webcontent; + my $jsoncontent = $self->{http}->request(method => $self->{method}); + + my $json = JSON->new; + + eval { + $webcontent = $json->decode($jsoncontent); + }; + + $self->{volume_infos}->{name} = $webcontent->{volume}->{name}; + $self->{volume_infos}->{status} = $webcontent->{volume}->{status}; +} + + +sub get_severity { + my ($self, %options) = @_; + my $status = 'UNKNOWN'; # default + + if (defined($self->{overload_th}->{$options{section}})) { + foreach (@{$self->{overload_th}->{$options{section}}}) { + if ($options{value} =~ /$_->{filter}/i) { + $status = $_->{status}; + return $status; + } + } + } + foreach (@{$thresholds->{$options{section}}}) { + if ($options{value} =~ /$$_[0]/i) { + $status = $$_[1]; + return $status; + } + } + + return $status; +} + +sub run { + my ($self, %options) = @_; + + $self->token_request(); + $self->api_request(); + + my $exit = $self->get_severity(section => 'status', value => $self->{volume_infos}->{status}); + $self->{output}->output_add(severity => $exit, + short_msg => sprintf("Volume %s is in %s state", + $self->{volume_infos}->{name}, + $self->{volume_infos}->{status})); + + $self->{output}->display(); + $self->{output}->exit(); + + exit 0; +} + +1; + +__END__ + +=head1 MODE + +List OpenStack instances through Compute API V2 + +JSON OPTIONS: + +=over 8 + +=item B<--data> + +Set file with JSON request + +=back + +HTTP OPTIONS: + +=over 8 + +=item B<--hostname> + +IP Addr/FQDN of OpenStack Compute's API + +=item B<--http-peer-addr> + +Set the address you want to connect (Useful if hostname is only a vhost. no ip resolve) + +=item B<--port> + +Port used by OpenStack Keystone's API (Default: '5000') + +=item B<--proto> + +Specify https if needed (Default: 'http') + +=item B<--urlpath> + +Set path to get API's Token (Default: '/v3/auth/tokens') + +=item B<--proxyurl> + +Proxy URL + +=item B<--proxypac> + +Proxy pac file (can be an url or local file) + +=item B<--credentials> + +Specify this option if you access webpage over basic authentification + +=item B<--username> + +Specify username + +=item B<--password> + +Specify password + +=item B<--ssl> + +Specify SSL version (example : 'sslv3', 'tlsv1'...) + +=item B<--header> + +Set HTTP headers (Multiple option. Example: --header='Content-Type: xxxxx') + +=item B<--timeout> + +Threshold for HTTP timeout (Default: 3) + +=back + +=item B<--threshold-overload> + +Set to overload default threshold values (syntax: section,status,regexp) +It used before default thresholds (order stays). +Example: --threshold-overload='status,CRITICAL,^deleting$)' + +=back + +OPENSTACK OPTIONS: + +=over 8 + +=item B<--tenant-id> + +Set Tenant's ID + +=item B<--volume-id> + +Set Volume's ID + +=back + +=cut + diff --git a/cloud/openstack/restapi/plugin.pm b/cloud/openstack/restapi/plugin.pm new file mode 100644 index 000000000..ed9951b1c --- /dev/null +++ b/cloud/openstack/restapi/plugin.pm @@ -0,0 +1,56 @@ +# +# Copyright 2015 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 cloud::openstack::restapi::plugin; + +use strict; +use warnings; +use base qw(centreon::plugins::script_simple); + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + # $options->{options} = options object + + $self->{version} = '0.1'; + %{$self->{modes}} = ( + 'hypervisor' => 'cloud::openstack::restapi::mode::hypervisor', + 'instance' => 'cloud::openstack::restapi::mode::instance', + 'list-hypervisors' => 'cloud::openstack::restapi::mode::listhypervisors', + 'list-instances' => 'cloud::openstack::restapi::mode::listinstances', + 'list-networks' => 'cloud::openstack::restapi::mode::listnetworks', + 'list-volumes' => 'cloud::openstack::restapi::mode::listvolumes', + 'network' => 'cloud::openstack::restapi::mode::network', + 'port' => 'cloud::openstack::restapi::mode::port', + 'volume' => 'cloud::openstack::restapi::mode::volume', + ); + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check Openstack Components with their API. + +=cut