From fb0b50554f2b91b60222e46c4e67ab2b5c2802df Mon Sep 17 00:00:00 2001 From: Shini31 Date: Mon, 6 Jun 2016 14:44:45 +0200 Subject: [PATCH 01/17] Api token with get_header method --- cloud/openstack/restapi/mode/floatingips.pm | 244 +++++++++++++++ cloud/openstack/restapi/mode/infoinstance.pm | 242 +++++++++++++++ .../openstack/restapi/mode/listhypervisors.pm | 272 +++++++++++++++++ cloud/openstack/restapi/mode/listinstances.pm | 285 ++++++++++++++++++ cloud/openstack/restapi/mode/volumes.pm | 244 +++++++++++++++ cloud/openstack/restapi/plugin.pm | 52 ++++ 6 files changed, 1339 insertions(+) create mode 100644 cloud/openstack/restapi/mode/floatingips.pm create mode 100644 cloud/openstack/restapi/mode/infoinstance.pm create mode 100644 cloud/openstack/restapi/mode/listhypervisors.pm create mode 100644 cloud/openstack/restapi/mode/listinstances.pm create mode 100644 cloud/openstack/restapi/mode/volumes.pm create mode 100644 cloud/openstack/restapi/plugin.pm diff --git a/cloud/openstack/restapi/mode/floatingips.pm b/cloud/openstack/restapi/mode/floatingips.pm new file mode 100644 index 000000000..940f08f82 --- /dev/null +++ b/cloud/openstack/restapi/mode/floatingips.pm @@ -0,0 +1,244 @@ +# +# 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::floatingips; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::http; +use JSON; +use Data::Dumper; + +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); + + $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-floating-ips"; + $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); + }; + + print Dumper($webcontent); + exit 1; +} + +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 , compute = %s, osname = %s, state = %s]", + $instancename, + $self->{instance_infos}->{$instancename}->{id}, + $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/infoinstance.pm b/cloud/openstack/restapi/mode/infoinstance.pm new file mode 100644 index 000000000..58b05eab0 --- /dev/null +++ b/cloud/openstack/restapi/mode/infoinstance.pm @@ -0,0 +1,242 @@ +# +# 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::infoinstance; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::http; +use JSON; +use Data::Dumper; + +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' }, + "server-response:s" => { name => 'server_response', default => 'full' }, + "tenant-id:s" => { name => 'tenant_id' }, + "server-id:s" => { name => 'server_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); + + $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}); + + eval { + $self->{header} = $response->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}->{server_id}; + $self->{option_results}->{port} = '8774'; + @{$self->{option_results}->{header}} = ('X-Auth-Token:' . $self->{header}, 'Accept:application/json'); + $self->{option_results}->{server_response} = 'content'; + $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); + }; + + print Dumper($webcontent); + + #foreach my $val (@{$webcontent->{servers}}) { + # $self->{instance_infos}->{compute} = $val->{'OS-EXT-SRV-ATTR:host'}; + # $self->{instance_infos}->{osname} = $val->{'OS-EXT-SRV-ATTR:instance_name'}; + # $self->{instance_infos}->{state} = $val->{status}; + #} +} + +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 , compute = %s, osname = %s, state = %s]", + # $instancename, + # $self->{instance_infos}->{$instancename}->{id}, + # $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/listhypervisors.pm b/cloud/openstack/restapi/mode/listhypervisors.pm new file mode 100644 index 000000000..5165c872e --- /dev/null +++ b/cloud/openstack/restapi/mode/listhypervisors.pm @@ -0,0 +1,272 @@ +# +# 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); + + $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}->{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, + ip => $self->{hypervisor_infos}->{$hypervisorname}->{ipaddress}, + type => $self->{hypervisor_infos}->{$hypervisorname}->{type}, + status => $self->{hypervisor_infos}->{$hypervisorname}->{statuse}, + 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 [ip = %s, type = %s, status = %s, state = %s]", + $hypervisorname, + $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=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/listinstances.pm b/cloud/openstack/restapi/mode/listinstances.pm new file mode 100644 index 000000000..25decf343 --- /dev/null +++ b/cloud/openstack/restapi/mode/listinstances.pm @@ -0,0 +1,285 @@ +# +# 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); + + $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}->{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', '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}, + 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 , compute = %s, osname = %s, state = %s]", + $instancename, + $self->{instance_infos}->{$instancename}->{id}, + $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/volumes.pm b/cloud/openstack/restapi/mode/volumes.pm new file mode 100644 index 000000000..7ba580f91 --- /dev/null +++ b/cloud/openstack/restapi/mode/volumes.pm @@ -0,0 +1,244 @@ +# +# 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::volumes; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::http; +use JSON; +use Data::Dumper; + +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); + + $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} = '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); + }; + + print Dumper($webcontent); + exit 1; +} + +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 , compute = %s, osname = %s, state = %s]", + $instancename, + $self->{instance_infos}->{$instancename}->{id}, + $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/plugin.pm b/cloud/openstack/restapi/plugin.pm new file mode 100644 index 000000000..0b0edb844 --- /dev/null +++ b/cloud/openstack/restapi/plugin.pm @@ -0,0 +1,52 @@ +# +# 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}} = ( + 'info-instance' => 'cloud::openstack::restapi::mode::infoinstance', + 'list-instances' => 'cloud::openstack::restapi::mode::listinstances', + 'list-hypervisors' => 'cloud::openstack::restapi::mode::listhypervisors', + 'floatingips' => 'cloud::openstack::restapi::mode::floatingips', + 'volumes' => 'cloud::openstack::restapi::mode::volumes', + ); + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check Openstack Components with their API. + +=cut From 8ecd9f795e648e198a193abff5e651e9c077efc9 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Tue, 7 Jun 2016 16:05:17 +0200 Subject: [PATCH 02/17] add monitoring for hypervisor/instance/network/port --- .../mode/{floatingips.pm => hypervisor.pm} | 121 +++++--- cloud/openstack/restapi/mode/instance.pm | 285 ++++++++++++++++++ .../openstack/restapi/mode/listhypervisors.pm | 7 +- cloud/openstack/restapi/mode/network.pm | 278 +++++++++++++++++ .../restapi/mode/{infoinstance.pm => port.pm} | 121 +++++--- .../restapi/mode/{volumes.pm => volume.pm} | 128 +++++--- cloud/openstack/restapi/plugin.pm | 8 +- 7 files changed, 812 insertions(+), 136 deletions(-) rename cloud/openstack/restapi/mode/{floatingips.pm => hypervisor.pm} (55%) create mode 100644 cloud/openstack/restapi/mode/instance.pm create mode 100644 cloud/openstack/restapi/mode/network.pm rename cloud/openstack/restapi/mode/{infoinstance.pm => port.pm} (53%) rename cloud/openstack/restapi/mode/{volumes.pm => volume.pm} (54%) diff --git a/cloud/openstack/restapi/mode/floatingips.pm b/cloud/openstack/restapi/mode/hypervisor.pm similarity index 55% rename from cloud/openstack/restapi/mode/floatingips.pm rename to cloud/openstack/restapi/mode/hypervisor.pm index 940f08f82..0e0235556 100644 --- a/cloud/openstack/restapi/mode/floatingips.pm +++ b/cloud/openstack/restapi/mode/hypervisor.pm @@ -18,7 +18,7 @@ # limitations under the License. # -package cloud::openstack::restapi::mode::floatingips; +package cloud::openstack::restapi::mode::hypervisor; use base qw(centreon::plugins::mode); @@ -26,7 +26,13 @@ use strict; use warnings; use centreon::plugins::http; use JSON; -use Data::Dumper; + +my $thresholds = { + status => [ + ['up', 'OK'], + ['down', 'CRITICAL'], + ], +}; sub new { my ($class, %options) = @_; @@ -36,26 +42,28 @@ sub new { $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' }, + "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' }, + "hypervisor-id:s" => { name => 'hypervisor_id' }, + "threshold-overload:s@" => { name => 'threshold_overload' }, }); $self->{http} = centreon::plugins::http->new(output => $self->{output}); - $self->{instance_infos} = (); + $self->{hypervisor_infos} = (); return $self; } @@ -63,17 +71,22 @@ sub check_options { my ($self, %options) = @_; $self->SUPER::init(%options); - $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; + $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}; } - return 0; + + $self->{http}->set_options(%{$self->{option_results}}) } sub token_request { @@ -95,6 +108,7 @@ sub token_request { 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'); }; @@ -109,7 +123,7 @@ sub api_request { my ($self, %options) = @_; $self->{method} = 'GET'; - $self->{option_results}->{url_path} = "/v2/".$self->{option_results}->{tenant_id}."/os-floating-ips"; + $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}}); @@ -123,8 +137,32 @@ sub api_request { $webcontent = $json->decode($jsoncontent); }; - print Dumper($webcontent); - exit 1; + $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 { @@ -133,19 +171,14 @@ sub run { $self->token_request(); $self->api_request(); - foreach my $instancename (keys %{$self->{instance_infos}}) { - $self->{output}->output_add(long_msg => sprintf("%s [id = %s , compute = %s, osname = %s, state = %s]", - $instancename, - $self->{instance_infos}->{$instancename}->{id}, - $self->{instance_infos}->{$instancename}->{compute}, - $self->{instance_infos}->{$instancename}->{osname}, - $self->{instance_infos}->{$instancename}->{state})); - } + 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}->output_add(severity => 'OK', - short_msg => 'List instances:'); - - $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); + $self->{output}->display(); $self->{output}->exit(); exit 0; diff --git a/cloud/openstack/restapi/mode/instance.pm b/cloud/openstack/restapi/mode/instance.pm new file mode 100644 index 000000000..c2d04433c --- /dev/null +++ b/cloud/openstack/restapi/mode/instance.pm @@ -0,0 +1,285 @@ +# +# 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' }, + "exclude:s" => { name => 'exclude' }, + "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}; + } + + $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 + +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/listhypervisors.pm b/cloud/openstack/restapi/mode/listhypervisors.pm index 5165c872e..5e0e250a9 100644 --- a/cloud/openstack/restapi/mode/listhypervisors.pm +++ b/cloud/openstack/restapi/mode/listhypervisors.pm @@ -125,6 +125,7 @@ sub api_request { 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}; @@ -147,9 +148,10 @@ sub disco_show { 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}->{statuse}, + status => $self->{hypervisor_infos}->{$hypervisorname}->{status}, state => $self->{hypervisor_infos}->{$hypervisorname}->{state}, ); } @@ -162,8 +164,9 @@ sub run { $self->api_request(); foreach my $hypervisorname (keys %{$self->{hypervisor_infos}}) { - $self->{output}->output_add(long_msg => sprintf("%s [ip = %s, type = %s, status = %s, state = %s]", + $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}, diff --git a/cloud/openstack/restapi/mode/network.pm b/cloud/openstack/restapi/mode/network.pm new file mode 100644 index 000000000..71dde5f42 --- /dev/null +++ b/cloud/openstack/restapi/mode/network.pm @@ -0,0 +1,278 @@ +# +# 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' }, + "exclude:s" => { name => 'exclude' }, + "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}; + } + + $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<--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/infoinstance.pm b/cloud/openstack/restapi/mode/port.pm similarity index 53% rename from cloud/openstack/restapi/mode/infoinstance.pm rename to cloud/openstack/restapi/mode/port.pm index 58b05eab0..fe5265924 100644 --- a/cloud/openstack/restapi/mode/infoinstance.pm +++ b/cloud/openstack/restapi/mode/port.pm @@ -18,7 +18,7 @@ # limitations under the License. # -package cloud::openstack::restapi::mode::infoinstance; +package cloud::openstack::restapi::mode::port; use base qw(centreon::plugins::mode); @@ -26,7 +26,14 @@ use strict; use warnings; use centreon::plugins::http; use JSON; -use Data::Dumper; + +my $thresholds = { + status => [ + ['ACTIVE', 'OK'], + ['DOWN', 'CRITICAL'], + ['N/A', 'UNKNOWN'], + ], +}; sub new { my ($class, %options) = @_; @@ -36,28 +43,27 @@ sub new { $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' }, - "server-response:s" => { name => 'server_response', default => 'full' }, - "tenant-id:s" => { name => 'tenant_id' }, - "server-id:s" => { name => 'server_id' }, + "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->{instance_infos} = (); + $self->{port_infos} = (); return $self; } @@ -65,6 +71,21 @@ 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}; + } + $self->{http}->set_options(%{$self->{option_results}}) } @@ -86,9 +107,10 @@ sub token_request { } my $response = $self->{http}->request(method => $self->{method}, query_form_post => $self->{json_request}); + my $headers = $self->{http}->get_header(); eval { - $self->{header} = $response->header('X-Subject-Token'); + $self->{header} = $headers->header('X-Subject-Token'); }; if ($@) { @@ -101,10 +123,9 @@ sub api_request { my ($self, %options) = @_; $self->{method} = 'GET'; - $self->{option_results}->{url_path} = "/v2/".$self->{option_results}->{tenant_id}."/servers/".$self->{option_results}->{server_id}; - $self->{option_results}->{port} = '8774'; + $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->{option_results}->{server_response} = 'content'; $self->{http}->set_options(%{$self->{option_results}}); my $webcontent; @@ -116,13 +137,32 @@ sub api_request { $webcontent = $json->decode($jsoncontent); }; - print Dumper($webcontent); + $self->{port_infos}->{name} = $webcontent->{port}->{name}; + $self->{port_infos}->{admin_state} = $webcontent->{port}->{admin_state_up}; + $self->{port_infos}->{status} = $webcontent->{port}->{status}; +} - #foreach my $val (@{$webcontent->{servers}}) { - # $self->{instance_infos}->{compute} = $val->{'OS-EXT-SRV-ATTR:host'}; - # $self->{instance_infos}->{osname} = $val->{'OS-EXT-SRV-ATTR:instance_name'}; - # $self->{instance_infos}->{state} = $val->{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 { @@ -131,19 +171,14 @@ sub run { $self->token_request(); $self->api_request(); - #foreach my $instancename (keys %{$self->{instance_infos}}) { - # $self->{output}->output_add(long_msg => sprintf("%s [id = %s , compute = %s, osname = %s, state = %s]", - # $instancename, - # $self->{instance_infos}->{$instancename}->{id}, - # $self->{instance_infos}->{$instancename}->{compute}, - # $self->{instance_infos}->{$instancename}->{osname}, - # $self->{instance_infos}->{$instancename}->{state})); - #} + 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}->output_add(severity => 'OK', - short_msg => 'List instances:'); - - $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); + $self->{output}->display(); $self->{output}->exit(); exit 0; diff --git a/cloud/openstack/restapi/mode/volumes.pm b/cloud/openstack/restapi/mode/volume.pm similarity index 54% rename from cloud/openstack/restapi/mode/volumes.pm rename to cloud/openstack/restapi/mode/volume.pm index 7ba580f91..102c3315b 100644 --- a/cloud/openstack/restapi/mode/volumes.pm +++ b/cloud/openstack/restapi/mode/volume.pm @@ -18,7 +18,7 @@ # limitations under the License. # -package cloud::openstack::restapi::mode::volumes; +package cloud::openstack::restapi::mode::volume; use base qw(centreon::plugins::mode); @@ -26,7 +26,22 @@ use strict; use warnings; use centreon::plugins::http; use JSON; -use Data::Dumper; + +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) = @_; @@ -36,26 +51,28 @@ sub new { $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' }, + "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' }, + "volume-id:s" => { name => 'volume_id' }, + "threshold-overload:s@" => { name => 'threshold_overload' }, }); $self->{http} = centreon::plugins::http->new(output => $self->{output}); - $self->{instance_infos} = (); + $self->{volume_infos} = (); return $self; } @@ -63,17 +80,22 @@ sub check_options { my ($self, %options) = @_; $self->SUPER::init(%options); - $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; + $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}; } - return 0; + + $self->{http}->set_options(%{$self->{option_results}}) } sub token_request { @@ -95,6 +117,7 @@ sub token_request { 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'); }; @@ -109,7 +132,7 @@ 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}->{url_path} = "/v2/".$self->{option_results}->{tenant_id}."/volume/".$self->{option_results}->{volume_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}}); @@ -123,8 +146,31 @@ sub api_request { $webcontent = $json->decode($jsoncontent); }; - print Dumper($webcontent); - exit 1; + $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 { @@ -133,19 +179,13 @@ sub run { $self->token_request(); $self->api_request(); - foreach my $instancename (keys %{$self->{instance_infos}}) { - $self->{output}->output_add(long_msg => sprintf("%s [id = %s , compute = %s, osname = %s, state = %s]", - $instancename, - $self->{instance_infos}->{$instancename}->{id}, - $self->{instance_infos}->{$instancename}->{compute}, - $self->{instance_infos}->{$instancename}->{osname}, - $self->{instance_infos}->{$instancename}->{state})); - } + 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}->output_add(severity => 'OK', - short_msg => 'List instances:'); - - $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); + $self->{output}->display(); $self->{output}->exit(); exit 0; diff --git a/cloud/openstack/restapi/plugin.pm b/cloud/openstack/restapi/plugin.pm index 0b0edb844..02472d562 100644 --- a/cloud/openstack/restapi/plugin.pm +++ b/cloud/openstack/restapi/plugin.pm @@ -32,11 +32,13 @@ sub new { $self->{version} = '0.1'; %{$self->{modes}} = ( - 'info-instance' => 'cloud::openstack::restapi::mode::infoinstance', + 'instance' => 'cloud::openstack::restapi::mode::instance', 'list-instances' => 'cloud::openstack::restapi::mode::listinstances', 'list-hypervisors' => 'cloud::openstack::restapi::mode::listhypervisors', - 'floatingips' => 'cloud::openstack::restapi::mode::floatingips', - 'volumes' => 'cloud::openstack::restapi::mode::volumes', + 'volume' => 'cloud::openstack::restapi::mode::volume', + 'hypervisor' => 'cloud::openstack::restapi::mode::hypervisor', + 'network' => 'cloud::openstack::restapi::mode::network', + 'port' => 'cloud::openstack::restapi::mode::port', ); return $self; } From 38b9501476ea228211efd8c193d81c659f7e9d5a Mon Sep 17 00:00:00 2001 From: Shini31 Date: Tue, 7 Jun 2016 16:36:37 +0200 Subject: [PATCH 03/17] fix typo and bad port --- cloud/openstack/restapi/mode/volume.pm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cloud/openstack/restapi/mode/volume.pm b/cloud/openstack/restapi/mode/volume.pm index 102c3315b..219e87771 100644 --- a/cloud/openstack/restapi/mode/volume.pm +++ b/cloud/openstack/restapi/mode/volume.pm @@ -132,8 +132,8 @@ sub api_request { my ($self, %options) = @_; $self->{method} = 'GET'; - $self->{option_results}->{url_path} = "/v2/".$self->{option_results}->{tenant_id}."/volume/".$self->{option_results}->{volume_id}; - $self->{option_results}->{port} = '8774'; + $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}}); @@ -183,7 +183,7 @@ sub run { $self->{output}->output_add(severity => $exit, short_msg => sprintf("Volume %s is in %s state", $self->{volume_infos}->{name}, - $self->{volume_infos}->{status}, + $self->{volume_infos}->{status})); $self->{output}->display(); $self->{output}->exit(); From a3b57bfc243d59992d3f790ea0fcf9805e1ff3a2 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Tue, 7 Jun 2016 16:55:23 +0200 Subject: [PATCH 04/17] add volumes listing --- cloud/openstack/restapi/mode/listvolumes.pm | 271 ++++++++++++++++++++ cloud/openstack/restapi/plugin.pm | 9 +- 2 files changed, 276 insertions(+), 4 deletions(-) create mode 100644 cloud/openstack/restapi/mode/listvolumes.pm diff --git a/cloud/openstack/restapi/mode/listvolumes.pm b/cloud/openstack/restapi/mode/listvolumes.pm new file mode 100644 index 000000000..62ca59e42 --- /dev/null +++ b/cloud/openstack/restapi/mode/listvolumes.pm @@ -0,0 +1,271 @@ +# +# 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); + + $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}->{$instancename}->{id} = $val->{id}; + $self->{volumes_infos}->{$instancename}->{size} = $val->{size}; + $self->{volumes_infos}->{$instancename}->{type} = $val->{ivolume_type}; + $self->{volumes_infos}->{$instancename}->{state} = $val->{status}; + } +} + +sub disco_format { + my ($self, %options) = @_; + + my $names = ['name', 'id', 'type', 'size', '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->{volumes_infos}}) { + $self->{output}->add_disco_entry(name => $volumename, + id => $self->{volumes_infos}->{$volumename}->{id}, + size => $self->{volumes_infos}->{$volumename}->{size}, + 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 $instancename (keys %{$self->{volumes_infos}}) { + $self->{output}->output_add(long_msg => sprintf("%s [id = %s , size = %s, type = %s, state = %s]", + $volumename, + $self->{volumes_infos}->{$volumename}->{id}, + $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 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/plugin.pm b/cloud/openstack/restapi/plugin.pm index 02472d562..6fddcf884 100644 --- a/cloud/openstack/restapi/plugin.pm +++ b/cloud/openstack/restapi/plugin.pm @@ -32,13 +32,14 @@ sub new { $self->{version} = '0.1'; %{$self->{modes}} = ( - 'instance' => 'cloud::openstack::restapi::mode::instance', - 'list-instances' => 'cloud::openstack::restapi::mode::listinstances', - 'list-hypervisors' => 'cloud::openstack::restapi::mode::listhypervisors', - 'volume' => 'cloud::openstack::restapi::mode::volume', '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-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; } From ec7fa81f213d1a9201f71172524bf805e1416489 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Tue, 7 Jun 2016 16:59:25 +0200 Subject: [PATCH 05/17] fix bad remplacement --- cloud/openstack/restapi/mode/listvolumes.pm | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cloud/openstack/restapi/mode/listvolumes.pm b/cloud/openstack/restapi/mode/listvolumes.pm index 62ca59e42..e6c77f86b 100644 --- a/cloud/openstack/restapi/mode/listvolumes.pm +++ b/cloud/openstack/restapi/mode/listvolumes.pm @@ -124,10 +124,10 @@ sub api_request { foreach my $val (@{$webcontent->{volumes}}) { my $volumename = $val->{name}; - $self->{volumes_infos}->{$instancename}->{id} = $val->{id}; - $self->{volumes_infos}->{$instancename}->{size} = $val->{size}; - $self->{volumes_infos}->{$instancename}->{type} = $val->{ivolume_type}; - $self->{volumes_infos}->{$instancename}->{state} = $val->{status}; + $self->{volumes_infos}->{$volumename}->{id} = $val->{id}; + $self->{volumes_infos}->{$volumename}->{size} = $val->{size}; + $self->{volumes_infos}->{$volumename}->{type} = $val->{ivolume_type}; + $self->{volumes_infos}->{$volumename}->{state} = $val->{status}; } } @@ -160,7 +160,7 @@ sub run { $self->token_request(); $self->api_request(); - foreach my $instancename (keys %{$self->{volumes_infos}}) { + foreach my $volumename (keys %{$self->{volumes_infos}}) { $self->{output}->output_add(long_msg => sprintf("%s [id = %s , size = %s, type = %s, state = %s]", $volumename, $self->{volumes_infos}->{$volumename}->{id}, From c4721fdcceb4a82043da5e41abb78fc5f03666df Mon Sep 17 00:00:00 2001 From: Shini31 Date: Tue, 7 Jun 2016 17:00:29 +0200 Subject: [PATCH 06/17] fix bad remplacement again --- cloud/openstack/restapi/mode/listvolumes.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloud/openstack/restapi/mode/listvolumes.pm b/cloud/openstack/restapi/mode/listvolumes.pm index e6c77f86b..f20f89fe1 100644 --- a/cloud/openstack/restapi/mode/listvolumes.pm +++ b/cloud/openstack/restapi/mode/listvolumes.pm @@ -144,7 +144,7 @@ sub disco_show { $self->token_request(); $self->api_request(); - foreach my $instancename (keys %{$self->{volumes_infos}}) { + foreach my $volumename (keys %{$self->{volumes_infos}}) { $self->{output}->add_disco_entry(name => $volumename, id => $self->{volumes_infos}->{$volumename}->{id}, size => $self->{volumes_infos}->{$volumename}->{size}, From ff2b8c55e0836eb23509a49521f1066ccda38720 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Tue, 7 Jun 2016 17:20:22 +0200 Subject: [PATCH 07/17] port can be exist without name --- cloud/openstack/restapi/mode/port.pm | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cloud/openstack/restapi/mode/port.pm b/cloud/openstack/restapi/mode/port.pm index fe5265924..a1c90da35 100644 --- a/cloud/openstack/restapi/mode/port.pm +++ b/cloud/openstack/restapi/mode/port.pm @@ -136,8 +136,11 @@ sub api_request { eval { $webcontent = $json->decode($jsoncontent); }; - - $self->{port_infos}->{name} = $webcontent->{port}->{name}; + if ($self->{port_infos}->{name} eq '') { + $self->{port_infos}->{name} = "unknown"; + 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}; } From a8d77f0e8905dfcab537721639e142cfca552eb7 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Tue, 7 Jun 2016 17:23:13 +0200 Subject: [PATCH 08/17] add unit --- cloud/openstack/restapi/mode/listvolumes.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cloud/openstack/restapi/mode/listvolumes.pm b/cloud/openstack/restapi/mode/listvolumes.pm index f20f89fe1..7bd5b31f7 100644 --- a/cloud/openstack/restapi/mode/listvolumes.pm +++ b/cloud/openstack/restapi/mode/listvolumes.pm @@ -147,7 +147,7 @@ sub disco_show { foreach my $volumename (keys %{$self->{volumes_infos}}) { $self->{output}->add_disco_entry(name => $volumename, id => $self->{volumes_infos}->{$volumename}->{id}, - size => $self->{volumes_infos}->{$volumename}->{size}, + size => $self->{volumes_infos}->{$volumename}->{size}."Gb", type => $self->{volumes_infos}->{$volumename}->{type}, state => $self->{volumes_infos}->{$volumename}->{state}, ); @@ -161,7 +161,7 @@ sub run { $self->api_request(); foreach my $volumename (keys %{$self->{volumes_infos}}) { - $self->{output}->output_add(long_msg => sprintf("%s [id = %s , size = %s, type = %s, state = %s]", + $self->{output}->output_add(long_msg => sprintf("%s [id = %s , size = %sGb, type = %s, state = %s]", $volumename, $self->{volumes_infos}->{$volumename}->{id}, $self->{volumes_infos}->{$volumename}->{size}, From bf984c2e065def54437b884c7f4a68e500d92220 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Tue, 7 Jun 2016 17:25:16 +0200 Subject: [PATCH 09/17] bad syntax for if condition --- cloud/openstack/restapi/mode/port.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloud/openstack/restapi/mode/port.pm b/cloud/openstack/restapi/mode/port.pm index a1c90da35..ca076c1ea 100644 --- a/cloud/openstack/restapi/mode/port.pm +++ b/cloud/openstack/restapi/mode/port.pm @@ -138,7 +138,7 @@ sub api_request { }; if ($self->{port_infos}->{name} eq '') { $self->{port_infos}->{name} = "unknown"; - else { + } else { $self->{port_infos}->{name} = $webcontent->{port}->{name}; } $self->{port_infos}->{admin_state} = $webcontent->{port}->{admin_state_up}; From fdea339f95e003bd78b734c0b3a4d1582fff3eb9 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Tue, 7 Jun 2016 17:26:55 +0200 Subject: [PATCH 10/17] bug day... --- cloud/openstack/restapi/mode/port.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloud/openstack/restapi/mode/port.pm b/cloud/openstack/restapi/mode/port.pm index ca076c1ea..1adc57e55 100644 --- a/cloud/openstack/restapi/mode/port.pm +++ b/cloud/openstack/restapi/mode/port.pm @@ -136,7 +136,7 @@ sub api_request { eval { $webcontent = $json->decode($jsoncontent); }; - if ($self->{port_infos}->{name} eq '') { + if ($webcontent->{port}->{name} eq '') { $self->{port_infos}->{name} = "unknown"; } else { $self->{port_infos}->{name} = $webcontent->{port}->{name}; From cfe331cedcbb1cbc804ed1a496d987edec0d8625 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Tue, 7 Jun 2016 17:29:37 +0200 Subject: [PATCH 11/17] change unknow to id for no name port --- cloud/openstack/restapi/mode/port.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloud/openstack/restapi/mode/port.pm b/cloud/openstack/restapi/mode/port.pm index 1adc57e55..6cb6ab783 100644 --- a/cloud/openstack/restapi/mode/port.pm +++ b/cloud/openstack/restapi/mode/port.pm @@ -137,7 +137,7 @@ sub api_request { $webcontent = $json->decode($jsoncontent); }; if ($webcontent->{port}->{name} eq '') { - $self->{port_infos}->{name} = "unknown"; + $self->{port_infos}->{name} = $webcontent->{port}->{id}; } else { $self->{port_infos}->{name} = $webcontent->{port}->{name}; } From d9ff13b7456e0c853fb3760fd0303d0511868cfd Mon Sep 17 00:00:00 2001 From: Shini31 Date: Wed, 8 Jun 2016 11:27:00 +0200 Subject: [PATCH 12/17] add list networks --- cloud/openstack/restapi/mode/listnetworks.pm | 260 +++++++++++++++++++ cloud/openstack/restapi/plugin.pm | 1 + 2 files changed, 261 insertions(+) create mode 100644 cloud/openstack/restapi/mode/listnetworks.pm diff --git a/cloud/openstack/restapi/mode/listnetworks.pm b/cloud/openstack/restapi/mode/listnetworks.pm new file mode 100644 index 000000000..2f55dcdc0 --- /dev/null +++ b/cloud/openstack/restapi/mode/listnetworks.pm @@ -0,0 +1,260 @@ +# +# 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); + + $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.O/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_id} = $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 = %sGb, 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/plugin.pm b/cloud/openstack/restapi/plugin.pm index 6fddcf884..ed9951b1c 100644 --- a/cloud/openstack/restapi/plugin.pm +++ b/cloud/openstack/restapi/plugin.pm @@ -36,6 +36,7 @@ sub new { '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', From fe9832dfb054d6fb749ca399068a9849289639ca Mon Sep 17 00:00:00 2001 From: Shini31 Date: Wed, 8 Jun 2016 11:29:31 +0200 Subject: [PATCH 13/17] update doc --- cloud/openstack/restapi/mode/hypervisor.pm | 15 ++++++++++----- cloud/openstack/restapi/mode/instance.pm | 17 +++++++++++------ .../openstack/restapi/mode/listhypervisors.pm | 2 +- cloud/openstack/restapi/mode/listinstances.pm | 7 +++++-- cloud/openstack/restapi/mode/listvolumes.pm | 9 ++++++--- cloud/openstack/restapi/mode/network.pm | 15 ++++++++------- cloud/openstack/restapi/mode/port.pm | 17 ++++++++++------- cloud/openstack/restapi/mode/volume.pm | 18 +++++++++++++----- 8 files changed, 64 insertions(+), 36 deletions(-) diff --git a/cloud/openstack/restapi/mode/hypervisor.pm b/cloud/openstack/restapi/mode/hypervisor.pm index 0e0235556..8f1ad7284 100644 --- a/cloud/openstack/restapi/mode/hypervisor.pm +++ b/cloud/openstack/restapi/mode/hypervisor.pm @@ -55,7 +55,6 @@ sub new { "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' }, "hypervisor-id:s" => { name => 'hypervisor_id' }, @@ -254,14 +253,16 @@ Specify SSL version (example : 'sslv3', 'tlsv1'...) 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) +=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: @@ -272,6 +273,10 @@ OPENSTACK OPTIONS: 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 index c2d04433c..1057cae41 100644 --- a/cloud/openstack/restapi/mode/instance.pm +++ b/cloud/openstack/restapi/mode/instance.pm @@ -63,7 +63,6 @@ sub new { "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' }, "instance-id:s" => { name => 'instance_id' }, @@ -198,7 +197,7 @@ __END__ =head1 MODE -List OpenStack instances through Compute API V2 +Monitor instance status through Compute API V2 JSON OPTIONS: @@ -262,14 +261,16 @@ Specify SSL version (example : 'sslv3', 'tlsv1'...) 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) +=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: @@ -280,6 +281,10 @@ OPENSTACK OPTIONS: 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 index 5e0e250a9..658e1910a 100644 --- a/cloud/openstack/restapi/mode/listhypervisors.pm +++ b/cloud/openstack/restapi/mode/listhypervisors.pm @@ -254,7 +254,7 @@ 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) +Exclude specific instance's state (comma seperated list) (Example: --exclude=disabled) =item B<--timeout> diff --git a/cloud/openstack/restapi/mode/listinstances.pm b/cloud/openstack/restapi/mode/listinstances.pm index 25decf343..a3836c5ff 100644 --- a/cloud/openstack/restapi/mode/listinstances.pm +++ b/cloud/openstack/restapi/mode/listinstances.pm @@ -139,6 +139,7 @@ sub api_request { } 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; @@ -148,7 +149,7 @@ sub api_request { sub disco_format { my ($self, %options) = @_; - my $names = ['name', 'id', 'compute', 'osname', 'state']; + my $names = ['name', 'id', 'zone', 'compute', 'osname', 'state']; $self->{output}->add_disco_format(elements => $names); } @@ -161,6 +162,7 @@ sub disco_show { 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}, @@ -175,9 +177,10 @@ sub run { $self->api_request(); foreach my $instancename (keys %{$self->{instance_infos}}) { - $self->{output}->output_add(long_msg => sprintf("%s [id = %s , compute = %s, osname = %s, state = %s]", + $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})); diff --git a/cloud/openstack/restapi/mode/listvolumes.pm b/cloud/openstack/restapi/mode/listvolumes.pm index 7bd5b31f7..530a2c424 100644 --- a/cloud/openstack/restapi/mode/listvolumes.pm +++ b/cloud/openstack/restapi/mode/listvolumes.pm @@ -125,6 +125,7 @@ sub api_request { 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->{ivolume_type}; $self->{volumes_infos}->{$volumename}->{state} = $val->{status}; @@ -134,7 +135,7 @@ sub api_request { sub disco_format { my ($self, %options) = @_; - my $names = ['name', 'id', 'type', 'size', 'state']; + my $names = ['name', 'id', 'zone', 'type', 'size', 'state']; $self->{output}->add_disco_format(elements => $names); } @@ -147,6 +148,7 @@ sub disco_show { 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}, @@ -161,9 +163,10 @@ sub run { $self->api_request(); foreach my $volumename (keys %{$self->{volumes_infos}}) { - $self->{output}->output_add(long_msg => sprintf("%s [id = %s , size = %sGb, type = %s, state = %s]", + $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})); @@ -184,7 +187,7 @@ __END__ =head1 MODE -List OpenStack instances through Compute API V2 +List OpenStack volumes through Block Storage API V2 JSON OPTIONS: diff --git a/cloud/openstack/restapi/mode/network.pm b/cloud/openstack/restapi/mode/network.pm index 71dde5f42..b97807980 100644 --- a/cloud/openstack/restapi/mode/network.pm +++ b/cloud/openstack/restapi/mode/network.pm @@ -57,7 +57,6 @@ sub new { "password:s" => { name => 'password' }, "ssl:s" => { name => 'ssl', }, "header:s@" => { name => 'header' }, - "exclude:s" => { name => 'exclude' }, "timeout:s" => { name => 'timeout' }, "network-id:s" => { name => 'network_id' }, "threshold-overload:s@" => { name => 'threshold_overload' }, @@ -255,23 +254,25 @@ Specify SSL version (example : 'sslv3', 'tlsv1'...) 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) +=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<--tenant-id> +=item B<--network-id> -Set Tenant's ID +Set Network's ID =back diff --git a/cloud/openstack/restapi/mode/port.pm b/cloud/openstack/restapi/mode/port.pm index 6cb6ab783..ac17a3aa1 100644 --- a/cloud/openstack/restapi/mode/port.pm +++ b/cloud/openstack/restapi/mode/port.pm @@ -257,24 +257,27 @@ Specify SSL version (example : 'sslv3', 'tlsv1'...) 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 +=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<--tenant-id> +=item B<--port-id> -Set Tenant's ID +Set Port's ID =back -=cut diff --git a/cloud/openstack/restapi/mode/volume.pm b/cloud/openstack/restapi/mode/volume.pm index 219e87771..7f9fb8b3d 100644 --- a/cloud/openstack/restapi/mode/volume.pm +++ b/cloud/openstack/restapi/mode/volume.pm @@ -64,7 +64,6 @@ sub new { "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' }, "volume-id:s" => { name => 'volume_id' }, @@ -261,16 +260,20 @@ Specify SSL version (example : 'sslv3', 'tlsv1'...) 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 +=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 @@ -279,6 +282,11 @@ OPENSTACK OPTIONS: Set Tenant's ID +=item B<--volume-id> + +Set Volume's ID + =back =cut + From f3b405c09b18e9f56922b490d6521d17dfe040f6 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Wed, 8 Jun 2016 13:57:55 +0200 Subject: [PATCH 14/17] fix bad copy/paste --- cloud/openstack/restapi/mode/listnetworks.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cloud/openstack/restapi/mode/listnetworks.pm b/cloud/openstack/restapi/mode/listnetworks.pm index 2f55dcdc0..f341f8600 100644 --- a/cloud/openstack/restapi/mode/listnetworks.pm +++ b/cloud/openstack/restapi/mode/listnetworks.pm @@ -107,7 +107,7 @@ sub api_request { my ($self, %options) = @_; $self->{method} = 'GET'; - $self->{option_results}->{url_path} = "/v2.O/networks"; + $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}}); @@ -160,7 +160,7 @@ sub run { $self->api_request(); foreach my $networkname (keys %{$self->{networks_infos}}) { - $self->{output}->output_add(long_msg => sprintf("%s [id = %s, tenant = %s, state = %sGb, admin_state = %s]", + $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}, From 39f7e7b0c08acbdb602dbcd65c04d4989a9df921 Mon Sep 17 00:00:00 2001 From: Shini31 Date: Wed, 8 Jun 2016 14:02:13 +0200 Subject: [PATCH 15/17] tenant instead of tenant_id --- cloud/openstack/restapi/mode/listnetworks.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloud/openstack/restapi/mode/listnetworks.pm b/cloud/openstack/restapi/mode/listnetworks.pm index f341f8600..5cd37c8e4 100644 --- a/cloud/openstack/restapi/mode/listnetworks.pm +++ b/cloud/openstack/restapi/mode/listnetworks.pm @@ -124,7 +124,7 @@ sub api_request { foreach my $val (@{$webcontent->{networks}}) { my $networkname = $val->{name}; $self->{networks_infos}->{$networkname}->{id} = $val->{id}; - $self->{networks_infos}->{$networkname}->{tenant_id} = $val->{tenant_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}; } From c846e123d3afdac48549d5fc9fa8df67642553fe Mon Sep 17 00:00:00 2001 From: Shini31 Date: Wed, 8 Jun 2016 14:12:19 +0200 Subject: [PATCH 16/17] fix typo --- cloud/openstack/restapi/mode/listvolumes.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloud/openstack/restapi/mode/listvolumes.pm b/cloud/openstack/restapi/mode/listvolumes.pm index 530a2c424..76cbac987 100644 --- a/cloud/openstack/restapi/mode/listvolumes.pm +++ b/cloud/openstack/restapi/mode/listvolumes.pm @@ -127,7 +127,7 @@ sub api_request { $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->{ivolume_type}; + $self->{volumes_infos}->{$volumename}->{type} = $val->{volume_type}; $self->{volumes_infos}->{$volumename}->{state} = $val->{status}; } } From fd425f60b6805d9dd3d7320cd3bf112c7706252f Mon Sep 17 00:00:00 2001 From: Shini31 Date: Wed, 8 Jun 2016 15:17:07 +0200 Subject: [PATCH 17/17] add check options --- cloud/openstack/restapi/mode/hypervisor.pm | 21 +++++++++++++++++++ cloud/openstack/restapi/mode/instance.pm | 21 +++++++++++++++++++ .../openstack/restapi/mode/listhypervisors.pm | 17 +++++++++++++++ cloud/openstack/restapi/mode/listinstances.pm | 17 +++++++++++++++ cloud/openstack/restapi/mode/listnetworks.pm | 13 ++++++++++++ cloud/openstack/restapi/mode/listvolumes.pm | 17 +++++++++++++++ cloud/openstack/restapi/mode/network.pm | 17 +++++++++++++++ cloud/openstack/restapi/mode/port.pm | 17 +++++++++++++++ cloud/openstack/restapi/mode/volume.pm | 21 +++++++++++++++++++ 9 files changed, 161 insertions(+) diff --git a/cloud/openstack/restapi/mode/hypervisor.pm b/cloud/openstack/restapi/mode/hypervisor.pm index 8f1ad7284..7655a153e 100644 --- a/cloud/openstack/restapi/mode/hypervisor.pm +++ b/cloud/openstack/restapi/mode/hypervisor.pm @@ -85,6 +85,27 @@ sub check_options { 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}}) } diff --git a/cloud/openstack/restapi/mode/instance.pm b/cloud/openstack/restapi/mode/instance.pm index 1057cae41..6cd5b5bc6 100644 --- a/cloud/openstack/restapi/mode/instance.pm +++ b/cloud/openstack/restapi/mode/instance.pm @@ -93,6 +93,27 @@ sub check_options { 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}}) } diff --git a/cloud/openstack/restapi/mode/listhypervisors.pm b/cloud/openstack/restapi/mode/listhypervisors.pm index 658e1910a..f6763f998 100644 --- a/cloud/openstack/restapi/mode/listhypervisors.pm +++ b/cloud/openstack/restapi/mode/listhypervisors.pm @@ -62,6 +62,23 @@ 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}}) } diff --git a/cloud/openstack/restapi/mode/listinstances.pm b/cloud/openstack/restapi/mode/listinstances.pm index a3836c5ff..323589ad7 100644 --- a/cloud/openstack/restapi/mode/listinstances.pm +++ b/cloud/openstack/restapi/mode/listinstances.pm @@ -62,6 +62,23 @@ 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}}) } diff --git a/cloud/openstack/restapi/mode/listnetworks.pm b/cloud/openstack/restapi/mode/listnetworks.pm index 5cd37c8e4..162f0f6b1 100644 --- a/cloud/openstack/restapi/mode/listnetworks.pm +++ b/cloud/openstack/restapi/mode/listnetworks.pm @@ -61,6 +61,19 @@ 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}}) } diff --git a/cloud/openstack/restapi/mode/listvolumes.pm b/cloud/openstack/restapi/mode/listvolumes.pm index 76cbac987..1ad82ef90 100644 --- a/cloud/openstack/restapi/mode/listvolumes.pm +++ b/cloud/openstack/restapi/mode/listvolumes.pm @@ -62,6 +62,23 @@ 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}}) } diff --git a/cloud/openstack/restapi/mode/network.pm b/cloud/openstack/restapi/mode/network.pm index b97807980..f1b4ba91c 100644 --- a/cloud/openstack/restapi/mode/network.pm +++ b/cloud/openstack/restapi/mode/network.pm @@ -86,6 +86,23 @@ sub check_options { 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}}) } diff --git a/cloud/openstack/restapi/mode/port.pm b/cloud/openstack/restapi/mode/port.pm index ac17a3aa1..51c51e703 100644 --- a/cloud/openstack/restapi/mode/port.pm +++ b/cloud/openstack/restapi/mode/port.pm @@ -86,6 +86,23 @@ sub check_options { 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}}) } diff --git a/cloud/openstack/restapi/mode/volume.pm b/cloud/openstack/restapi/mode/volume.pm index 7f9fb8b3d..3ca349c5d 100644 --- a/cloud/openstack/restapi/mode/volume.pm +++ b/cloud/openstack/restapi/mode/volume.pm @@ -94,6 +94,27 @@ sub check_options { 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}}) }