From a29f95b69cca831f41881e4850884a492ed5b1d5 Mon Sep 17 00:00:00 2001 From: Sims24 Date: Thu, 2 Jun 2016 09:46:20 +0200 Subject: [PATCH 01/30] + add ha-nodes mode Ref https://github.com/centreon/centreon-plugins/issues/99 --- network/netasq/snmp/plugin.pm | 1 + 1 file changed, 1 insertion(+) diff --git a/network/netasq/snmp/plugin.pm b/network/netasq/snmp/plugin.pm index 751fb4acf..a77fcd082 100644 --- a/network/netasq/snmp/plugin.pm +++ b/network/netasq/snmp/plugin.pm @@ -39,6 +39,7 @@ sub new { 'list-interfaces' => 'snmp_standard::mode::listinterfaces', 'load' => 'snmp_standard::mode::loadaverage', 'ha-status' => 'network::netasq::snmp::mode::hastatus', + 'ha-nodes' => 'network::netasq::snmp::mode::hanodes', 'memory' => 'os::freebsd::snmp::mode::memory', 'storage' => 'snmp_standard::mode::storage', 'swap' => 'snmp_standard::mode::swap', From b112da6e070ce3dacdc45fc56cafb7a6318ea17c Mon Sep 17 00:00:00 2001 From: Sims24 Date: Thu, 2 Jun 2016 09:48:08 +0200 Subject: [PATCH 02/30] + add ha-nodes mode Ref https://github.com/centreon/centreon-plugins/issues/99 --- network/netasq/snmp/mode/hanodes.pm | 205 ++++++++++++++++++++++++++++ 1 file changed, 205 insertions(+) create mode 100644 network/netasq/snmp/mode/hanodes.pm diff --git a/network/netasq/snmp/mode/hanodes.pm b/network/netasq/snmp/mode/hanodes.pm new file mode 100644 index 000000000..d12d6c8fd --- /dev/null +++ b/network/netasq/snmp/mode/hanodes.pm @@ -0,0 +1,205 @@ +# +# Copyright 2016 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 network::netasq::snmp::mode::hanodes; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; + +my $instance_mode; + +sub custom_threshold_output { + my ($self, %options) = @_; + my $status = 'ok'; + my $message; + + eval { + local $SIG{__WARN__} = sub { $message = $_[0]; }; + local $SIG{__DIE__} = sub { $message = $_[0]; }; + + if (defined($instance_mode->{option_results}->{critical_state}) && $instance_mode->{option_results}->{critical_state} ne '' && + eval "$instance_mode->{option_results}->{critical_state}") { + $status = 'critical'; + } elsif (defined($instance_mode->{option_results}->{warning_state}) && $instance_mode->{option_results}->{warning_state} ne '' && + eval "$instance_mode->{option_results}->{warning_state}") { + $status = 'warning'; + } + }; + if (defined($message)) { + $self->{output}->output_add(long_msg => 'filter status issue: ' . $message); + } + + return $status; +} + +sub custom_state_output { + my ($self, %options) = @_; + + my $msg = sprintf("state is '%s'", $self->{result_values}->{state}); + return $msg; +} + +sub custom_state_calc { + my ($self, %options) = @_; + + $self->{result_values}->{state} = $options{new_datas}->{$self->{instance} . '_state'}; + $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; + return 0; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'nodes', type => 1, cb_prefix_output => 'prefix_node_output', message_multiple => 'All HA nodes are OK' }, + ]; + + $self->{maps_counters}->{nodes} = [ + { label => 'state', threshold => 0, set => { + key_values => [ { name => 'state' }, { name => 'display' } ], + closure_custom_calc => \&custom_state_calc, + closure_custom_output => \&custom_state_output, + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&custom_threshold_output, + } + }, + { label => 'health', set => { + key_values => [ { name => 'health' }, { name => 'display' } ], + output_template => 'node Health: %s%%', + perfdatas => [ + { label => 'health', value => 'health_absolute', template => '%d', + min => 0, max => 100, label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + } + ]; +} + +sub prefix_node_output { + my ($self, %options) = @_; + + return "Node '" . $options{instance_value}->{display} . "' "; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => + { + "filter-node:s" => { name => 'filter_node' }, + "warning-state:s" => { name => 'warning_state', default => '' }, + "critical-state:s" => { name => 'critical_state', default => '%{state} eq "offline"' }, + }); + return $self; +} + +sub change_macros { + my ($self, %options) = @_; + + foreach (('warning_state', 'critical_state')) { + if (defined($self->{option_results}->{$_})) { + $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{result_values}->{$1}/g; + } + } +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $self->change_macros(); + $instance_mode = $self; +} + +my %map_status = ( + 1 => 'online', + 2 => 'offline', +); + +my $mapping = { + ntqFwSerial => { oid => '.1.3.6.1.4.1.11256.1.11.7.1.2' }, + ntqHAQuality => { oid => '.1.3.6.1.4.1.11256.1.11.7.1.7' }, + ntqOnline => { oid => '.1.3.6.1.4.1.11256.1.11.7.1.3', map => \%map_status }, +}; + +my $oid_ntqNodeTable = '.1.3.6.1.4.1.11256.1.11.7'; + +sub manage_selection { + my ($self, %options) = @_; + $self->{snmp} = $options{snmp}; + + $self->{results} = $options{snmp}->get_table(oid => $oid_ntqNodeTable, + nothing_quit => 1); + foreach my $oid (keys %{$self->{results}}) { + $oid =~ /^$mapping->{ntqOnline}->{oid}\.(.*)$/; + my $instance = $1; + my $result = $self->{snmp}->map_instance(mapping => $mapping, results => $self->{results}, instance => $instance); + + if (defined($self->{option_results}->{filter_nodes}) && $self->{option_results}->{filter_nodes} ne '' && + $result->{ntqFwSerial} !~ /$self->{option_results}->{filter_nodes}/) { + $self->{output}->output_add(long_msg => "Skipping '" . $result->{ntqFwSerial} . "': no matching filter.", debug => 1); + next; + } + $self->{nodes}->{$result->{ntqFwSerial}} = { state => $result->{ntqOnline}, + health => $result->{ntqHAQuality}, + display => $result->{ntqFwSerial} }; + } +} + +1; + +__END__ + +=head1 MODE + +Check Netasq node state and health + +=over 8 + +=item B<--filter-node> + +Filter name with regexp. + +=item B<--warning-health> + +Warning on health level. (e.g --warning 90:) + +=item B<--critical-health> + +Critical on health level. (e.g --critical 80:) + +=item B<--warning-state> + +Set warning threshold for status. Use "%{state}" as a special variable. +Value can be 'online' or 'offline'. + +=item B<--critical-state> + +Set critical threshold for status. Use "%{state}" as a special variable. +Value can be 'online' or 'offline'. + +=back + +=cut From 0be270e7579256032af5e90b91e21f901e2b2aba Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Fri, 3 Jun 2016 12:00:49 +0200 Subject: [PATCH 03/30] + Fix subject for x509 --- apps/protocols/x509/mode/validity.pm | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/apps/protocols/x509/mode/validity.pm b/apps/protocols/x509/mode/validity.pm index 30639a39d..1061fa6f3 100644 --- a/apps/protocols/x509/mode/validity.pm +++ b/apps/protocols/x509/mode/validity.pm @@ -113,17 +113,28 @@ sub run { #Subject Name } elsif ($self->{option_results}->{validity_mode} eq 'subject') { my @subject_matched = (); - my @subject_name = Net::SSLeay::X509_get_subjectAltNames($cert); - foreach my $subject_name (@subject_name) { - if ($subject_name =~ /$self->{option_results}->{subjectname}/mi) { - push @subject_matched, $subject_name; + + my $subject = Net::SSLeay::X509_NAME_get_text_by_NID( + Net::SSLeay::X509_get_subject_name($cert), 13); # NID_CommonName + $subject =~s{\0$}{}; # work around Bug in Net::SSLeay <1.33 + if ($subject =~ /$self->{option_results}->{subjectname}/mi) { + push @subject_matched, $subject; + } else { + $self->{output}->output_add(long_msg => sprintf("Subject Name '%s' is also present in Certificate", $subject), debug => 1); + } + + my @subject_alt_names = Net::SSLeay::X509_get_subjectAltNames($cert); + for (my $i = 0; $i < $#subject_alt_names; $i++) { + next if ($i % 2 == 0); + if ($subject_alt_names[$i] =~ /$self->{option_results}->{subjectname}/mi) { + push @subject_matched, $subject_alt_names[$i]; } else { - if ($subject_name =~ /[\w\-]+(\.[\w\-]+)*\.\w+/) { - $self->{output}->output_add(long_msg => sprintf("Subject Name '%s' is also present in Certificate", $subject_name), debug => 1); - } + $self->{output}->output_add(long_msg => sprintf("Subject Name '%s' is also present in Certificate", $subject_alt_names[$i]), debug => 1); } } + + if (@subject_matched == 0) { $self->{output}->output_add(severity => 'CRITICAL', short_msg => sprintf("No Subject Name matched '%s' in Certificate", $self->{option_results}->{subjectname})); From 5916c3a9d941dbdb8cf866ef3118b11a997b1472 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Mon, 6 Jun 2016 11:26:35 +0200 Subject: [PATCH 04/30] + Fix header in http lib --- centreon/plugins/http.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/centreon/plugins/http.pm b/centreon/plugins/http.pm index 59e5fb41b..4f540d675 100644 --- a/centreon/plugins/http.pm +++ b/centreon/plugins/http.pm @@ -306,7 +306,7 @@ sub request { $self->{output}->exit(); } - $self->{headers} = $response->{headers}; + $self->{headers} = $response->headers(); $self->{response} = $response; return $response->content; } From fb0b50554f2b91b60222e46c4e67ab2b5c2802df Mon Sep 17 00:00:00 2001 From: Shini31 Date: Mon, 6 Jun 2016 14:44:45 +0200 Subject: [PATCH 05/30] 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 06/30] 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 07/30] 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 08/30] 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 09/30] 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 10/30] 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 11/30] 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 12/30] 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 13/30] 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 14/30] 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 15/30] 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 16/30] 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 17/30] 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 18/30] 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 19/30] 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 20/30] 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 21/30] 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}}) } From 6aa9b012946c74b83a5dbcf2babaaf87c9ee8245 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Wed, 8 Jun 2016 23:24:35 +0200 Subject: [PATCH 22/30] + Fix #422 --- storage/netapp/snmp/mode/cacheage.pm | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/storage/netapp/snmp/mode/cacheage.pm b/storage/netapp/snmp/mode/cacheage.pm index 604711be2..396e30cc0 100644 --- a/storage/netapp/snmp/mode/cacheage.pm +++ b/storage/netapp/snmp/mode/cacheage.pm @@ -44,18 +44,17 @@ sub check_options { $self->SUPER::init(%options); if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'."); - $self->{output}->option_exit(); + $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'."); + $self->{output}->option_exit(); } if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'."); - $self->{output}->option_exit(); + $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'."); + $self->{output}->option_exit(); } } sub run { my ($self, %options) = @_; - # $options{snmp} = snmp object $self->{snmp} = $options{snmp}; my $oid_cacheAge = ".1.3.6.1.4.1.789.1.2.2.23.0"; @@ -63,13 +62,13 @@ sub run { my $result = $self->{snmp}->get_leef(oids => [$oid_cacheAge], nothing_quit => 1); - my $exit = $self->{perfdata}->threshold_check(value => $results->{$oid_cacheAge}, + my $exit = $self->{perfdata}->threshold_check(value => $result->{$oid_cacheAge}, threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); $self->{output}->output_add(severity => $exit, - short_msg => sprintf("Cache age is '%s' minutes", $results->{$oid_cacheAge})); + short_msg => sprintf("Cache age is '%s' minutes", $result->{$oid_cacheAge})); $self->{output}->perfdata_add(label => 'cache_age', unit => 'm', - value => $results->{$oid_cacheAge}, + value => $result->{$oid_cacheAge}, warning => $self->{option_results}->{warning}, critical => $self->{option_results}->{critical}, min => 0); From 25b82241b6f23b6bf95fba4c198e07eb40aad541 Mon Sep 17 00:00:00 2001 From: qgarnier Date: Thu, 9 Jun 2016 23:01:00 +0200 Subject: [PATCH 23/30] Revert "Openstack" --- cloud/openstack/restapi/mode/hypervisor.pm | 303 ----------------- cloud/openstack/restapi/mode/instance.pm | 311 ----------------- .../openstack/restapi/mode/listhypervisors.pm | 292 ---------------- cloud/openstack/restapi/mode/listinstances.pm | 305 ----------------- cloud/openstack/restapi/mode/listnetworks.pm | 273 --------------- cloud/openstack/restapi/mode/listvolumes.pm | 291 ---------------- cloud/openstack/restapi/mode/network.pm | 296 ----------------- cloud/openstack/restapi/mode/port.pm | 300 ----------------- cloud/openstack/restapi/mode/volume.pm | 313 ------------------ cloud/openstack/restapi/plugin.pm | 56 ---- 10 files changed, 2740 deletions(-) delete mode 100644 cloud/openstack/restapi/mode/hypervisor.pm delete mode 100644 cloud/openstack/restapi/mode/instance.pm delete mode 100644 cloud/openstack/restapi/mode/listhypervisors.pm delete mode 100644 cloud/openstack/restapi/mode/listinstances.pm delete mode 100644 cloud/openstack/restapi/mode/listnetworks.pm delete mode 100644 cloud/openstack/restapi/mode/listvolumes.pm delete mode 100644 cloud/openstack/restapi/mode/network.pm delete mode 100644 cloud/openstack/restapi/mode/port.pm delete mode 100644 cloud/openstack/restapi/mode/volume.pm delete mode 100644 cloud/openstack/restapi/plugin.pm diff --git a/cloud/openstack/restapi/mode/hypervisor.pm b/cloud/openstack/restapi/mode/hypervisor.pm deleted file mode 100644 index 7655a153e..000000000 --- a/cloud/openstack/restapi/mode/hypervisor.pm +++ /dev/null @@ -1,303 +0,0 @@ -# -# Copyright 2015 Centreon (http://www.centreon.com/) -# -# Centreon is a full-fledged industry-strength solution that meets -# the needs in IT infrastructure and application monitoring for -# service performance. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -package cloud::openstack::restapi::mode::hypervisor; - -use base qw(centreon::plugins::mode); - -use strict; -use warnings; -use centreon::plugins::http; -use JSON; - -my $thresholds = { - status => [ - ['up', 'OK'], - ['down', 'CRITICAL'], - ], -}; - -sub new { - my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); - bless $self, $class; - - $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "data:s" => { name => 'data' }, - "hostname:s" => { name => 'hostname' }, - "http-peer-addr:s" => { name => 'http_peer_addr' }, - "port:s" => { name => 'port', default => '5000' }, - "proto:s" => { name => 'proto' }, - "urlpath:s" => { name => 'url_path', default => '/v3/auth/tokens' }, - "proxyurl:s" => { name => 'proxyurl' }, - "proxypac:s" => { name => 'proxypac' }, - "credentials" => { name => 'credentials' }, - "username:s" => { name => 'username' }, - "password:s" => { name => 'password' }, - "ssl:s" => { name => 'ssl', }, - "header:s@" => { name => 'header' }, - "timeout:s" => { name => 'timeout' }, - "tenant-id:s" => { name => 'tenant_id' }, - "hypervisor-id:s" => { name => 'hypervisor_id' }, - "threshold-overload:s@" => { name => 'threshold_overload' }, - }); - - $self->{http} = centreon::plugins::http->new(output => $self->{output}); - $self->{hypervisor_infos} = (); - return $self; -} - -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - $self->{overload_th} = {}; - foreach my $val (@{$self->{option_results}->{threshold_overload}}) { - if ($val !~ /^(.*?),(.*?),(.*)$/) { - $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload option '" . $val . "'."); - $self->{output}->option_exit(); - } - my ($section, $status, $filter) = ($1, $2, $3); - if ($self->{output}->is_litteral_status(status => $status) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload status '" . $val . "'."); - $self->{output}->option_exit(); - } - $self->{overload_th}->{$section} = [] if (!defined($self->{overload_th}->{$section})); - push @{$self->{overload_th}->{$section}}, {filter => $filter, status => $status}; - } - - if (!defined($self->{option_results}->{header}) || $self->{option_results}->{header} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --header option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{data}) || $self->{option_results}->{data} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --data option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{hostname}) || $self->{option_results}->{hostname} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --hostname option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{tenant_id}) || $self->{option_results}->{tenant_id} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --tenant-id option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{hypervisor_id}) || $self->{option_results}->{hypervisor_id} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --hypervisor-id option."); - $self->{output}->option_exit(); - } - - $self->{http}->set_options(%{$self->{option_results}}) -} - -sub token_request { - my ($self, %options) = @_; - - $self->{method} = 'GET'; - if (defined($self->{option_results}->{data})) { - local $/ = undef; - if (!open(FILE, "<", $self->{option_results}->{data})) { - $self->{output}->output_add(severity => 'UNKNOWN', - short_msg => sprintf("Could not read file '%s': %s", $self->{option_results}->{data}, $!)); - $self->{output}->display(); - $self->{output}->exit(); - } - $self->{json_request} = ; - close FILE; - $self->{method} = 'POST'; - } - - my $response = $self->{http}->request(method => $self->{method}, query_form_post => $self->{json_request}); - my $headers = $self->{http}->get_header(); - - eval { - $self->{header} = $headers->header('X-Subject-Token'); - }; - - if ($@) { - $self->{output}->add_option_msg(short_msg => "Cannot retrieve API Token"); - $self->{output}->option_exit(); - } -} - -sub api_request { - my ($self, %options) = @_; - - $self->{method} = 'GET'; - $self->{option_results}->{url_path} = "/v2/".$self->{option_results}->{tenant_id}."/os-hypervisors/".$self->{option_results}->{hypervisor_id}; - $self->{option_results}->{port} = '8774'; - @{$self->{option_results}->{header}} = ('X-Auth-Token:' . $self->{header}, 'Accept:application/json'); - $self->{http}->set_options(%{$self->{option_results}}); - - my $webcontent; - my $jsoncontent = $self->{http}->request(method => $self->{method}); - - my $json = JSON->new; - - eval { - $webcontent = $json->decode($jsoncontent); - }; - - $self->{hypervisor_infos}->{name} = $webcontent->{hypervisor}->{hypervisor_hostname}; - $self->{hypervisor_infos}->{state} = $webcontent->{hypervisor}->{state}; - $self->{hypervisor_infos}->{status} = $webcontent->{hypervisor}->{status}; -} - - -sub get_severity { - my ($self, %options) = @_; - my $status = 'UNKNOWN'; # default - - if (defined($self->{overload_th}->{$options{section}})) { - foreach (@{$self->{overload_th}->{$options{section}}}) { - if ($options{value} =~ /$_->{filter}/i) { - $status = $_->{status}; - return $status; - } - } - } - foreach (@{$thresholds->{$options{section}}}) { - if ($options{value} =~ /$$_[0]/i) { - $status = $$_[1]; - return $status; - } - } - - return $status; -} - -sub run { - my ($self, %options) = @_; - - $self->token_request(); - $self->api_request(); - - my $exit = $self->get_severity(section => 'status', value => $self->{hypervisor_infos}->{state}); - $self->{output}->output_add(severity => $exit, - short_msg => sprintf("Hypervisor %s is %s (status: %s)", - $self->{hypervisor_infos}->{name}, - $self->{hypervisor_infos}->{state}, - $self->{hypervisor_infos}->{status})); - - $self->{output}->display(); - $self->{output}->exit(); - - exit 0; -} - -1; - -__END__ - -=head1 MODE - -List OpenStack instances through Compute API V2 - -JSON OPTIONS: - -=over 8 - -=item B<--data> - -Set file with JSON request - -=back - -HTTP OPTIONS: - -=over 8 - -=item B<--hostname> - -IP Addr/FQDN of OpenStack Compute's API - -=item B<--http-peer-addr> - -Set the address you want to connect (Useful if hostname is only a vhost. no ip resolve) - -=item B<--port> - -Port used by OpenStack Keystone's API (Default: '5000') - -=item B<--proto> - -Specify https if needed (Default: 'http') - -=item B<--urlpath> - -Set path to get API's Token (Default: '/v3/auth/tokens') - -=item B<--proxyurl> - -Proxy URL - -=item B<--proxypac> - -Proxy pac file (can be an url or local file) - -=item B<--credentials> - -Specify this option if you access webpage over basic authentification - -=item B<--username> - -Specify username - -=item B<--password> - -Specify password - -=item B<--ssl> - -Specify SSL version (example : 'sslv3', 'tlsv1'...) - -=item B<--header> - -Set HTTP headers (Multiple option. Example: --header='Content-Type: xxxxx') - -=item B<--timeout> - -Threshold for HTTP timeout (Default: 3) - -=item B<--threshold-overload> - -Set to overload default threshold values (syntax: section,status,regexp) -It used before default thresholds (order stays). -Example: --threshold-overload='status,WARNING,^down$)' - -=back - -OPENSTACK OPTIONS: - -=over 8 - -=item B<--tenant-id> - -Set Tenant's ID - -=item B<--hypervisor-id> - -Set Hypervisor's ID - -=back - -=cut diff --git a/cloud/openstack/restapi/mode/instance.pm b/cloud/openstack/restapi/mode/instance.pm deleted file mode 100644 index 6cd5b5bc6..000000000 --- a/cloud/openstack/restapi/mode/instance.pm +++ /dev/null @@ -1,311 +0,0 @@ -# -# Copyright 2015 Centreon (http://www.centreon.com/) -# -# Centreon is a full-fledged industry-strength solution that meets -# the needs in IT infrastructure and application monitoring for -# service performance. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -package cloud::openstack::restapi::mode::instance; - -use base qw(centreon::plugins::mode); - -use strict; -use warnings; -use centreon::plugins::http; -use JSON; - -my $thresholds = { - status => [ - ['ACTIVE', 'OK'], - ['PAUSED', 'WARNING'], - ['SUSPENDED', 'WARNING'], - ['SHUTOFF', 'CRITICAL'], - ['REBUILD', 'WARNING'], - ['HARD_REBOOT', 'WARNING'], - ['ERROR', 'CRITCAL'], - ['BUILDING', 'OK'], - ['STOPPED', 'WARNING'], - ['DELETED', 'OK'], - ], -}; - -sub new { - my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); - bless $self, $class; - - $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "data:s" => { name => 'data' }, - "hostname:s" => { name => 'hostname' }, - "http-peer-addr:s" => { name => 'http_peer_addr' }, - "port:s" => { name => 'port', default => '5000' }, - "proto:s" => { name => 'proto' }, - "urlpath:s" => { name => 'url_path', default => '/v3/auth/tokens' }, - "proxyurl:s" => { name => 'proxyurl' }, - "proxypac:s" => { name => 'proxypac' }, - "credentials" => { name => 'credentials' }, - "username:s" => { name => 'username' }, - "password:s" => { name => 'password' }, - "ssl:s" => { name => 'ssl', }, - "header:s@" => { name => 'header' }, - "timeout:s" => { name => 'timeout' }, - "tenant-id:s" => { name => 'tenant_id' }, - "instance-id:s" => { name => 'instance_id' }, - "threshold-overload:s@" => { name => 'threshold_overload' }, - }); - - $self->{http} = centreon::plugins::http->new(output => $self->{output}); - $self->{instance_infos} = (); - return $self; -} - -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - $self->{overload_th} = {}; - foreach my $val (@{$self->{option_results}->{threshold_overload}}) { - if ($val !~ /^(.*?),(.*?),(.*)$/) { - $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload option '" . $val . "'."); - $self->{output}->option_exit(); - } - my ($section, $status, $filter) = ($1, $2, $3); - if ($self->{output}->is_litteral_status(status => $status) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload status '" . $val . "'."); - $self->{output}->option_exit(); - } - $self->{overload_th}->{$section} = [] if (!defined($self->{overload_th}->{$section})); - push @{$self->{overload_th}->{$section}}, {filter => $filter, status => $status}; - } - - if (!defined($self->{option_results}->{header}) || $self->{option_results}->{header} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --header option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{data}) || $self->{option_results}->{data} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --data option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{hostname}) || $self->{option_results}->{hostname} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --hostname option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{tenant_id}) || $self->{option_results}->{tenant_id} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --tenant-id option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{instance_id}) || $self->{option_results}->{instance_id} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --instance-id option."); - $self->{output}->option_exit(); - } - - $self->{http}->set_options(%{$self->{option_results}}) -} - -sub token_request { - my ($self, %options) = @_; - - $self->{method} = 'GET'; - if (defined($self->{option_results}->{data})) { - local $/ = undef; - if (!open(FILE, "<", $self->{option_results}->{data})) { - $self->{output}->output_add(severity => 'UNKNOWN', - short_msg => sprintf("Could not read file '%s': %s", $self->{option_results}->{data}, $!)); - $self->{output}->display(); - $self->{output}->exit(); - } - $self->{json_request} = ; - close FILE; - $self->{method} = 'POST'; - } - - my $response = $self->{http}->request(method => $self->{method}, query_form_post => $self->{json_request}); - my $headers = $self->{http}->get_header(); - - eval { - $self->{header} = $headers->header('X-Subject-Token'); - }; - - if ($@) { - $self->{output}->add_option_msg(short_msg => "Cannot retrieve API Token"); - $self->{output}->option_exit(); - } -} - -sub api_request { - my ($self, %options) = @_; - - $self->{method} = 'GET'; - $self->{option_results}->{url_path} = "/v2/".$self->{option_results}->{tenant_id}."/servers/".$self->{option_results}->{instance_id}; - $self->{option_results}->{port} = '8774'; - @{$self->{option_results}->{header}} = ('X-Auth-Token:' . $self->{header}, 'Accept:application/json'); - $self->{http}->set_options(%{$self->{option_results}}); - - my $webcontent; - my $jsoncontent = $self->{http}->request(method => $self->{method}); - - my $json = JSON->new; - - eval { - $webcontent = $json->decode($jsoncontent); - }; - - $self->{instance_infos}->{name} = $webcontent->{server}->{name}; - $self->{instance_infos}->{vm_state} = $webcontent->{server}->{'OS-EXT-STS:vm_state'}; - $self->{instance_infos}->{state} = $webcontent->{server}->{status}; -} - - -sub get_severity { - my ($self, %options) = @_; - my $status = 'UNKNOWN'; # default - - if (defined($self->{overload_th}->{$options{section}})) { - foreach (@{$self->{overload_th}->{$options{section}}}) { - if ($options{value} =~ /$_->{filter}/i) { - $status = $_->{status}; - return $status; - } - } - } - foreach (@{$thresholds->{$options{section}}}) { - if ($options{value} =~ /$$_[0]/i) { - $status = $$_[1]; - return $status; - } - } - - return $status; -} - -sub run { - my ($self, %options) = @_; - - $self->token_request(); - $self->api_request(); - - my $exit = $self->get_severity(section => 'status', value => $self->{instance_infos}->{state}); - $self->{output}->output_add(severity => $exit, - short_msg => sprintf("Instance %s is in %s state (vm_state: %s)", - $self->{instance_infos}->{name}, - $self->{instance_infos}->{state}, - $self->{instance_infos}->{vm_state})); - - $self->{output}->display(); - $self->{output}->exit(); - - exit 0; -} - -1; - -__END__ - -=head1 MODE - -Monitor instance status through Compute API V2 - -JSON OPTIONS: - -=over 8 - -=item B<--data> - -Set file with JSON request - -=back - -HTTP OPTIONS: - -=over 8 - -=item B<--hostname> - -IP Addr/FQDN of OpenStack Compute's API - -=item B<--http-peer-addr> - -Set the address you want to connect (Useful if hostname is only a vhost. no ip resolve) - -=item B<--port> - -Port used by OpenStack Keystone's API (Default: '5000') - -=item B<--proto> - -Specify https if needed (Default: 'http') - -=item B<--urlpath> - -Set path to get API's Token (Default: '/v3/auth/tokens') - -=item B<--proxyurl> - -Proxy URL - -=item B<--proxypac> - -Proxy pac file (can be an url or local file) - -=item B<--credentials> - -Specify this option if you access webpage over basic authentification - -=item B<--username> - -Specify username - -=item B<--password> - -Specify password - -=item B<--ssl> - -Specify SSL version (example : 'sslv3', 'tlsv1'...) - -=item B<--header> - -Set HTTP headers (Multiple option. Example: --header='Content-Type: xxxxx') - -=item B<--timeout> - -Threshold for HTTP timeout (Default: 3) - -=item B<--threshold-overload> - -Set to overload default threshold values (syntax: section,status,regexp) -It used before default thresholds (order stays). -Example: --threshold-overload='status,CRITICAL,^STOPPED$)' - -=back - -OPENSTACK OPTIONS: - -=over 8 - -=item B<--tenant-id> - -Set Tenant's ID - -=item B<--instance-id> - -Set Instance's ID - -=back - -=cut diff --git a/cloud/openstack/restapi/mode/listhypervisors.pm b/cloud/openstack/restapi/mode/listhypervisors.pm deleted file mode 100644 index f6763f998..000000000 --- a/cloud/openstack/restapi/mode/listhypervisors.pm +++ /dev/null @@ -1,292 +0,0 @@ -# -# Copyright 2015 Centreon (http://www.centreon.com/) -# -# Centreon is a full-fledged industry-strength solution that meets -# the needs in IT infrastructure and application monitoring for -# service performance. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -package cloud::openstack::restapi::mode::listhypervisors; - -use base qw(centreon::plugins::mode); - -use strict; -use warnings; -use centreon::plugins::http; -use JSON; - -sub new { - my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); - bless $self, $class; - - $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "data:s" => { name => 'data' }, - "hostname:s" => { name => 'hostname' }, - "http-peer-addr:s" => { name => 'http_peer_addr' }, - "port:s" => { name => 'port', default => '5000' }, - "proto:s" => { name => 'proto' }, - "urlpath:s" => { name => 'url_path', default => '/v3/auth/tokens' }, - "proxyurl:s" => { name => 'proxyurl' }, - "proxypac:s" => { name => 'proxypac' }, - "credentials" => { name => 'credentials' }, - "username:s" => { name => 'username' }, - "password:s" => { name => 'password' }, - "ssl:s" => { name => 'ssl', }, - "header:s@" => { name => 'header' }, - "exclude:s" => { name => 'exclude' }, - "timeout:s" => { name => 'timeout' }, - "tenant-id:s" => { name => 'tenant_id' }, - }); - - $self->{http} = centreon::plugins::http->new(output => $self->{output}); - $self->{hypervisor_infos} = (); - return $self; -} - -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - if (!defined($self->{option_results}->{header}) || $self->{option_results}->{header} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --header option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{data}) || $self->{option_results}->{data} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --data option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{hostname}) || $self->{option_results}->{hostname} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --hostname option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{tenant_id}) || $self->{option_results}->{tenant_id} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --tenant-id option."); - $self->{output}->option_exit(); - } - - $self->{http}->set_options(%{$self->{option_results}}) -} - -sub check_exclude { - my ($self, %options) = @_; - - if (defined($self->{option_results}->{exclude}) && $self->{option_results}->{exclude} =~ /(^|\s|,)${options{status}}(\s|,|$)/) { - $self->{output}->output_add(long_msg => sprintf("Skipping ${options{status}} instance.")); - return 1; - } - return 0; -} - -sub token_request { - my ($self, %options) = @_; - - $self->{method} = 'GET'; - if (defined($self->{option_results}->{data})) { - local $/ = undef; - if (!open(FILE, "<", $self->{option_results}->{data})) { - $self->{output}->output_add(severity => 'UNKNOWN', - short_msg => sprintf("Could not read file '%s': %s", $self->{option_results}->{data}, $!)); - $self->{output}->display(); - $self->{output}->exit(); - } - $self->{json_request} = ; - close FILE; - $self->{method} = 'POST'; - } - - my $response = $self->{http}->request(method => $self->{method}, query_form_post => $self->{json_request}); - my $headers = $self->{http}->get_header(); - - eval { - $self->{header} = $headers->header('X-Subject-Token'); - }; - - if ($@) { - $self->{output}->add_option_msg(short_msg => "Cannot retrieve API Token"); - $self->{output}->option_exit(); - } -} - -sub api_request { - my ($self, %options) = @_; - - $self->{method} = 'GET'; - $self->{option_results}->{url_path} = "/v2/".$self->{option_results}->{tenant_id}."/os-hypervisors/detail"; - $self->{option_results}->{port} = '8774'; - @{$self->{option_results}->{header}} = ('X-Auth-Token:' . $self->{header}, 'Accept:application/json'); - $self->{http}->set_options(%{$self->{option_results}}); - - my $webcontent; - my $jsoncontent = $self->{http}->request(method => $self->{method}); - - my $json = JSON->new; - - eval { - $webcontent = $json->decode($jsoncontent); - }; - - foreach my $val (@{$webcontent->{hypervisors}}) { - my $hypervisorname = $val->{hypervisor_hostname}; - $self->{hypervisor_infos}->{$hypervisorname}->{id} = $val->{id}; - $self->{hypervisor_infos}->{$hypervisorname}->{ipaddress} = $val->{host_ip}; - $self->{hypervisor_infos}->{$hypervisorname}->{type} = $val->{hypervisor_type}; - $self->{hypervisor_infos}->{$hypervisorname}->{status} = $val->{status}; - $self->{hypervisor_infos}->{$hypervisorname}->{state} = $val->{state}; - } -} - -sub disco_format { - my ($self, %options) = @_; - - my $names = ['name', 'ip', 'type', 'status', 'state']; - $self->{output}->add_disco_format(elements => $names); -} - -sub disco_show { - my ($self, %options) = @_; - - $self->token_request(); - $self->api_request(); - - foreach my $hypervisorname (keys %{$self->{hypervisor_infos}}) { - $self->{output}->add_disco_entry(name => $hypervisorname, - id => $self->{hypervisor_infos}->{$hypervisorname}->{id}, - ip => $self->{hypervisor_infos}->{$hypervisorname}->{ipaddress}, - type => $self->{hypervisor_infos}->{$hypervisorname}->{type}, - status => $self->{hypervisor_infos}->{$hypervisorname}->{status}, - state => $self->{hypervisor_infos}->{$hypervisorname}->{state}, - ); - } -} - -sub run { - my ($self, %options) = @_; - - $self->token_request(); - $self->api_request(); - - foreach my $hypervisorname (keys %{$self->{hypervisor_infos}}) { - $self->{output}->output_add(long_msg => sprintf("%s [id = %s, ip = %s, type = %s, status = %s, state = %s]", - $hypervisorname, - $self->{hypervisor_infos}->{$hypervisorname}->{id}, - $self->{hypervisor_infos}->{$hypervisorname}->{ipaddress}, - $self->{hypervisor_infos}->{$hypervisorname}->{type}, - $self->{hypervisor_infos}->{$hypervisorname}->{status}, - $self->{hypervisor_infos}->{$hypervisorname}->{state},)); - } - - $self->{output}->output_add(severity => 'OK', - short_msg => 'List hypervisors:'); - - $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); - $self->{output}->exit(); - - exit 0; -} - -1; - -__END__ - -=head1 MODE - -List OpenStack hypervisors through Compute API V2 - -JSON OPTIONS: - -=over 8 - -=item B<--data> - -Set file with JSON request - -=back - -HTTP OPTIONS: - -=over 8 - -=item B<--hostname> - -IP Addr/FQDN of OpenStack Compute's API - -=item B<--http-peer-addr> - -Set the address you want to connect (Useful if hostname is only a vhost. no ip resolve) - -=item B<--port> - -Port used by OpenStack Keystone's API (Default: '5000') - -=item B<--proto> - -Specify https if needed (Default: 'http') - -=item B<--urlpath> - -Set path to get API's Token (Default: '/v3/auth/tokens') - -=item B<--proxyurl> - -Proxy URL - -=item B<--proxypac> - -Proxy pac file (can be an url or local file) - -=item B<--credentials> - -Specify this option if you access webpage over basic authentification - -=item B<--username> - -Specify username - -=item B<--password> - -Specify password - -=item B<--ssl> - -Specify SSL version (example : 'sslv3', 'tlsv1'...) - -=item B<--header> - -Set HTTP headers (Multiple option. Example: --header='Content-Type: xxxxx') - -=item B<--exlude> - -Exclude specific instance's state (comma seperated list) (Example: --exclude=disabled) - -=item B<--timeout> - -Threshold for HTTP timeout (Default: 3) - -=back - -OPENSTACK OPTIONS: - -=over 8 - -=item B<--tenant-id> - -Set Tenant's ID - -=back - -=cut diff --git a/cloud/openstack/restapi/mode/listinstances.pm b/cloud/openstack/restapi/mode/listinstances.pm deleted file mode 100644 index 323589ad7..000000000 --- a/cloud/openstack/restapi/mode/listinstances.pm +++ /dev/null @@ -1,305 +0,0 @@ -# -# Copyright 2015 Centreon (http://www.centreon.com/) -# -# Centreon is a full-fledged industry-strength solution that meets -# the needs in IT infrastructure and application monitoring for -# service performance. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -package cloud::openstack::restapi::mode::listinstances; - -use base qw(centreon::plugins::mode); - -use strict; -use warnings; -use centreon::plugins::http; -use JSON; - -sub new { - my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); - bless $self, $class; - - $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "data:s" => { name => 'data' }, - "hostname:s" => { name => 'hostname' }, - "http-peer-addr:s" => { name => 'http_peer_addr' }, - "port:s" => { name => 'port', default => '5000' }, - "proto:s" => { name => 'proto' }, - "urlpath:s" => { name => 'url_path', default => '/v3/auth/tokens' }, - "proxyurl:s" => { name => 'proxyurl' }, - "proxypac:s" => { name => 'proxypac' }, - "credentials" => { name => 'credentials' }, - "username:s" => { name => 'username' }, - "password:s" => { name => 'password' }, - "ssl:s" => { name => 'ssl', }, - "header:s@" => { name => 'header' }, - "exclude:s" => { name => 'exclude' }, - "timeout:s" => { name => 'timeout' }, - "tenant-id:s" => { name => 'tenant_id' }, - }); - - $self->{http} = centreon::plugins::http->new(output => $self->{output}); - $self->{instance_infos} = (); - return $self; -} - -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - if (!defined($self->{option_results}->{header}) || $self->{option_results}->{header} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --header option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{data}) || $self->{option_results}->{data} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --data option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{hostname}) || $self->{option_results}->{hostname} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --hostname option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{tenant_id}) || $self->{option_results}->{tenant_id} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --tenant-id option."); - $self->{output}->option_exit(); - } - - $self->{http}->set_options(%{$self->{option_results}}) -} - -sub check_exclude { - my ($self, %options) = @_; - - if (defined($self->{option_results}->{exclude}) && $self->{option_results}->{exclude} =~ /(^|\s|,)${options{status}}(\s|,|$)/) { - $self->{output}->output_add(long_msg => sprintf("Skipping ${options{status}} instance.")); - return 1; - } - return 0; -} - -sub token_request { - my ($self, %options) = @_; - - $self->{method} = 'GET'; - if (defined($self->{option_results}->{data})) { - local $/ = undef; - if (!open(FILE, "<", $self->{option_results}->{data})) { - $self->{output}->output_add(severity => 'UNKNOWN', - short_msg => sprintf("Could not read file '%s': %s", $self->{option_results}->{data}, $!)); - $self->{output}->display(); - $self->{output}->exit(); - } - $self->{json_request} = ; - close FILE; - $self->{method} = 'POST'; - } - - my $response = $self->{http}->request(method => $self->{method}, query_form_post => $self->{json_request}); - my $headers = $self->{http}->get_header(); - eval { - $self->{header} = $headers->header('X-Subject-Token'); - }; - - if ($@) { - $self->{output}->add_option_msg(short_msg => "Cannot retrieve API Token"); - $self->{output}->option_exit(); - } -} - -sub api_request { - my ($self, %options) = @_; - - $self->{method} = 'GET'; - $self->{option_results}->{url_path} = "/v2/".$self->{option_results}->{tenant_id}."/servers/detail"; - $self->{option_results}->{port} = '8774'; - @{$self->{option_results}->{header}} = ('X-Auth-Token:' . $self->{header}, 'Accept:application/json'); - $self->{http}->set_options(%{$self->{option_results}}); - - my $webcontent; - my $jsoncontent = $self->{http}->request(method => $self->{method}); - - my $json = JSON->new; - - eval { - $webcontent = $json->decode($jsoncontent); - }; - - foreach my $val (@{$webcontent->{servers}}) { - my $instancestate; - if ($val->{status} eq "ACTIVE") { - next if ($self->check_exclude(status => 'Running')); - $instancestate = $val->{status}; - } elsif ($val->{status} eq "SUSPENDED" || $val->{status} eq "PAUSED") { - next if ($self->check_exclude(status => 'Paused')); - $instancestate = $val->{status}; - } elsif ($val->{status} eq "SHUTOFF") { - next if ($self->check_exclude(status => 'Off')); - $instancestate = $val->{status}; - } elsif ($val->{status} eq "REBUILD" || $val->{status} eq "HARD_REBOOT") { - next if ($self->check_exclude(status => 'Reboot')); - $instancestate = $val->{status}; - } - my $instancename = $val->{name}; - $self->{instance_infos}->{$instancename}->{id} = $val->{id}; - $self->{instance_infos}->{$instancename}->{zone} = $val->{'OS-EXT-AZ:availability_zone'}; - $self->{instance_infos}->{$instancename}->{compute} = $val->{'OS-EXT-SRV-ATTR:host'}; - $self->{instance_infos}->{$instancename}->{osname} = $val->{'OS-EXT-SRV-ATTR:instance_name'}; - $self->{instance_infos}->{$instancename}->{state} = $instancestate; - } -} - -sub disco_format { - my ($self, %options) = @_; - - my $names = ['name', 'id', 'zone', 'compute', 'osname', 'state']; - $self->{output}->add_disco_format(elements => $names); -} - -sub disco_show { - my ($self, %options) = @_; - - $self->token_request(); - $self->api_request(); - - foreach my $instancename (keys %{$self->{instance_infos}}) { - $self->{output}->add_disco_entry(name => $instancename, - id => $self->{instance_infos}->{$instancename}->{id}, - zone => $self->{instance_infos}->{$instancename}->{zone}, - compute => $self->{instance_infos}->{$instancename}->{compute}, - osname => $self->{instance_infos}->{$instancename}->{osname}, - state => $self->{instance_infos}->{$instancename}->{state}, - ); - } -} - -sub run { - my ($self, %options) = @_; - - $self->token_request(); - $self->api_request(); - - foreach my $instancename (keys %{$self->{instance_infos}}) { - $self->{output}->output_add(long_msg => sprintf("%s [id = %s, zone = %s, compute = %s, osname = %s, state = %s]", - $instancename, - $self->{instance_infos}->{$instancename}->{id}, - $self->{instance_infos}->{$instancename}->{zone}, - $self->{instance_infos}->{$instancename}->{compute}, - $self->{instance_infos}->{$instancename}->{osname}, - $self->{instance_infos}->{$instancename}->{state})); - } - - $self->{output}->output_add(severity => 'OK', - short_msg => 'List instances:'); - - $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); - $self->{output}->exit(); - - exit 0; -} - -1; - -__END__ - -=head1 MODE - -List OpenStack instances through Compute API V2 - -JSON OPTIONS: - -=over 8 - -=item B<--data> - -Set file with JSON request - -=back - -HTTP OPTIONS: - -=over 8 - -=item B<--hostname> - -IP Addr/FQDN of OpenStack Compute's API - -=item B<--http-peer-addr> - -Set the address you want to connect (Useful if hostname is only a vhost. no ip resolve) - -=item B<--port> - -Port used by OpenStack Keystone's API (Default: '5000') - -=item B<--proto> - -Specify https if needed (Default: 'http') - -=item B<--urlpath> - -Set path to get API's Token (Default: '/v3/auth/tokens') - -=item B<--proxyurl> - -Proxy URL - -=item B<--proxypac> - -Proxy pac file (can be an url or local file) - -=item B<--credentials> - -Specify this option if you access webpage over basic authentification - -=item B<--username> - -Specify username - -=item B<--password> - -Specify password - -=item B<--ssl> - -Specify SSL version (example : 'sslv3', 'tlsv1'...) - -=item B<--header> - -Set HTTP headers (Multiple option. Example: --header='Content-Type: xxxxx') - -=item B<--exlude> - -Exclude specific instance's state (comma seperated list) (Example: --exclude=Paused,Running,Off,Exited) - -=item B<--timeout> - -Threshold for HTTP timeout (Default: 3) - -=back - -OPENSTACK OPTIONS: - -=over 8 - -=item B<--tenant-id> - -Set Tenant's ID - -=back - -=cut diff --git a/cloud/openstack/restapi/mode/listnetworks.pm b/cloud/openstack/restapi/mode/listnetworks.pm deleted file mode 100644 index 162f0f6b1..000000000 --- a/cloud/openstack/restapi/mode/listnetworks.pm +++ /dev/null @@ -1,273 +0,0 @@ -# -# Copyright 2015 Centreon (http://www.centreon.com/) -# -# Centreon is a full-fledged industry-strength solution that meets -# the needs in IT infrastructure and application monitoring for -# service performance. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -package cloud::openstack::restapi::mode::listnetworks; - -use base qw(centreon::plugins::mode); - -use strict; -use warnings; -use centreon::plugins::http; -use JSON; - -sub new { - my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); - bless $self, $class; - - $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "data:s" => { name => 'data' }, - "hostname:s" => { name => 'hostname' }, - "http-peer-addr:s" => { name => 'http_peer_addr' }, - "port:s" => { name => 'port', default => '5000' }, - "proto:s" => { name => 'proto' }, - "urlpath:s" => { name => 'url_path', default => '/v3/auth/tokens' }, - "proxyurl:s" => { name => 'proxyurl' }, - "proxypac:s" => { name => 'proxypac' }, - "credentials" => { name => 'credentials' }, - "username:s" => { name => 'username' }, - "password:s" => { name => 'password' }, - "ssl:s" => { name => 'ssl', }, - "header:s@" => { name => 'header' }, - "exclude:s" => { name => 'exclude' }, - "timeout:s" => { name => 'timeout' }, - }); - - $self->{http} = centreon::plugins::http->new(output => $self->{output}); - $self->{networks_infos} = (); - return $self; -} - -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - if (!defined($self->{option_results}->{header}) || $self->{option_results}->{header} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --header option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{data}) || $self->{option_results}->{data} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --data option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{hostname}) || $self->{option_results}->{hostname} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --hostname option."); - $self->{output}->option_exit(); - } - - $self->{http}->set_options(%{$self->{option_results}}) -} - -sub check_exclude { - my ($self, %options) = @_; - - if (defined($self->{option_results}->{exclude}) && $self->{option_results}->{exclude} =~ /(^|\s|,)${options{status}}(\s|,|$)/) { - $self->{output}->output_add(long_msg => sprintf("Skipping ${options{status}} instance.")); - return 1; - } - return 0; -} - -sub token_request { - my ($self, %options) = @_; - - $self->{method} = 'GET'; - if (defined($self->{option_results}->{data})) { - local $/ = undef; - if (!open(FILE, "<", $self->{option_results}->{data})) { - $self->{output}->output_add(severity => 'UNKNOWN', - short_msg => sprintf("Could not read file '%s': %s", $self->{option_results}->{data}, $!)); - $self->{output}->display(); - $self->{output}->exit(); - } - $self->{json_request} = ; - close FILE; - $self->{method} = 'POST'; - } - - my $response = $self->{http}->request(method => $self->{method}, query_form_post => $self->{json_request}); - my $headers = $self->{http}->get_header(); - eval { - $self->{header} = $headers->header('X-Subject-Token'); - }; - - if ($@) { - $self->{output}->add_option_msg(short_msg => "Cannot retrieve API Token"); - $self->{output}->option_exit(); - } -} - -sub api_request { - my ($self, %options) = @_; - - $self->{method} = 'GET'; - $self->{option_results}->{url_path} = "/v2.0/networks"; - $self->{option_results}->{port} = '9696'; - @{$self->{option_results}->{header}} = ('X-Auth-Token:' . $self->{header}, 'Accept:application/json'); - $self->{http}->set_options(%{$self->{option_results}}); - - my $webcontent; - my $jsoncontent = $self->{http}->request(method => $self->{method}); - - my $json = JSON->new; - - eval { - $webcontent = $json->decode($jsoncontent); - }; - - foreach my $val (@{$webcontent->{networks}}) { - my $networkname = $val->{name}; - $self->{networks_infos}->{$networkname}->{id} = $val->{id}; - $self->{networks_infos}->{$networkname}->{tenant} = $val->{tenant_id}; - $self->{networks_infos}->{$networkname}->{state} = $val->{status}; - $self->{networks_infos}->{$networkname}->{admin_state} = $val->{admin_state_up}; - } -} - -sub disco_format { - my ($self, %options) = @_; - - my $names = ['name', 'id', 'tenant', 'state', 'admin_state']; - $self->{output}->add_disco_format(elements => $names); -} - -sub disco_show { - my ($self, %options) = @_; - - $self->token_request(); - $self->api_request(); - - foreach my $networkname (keys %{$self->{networks_infos}}) { - $self->{output}->add_disco_entry(name => $networkname, - id => $self->{networks_infos}->{$networkname}->{id}, - tenant => $self->{networks_infos}->{$networkname}->{tenant}, - state => $self->{networks_infos}->{$networkname}->{state}, - admin_state => $self->{networks_infos}->{$networkname}->{admin_state}, - ); - } -} - -sub run { - my ($self, %options) = @_; - - $self->token_request(); - $self->api_request(); - - foreach my $networkname (keys %{$self->{networks_infos}}) { - $self->{output}->output_add(long_msg => sprintf("%s [id = %s, tenant = %s, state = %s, admin_state = %s]", - $networkname, - $self->{networks_infos}->{$networkname}->{id}, - $self->{networks_infos}->{$networkname}->{tenant}, - $self->{networks_infos}->{$networkname}->{state}, - $self->{networks_infos}->{$networkname}->{admin_state})); - } - - $self->{output}->output_add(severity => 'OK', - short_msg => 'List networks:'); - - $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); - $self->{output}->exit(); - - exit 0; -} - -1; - -__END__ - -=head1 MODE - -List OpenStack networks through Networking API V2.0 - -JSON OPTIONS: - -=over 8 - -=item B<--data> - -Set file with JSON request - -=back - -HTTP OPTIONS: - -=over 8 - -=item B<--hostname> - -IP Addr/FQDN of OpenStack Compute's API - -=item B<--http-peer-addr> - -Set the address you want to connect (Useful if hostname is only a vhost. no ip resolve) - -=item B<--port> - -Port used by OpenStack Keystone's API (Default: '5000') - -=item B<--proto> - -Specify https if needed (Default: 'http') - -=item B<--urlpath> - -Set path to get API's Token (Default: '/v3/auth/tokens') - -=item B<--proxyurl> - -Proxy URL - -=item B<--proxypac> - -Proxy pac file (can be an url or local file) - -=item B<--credentials> - -Specify this option if you access webpage over basic authentification - -=item B<--username> - -Specify username - -=item B<--password> - -Specify password - -=item B<--ssl> - -Specify SSL version (example : 'sslv3', 'tlsv1'...) - -=item B<--header> - -Set HTTP headers (Multiple option. Example: --header='Content-Type: xxxxx') - -=item B<--exlude> - -Exclude specific instance's state (comma seperated list) (Example: --exclude=ACTIVE) - -=item B<--timeout> - -Threshold for HTTP timeout (Default: 3) - -=back - -=cut diff --git a/cloud/openstack/restapi/mode/listvolumes.pm b/cloud/openstack/restapi/mode/listvolumes.pm deleted file mode 100644 index 1ad82ef90..000000000 --- a/cloud/openstack/restapi/mode/listvolumes.pm +++ /dev/null @@ -1,291 +0,0 @@ -# -# Copyright 2015 Centreon (http://www.centreon.com/) -# -# Centreon is a full-fledged industry-strength solution that meets -# the needs in IT infrastructure and application monitoring for -# service performance. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -package cloud::openstack::restapi::mode::listvolumes; - -use base qw(centreon::plugins::mode); - -use strict; -use warnings; -use centreon::plugins::http; -use JSON; - -sub new { - my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); - bless $self, $class; - - $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "data:s" => { name => 'data' }, - "hostname:s" => { name => 'hostname' }, - "http-peer-addr:s" => { name => 'http_peer_addr' }, - "port:s" => { name => 'port', default => '5000' }, - "proto:s" => { name => 'proto' }, - "urlpath:s" => { name => 'url_path', default => '/v3/auth/tokens' }, - "proxyurl:s" => { name => 'proxyurl' }, - "proxypac:s" => { name => 'proxypac' }, - "credentials" => { name => 'credentials' }, - "username:s" => { name => 'username' }, - "password:s" => { name => 'password' }, - "ssl:s" => { name => 'ssl', }, - "header:s@" => { name => 'header' }, - "exclude:s" => { name => 'exclude' }, - "timeout:s" => { name => 'timeout' }, - "tenant-id:s" => { name => 'tenant_id' }, - }); - - $self->{http} = centreon::plugins::http->new(output => $self->{output}); - $self->{volumes_infos} = (); - return $self; -} - -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - if (!defined($self->{option_results}->{header}) || $self->{option_results}->{header} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --header option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{data}) || $self->{option_results}->{data} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --data option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{hostname}) || $self->{option_results}->{hostname} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --hostname option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{tenant_id}) || $self->{option_results}->{tenant_id} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --tenant-id option."); - $self->{output}->option_exit(); - } - - $self->{http}->set_options(%{$self->{option_results}}) -} - -sub check_exclude { - my ($self, %options) = @_; - - if (defined($self->{option_results}->{exclude}) && $self->{option_results}->{exclude} =~ /(^|\s|,)${options{status}}(\s|,|$)/) { - $self->{output}->output_add(long_msg => sprintf("Skipping ${options{status}} instance.")); - return 1; - } - return 0; -} - -sub token_request { - my ($self, %options) = @_; - - $self->{method} = 'GET'; - if (defined($self->{option_results}->{data})) { - local $/ = undef; - if (!open(FILE, "<", $self->{option_results}->{data})) { - $self->{output}->output_add(severity => 'UNKNOWN', - short_msg => sprintf("Could not read file '%s': %s", $self->{option_results}->{data}, $!)); - $self->{output}->display(); - $self->{output}->exit(); - } - $self->{json_request} = ; - close FILE; - $self->{method} = 'POST'; - } - - my $response = $self->{http}->request(method => $self->{method}, query_form_post => $self->{json_request}); - my $headers = $self->{http}->get_header(); - eval { - $self->{header} = $headers->header('X-Subject-Token'); - }; - - if ($@) { - $self->{output}->add_option_msg(short_msg => "Cannot retrieve API Token"); - $self->{output}->option_exit(); - } -} - -sub api_request { - my ($self, %options) = @_; - - $self->{method} = 'GET'; - $self->{option_results}->{url_path} = "/v2/".$self->{option_results}->{tenant_id}."/volumes/detail"; - $self->{option_results}->{port} = '8776'; - @{$self->{option_results}->{header}} = ('X-Auth-Token:' . $self->{header}, 'Accept:application/json'); - $self->{http}->set_options(%{$self->{option_results}}); - - my $webcontent; - my $jsoncontent = $self->{http}->request(method => $self->{method}); - - my $json = JSON->new; - - eval { - $webcontent = $json->decode($jsoncontent); - }; - - foreach my $val (@{$webcontent->{volumes}}) { - my $volumename = $val->{name}; - $self->{volumes_infos}->{$volumename}->{id} = $val->{id}; - $self->{volumes_infos}->{$volumename}->{zone} = $val->{availability_zone}; - $self->{volumes_infos}->{$volumename}->{size} = $val->{size}; - $self->{volumes_infos}->{$volumename}->{type} = $val->{volume_type}; - $self->{volumes_infos}->{$volumename}->{state} = $val->{status}; - } -} - -sub disco_format { - my ($self, %options) = @_; - - my $names = ['name', 'id', 'zone', 'type', 'size', 'state']; - $self->{output}->add_disco_format(elements => $names); -} - -sub disco_show { - my ($self, %options) = @_; - - $self->token_request(); - $self->api_request(); - - foreach my $volumename (keys %{$self->{volumes_infos}}) { - $self->{output}->add_disco_entry(name => $volumename, - id => $self->{volumes_infos}->{$volumename}->{id}, - zone => $self->{volumes_infos}->{$volumename}->{zone}, - size => $self->{volumes_infos}->{$volumename}->{size}."Gb", - type => $self->{volumes_infos}->{$volumename}->{type}, - state => $self->{volumes_infos}->{$volumename}->{state}, - ); - } -} - -sub run { - my ($self, %options) = @_; - - $self->token_request(); - $self->api_request(); - - foreach my $volumename (keys %{$self->{volumes_infos}}) { - $self->{output}->output_add(long_msg => sprintf("%s [id = %s, zone = %s, size = %sGb, type = %s, state = %s]", - $volumename, - $self->{volumes_infos}->{$volumename}->{id}, - $self->{volumes_infos}->{$volumename}->{zone}, - $self->{volumes_infos}->{$volumename}->{size}, - $self->{volumes_infos}->{$volumename}->{type}, - $self->{volumes_infos}->{$volumename}->{state})); - } - - $self->{output}->output_add(severity => 'OK', - short_msg => 'List volumes:'); - - $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); - $self->{output}->exit(); - - exit 0; -} - -1; - -__END__ - -=head1 MODE - -List OpenStack volumes through Block Storage API V2 - -JSON OPTIONS: - -=over 8 - -=item B<--data> - -Set file with JSON request - -=back - -HTTP OPTIONS: - -=over 8 - -=item B<--hostname> - -IP Addr/FQDN of OpenStack Compute's API - -=item B<--http-peer-addr> - -Set the address you want to connect (Useful if hostname is only a vhost. no ip resolve) - -=item B<--port> - -Port used by OpenStack Keystone's API (Default: '5000') - -=item B<--proto> - -Specify https if needed (Default: 'http') - -=item B<--urlpath> - -Set path to get API's Token (Default: '/v3/auth/tokens') - -=item B<--proxyurl> - -Proxy URL - -=item B<--proxypac> - -Proxy pac file (can be an url or local file) - -=item B<--credentials> - -Specify this option if you access webpage over basic authentification - -=item B<--username> - -Specify username - -=item B<--password> - -Specify password - -=item B<--ssl> - -Specify SSL version (example : 'sslv3', 'tlsv1'...) - -=item B<--header> - -Set HTTP headers (Multiple option. Example: --header='Content-Type: xxxxx') - -=item B<--exlude> - -Exclude specific instance's state (comma seperated list) (Example: --exclude=Paused,Running,Off,Exited) - -=item B<--timeout> - -Threshold for HTTP timeout (Default: 3) - -=back - -OPENSTACK OPTIONS: - -=over 8 - -=item B<--tenant-id> - -Set Tenant's ID - -=back - -=cut diff --git a/cloud/openstack/restapi/mode/network.pm b/cloud/openstack/restapi/mode/network.pm deleted file mode 100644 index f1b4ba91c..000000000 --- a/cloud/openstack/restapi/mode/network.pm +++ /dev/null @@ -1,296 +0,0 @@ -# -# Copyright 2015 Centreon (http://www.centreon.com/) -# -# Centreon is a full-fledged industry-strength solution that meets -# the needs in IT infrastructure and application monitoring for -# service performance. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -package cloud::openstack::restapi::mode::network; - -use base qw(centreon::plugins::mode); - -use strict; -use warnings; -use centreon::plugins::http; -use JSON; - -my $thresholds = { - status => [ - ['ACTIVE', 'OK'], - ['BUILD', 'OK'], - ['DOWN', 'CRITICAL'], - ['ERROR', 'CRITICAL'], - ], -}; - -sub new { - my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); - bless $self, $class; - - $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "data:s" => { name => 'data' }, - "hostname:s" => { name => 'hostname' }, - "http-peer-addr:s" => { name => 'http_peer_addr' }, - "port:s" => { name => 'port', default => '5000' }, - "proto:s" => { name => 'proto' }, - "urlpath:s" => { name => 'url_path', default => '/v3/auth/tokens' }, - "proxyurl:s" => { name => 'proxyurl' }, - "proxypac:s" => { name => 'proxypac' }, - "credentials" => { name => 'credentials' }, - "username:s" => { name => 'username' }, - "password:s" => { name => 'password' }, - "ssl:s" => { name => 'ssl', }, - "header:s@" => { name => 'header' }, - "timeout:s" => { name => 'timeout' }, - "network-id:s" => { name => 'network_id' }, - "threshold-overload:s@" => { name => 'threshold_overload' }, - }); - - $self->{http} = centreon::plugins::http->new(output => $self->{output}); - $self->{network_infos} = (); - return $self; -} - -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - $self->{overload_th} = {}; - foreach my $val (@{$self->{option_results}->{threshold_overload}}) { - if ($val !~ /^(.*?),(.*?),(.*)$/) { - $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload option '" . $val . "'."); - $self->{output}->option_exit(); - } - my ($section, $status, $filter) = ($1, $2, $3); - if ($self->{output}->is_litteral_status(status => $status) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload status '" . $val . "'."); - $self->{output}->option_exit(); - } - $self->{overload_th}->{$section} = [] if (!defined($self->{overload_th}->{$section})); - push @{$self->{overload_th}->{$section}}, {filter => $filter, status => $status}; - } - - if (!defined($self->{option_results}->{header}) || $self->{option_results}->{header} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --header option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{data}) || $self->{option_results}->{data} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --data option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{hostname}) || $self->{option_results}->{hostname} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --hostname option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{network_id}) || $self->{option_results}->{network_id} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --network-id option."); - $self->{output}->option_exit(); - } - - $self->{http}->set_options(%{$self->{option_results}}) -} - -sub token_request { - my ($self, %options) = @_; - - $self->{method} = 'GET'; - if (defined($self->{option_results}->{data})) { - local $/ = undef; - if (!open(FILE, "<", $self->{option_results}->{data})) { - $self->{output}->output_add(severity => 'UNKNOWN', - short_msg => sprintf("Could not read file '%s': %s", $self->{option_results}->{data}, $!)); - $self->{output}->display(); - $self->{output}->exit(); - } - $self->{json_request} = ; - close FILE; - $self->{method} = 'POST'; - } - - my $response = $self->{http}->request(method => $self->{method}, query_form_post => $self->{json_request}); - my $headers = $self->{http}->get_header(); - - eval { - $self->{header} = $headers->header('X-Subject-Token'); - }; - - if ($@) { - $self->{output}->add_option_msg(short_msg => "Cannot retrieve API Token"); - $self->{output}->option_exit(); - } -} - -sub api_request { - my ($self, %options) = @_; - - $self->{method} = 'GET'; - $self->{option_results}->{url_path} = "/v2.0/networks/".$self->{option_results}->{network_id}; - $self->{option_results}->{port} = '9696'; - @{$self->{option_results}->{header}} = ('X-Auth-Token:' . $self->{header}, 'Accept:application/json'); - $self->{http}->set_options(%{$self->{option_results}}); - - my $webcontent; - my $jsoncontent = $self->{http}->request(method => $self->{method}); - - my $json = JSON->new; - - eval { - $webcontent = $json->decode($jsoncontent); - }; - - $self->{network_infos}->{name} = $webcontent->{network}->{name}; - $self->{network_infos}->{admin_state} = $webcontent->{network}->{admin_state_up}; - $self->{network_infos}->{status} = $webcontent->{network}->{status}; -} - - -sub get_severity { - my ($self, %options) = @_; - my $status = 'UNKNOWN'; # default - - if (defined($self->{overload_th}->{$options{section}})) { - foreach (@{$self->{overload_th}->{$options{section}}}) { - if ($options{value} =~ /$_->{filter}/i) { - $status = $_->{status}; - return $status; - } - } - } - foreach (@{$thresholds->{$options{section}}}) { - if ($options{value} =~ /$$_[0]/i) { - $status = $$_[1]; - return $status; - } - } - - return $status; -} - -sub run { - my ($self, %options) = @_; - - $self->token_request(); - $self->api_request(); - - my $exit = $self->get_severity(section => 'status', value => $self->{network_infos}->{status}); - $self->{output}->output_add(severity => $exit, - short_msg => sprintf("Network %s is in %s state (admin_state: %s)", - $self->{network_infos}->{name}, - $self->{network_infos}->{status}, - $self->{network_infos}->{admin_state})); - - $self->{output}->display(); - $self->{output}->exit(); - - exit 0; -} - -1; - -__END__ - -=head1 MODE - -List OpenStack instances through Compute API V2 - -JSON OPTIONS: - -=over 8 - -=item B<--data> - -Set file with JSON request - -=back - -HTTP OPTIONS: - -=over 8 - -=item B<--hostname> - -IP Addr/FQDN of OpenStack Compute's API - -=item B<--http-peer-addr> - -Set the address you want to connect (Useful if hostname is only a vhost. no ip resolve) - -=item B<--port> - -Port used by OpenStack Keystone's API (Default: '5000') - -=item B<--proto> - -Specify https if needed (Default: 'http') - -=item B<--urlpath> - -Set path to get API's Token (Default: '/v3/auth/tokens') - -=item B<--proxyurl> - -Proxy URL - -=item B<--proxypac> - -Proxy pac file (can be an url or local file) - -=item B<--credentials> - -Specify this option if you access webpage over basic authentification - -=item B<--username> - -Specify username - -=item B<--password> - -Specify password - -=item B<--ssl> - -Specify SSL version (example : 'sslv3', 'tlsv1'...) - -=item B<--header> - -Set HTTP headers (Multiple option. Example: --header='Content-Type: xxxxx') - -=item B<--timeout> - -Threshold for HTTP timeout (Default: 3) - -=item B<--threshold-overload> - -Set to overload default threshold values (syntax: section,status,regexp) -It used before default thresholds (order stays). -Example: --threshold-overload='status,CRITICAL,^BUILD$)' - -=back - -OPENSTACK OPTIONS: - -=over 8 - -=item B<--network-id> - -Set Network's ID - -=back - -=cut diff --git a/cloud/openstack/restapi/mode/port.pm b/cloud/openstack/restapi/mode/port.pm deleted file mode 100644 index 51c51e703..000000000 --- a/cloud/openstack/restapi/mode/port.pm +++ /dev/null @@ -1,300 +0,0 @@ -# -# Copyright 2015 Centreon (http://www.centreon.com/) -# -# Centreon is a full-fledged industry-strength solution that meets -# the needs in IT infrastructure and application monitoring for -# service performance. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -package cloud::openstack::restapi::mode::port; - -use base qw(centreon::plugins::mode); - -use strict; -use warnings; -use centreon::plugins::http; -use JSON; - -my $thresholds = { - status => [ - ['ACTIVE', 'OK'], - ['DOWN', 'CRITICAL'], - ['N/A', 'UNKNOWN'], - ], -}; - -sub new { - my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); - bless $self, $class; - - $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "data:s" => { name => 'data' }, - "hostname:s" => { name => 'hostname' }, - "http-peer-addr:s" => { name => 'http_peer_addr' }, - "port:s" => { name => 'port', default => '5000' }, - "proto:s" => { name => 'proto' }, - "urlpath:s" => { name => 'url_path', default => '/v3/auth/tokens' }, - "proxyurl:s" => { name => 'proxyurl' }, - "proxypac:s" => { name => 'proxypac' }, - "credentials" => { name => 'credentials' }, - "username:s" => { name => 'username' }, - "password:s" => { name => 'password' }, - "ssl:s" => { name => 'ssl', }, - "header:s@" => { name => 'header' }, - "exclude:s" => { name => 'exclude' }, - "timeout:s" => { name => 'timeout' }, - "port-id:s" => { name => 'port_id' }, - "threshold-overload:s@" => { name => 'threshold_overload' }, - }); - - $self->{http} = centreon::plugins::http->new(output => $self->{output}); - $self->{port_infos} = (); - return $self; -} - -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - $self->{overload_th} = {}; - foreach my $val (@{$self->{option_results}->{threshold_overload}}) { - if ($val !~ /^(.*?),(.*?),(.*)$/) { - $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload option '" . $val . "'."); - $self->{output}->option_exit(); - } - my ($section, $status, $filter) = ($1, $2, $3); - if ($self->{output}->is_litteral_status(status => $status) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload status '" . $val . "'."); - $self->{output}->option_exit(); - } - $self->{overload_th}->{$section} = [] if (!defined($self->{overload_th}->{$section})); - push @{$self->{overload_th}->{$section}}, {filter => $filter, status => $status}; - } - - if (!defined($self->{option_results}->{header}) || $self->{option_results}->{header} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --header option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{data}) || $self->{option_results}->{data} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --data option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{hostname}) || $self->{option_results}->{hostname} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --hostname option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{port_id}) || $self->{option_results}->{port_id} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --port-id option."); - $self->{output}->option_exit(); - } - - $self->{http}->set_options(%{$self->{option_results}}) -} - -sub token_request { - my ($self, %options) = @_; - - $self->{method} = 'GET'; - if (defined($self->{option_results}->{data})) { - local $/ = undef; - if (!open(FILE, "<", $self->{option_results}->{data})) { - $self->{output}->output_add(severity => 'UNKNOWN', - short_msg => sprintf("Could not read file '%s': %s", $self->{option_results}->{data}, $!)); - $self->{output}->display(); - $self->{output}->exit(); - } - $self->{json_request} = ; - close FILE; - $self->{method} = 'POST'; - } - - my $response = $self->{http}->request(method => $self->{method}, query_form_post => $self->{json_request}); - my $headers = $self->{http}->get_header(); - - eval { - $self->{header} = $headers->header('X-Subject-Token'); - }; - - if ($@) { - $self->{output}->add_option_msg(short_msg => "Cannot retrieve API Token"); - $self->{output}->option_exit(); - } -} - -sub api_request { - my ($self, %options) = @_; - - $self->{method} = 'GET'; - $self->{option_results}->{url_path} = "/v2.0/ports/".$self->{option_results}->{port_id}; - $self->{option_results}->{port} = '9696'; - @{$self->{option_results}->{header}} = ('X-Auth-Token:' . $self->{header}, 'Accept:application/json'); - $self->{http}->set_options(%{$self->{option_results}}); - - my $webcontent; - my $jsoncontent = $self->{http}->request(method => $self->{method}); - - my $json = JSON->new; - - eval { - $webcontent = $json->decode($jsoncontent); - }; - if ($webcontent->{port}->{name} eq '') { - $self->{port_infos}->{name} = $webcontent->{port}->{id}; - } else { - $self->{port_infos}->{name} = $webcontent->{port}->{name}; - } - $self->{port_infos}->{admin_state} = $webcontent->{port}->{admin_state_up}; - $self->{port_infos}->{status} = $webcontent->{port}->{status}; -} - - -sub get_severity { - my ($self, %options) = @_; - my $status = 'UNKNOWN'; # default - - if (defined($self->{overload_th}->{$options{section}})) { - foreach (@{$self->{overload_th}->{$options{section}}}) { - if ($options{value} =~ /$_->{filter}/i) { - $status = $_->{status}; - return $status; - } - } - } - foreach (@{$thresholds->{$options{section}}}) { - if ($options{value} =~ /$$_[0]/i) { - $status = $$_[1]; - return $status; - } - } - - return $status; -} - -sub run { - my ($self, %options) = @_; - - $self->token_request(); - $self->api_request(); - - my $exit = $self->get_severity(section => 'status', value => $self->{port_infos}->{status}); - $self->{output}->output_add(severity => $exit, - short_msg => sprintf("Port %s is in %s state (admin_state: %s)", - $self->{port_infos}->{name}, - $self->{port_infos}->{status}, - $self->{port_infos}->{admin_state})); - - $self->{output}->display(); - $self->{output}->exit(); - - exit 0; -} - -1; - -__END__ - -=head1 MODE - -List OpenStack instances through Compute API V2 - -JSON OPTIONS: - -=over 8 - -=item B<--data> - -Set file with JSON request - -=back - -HTTP OPTIONS: - -=over 8 - -=item B<--hostname> - -IP Addr/FQDN of OpenStack Compute's API - -=item B<--http-peer-addr> - -Set the address you want to connect (Useful if hostname is only a vhost. no ip resolve) - -=item B<--port> - -Port used by OpenStack Keystone's API (Default: '5000') - -=item B<--proto> - -Specify https if needed (Default: 'http') - -=item B<--urlpath> - -Set path to get API's Token (Default: '/v3/auth/tokens') - -=item B<--proxyurl> - -Proxy URL - -=item B<--proxypac> - -Proxy pac file (can be an url or local file) - -=item B<--credentials> - -Specify this option if you access webpage over basic authentification - -=item B<--username> - -Specify username - -=item B<--password> - -Specify password - -=item B<--ssl> - -Specify SSL version (example : 'sslv3', 'tlsv1'...) - -=item B<--header> - -Set HTTP headers (Multiple option. Example: --header='Content-Type: xxxxx') - -=item B<--timeout> - -Threshold for HTTP timeout (Default: 3) - -=back - -=item B<--threshold-overload> - -Set to overload default threshold values (syntax: section,status,regexp) -It used before default thresholds (order stays). -Example: --threshold-overload='status,CRITICAL,^N\/A$)' - -=back - -OPENSTACK OPTIONS: - -=over 8 - -=item B<--port-id> - -Set Port's ID - -=back - diff --git a/cloud/openstack/restapi/mode/volume.pm b/cloud/openstack/restapi/mode/volume.pm deleted file mode 100644 index 3ca349c5d..000000000 --- a/cloud/openstack/restapi/mode/volume.pm +++ /dev/null @@ -1,313 +0,0 @@ -# -# Copyright 2015 Centreon (http://www.centreon.com/) -# -# Centreon is a full-fledged industry-strength solution that meets -# the needs in IT infrastructure and application monitoring for -# service performance. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -package cloud::openstack::restapi::mode::volume; - -use base qw(centreon::plugins::mode); - -use strict; -use warnings; -use centreon::plugins::http; -use JSON; - -my $thresholds = { - status => [ - ['creating', 'OK'], - ['available', 'OK'], - ['attaching', 'OK'], - ['in-use', 'OK'], - ['deleting', 'OK'], - ['backing-up', 'WARNING'], - ['restoring-backup', 'WARNING'], - ['error', 'CRITICAL'], - ['error_deleting', 'CRITICAL'], - ['error_restoring', 'CRITICAL'], - ['error_extending', 'CRITICAL'], - ], -}; - -sub new { - my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); - bless $self, $class; - - $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "data:s" => { name => 'data' }, - "hostname:s" => { name => 'hostname' }, - "http-peer-addr:s" => { name => 'http_peer_addr' }, - "port:s" => { name => 'port', default => '5000' }, - "proto:s" => { name => 'proto' }, - "urlpath:s" => { name => 'url_path', default => '/v3/auth/tokens' }, - "proxyurl:s" => { name => 'proxyurl' }, - "proxypac:s" => { name => 'proxypac' }, - "credentials" => { name => 'credentials' }, - "username:s" => { name => 'username' }, - "password:s" => { name => 'password' }, - "ssl:s" => { name => 'ssl', }, - "header:s@" => { name => 'header' }, - "timeout:s" => { name => 'timeout' }, - "tenant-id:s" => { name => 'tenant_id' }, - "volume-id:s" => { name => 'volume_id' }, - "threshold-overload:s@" => { name => 'threshold_overload' }, - }); - - $self->{http} = centreon::plugins::http->new(output => $self->{output}); - $self->{volume_infos} = (); - return $self; -} - -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - $self->{overload_th} = {}; - foreach my $val (@{$self->{option_results}->{threshold_overload}}) { - if ($val !~ /^(.*?),(.*?),(.*)$/) { - $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload option '" . $val . "'."); - $self->{output}->option_exit(); - } - my ($section, $status, $filter) = ($1, $2, $3); - if ($self->{output}->is_litteral_status(status => $status) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong threshold-overload status '" . $val . "'."); - $self->{output}->option_exit(); - } - $self->{overload_th}->{$section} = [] if (!defined($self->{overload_th}->{$section})); - push @{$self->{overload_th}->{$section}}, {filter => $filter, status => $status}; - } - - if (!defined($self->{option_results}->{header}) || $self->{option_results}->{header} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --header option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{data}) || $self->{option_results}->{data} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --data option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{hostname}) || $self->{option_results}->{hostname} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --hostname option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{tenant_id}) || $self->{option_results}->{tenant_id} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --tenant-id option."); - $self->{output}->option_exit(); - } - if (!defined($self->{option_results}->{volume_id}) || $self->{option_results}->{volume_id} eq '') { - $self->{output}->add_option_msg(short_msg => "You need to specify --volume-id option."); - $self->{output}->option_exit(); - } - - $self->{http}->set_options(%{$self->{option_results}}) -} - -sub token_request { - my ($self, %options) = @_; - - $self->{method} = 'GET'; - if (defined($self->{option_results}->{data})) { - local $/ = undef; - if (!open(FILE, "<", $self->{option_results}->{data})) { - $self->{output}->output_add(severity => 'UNKNOWN', - short_msg => sprintf("Could not read file '%s': %s", $self->{option_results}->{data}, $!)); - $self->{output}->display(); - $self->{output}->exit(); - } - $self->{json_request} = ; - close FILE; - $self->{method} = 'POST'; - } - - my $response = $self->{http}->request(method => $self->{method}, query_form_post => $self->{json_request}); - my $headers = $self->{http}->get_header(); - - eval { - $self->{header} = $headers->header('X-Subject-Token'); - }; - - if ($@) { - $self->{output}->add_option_msg(short_msg => "Cannot retrieve API Token"); - $self->{output}->option_exit(); - } -} - -sub api_request { - my ($self, %options) = @_; - - $self->{method} = 'GET'; - $self->{option_results}->{url_path} = "/v2/".$self->{option_results}->{tenant_id}."/volumes/".$self->{option_results}->{volume_id}; - $self->{option_results}->{port} = '8776'; - @{$self->{option_results}->{header}} = ('X-Auth-Token:' . $self->{header}, 'Accept:application/json'); - $self->{http}->set_options(%{$self->{option_results}}); - - my $webcontent; - my $jsoncontent = $self->{http}->request(method => $self->{method}); - - my $json = JSON->new; - - eval { - $webcontent = $json->decode($jsoncontent); - }; - - $self->{volume_infos}->{name} = $webcontent->{volume}->{name}; - $self->{volume_infos}->{status} = $webcontent->{volume}->{status}; -} - - -sub get_severity { - my ($self, %options) = @_; - my $status = 'UNKNOWN'; # default - - if (defined($self->{overload_th}->{$options{section}})) { - foreach (@{$self->{overload_th}->{$options{section}}}) { - if ($options{value} =~ /$_->{filter}/i) { - $status = $_->{status}; - return $status; - } - } - } - foreach (@{$thresholds->{$options{section}}}) { - if ($options{value} =~ /$$_[0]/i) { - $status = $$_[1]; - return $status; - } - } - - return $status; -} - -sub run { - my ($self, %options) = @_; - - $self->token_request(); - $self->api_request(); - - my $exit = $self->get_severity(section => 'status', value => $self->{volume_infos}->{status}); - $self->{output}->output_add(severity => $exit, - short_msg => sprintf("Volume %s is in %s state", - $self->{volume_infos}->{name}, - $self->{volume_infos}->{status})); - - $self->{output}->display(); - $self->{output}->exit(); - - exit 0; -} - -1; - -__END__ - -=head1 MODE - -List OpenStack instances through Compute API V2 - -JSON OPTIONS: - -=over 8 - -=item B<--data> - -Set file with JSON request - -=back - -HTTP OPTIONS: - -=over 8 - -=item B<--hostname> - -IP Addr/FQDN of OpenStack Compute's API - -=item B<--http-peer-addr> - -Set the address you want to connect (Useful if hostname is only a vhost. no ip resolve) - -=item B<--port> - -Port used by OpenStack Keystone's API (Default: '5000') - -=item B<--proto> - -Specify https if needed (Default: 'http') - -=item B<--urlpath> - -Set path to get API's Token (Default: '/v3/auth/tokens') - -=item B<--proxyurl> - -Proxy URL - -=item B<--proxypac> - -Proxy pac file (can be an url or local file) - -=item B<--credentials> - -Specify this option if you access webpage over basic authentification - -=item B<--username> - -Specify username - -=item B<--password> - -Specify password - -=item B<--ssl> - -Specify SSL version (example : 'sslv3', 'tlsv1'...) - -=item B<--header> - -Set HTTP headers (Multiple option. Example: --header='Content-Type: xxxxx') - -=item B<--timeout> - -Threshold for HTTP timeout (Default: 3) - -=back - -=item B<--threshold-overload> - -Set to overload default threshold values (syntax: section,status,regexp) -It used before default thresholds (order stays). -Example: --threshold-overload='status,CRITICAL,^deleting$)' - -=back - -OPENSTACK OPTIONS: - -=over 8 - -=item B<--tenant-id> - -Set Tenant's ID - -=item B<--volume-id> - -Set Volume's ID - -=back - -=cut - diff --git a/cloud/openstack/restapi/plugin.pm b/cloud/openstack/restapi/plugin.pm deleted file mode 100644 index ed9951b1c..000000000 --- a/cloud/openstack/restapi/plugin.pm +++ /dev/null @@ -1,56 +0,0 @@ -# -# Copyright 2015 Centreon (http://www.centreon.com/) -# -# Centreon is a full-fledged industry-strength solution that meets -# the needs in IT infrastructure and application monitoring for -# service performance. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -package cloud::openstack::restapi::plugin; - -use strict; -use warnings; -use base qw(centreon::plugins::script_simple); - -sub new { - my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); - bless $self, $class; - # $options->{options} = options object - - $self->{version} = '0.1'; - %{$self->{modes}} = ( - 'hypervisor' => 'cloud::openstack::restapi::mode::hypervisor', - 'instance' => 'cloud::openstack::restapi::mode::instance', - 'list-hypervisors' => 'cloud::openstack::restapi::mode::listhypervisors', - 'list-instances' => 'cloud::openstack::restapi::mode::listinstances', - 'list-networks' => 'cloud::openstack::restapi::mode::listnetworks', - 'list-volumes' => 'cloud::openstack::restapi::mode::listvolumes', - 'network' => 'cloud::openstack::restapi::mode::network', - 'port' => 'cloud::openstack::restapi::mode::port', - 'volume' => 'cloud::openstack::restapi::mode::volume', - ); - return $self; -} - -1; - -__END__ - -=head1 PLUGIN DESCRIPTION - -Check Openstack Components with their API. - -=cut From d2dbf1822702922eebc3fe042852890afea0681f Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Wed, 15 Jun 2016 15:49:52 +0200 Subject: [PATCH 24/30] + fix emc dmx --- storage/emc/symmetrix/dmx34/local/mode/hardware.pm | 1 + 1 file changed, 1 insertion(+) diff --git a/storage/emc/symmetrix/dmx34/local/mode/hardware.pm b/storage/emc/symmetrix/dmx34/local/mode/hardware.pm index f2667401b..9018474ed 100644 --- a/storage/emc/symmetrix/dmx34/local/mode/hardware.pm +++ b/storage/emc/symmetrix/dmx34/local/mode/hardware.pm @@ -185,6 +185,7 @@ sub send_email { $send_email = 0 if ($status ne 'ok' && defined($prev_output) && $prev_output eq $subject); # recovery email $send_email = 1 if ($status eq 'ok' && defined($prev_status) && $prev_status ne 'ok'); + $self->{statefile_cache}->write(data => $self->{new_datas}); } my $smtp_to = ''; From 768eb7943020391fe40d075c0aaa7769f41963db Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Thu, 16 Jun 2016 16:47:44 +0200 Subject: [PATCH 25/30] + Add symmetrix vmax --- .../vmax/local/mode/components/cabling.pm | 70 +++++ .../vmax/local/mode/components/director.pm | 72 +++++ .../vmax/local/mode/components/fabric.pm | 91 ++++++ .../vmax/local/mode/components/module.pm | 71 +++++ .../vmax/local/mode/components/power.pm | 95 ++++++ .../vmax/local/mode/components/sparedisk.pm | 62 ++++ .../vmax/local/mode/components/temperature.pm | 74 +++++ .../vmax/local/mode/components/voltage.pm | 55 ++++ .../emc/symmetrix/vmax/local/mode/hardware.pm | 273 ++++++++++++++++++ storage/emc/symmetrix/vmax/local/plugin.pm | 48 +++ 10 files changed, 911 insertions(+) create mode 100644 storage/emc/symmetrix/vmax/local/mode/components/cabling.pm create mode 100644 storage/emc/symmetrix/vmax/local/mode/components/director.pm create mode 100644 storage/emc/symmetrix/vmax/local/mode/components/fabric.pm create mode 100644 storage/emc/symmetrix/vmax/local/mode/components/module.pm create mode 100644 storage/emc/symmetrix/vmax/local/mode/components/power.pm create mode 100644 storage/emc/symmetrix/vmax/local/mode/components/sparedisk.pm create mode 100644 storage/emc/symmetrix/vmax/local/mode/components/temperature.pm create mode 100644 storage/emc/symmetrix/vmax/local/mode/components/voltage.pm create mode 100644 storage/emc/symmetrix/vmax/local/mode/hardware.pm create mode 100644 storage/emc/symmetrix/vmax/local/plugin.pm diff --git a/storage/emc/symmetrix/vmax/local/mode/components/cabling.pm b/storage/emc/symmetrix/vmax/local/mode/components/cabling.pm new file mode 100644 index 000000000..c904cdd3a --- /dev/null +++ b/storage/emc/symmetrix/vmax/local/mode/components/cabling.pm @@ -0,0 +1,70 @@ +# +# Copyright 2016 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package storage::emc::symmetrix::vmax::local::mode::components::cabling; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::misc; + +#Ethernet cabling: OK +# +#+--------------------------------------------------+--------+----------------------+--------------------+----------------------+----------------------+------------+--------------+ +#| Cable Name | Status | Expected 'From' Port | Actual 'From' Port | Expected 'To' Port | Actual 'To' Port | Error Code | Error string | +#+--------------------------------------------------+--------+----------------------+--------------------+----------------------+----------------------+------------+--------------+ +#| Cable from SRV A Adapter to MM-A ES-4 Lower port | OK | SRV A Adapter | SRV A Adapter | MM-A ES-4 Lower port | MM-A ES-4 Lower port | None | | +#| Cable from SRV B Adapter to MM-B ES-4 Lower port | OK | SRV B Adapter | SRV B Adapter | MM-B ES-4 Lower port | MM-B ES-4 Lower port | None | | +#+--------------------------------------------------+--------+----------------------+--------------------+----------------------+----------------------+------------+--------------+ +#| Cable Name | Status | Expected 'From' Port | Actual 'From' Port | Expected 'To' Port | Actual 'To' Port | Error Code | Error string | +#+--------------------------------------------------+--------+----------------------+--------------------+----------------------+----------------------+------------+--------------+ + +sub check { + my ($self) = @_; + + $self->{output}->output_add(long_msg => "Checking cabling"); + $self->{components}->{cabling} = {name => 'cabling', total => 0, skip => 0}; + return if ($self->check_filter(section => 'cabling')); + + if ($self->{content_file_health_env} !~ /Ethernet cabling.*?Ethernet cabling.*?---------.*?Cable Name.*?---------.*?\n(.*?\n)\+---------/msi) { + $self->{output}->output_add(long_msg => 'skipping: cannot find cabling'); + return ; + } + + my $content = $1; + while ($content =~ /^\|(.*?)\|(.*?)\|.*?\n/msig) { + my ($cabling, $status) = (centreon::plugins::misc::trim($1), centreon::plugins::misc::trim($2)); + + next if ($self->check_filter(section => 'cabling', instance => $cabling)); + $self->{components}->{cabling}->{total}++; + + $self->{output}->output_add(long_msg => sprintf("cabling '%s' status is '%s'", + $cabling, $status)); + my $exit = $self->get_severity(label => 'default', section => 'cabling', value => $status); + if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { + $self->{output}->output_add(severity => $exit, + short_msg => sprintf("Cabling '%s' status is '%s'", + $cabling, $status)); + } + } +} + +1; diff --git a/storage/emc/symmetrix/vmax/local/mode/components/director.pm b/storage/emc/symmetrix/vmax/local/mode/components/director.pm new file mode 100644 index 000000000..93ab9d296 --- /dev/null +++ b/storage/emc/symmetrix/vmax/local/mode/components/director.pm @@ -0,0 +1,72 @@ +# +# Copyright 2016 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package storage::emc::symmetrix::vmax::local::mode::components::director; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::misc; + +#Directors status: OK +# +#+----------+--------+---------+-------------+---------+-----------------+-------------+ +#| Director | Type | State | Flags | Version | Address | Last Update | +#+----------+--------+---------+-------------+---------+-----------------+-------------+ +#| 07A | DA_4x1 | Online | [ON] (0x80) | 05 | 192.168.177.7 | 1110 | +#| 07B | DA_4x1 | Online | [ON] (0x80) | 05 | 192.168.177.23 | 1110 | +#| 07C | DA_4x1 | Online | [ON] (0x80) | 05 | 192.168.177.39 | 1110 | +#| 07D | DA_4x1 | Online | [ON] (0x80) | 05 | 192.168.177.55 | 1110 | +#+----------+--------+---------+-------------+---------+-----------------+-------------+ +#| Director | Type | State | Flags | Version | Address | Last Update | +#+----------+--------+---------+-------------+---------+-----------------+-------------+ + +sub check { + my ($self) = @_; + + $self->{output}->output_add(long_msg => "Checking directors"); + $self->{components}->{director} = {name => 'directors', total => 0, skip => 0}; + return if ($self->check_filter(section => 'director')); + + if ($self->{content_file_health_env} !~ /Ethernet cabling.*?Directors status.*?---------.*?Director.*?---------.*?\n(.*?\n)\+---------/msi) { + $self->{output}->output_add(long_msg => 'skipping: cannot find directors'); + return ; + } + + my $content = $1; + while ($content =~ /^\|(.*?)\|.*?\|(.*?)\|.*?\n/msig) { + my ($director, $status) = (centreon::plugins::misc::trim($1), centreon::plugins::misc::trim($2)); + + next if ($self->check_filter(section => 'director', instance => $director)); + $self->{components}->{director}->{total}++; + + $self->{output}->output_add(long_msg => sprintf("director '%s' status is '%s'", + $director, $status)); + my $exit = $self->get_severity(label => 'default', section => 'director', value => $status); + if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { + $self->{output}->output_add(severity => $exit, + short_msg => sprintf("Director '%s' status is '%s'", + $director, $status)); + } + } +} + +1; diff --git a/storage/emc/symmetrix/vmax/local/mode/components/fabric.pm b/storage/emc/symmetrix/vmax/local/mode/components/fabric.pm new file mode 100644 index 000000000..b2b3df797 --- /dev/null +++ b/storage/emc/symmetrix/vmax/local/mode/components/fabric.pm @@ -0,0 +1,91 @@ +# +# Copyright 2016 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package storage::emc::symmetrix::vmax::local::mode::components::fabric; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::misc; + +#Fabric system: OK +# +#+-------------------------------------------------+---------------------------------+ +#| Item | Status | +#+-------------------------------------------------+---------------------------------+ +#| Fabric System General Status | OK | +#| Usage Mode | Full usage - BOSCO and Ethernet | +#| Initialization Status | OK | +#| Configuration Status | OK | +#| Fabric A Availability | Available | +#| Fabric B Availability | Available | +#| Directors' Fabric Links Status | OK | +#| Dir 7 (RIO 0x0C) | All links are up | +#| Link A status | Up | +#| Link B status | Up | +#+-------------------------------------------------+---------------------------------+ +#| Item | Status | +#+-------------------------------------------------+---------------------------------+ + +sub check { + my ($self) = @_; + + $self->{output}->output_add(long_msg => "Checking fabrics"); + $self->{components}->{fabric} = {name => 'fabrics', total => 0, skip => 0}; + return if ($self->check_filter(section => 'fabric')); + + if ($self->{content_file_health_env} !~ /Ethernet cabling.*?Fabric system.*?---------.*?Item.*?---------.*?\n(.*?\n)\+---------/msi) { + $self->{output}->output_add(long_msg => 'skipping: cannot find fabrics'); + return ; + } + + my $content = $1; + + my $total_components = 0; + my @stack = ({ indent => 0, long_instance => '' }); + while ($content =~ /^\|([ \t]+)(.*?)\|(.*?)\|\n/msig) { + my ($indent, $name, $status) = (length($1), centreon::plugins::misc::trim($2), centreon::plugins::misc::trim($3)); + + pop @stack while ($indent <= $stack[$#stack]->{indent}); + + my $long_instance = $stack[$#stack]->{long_instance} . '>' . $name; + if ($indent > $stack[$#stack]->{indent}) { + push @stack, { indent => $indent, + long_instance => $stack[$#stack]->{long_instance} . '>' . $name }; + } + + next if ($name !~ /status/i); + + next if ($self->check_filter(section => 'fabric', instance => $long_instance)); + $self->{components}->{fabric}->{total}++; + + $self->{output}->output_add(long_msg => sprintf("fabric '%s' status is '%s'", + $long_instance, $status)); + my $exit = $self->get_severity(label => 'default', section => 'fabric', value => $status); + if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { + $self->{output}->output_add(severity => $exit, + short_msg => sprintf("Fabric '%s' status is '%s'", + $long_instance, $status)); + } + } +} + +1; diff --git a/storage/emc/symmetrix/vmax/local/mode/components/module.pm b/storage/emc/symmetrix/vmax/local/mode/components/module.pm new file mode 100644 index 000000000..f9b1b4667 --- /dev/null +++ b/storage/emc/symmetrix/vmax/local/mode/components/module.pm @@ -0,0 +1,71 @@ +# +# Copyright 2016 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package storage::emc::symmetrix::vmax::local::mode::components::module; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::misc; + +#Modules status (alarms): OK +#+-------------------------------------------------------------------+-----------+----------+--------+------------+ +#| Module | Ctrl dirs | Rep dirs | Status | Rel Status | +#+-------------------------------------------------------------------+-----------+----------+--------+------------+ +#| Engine 4 | 07A,08A | 07A,08A | OK | | +#| Engine SPS 4A | 07A | 07A | OK | | +#| Engine SPS 4B | 08A | 08A | OK | | +#| Engine Power Supply A of ES-4 | 07A | 07A | OK | | +#+-------------------------------------------------------------------+-----------+----------+--------+------------+ +#| Module | Ctrl dirs | Rep dirs | Status | Rel Status | +#+-------------------------------------------------------------------+-----------+----------+--------+------------+ + +sub check { + my ($self) = @_; + + $self->{output}->output_add(long_msg => "Checking modules"); + $self->{components}->{module} = {name => 'modules', total => 0, skip => 0}; + return if ($self->check_filter(section => 'module')); + + if ($self->{content_file_health_env} !~ /Ethernet cabling.*?Modules status.*?---------.*?Module.*?---------.*?\n(.*?\n)\+---------/msi) { + $self->{output}->output_add(long_msg => 'skipping: cannot find modules'); + return ; + } + + my $content = $1; + while ($content =~ /^\|(.*?)\|.*?\|.*?\|(.*?)\|.*?\n/msig) { + my ($module, $status) = (centreon::plugins::misc::trim($1), centreon::plugins::misc::trim($2)); + + next if ($self->check_filter(section => 'module', instance => $module)); + $self->{components}->{module}->{total}++; + + $self->{output}->output_add(long_msg => sprintf("module '%s' status is '%s'", + $module, $status)); + my $exit = $self->get_severity(label => 'default', section => 'module', value => $status); + if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { + $self->{output}->output_add(severity => $exit, + short_msg => sprintf("Module '%s' status is '%s'", + $module, $status)); + } + } +} + +1; diff --git a/storage/emc/symmetrix/vmax/local/mode/components/power.pm b/storage/emc/symmetrix/vmax/local/mode/components/power.pm new file mode 100644 index 000000000..5bba09a19 --- /dev/null +++ b/storage/emc/symmetrix/vmax/local/mode/components/power.pm @@ -0,0 +1,95 @@ +# +# Copyright 2016 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package storage::emc::symmetrix::vmax::local::mode::components::power; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::misc; + +#Power system: OK +# +#+--------------------------------------------------------+--------------------------------------+ +#| Item | Status | +#+--------------------------------------------------------+--------------------------------------+ +#| Power input type | Three Phases | +#| System Bay AC Zones Status | OK | +#| AC Status Zone A | Zone AC OK | +#| AC Status Zone B | Zone AC OK | +#| Power modules status | OK | +#| System Bay 1 | OK | +#| Engine SPS 4A | OK | +#| General Status | OK | +#| Detailed Status | OK | +#| Condition Register | OK | +#| Battery Life (sec) | 600 | +#| Days of Operation | Unknown | +#| Slot | Slot B | +#| Manufacturer Information | ASTEC,AA23540,7E, 04/11/2008 | +#+--------------------------------------------------------+--------------------------------------+ +#| Item | Status | +#+--------------------------------------------------------+--------------------------------------+ + +sub check { + my ($self) = @_; + + $self->{output}->output_add(long_msg => "Checking powers"); + $self->{components}->{power} = {name => 'powers', total => 0, skip => 0}; + return if ($self->check_filter(section => 'power')); + + if ($self->{content_file_health_env} !~ /Ethernet cabling.*?Power system.*?---------.*?Item.*?---------.*?\n(.*?\n)\+---------/msi) { + $self->{output}->output_add(long_msg => 'skipping: cannot find powers'); + return ; + } + + my $content = $1; + + my $total_components = 0; + my @stack = ({ indent => 0, long_instance => '' }); + while ($content =~ /^\|([ \t]+)(.*?)\|(.*?)\|\n/msig) { + my ($indent, $name, $status) = (length($1), centreon::plugins::misc::trim($2), centreon::plugins::misc::trim($3)); + + pop @stack while ($indent <= $stack[$#stack]->{indent}); + + my $long_instance = $stack[$#stack]->{long_instance} . '>' . $name; + if ($indent > $stack[$#stack]->{indent}) { + push @stack, { indent => $indent, + long_instance => $stack[$#stack]->{long_instance} . '>' . $name }; + } + + next if ($name !~ /status/i); + + next if ($self->check_filter(section => 'power', instance => $long_instance)); + $self->{components}->{power}->{total}++; + + $self->{output}->output_add(long_msg => sprintf("power '%s' status is '%s'", + $long_instance, $status)); + my $exit = $self->get_severity(label => 'default', section => 'power', value => $status); + if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { + $self->{output}->output_add(severity => $exit, + short_msg => sprintf("Power '%s' status is '%s'", + $long_instance, $status)); + } + } +} + +1; diff --git a/storage/emc/symmetrix/vmax/local/mode/components/sparedisk.pm b/storage/emc/symmetrix/vmax/local/mode/components/sparedisk.pm new file mode 100644 index 000000000..62834a546 --- /dev/null +++ b/storage/emc/symmetrix/vmax/local/mode/components/sparedisk.pm @@ -0,0 +1,62 @@ +# +# Copyright 2016 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package storage::emc::symmetrix::vmax::local::mode::components::sparedisk; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; + +#06/15/2016 09:26:42.015 Verify Spare Status Test +#There are 1 non available Spare[s], deferred service is ON. +#06/15/2016 09:26:42.046 Verify Spare Status: Test Succeeded. + +sub check { + my ($self) = @_; + + $self->{output}->output_add(long_msg => "Checking spare disks"); + $self->{components}->{sparedisk} = {name => 'spare disks', total => 0, skip => 0}; + return if ($self->check_filter(section => 'sparedisk')); + + if ($self->{content_file_health} !~ /There are (\d+) non available Spare/msi) { + $self->{output}->output_add(long_msg => 'skipping: cannot find spare disks'); + return ; + } + + my $value = $1; + $self->{components}->{sparedisk}->{total}++; + + my ($exit, $warn, $crit) = $self->get_severity_numeric(section => 'sparedisk', instance => '1', value => $value); + $self->{output}->output_add(long_msg => sprintf("'%s' spare disk non availabled", + $value)); + if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { + $self->{output}->output_add(severity => $exit, + short_msg => sprintf("'%s' spare disk non availabled", + $value)); + } + + $self->{output}->perfdata_add(label => "disk_spare_non_available", + value => $value, + warning => $warn, + critical => $crit, min => 0); +} + +1; diff --git a/storage/emc/symmetrix/vmax/local/mode/components/temperature.pm b/storage/emc/symmetrix/vmax/local/mode/components/temperature.pm new file mode 100644 index 000000000..b1e561074 --- /dev/null +++ b/storage/emc/symmetrix/vmax/local/mode/components/temperature.pm @@ -0,0 +1,74 @@ +# +# Copyright 2016 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package storage::emc::symmetrix::vmax::local::mode::components::temperature; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::misc; + +#Temperatures check: OK +# +#+-------------------+------------+------------+------------------+--------+ +#| Module | Temp [°C] | Temp [°F] | High Limit [°C] | Status | +#+-------------------+------------+------------+------------------+--------+ +#| ES-PWS-A ES-4 | 24 | 75 | | OK | +#| ES-PWS-B ES-4 | 22 | 71 | | OK | +#| DIR-7 ES-4 | 34 | 93 | | OK | +#| DIR-8 ES-4 | 36 | 96 | | OK | +#| DIMM-0 DIR-7 ES-4 | 43 | 109 | 88 | OK | +#| DIMM-1 DIR-7 ES-4 | 48 | 118 | 91 | OK | +#+-------------------+------------+------------+------------------+--------+ +#| Module | Temp [°C] | Temp [°F] | High Limit [°C] | Status | +#+-------------------+------------+------------+------------------+--------+ + +sub check { + my ($self) = @_; + + $self->{output}->output_add(long_msg => "Checking temperatures"); + $self->{components}->{temperature} = {name => 'temperatures', total => 0, skip => 0}; + return if ($self->check_filter(section => 'temperature')); + + if ($self->{content_file_health_env} !~ /Ethernet cabling.*?Temperatures check.*?---------.*?Module.*?---------.*?\n(.*?\n)\+---------/msi) { + $self->{output}->output_add(long_msg => 'skipping: cannot find temperatures'); + return ; + } + + my $content = $1; + while ($content =~ /^\|(.*?)\|.*?\|.*?\|.*?\|(.*?)\|.*?\n/msig) { + my ($temperature, $status) = (centreon::plugins::misc::trim($1), centreon::plugins::misc::trim($2)); + + next if ($self->check_filter(section => 'temperature', instance => $temperature)); + $self->{components}->{temperature}->{total}++; + + $self->{output}->output_add(long_msg => sprintf("temperature '%s' status is '%s'", + $temperature, $status)); + my $exit = $self->get_severity(label => 'default', section => 'temperature', value => $status); + if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { + $self->{output}->output_add(severity => $exit, + short_msg => sprintf("Temperature '%s' status is '%s'", + $temperature, $status)); + } + } +} + +1; diff --git a/storage/emc/symmetrix/vmax/local/mode/components/voltage.pm b/storage/emc/symmetrix/vmax/local/mode/components/voltage.pm new file mode 100644 index 000000000..195f17ddf --- /dev/null +++ b/storage/emc/symmetrix/vmax/local/mode/components/voltage.pm @@ -0,0 +1,55 @@ +# +# Copyright 2016 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package storage::emc::symmetrix::vmax::local::mode::components::voltage; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::misc; + +#Voltages check: OK +sub check { + my ($self) = @_; + + $self->{output}->output_add(long_msg => "Checking voltage"); + $self->{components}->{voltage} = {name => 'voltage', total => 0, skip => 0}; + return if ($self->check_filter(section => 'voltage')); + + if ($self->{content_file_health_env} !~ /Voltages check:(.*?)\n/msi) { + $self->{output}->output_add(long_msg => 'skipping: cannot find voltage'); + return ; + } + + my $content = centreon::plugins::misc::trim($1); + $self->{components}->{voltage}->{total}++; + + $self->{output}->output_add(long_msg => sprintf("voltage status is '%s'", + $content)); + my $exit = $self->get_severity(label => 'default', section => 'voltage', value => $content); + if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { + $self->{output}->output_add(severity => $exit, + short_msg => sprintf("Voltage status is '%s'", + $content)); + } +} + +1; diff --git a/storage/emc/symmetrix/vmax/local/mode/hardware.pm b/storage/emc/symmetrix/vmax/local/mode/hardware.pm new file mode 100644 index 000000000..8f2b7429a --- /dev/null +++ b/storage/emc/symmetrix/vmax/local/mode/hardware.pm @@ -0,0 +1,273 @@ +# +# Copyright 2016 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package storage::emc::symmetrix::vmax::local::mode::hardware; + +use base qw(centreon::plugins::templates::hardware); + +use strict; +use warnings; +use centreon::plugins::misc; +use centreon::plugins::statefile; + +sub set_system { + my ($self, %options) = @_; + + $self->{regexp_threshold_numeric_check_section_option} = '^(sparedisk)$'; + + $self->{cb_hook1} = 'read_files'; + $self->{cb_hook4} = 'send_email'; + + $self->{thresholds} = { + default => [ + ['Recoverable Error', 'OK'], # Fabric + ['Online', 'OK'], + ['Up', 'OK'], + ['OK', 'OK'], + ['.*', 'CRITICAL'], + ], + }; + + $self->{components_path} = 'storage::emc::symmetrix::vmax::local::mode::components'; + $self->{components_module} = ['module', 'temperature', 'director', 'cabling', 'power', 'fabric', 'voltage', 'sparedisk']; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $self->{statefile_cache}->check_options(%options); +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, no_absent => 1); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => + { + "file-health:s" => { name => 'file_health' }, + "file-health-env:s" => { name => 'file_health_env' }, + # Email + "email-warning:s" => { name => 'email_warning' }, + "email-critical:s" => { name => 'email_critical' }, + "email-smtp-host:s" => { name => 'email_smtp_host' }, + "email-smtp-username:s" => { name => 'email_smtp_username' }, + "email-smtp-password:s" => { name => 'email_smtp_password' }, + "email-smtp-from:s" => { name => 'email_smtp_from' }, + "email-smtp-options:s@" => { name => 'email_smtp_options' }, + "email-memory" => { name => 'email_memory' }, + }); + + $self->{statefile_cache} = centreon::plugins::statefile->new(%options); + $self->{components_exec_load} = 0; + return $self; +} + +sub read_files { + my ($self, %options) = @_; + + if (!defined($self->{option_results}->{file_health}) || !defined($self->{option_results}->{file_health_env})) { + $self->{output}->add_option_msg(short_msg => "Please set option --file-health and --file-health-env."); + $self->{output}->option_exit(); + } + + foreach (('file_health', 'file_health_env')) { + $self->{'content_' . $_} = do { + local $/ = undef; + if (!open my $fh, "<", $self->{option_results}->{$_}) { + $self->{output}->add_option_msg(short_msg => "Could not open file $self->{option_results}->{$_} : $!"); + $self->{output}->option_exit(); + } + <$fh>; + }; + # We remove color syntax + $self->{'content_' . $_} =~ s/\x{1b}\[.*?m|\r//msg; + } + + #Health Check Results Log: + #Service Processor Date: 06/15/2016 09:26:41 + #Symmetrix Date from director 07c: 06/15/2016 09:33:45 + #The time difference between Service Processor and Symmetrix is : 00:07:03.672 + #System SN: 000292602920 + #System Model: VMAX20K + #mCode Level: 5876.288 + my ($serial) = ('unknown'); + $serial = $1 if ($self->{content_file_health} =~ /System SN:\s*(\S+)/msi); + + $self->{output}->output_add(long_msg => sprintf('serial number: %s', $serial)); +} + +# +# maybe we should add it in core (with cleaner code ;) +# + +sub send_email { + my ($self, %options) = @_; + + ####### + # Check SMTP options + return if (!((defined($self->{option_results}->{email_warning}) && $self->{option_results}->{email_warning} ne '') + || (defined($self->{option_results}->{email_critical}) && $self->{option_results}->{email_critical} ne ''))); + + if (!defined($self->{option_results}->{email_smtp_host})) { + $self->{output}->add_option_msg(short_msg => "Please set the --email-smtp-host option"); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{email_smtp_from})) { + $self->{output}->add_option_msg(short_msg => "Please set --email-smtp-from option"); + $self->{output}->option_exit(); + } + + my %smtp_options = ('-auth' => 'none'); + if (defined($self->{option_results}->{email_smtp_username}) && $self->{option_results}->{email_smtp_username} ne '') { + $smtp_options{-login} = $self->{option_results}->{email_smtp_username}; + delete $smtp_options{-auth}; + } + if (defined($self->{option_results}->{email_smtp_username}) && defined($self->{option_results}->{email_smtp_password})) { + $smtp_options{-pass} = $self->{option_results}->{email_smtp_password}; + } + foreach my $option (@{$self->{option_results}->{email_smtp_options}}) { + next if ($option !~ /^(.+?)=(.+)$/); + $smtp_options{-$1} = $2; + } + + ####### + # Get current data + my $stdout; + { + local *STDOUT; + open STDOUT, '>', \$stdout; + $self->{output}->display(force_long_output => 1); + } + + $stdout =~ /^(.*?)(\||\n)/msi; + my $subject = $1; + my $status = lc($self->{output}->get_litteral_status()); + + my $send_email = 0; + $send_email = 1 if ($status ne 'ok'); + ####### + # Check memory file + if (defined($self->{option_results}->{email_memory})) { + $self->{new_datas} = { status => $status, output => $subject }; + $self->{statefile_cache}->read(statefile => "cache_emc_symmetrix_vmax_email"); + my $prev_status = $self->{statefile_cache}->get(name => 'status'); + my $prev_output = $self->{statefile_cache}->get(name => 'output'); + # non-ok output is the same + $send_email = 0 if ($status ne 'ok' && defined($prev_output) && $prev_output eq $subject); + # recovery email + $send_email = 1 if ($status eq 'ok' && defined($prev_status) && $prev_status ne 'ok'); + $self->{statefile_cache}->write(data => $self->{new_datas}); + } + + my $smtp_to = ''; + $smtp_to = $self->{option_results}->{email_warning} if ($status eq 'warning' && defined($self->{option_results}->{email_warning} && $self->{option_results}->{email_warning}) ne ''); + $smtp_to = $self->{option_results}->{email_critical} if ($status eq 'critical' && defined($self->{option_results}->{email_critical} && $self->{option_results}->{email_critical}) ne ''); + if ($send_email == 1 && $status eq 'ok') { + my $append = ''; + foreach (('email_warning', 'email_critical')) { + if (defined($self->{option_results}->{$_}) && $self->{option_results}->{$_} ne '') { + $smtp_to .= $append . $self->{option_results}->{$_}; + $append .= ','; + } + } + } + + if ($send_email == 0) { + $self->{output}->add_option_msg(severity => 'OK', short_msg => "No email to send"); + return ; + } + + centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'Email::Send::SMTP::Gmail', + error_msg => "Cannot load module 'Email::Send::SMTP::Gmail'."); + my ($mail, $error) = Email::Send::SMTP::Gmail->new(-smtp => $self->{option_results}->{email_smtp_host}, + %smtp_options); + if ($mail == -1) { + $self->{output}->add_option_msg(short_msg => "session error: " . $error); + $self->{output}->option_exit(); + } + my $result = $mail->send(-to => $smtp_to, + -from => $self->{option_results}->{email_smtp_from}, + -subject => $subject, + -body => $stdout, + -attachments => $self->{option_results}->{file_health_env}); + $mail->bye(); + if ($result == -1) { + $self->{output}->add_option_msg(severity => 'UNKNOWN', short_msg => "problem to send the email"); + } else { + $self->{output}->add_option_msg(severity => 'OK', short_msg => "email sent"); + } +} + +1; + +__END__ + +=head1 MODE + +Check hardware. + +=over 8 + +=item B<--component> + +Which component to check (Default: '.*'). +Can be: 'module', 'temperature', 'director, 'cabling', 'power', 'voltage', 'sparedisk'. + +=item B<--filter> + +Exclude some parts (comma seperated list) (Example: --filter=temperature --filter=module) +Can also exclude specific instance: --filter=temperature,ES-PWS-A ES-4 + +=item B<--no-component> + +Return an error if no compenents are checked. +If total (with skipped) is 0. (Default: 'critical' returns). + +=item B<--threshold-overload> + +Set to overload default threshold values (syntax: section,[instance,]status,regexp) +It used before default thresholds (order stays). +Example: --threshold-overload='director,WARNING,^(?!(OK)$)' + +=item B<--warning> + +Set warning threshold for disk (syntax: type,regexp,threshold) +Example: --warning='sparedisk,.*,5:' + +=item B<--critical> + +Set critical threshold for disk (syntax: type,regexp,threshold) +Example: --critical='sparedisk,.*,3:' + +=item B<--file-health> + +The location of the global storage file status (Should be something like: C:/xxxx/HealthCheck.log). + +=item B<--file-health-env> + +The location of the environment storage file status (Should be something like: C:/xxxx/sumpl_env_health.log). + +=back + +=cut + \ No newline at end of file diff --git a/storage/emc/symmetrix/vmax/local/plugin.pm b/storage/emc/symmetrix/vmax/local/plugin.pm new file mode 100644 index 000000000..18fc9a995 --- /dev/null +++ b/storage/emc/symmetrix/vmax/local/plugin.pm @@ -0,0 +1,48 @@ +# +# Copyright 2016 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package storage::emc::symmetrix::vmax::local::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; + + $self->{version} = '0.1'; + %{$self->{modes}} = ( + 'hardware' => 'storage::emc::symmetrix::vmax::local::mode::hardware', + ); + + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check vmax storage. The plugin needs to be installed on Windows Management. + +=cut From 4bd14895a18048cedd26c30f7d68d0b59901d977 Mon Sep 17 00:00:00 2001 From: Sims24 Date: Thu, 16 Jun 2016 16:52:47 +0200 Subject: [PATCH 26/30] + merge hanodes.pm and hastates.pm --- network/netasq/snmp/mode/hanodes.pm | 82 +++++++++++++++++++++++++---- 1 file changed, 73 insertions(+), 9 deletions(-) diff --git a/network/netasq/snmp/mode/hanodes.pm b/network/netasq/snmp/mode/hanodes.pm index d12d6c8fd..3734a0fdd 100644 --- a/network/netasq/snmp/mode/hanodes.pm +++ b/network/netasq/snmp/mode/hanodes.pm @@ -27,6 +27,31 @@ use warnings; my $instance_mode; +sub custom_node_threshold { + my ($self, %options) = @_; + + my ($exit, $threshold_value); + $threshold_value = defined($instance_mode->{option_results}->{percent}) ? $self->{result_values}->{prct_dead} : $self->{result_values}->{dead_nodes} ; + $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{label}, exit_litteral => 'critical' }, { label => 'warning-'. $self->{label}, exit_litteral => 'warning' } ]); + return $exit; +} + +sub custom_node_output { + my ($self, %options) = @_; + + my $msg = sprintf("Dead nodes: absolute: %d - percentage: %.2f%% ", $self->{result_values}->{dead_nodes}, $self->{result_values}->{prct_dead}); + return $msg; +} + +sub custom_node_calc { + my ($self, %options) = @_; + + $self->{result_values}->{dead_nodes} = $options{new_datas}->{$self->{instance} . '_dead_nodes'}; + $self->{result_values}->{prct_dead} = $options{new_datas}->{$self->{instance} . '_prct_dead'}; + + return 0; +} + sub custom_threshold_output { my ($self, %options) = @_; my $status = 'ok'; @@ -70,9 +95,25 @@ sub set_counters { my ($self, %options) = @_; $self->{maps_counters_type} = [ + { name => 'global', type => 0 }, { name => 'nodes', type => 1, cb_prefix_output => 'prefix_node_output', message_multiple => 'All HA nodes are OK' }, ]; + $self->{maps_counters}->{global} = [ + { label => 'dead-nodes', set => { + key_values => [ { name => 'dead_nodes' }, { name => 'prct_dead' } ], + closure_custom_calc => \&custom_node_calc, + closure_custom_output => \&custom_node_output, + closure_custom_threshold_check => \&custom_node_threshold, + perfdatas => [ + { label => 'dead_nodes', value => 'dead_nodes', template => '%d', + min => 0, unit => 'nodes' }, + { label => 'prct_dead', value => 'prct_dead', template => '%.2f', + min => 0, unit => '%' }, + ], + } + } + ]; $self->{maps_counters}->{nodes} = [ { label => 'state', threshold => 0, set => { key_values => [ { name => 'state' }, { name => 'display' } ], @@ -84,7 +125,7 @@ sub set_counters { }, { label => 'health', set => { key_values => [ { name => 'health' }, { name => 'display' } ], - output_template => 'node Health: %s%%', + output_template => 'node health: %s%%', perfdatas => [ { label => 'health', value => 'health_absolute', template => '%d', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display_absolute' }, @@ -109,6 +150,7 @@ sub new { $options{options}->add_options(arguments => { "filter-node:s" => { name => 'filter_node' }, + "percent" => { name => 'percent' }, "warning-state:s" => { name => 'warning_state', default => '' }, "critical-state:s" => { name => 'critical_state', default => '%{state} eq "offline"' }, }); @@ -145,20 +187,25 @@ my $mapping = { }; my $oid_ntqNodeTable = '.1.3.6.1.4.1.11256.1.11.7'; +my $oid_ntqNbNode = '.1.3.6.1.4.1.11256.1.11.1'; +my $oid_ntqNbDeadNode = '.1.3.6.1.4.1.11256.1.11.2'; sub manage_selection { my ($self, %options) = @_; $self->{snmp} = $options{snmp}; - $self->{results} = $options{snmp}->get_table(oid => $oid_ntqNodeTable, - nothing_quit => 1); - foreach my $oid (keys %{$self->{results}}) { + $self->{results} = $options{snmp}->get_multiple_table(oids => [ { oid => $oid_ntqNodeTable }, + { oid => $oid_ntqNbNode }, + { oid => $oid_ntqNbDeadNode } ], + nothing_quit => 1); + + foreach my $oid (keys %{$self->{results}->{$oid_ntqNodeTable}}) { $oid =~ /^$mapping->{ntqOnline}->{oid}\.(.*)$/; my $instance = $1; - my $result = $self->{snmp}->map_instance(mapping => $mapping, results => $self->{results}, instance => $instance); + my $result = $self->{snmp}->map_instance(mapping => $mapping, results => $self->{results}->{$oid_ntqNodeTable}, instance => $instance); - if (defined($self->{option_results}->{filter_nodes}) && $self->{option_results}->{filter_nodes} ne '' && - $result->{ntqFwSerial} !~ /$self->{option_results}->{filter_nodes}/) { + if (defined($self->{option_results}->{filter_node}) && $self->{option_results}->{filter_node} ne '' && + $result->{ntqFwSerial} !~ /$self->{option_results}->{filter_node}/) { $self->{output}->output_add(long_msg => "Skipping '" . $result->{ntqFwSerial} . "': no matching filter.", debug => 1); next; } @@ -166,6 +213,11 @@ sub manage_selection { health => $result->{ntqHAQuality}, display => $result->{ntqFwSerial} }; } + + my $prct_dead = $self->{results}->{$oid_ntqNbDeadNode}->{$oid_ntqNbDeadNode . '.' . '0'}/$self->{results}->{$oid_ntqNbNode}->{$oid_ntqNbNode . '.' . '0'}*100; + + $self->{global} = { dead_nodes => $self->{results}->{$oid_ntqNbDeadNode}->{$oid_ntqNbDeadNode . '.' . '0'}, + prct_dead => $prct_dead }; } 1; @@ -174,13 +226,13 @@ __END__ =head1 MODE -Check Netasq node state and health +Check Netasq dead nodes and state and health of nodes =over 8 =item B<--filter-node> -Filter name with regexp. +Filter name with regexp (based on serial) =item B<--warning-health> @@ -190,6 +242,14 @@ Warning on health level. (e.g --warning 90:) Critical on health level. (e.g --critical 80:) +=item B<--warning-dead-nodes> + +Warning on deadnode (absolute unless --percent is used) + +=item B<--critical-dead-nodes> + +Critical on deadnode (absolute unless --percent is used) + =item B<--warning-state> Set warning threshold for status. Use "%{state}" as a special variable. @@ -200,6 +260,10 @@ Value can be 'online' or 'offline'. Set critical threshold for status. Use "%{state}" as a special variable. Value can be 'online' or 'offline'. +=item B<--percent> + +Set this option if you want to warn on percent + =back =cut From 65bc2c0c1fd21ed5885065bc0b8770349d7a247e Mon Sep 17 00:00:00 2001 From: Sims24 Date: Fri, 17 Jun 2016 08:38:11 +0200 Subject: [PATCH 27/30] + remove ambigous perfdata for prct_dead --- network/netasq/snmp/mode/hanodes.pm | 2 -- 1 file changed, 2 deletions(-) diff --git a/network/netasq/snmp/mode/hanodes.pm b/network/netasq/snmp/mode/hanodes.pm index 3734a0fdd..8a11e801f 100644 --- a/network/netasq/snmp/mode/hanodes.pm +++ b/network/netasq/snmp/mode/hanodes.pm @@ -108,8 +108,6 @@ sub set_counters { perfdatas => [ { label => 'dead_nodes', value => 'dead_nodes', template => '%d', min => 0, unit => 'nodes' }, - { label => 'prct_dead', value => 'prct_dead', template => '%.2f', - min => 0, unit => '%' }, ], } } From 5dc79776ed295fb08ba9ecc520ece4664008dab3 Mon Sep 17 00:00:00 2001 From: Sims24 Date: Fri, 17 Jun 2016 08:38:49 +0200 Subject: [PATCH 28/30] + delete hastatus.pm Deprecated - now part of hanodes.pm --- network/netasq/snmp/mode/hastatus.pm | 105 --------------------------- 1 file changed, 105 deletions(-) delete mode 100644 network/netasq/snmp/mode/hastatus.pm diff --git a/network/netasq/snmp/mode/hastatus.pm b/network/netasq/snmp/mode/hastatus.pm deleted file mode 100644 index 41eb97e6b..000000000 --- a/network/netasq/snmp/mode/hastatus.pm +++ /dev/null @@ -1,105 +0,0 @@ -# -# Copyright 2016 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 network::netasq::snmp::mode::hastatus; - -use base qw(centreon::plugins::mode); - -use strict; -use warnings; - -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 => - { - "warning:s" => { name => 'warning' }, - "critical:s" => { name => 'critical' }, - }); - - return $self; -} - -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'."); - $self->{output}->option_exit(); - } -} - -sub run { - my ($self, %options) = @_; - # $options{snmp} = snmp object - $self->{snmp} = $options{snmp}; - - my $oid_ntqNbNode = '.1.3.6.1.4.1.11256.1.11.1.0'; - my $oid_ntqNbDeadNode = '.1.3.6.1.4.1.11256.1.11.2.0'; - my $result = $self->{snmp}->get_leef(oids => [$oid_ntqNbNode, $oid_ntqNbDeadNode], nothing_quit => 1); - - if ($result->{$oid_ntqNbNode} == 1) { - $self->{output}->output_add(severity => 'OK', - short_msg => "Only one node. No ha."); - } else { - my $exit = $self->{perfdata}->threshold_check(value => $result->{$oid_ntqNbDeadNode}, threshold => [ { label => 'critical', exit_litteral => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); - $self->{output}->output_add(severity => $exit, - short_msg => sprintf("%d dead nodes on %d nodes", - $result->{$oid_ntqNbDeadNode}, $result->{$oid_ntqNbNode})); - $self->{output}->perfdata_add(label => 'dead_nodes', - value => $result->{$oid_ntqNbDeadNode}, - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'), - min => 0, max => $result->{$oid_ntqNbNode}); - } - - $self->{output}->display(); - $self->{output}->exit(); -} - -1; - -__END__ - -=head1 MODE - -Check ha status. - -=over 8 - -=item B<--warning> - -Threshold warning (number of dead nodes). - -=item B<--critical> - -Threshold critical (number of dead nodes). - -=back - -=cut From 2a52fe61dda153b6e56b2479725c306786822847 Mon Sep 17 00:00:00 2001 From: Sims24 Date: Fri, 17 Jun 2016 09:32:55 +0200 Subject: [PATCH 29/30] + remove deprecated mode ha-status --- network/netasq/snmp/plugin.pm | 1 - 1 file changed, 1 deletion(-) diff --git a/network/netasq/snmp/plugin.pm b/network/netasq/snmp/plugin.pm index a77fcd082..8152bc355 100644 --- a/network/netasq/snmp/plugin.pm +++ b/network/netasq/snmp/plugin.pm @@ -38,7 +38,6 @@ sub new { 'interfaces' => 'snmp_standard::mode::interfaces', 'list-interfaces' => 'snmp_standard::mode::listinterfaces', 'load' => 'snmp_standard::mode::loadaverage', - 'ha-status' => 'network::netasq::snmp::mode::hastatus', 'ha-nodes' => 'network::netasq::snmp::mode::hanodes', 'memory' => 'os::freebsd::snmp::mode::memory', 'storage' => 'snmp_standard::mode::storage', From d4a9eef7bb8b0498cb38166041efeaffa6ca761c Mon Sep 17 00:00:00 2001 From: Sims24 Date: Fri, 17 Jun 2016 09:35:34 +0200 Subject: [PATCH 30/30] + Add total number of nodes --- network/netasq/snmp/mode/hanodes.pm | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/network/netasq/snmp/mode/hanodes.pm b/network/netasq/snmp/mode/hanodes.pm index 8a11e801f..4936330d2 100644 --- a/network/netasq/snmp/mode/hanodes.pm +++ b/network/netasq/snmp/mode/hanodes.pm @@ -39,7 +39,8 @@ sub custom_node_threshold { sub custom_node_output { my ($self, %options) = @_; - my $msg = sprintf("Dead nodes: absolute: %d - percentage: %.2f%% ", $self->{result_values}->{dead_nodes}, $self->{result_values}->{prct_dead}); + my $msg = sprintf("Dead nodes: absolute: %d/%d - percentage: %.2f%% ", + $self->{result_values}->{dead_nodes}, $self->{result_values}->{total_nodes}, $self->{result_values}->{prct_dead}); return $msg; } @@ -47,6 +48,7 @@ sub custom_node_calc { my ($self, %options) = @_; $self->{result_values}->{dead_nodes} = $options{new_datas}->{$self->{instance} . '_dead_nodes'}; + $self->{result_values}->{total_nodes} = $options{new_datas}->{$self->{instance} . '_total_nodes'}; $self->{result_values}->{prct_dead} = $options{new_datas}->{$self->{instance} . '_prct_dead'}; return 0; @@ -101,7 +103,7 @@ sub set_counters { $self->{maps_counters}->{global} = [ { label => 'dead-nodes', set => { - key_values => [ { name => 'dead_nodes' }, { name => 'prct_dead' } ], + key_values => [ { name => 'dead_nodes' }, { name => 'prct_dead' }, { name => 'total_nodes' } ], closure_custom_calc => \&custom_node_calc, closure_custom_output => \&custom_node_output, closure_custom_threshold_check => \&custom_node_threshold, @@ -215,6 +217,7 @@ sub manage_selection { my $prct_dead = $self->{results}->{$oid_ntqNbDeadNode}->{$oid_ntqNbDeadNode . '.' . '0'}/$self->{results}->{$oid_ntqNbNode}->{$oid_ntqNbNode . '.' . '0'}*100; $self->{global} = { dead_nodes => $self->{results}->{$oid_ntqNbDeadNode}->{$oid_ntqNbDeadNode . '.' . '0'}, + toatl_nodes => $self->{results}->{$oid_ntqNbDeadNode}->{$oid_ntqNbNode . '.' . '0'}, prct_dead => $prct_dead }; }