From 045845d9b452fa07035e06cbd7c36282e295835c Mon Sep 17 00:00:00 2001 From: cgagnaire Date: Wed, 4 Jul 2018 17:50:42 +0200 Subject: [PATCH] add cloudfoundry plugin (wip) (#1031) --- cloud/cloudfoundry/restapi/custom/api.pm | 341 ++++++++++++++++++ cloud/cloudfoundry/restapi/mode/appsstate.pm | 235 ++++++++++++ .../restapi/mode/instancesstate.pm | 287 +++++++++++++++ cloud/cloudfoundry/restapi/mode/listapps.pm | 163 +++++++++ .../restapi/mode/listorganizations.pm | 118 ++++++ cloud/cloudfoundry/restapi/mode/listspaces.pm | 137 +++++++ cloud/cloudfoundry/restapi/plugin.pm | 52 +++ 7 files changed, 1333 insertions(+) create mode 100644 cloud/cloudfoundry/restapi/custom/api.pm create mode 100644 cloud/cloudfoundry/restapi/mode/appsstate.pm create mode 100644 cloud/cloudfoundry/restapi/mode/instancesstate.pm create mode 100644 cloud/cloudfoundry/restapi/mode/listapps.pm create mode 100644 cloud/cloudfoundry/restapi/mode/listorganizations.pm create mode 100644 cloud/cloudfoundry/restapi/mode/listspaces.pm create mode 100644 cloud/cloudfoundry/restapi/plugin.pm diff --git a/cloud/cloudfoundry/restapi/custom/api.pm b/cloud/cloudfoundry/restapi/custom/api.pm new file mode 100644 index 000000000..8cfd9b193 --- /dev/null +++ b/cloud/cloudfoundry/restapi/custom/api.pm @@ -0,0 +1,341 @@ +# +# Copyright 2018 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::cloudfoundry::restapi::custom::api; + +use strict; +use warnings; +use centreon::plugins::http; +use JSON::XS; +use URI::Encode; + +sub new { + my ($class, %options) = @_; + my $self = {}; + bless $self, $class; + + if (!defined($options{output})) { + print "Class Custom: Need to specify 'output' argument.\n"; + exit 3; + } + if (!defined($options{options})) { + $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument."); + $options{output}->option_exit(); + } + + if (!defined($options{noptions})) { + $options{options}->add_options(arguments => + { + "hostname:s" => { name => 'hostname' }, + "port:s" => { name => 'port' }, + "proto:s" => { name => 'proto' }, + "username:s" => { name => 'username' }, + "password:s" => { name => 'password' }, + "proxyurl:s" => { name => 'proxyurl' }, + "timeout:s" => { name => 'timeout' }, + "ssl-opt:s@" => { name => 'ssl_opt' }, + "api-path:s" => { name => 'api_path' }, + "api-username:s" => { name => 'api_username' }, + "api-password:s" => { name => 'api_password' }, + }); + } + $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); + + $self->{output} = $options{output}; + $self->{mode} = $options{mode}; + $self->{http} = centreon::plugins::http->new(output => $self->{output}); + + return $self; + +} + +sub set_options { + my ($self, %options) = @_; + + $self->{option_results} = $options{option_results}; +} + +sub set_defaults { + my ($self, %options) = @_; + + foreach (keys %{$options{default}}) { + if ($_ eq $self->{mode}) { + for (my $i = 0; $i < scalar(@{$options{default}->{$_}}); $i++) { + foreach my $opt (keys %{$options{default}->{$_}[$i]}) { + if (!defined($self->{option_results}->{$opt}[$i])) { + $self->{option_results}->{$opt}[$i] = $options{default}->{$_}[$i]->{$opt}; + } + } + } + } + } +} + +sub check_options { + my ($self, %options) = @_; + + $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : undef; + $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443; + $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https'; + $self->{username} = (defined($self->{option_results}->{username})) ? $self->{option_results}->{username} : 'cf'; + $self->{password} = (defined($self->{option_results}->{password})) ? $self->{option_results}->{password} : ''; + $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10; + $self->{proxyurl} = (defined($self->{option_results}->{proxyurl})) ? $self->{option_results}->{proxyurl} : undef; + $self->{ssl_opt} = (defined($self->{option_results}->{ssl_opt})) ? $self->{option_results}->{ssl_opt} : undef; + $self->{api_path} = (defined($self->{option_results}->{api_path})) ? $self->{option_results}->{api_path} : '/v2'; + $self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : undef; + $self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : undef; + + if (!defined($self->{hostname})) { + $self->{output}->add_option_msg(short_msg => "Need to specify hostname option."); + $self->{output}->option_exit(); + } + if (!defined($self->{api_username})) { + $self->{output}->add_option_msg(short_msg => "Need to specify API username option."); + $self->{output}->option_exit(); + } + if (!defined($self->{api_password})) { + $self->{output}->add_option_msg(short_msg => "Need to specify API password option."); + $self->{output}->option_exit(); + } + + return 0; +} + +sub build_options_for_httplib { + my ($self, %options) = @_; + + $self->{option_results}->{hostname} = $self->{hostname}; + $self->{option_results}->{port} = $self->{port}; + $self->{option_results}->{proto} = $self->{proto}; + $self->{option_results}->{username} = $self->{username}; + $self->{option_results}->{password} = $self->{password}; + $self->{option_results}->{timeout} = $self->{timeout}; + $self->{option_results}->{proxyurl} = $self->{proxyurl}; + $self->{option_results}->{ssl_opt} = $self->{ssl_opt}; + $self->{option_results}->{warning_status} = ''; + $self->{option_results}->{critical_status} = ''; +} + +sub settings { + my ($self, %options) = @_; + + $self->build_options_for_httplib(); + $self->{http}->add_header(key => 'Accept', value => 'application/json'); + $self->{http}->add_header(key => 'Content-Type', value => 'application/x-www-form-urlencoded'); + if (defined($self->{access_token})) { + $self->{http}->add_header(key => 'Authorization', value => 'Bearer ' . $self->{access_token}); + } + $self->{http}->set_options(%{$self->{option_results}}); +} + +sub request_api { + my ($self, %options) = @_; + + my $content = $self->{http}->request(method => $options{method}, url_path => $self->{api_path} . $options{url_path}, + query_form_post => $options{query_form_post}, critical_status => '', warning_status => '', unknown_status => ''); + my $response = $self->{http}->get_response(); + my $decoded; + eval { + $decoded = decode_json($content); + }; + if ($@) { + $self->{output}->output_add(long_msg => $content, debug => 1); + $self->{output}->add_option_msg(short_msg => "Cannot decode json response"); + $self->{output}->option_exit(); + } + if ($response->code() != 200) { + $self->{output}->add_option_msg(short_msg => "Error code: " . $decoded->{error_code} . ". Description: " . $decoded->{description}); + $self->{output}->option_exit(); + } + + return $decoded; +} + +sub get_info { + my ($self, %options) = @_; + + $self->settings(); + + return $self->request_api(method => 'GET', url_path => '/info'); +} + + +sub get_access_token { + my ($self, %options) = @_; + + my $info = $self->get_info(); + + my $uri = URI::Encode->new({encode_reserved => 1}); + my $encoded_username = $uri->encode($self->{api_username}); + my $encoded_password = $uri->encode($self->{api_password}); + + my $content = $self->{http}->request(method => 'POST', query_form_post => 'username=' . $encoded_username . '&password=' . $encoded_password . '&grant_type=password', + full_url => $info->{authorization_endpoint} . '/oauth/token', hostname => '', + basic => 1, credentials => 1, username => $self->{username}, password => $self->{password}); + my $decoded; + eval { + $decoded = decode_json($content); + }; + if ($@) { + $self->{output}->output_add(long_msg => $content, debug => 1); + $self->{output}->add_option_msg(short_msg => "Cannot decode json response"); + $self->{output}->option_exit(); + } + + return $decoded->{access_token}; +} + +sub get_object_v2 { + my ($self, %options) = @_; + + $self->settings(); + + my $url_path = $options{url_path}; + + my @result; + while(my $content = $self->request_api(%options)) { + if (defined($content->{resources})) { + push @result, @{$content->{resources}}; + last if (!defined($content->{next_url}) || + $content->{next_url} !~ /^$self->{api_path}$url_path.*&page=(.*).*/ || + $content->{next_url} !~ /^$self->{api_path}$url_path.*?page=(.*).*/); + my $page = $1; + $options{url_path} = $url_path . '?page=' . $page; + } else { + return $content; + last; + } + } + + return \@result; +} + +sub get_object_v3 { + my ($self, %options) = @_; + + $self->settings(); + + my $url_path = $options{url_path}; + + my @result; + while(my $content = $self->request_api(%options)) { + if (defined($content->{resources})) { + push @result, @{$content->{resources}}; + last if (!defined($content->{pagination}->{next}->{href}) || + $content->{pagination}->{next}->{href} !~ /^$self->{proto}\:\/\/$self->{hostname}$self->{api_path}$url_path.*&page=(.*).*/ || + $content->{pagination}->{next}->{href} !~ /^$self->{proto}\:\/\/$self->{hostname}$self->{api_path}$url_path.*?page=(.*).*/); + my $page = $1; + $options{url_path} = $url_path . '?page=' . $page; + } else { + push @result, $content; + last; + } + } + + return \@result; +} + +sub get_object { + my ($self, %options) = @_; + + if (!defined($self->{access_token})) { + $self->{access_token} = $self->get_access_token(); + } + + my $info = $self->get_info(); + + if ($info->{api_version} =~ /^2\..*/) { + return $self->get_object_v2(%options); + } else { + return $self->get_object_v3(%options); # Not tested + } + + return undef; +} + +1; + +__END__ + +=head1 NAME + +Cloud Foundry REST API + +=head1 SYNOPSIS + +Cloud Foundry Rest API custom mode + +=head1 REST API OPTIONS + +=over 8 + +=item B<--hostname> + +Cloud Foundry API hostname. + +=item B<--api-path> + +Cloud Foundry API url path (Default: '/v2') + +=item B<--api-username> + +Cloud Foundry API username. + +=item B<--api-password> + +Cloud Foundry API password. + +=item B<--port> + +Cloud Foundry API port (Default: 443) + +=item B<--proto> + +Specify https if needed (Default: 'https') + +=item B<--username> + +Authorization endpoint username (Default: 'cf') + +=item B<--password> + +Authorization endpoint password (Default: '') + +=item B<--proxyurl> + +Proxy URL if any + +=item B<--timeout> + +Set HTTP timeout + +=item B<--ssl-opt> + +Set SSL Options (Examples: --ssl-opt="SSL_version => TLSv1" +--ssl-opt="SSL_verify_mode => SSL_VERIFY_NONE"). + +=back + +=head1 DESCRIPTION + +B. + +=cut diff --git a/cloud/cloudfoundry/restapi/mode/appsstate.pm b/cloud/cloudfoundry/restapi/mode/appsstate.pm new file mode 100644 index 000000000..d80b5e41a --- /dev/null +++ b/cloud/cloudfoundry/restapi/mode/appsstate.pm @@ -0,0 +1,235 @@ +# +# Copyright 2018 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::cloudfoundry::restapi::mode::appsstate; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; + +my $instance_mode; + +sub custom_state_threshold { + 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}->{name} = $options{new_datas}->{$self->{instance} . '_name'}; + $self->{result_values}->{state} = $options{new_datas}->{$self->{instance} . '_state'}; + + return 0; +} + +sub prefix_output { + my ($self, %options) = @_; + + return "App '" . $options{instance_value}->{name} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0 }, + { name => 'apps', type => 1, cb_prefix_output => 'prefix_output', message_multiple => 'All apps state are ok' }, + ]; + + $self->{maps_counters}->{global} = [ + { label => 'started', set => { + key_values => [ { name => 'started' } ], + output_template => 'Started : %d', + perfdatas => [ + { label => 'started', value => 'started_absolute', template => '%d', + min => 0 }, + ], + } + }, + { label => 'stopped', set => { + key_values => [ { name => 'stopped' } ], + output_template => 'Stopped : %d', + perfdatas => [ + { label => 'stopped', value => 'stopped_absolute', template => '%d', + min => 0 }, + ], + } + }, + ]; + + $self->{maps_counters}->{apps} = [ + { label => 'state', set => { + key_values => [ { name => 'state' }, { name => 'name' } ], + closure_custom_calc => $self->can('custom_state_calc'), + closure_custom_output => $self->can('custom_state_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => $self->can('custom_state_threshold'), + } + }, + ]; +} + +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 => + { + "organization-guid:s" => { name => 'organization_guid' }, + "space-guid:s" => { name => 'space_guid' }, + "filter-name:s" => { name => 'filter_name' }, + "warning-state:s" => { name => 'warning_state' }, + "critical-state:s" => { name => 'critical_state', default => '%{state} !~ /STARTED/i' }, + }); + + 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); + + $instance_mode = $self; + $self->change_macros(); +} + +sub manage_selection { + my ($self, %options) = @_; + + my $apps; + if (defined($self->{option_results}->{organization_guid}) && $self->{option_results}->{organization_guid} ne '') { + my $spaces = $options{custom}->get_object(url_path => '/organizations/' . $self->{option_results}->{organization_guid} . '/spaces'); + foreach my $space (@{$spaces}) { + next if (!defined($space->{entity}->{apps_url}) || $space->{entity}->{apps_url} !~ /^\/v2(.*)/); + my $result = $options{custom}->get_object(url_path => $1); + push @{$apps}, @{$result}; + } + } elsif (defined($self->{option_results}->{space_guid}) && $self->{option_results}->{space_guid} ne '') { + $apps = $options{custom}->get_object(url_path => '/spaces/' . $self->{option_results}->{space_guid} . '/apps'); + } else { + $apps = $options{custom}->get_object(url_path => '/apps'); + } + + $self->{global}->{started} = 0; + $self->{global}->{stopped} = 0; + + foreach my $app (@{$apps}) { + if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && + $app->{entity}->{name} !~ /$self->{option_results}->{filter_name}/) { + $self->{output}->output_add(long_msg => "skipping app '" . $app->{entity}->{name} . "': no matching filter name.", debug => 1); + next; + } + + $self->{apps}->{$app->{metadata}->{guid}} = { + name => $app->{entity}->{name}, + state => $app->{entity}->{state} + }; + $self->{global}->{lc($app->{entity}->{state})}++; + } + + if (scalar(keys %{$self->{apps}}) <= 0) { + $self->{output}->add_option_msg(short_msg => "No entry found."); + $self->{output}->option_exit(); + } +} + +1; + +__END__ + +=head1 MODE + +Check Cloud Foundry app state. + +=over 8 + +=item B<--organization-guid> + +Only looks for apps from an organization. + +=item B<--space-guid> + +Only looks for apps from a space. + +=item B<--filter-name> + +Filter apps name (can be a regexp). + +=item B<--warning-state> + +Threshold warning. + +=item B<--critical-state> + +Threshold critical (Default: '%{state} !~ /STARTED/i'). + +=item B<--warning-*> + +Threshold warning for apps count based +on state (Can be: 'started', 'stopped') + +=item B<--critical-*> + +Threshold critical for apps count based +on state (Can be: 'started', 'stopped'). + +=back + +=cut diff --git a/cloud/cloudfoundry/restapi/mode/instancesstate.pm b/cloud/cloudfoundry/restapi/mode/instancesstate.pm new file mode 100644 index 000000000..2d89d0ad9 --- /dev/null +++ b/cloud/cloudfoundry/restapi/mode/instancesstate.pm @@ -0,0 +1,287 @@ +# +# Copyright 2018 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::cloudfoundry::restapi::mode::instancesstate; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; + +my $instance_mode; + +sub custom_app_state_threshold { + 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_app_state}) && $instance_mode->{option_results}->{critical_app_state} ne '' && + eval "$instance_mode->{option_results}->{critical_app_state}") { + $status = 'critical'; + } elsif (defined($instance_mode->{option_results}->{warning_app_state}) && $instance_mode->{option_results}->{warning_app_state} ne '' && + eval "$instance_mode->{option_results}->{warning_app_state}") { + $status = 'warning'; + } + }; + if (defined($message)) { + $self->{output}->output_add(long_msg => 'filter status issue: ' . $message); + } + + return $status; +} + +sub custom_app_state_output { + my ($self, %options) = @_; + + my $msg = sprintf("App '%s' state is '%s'", + $self->{result_values}->{name}, $self->{result_values}->{state}); + return $msg; +} + +sub custom_app_state_calc { + my ($self, %options) = @_; + + $self->{result_values}->{name} = $options{new_datas}->{$self->{instance} . '_name'}; + $self->{result_values}->{state} = $options{new_datas}->{$self->{instance} . '_state'}; + + return 0; +} + +sub custom_inst_state_threshold { + 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_instance_state}) && $instance_mode->{option_results}->{critical_instance_state} ne '' && + eval "$instance_mode->{option_results}->{critical_instance_state}") { + $status = 'critical'; + } elsif (defined($instance_mode->{option_results}->{warning_instance_state}) && $instance_mode->{option_results}->{warning_instance_state} ne '' && + eval "$instance_mode->{option_results}->{warning_instance_state}") { + $status = 'warning'; + } + }; + if (defined($message)) { + $self->{output}->output_add(long_msg => 'filter status issue: ' . $message); + } + + return $status; +} + +sub custom_inst_state_output { + my ($self, %options) = @_; + + my $msg = sprintf("state is '%s'", $self->{result_values}->{state}); + return $msg; +} + +sub custom_inst_state_calc { + my ($self, %options) = @_; + + $self->{result_values}->{id} = $options{new_datas}->{$self->{instance} . '_id'}; + $self->{result_values}->{state} = $options{new_datas}->{$self->{instance} . '_state'}; + + return 0; +} + +sub prefix_output { + my ($self, %options) = @_; + + return "Instance '" . $options{instance_value}->{id} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'app', type => 0 }, + { name => 'global', type => 0 }, + { name => 'instances', type => 1, cb_prefix_output => 'prefix_output', message_multiple => 'All instances state are ok' }, + ]; + + $self->{maps_counters}->{app} = [ + { label => 'state', set => { + key_values => [ { name => 'state' }, { name => 'name' } ], + closure_custom_calc => $self->can('custom_app_state_calc'), + closure_custom_output => $self->can('custom_app_state_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => $self->can('custom_app_state_threshold'), + } + }, + ]; + + $self->{maps_counters}->{global} = [ + { label => 'running', set => { + key_values => [ { name => 'running' } ], + output_template => 'Running : %d', + perfdatas => [ + { label => 'running', value => 'running_absolute', template => '%d', + min => 0 }, + ], + } + }, + { label => 'stopped', set => { + key_values => [ { name => 'stopped' } ], + output_template => 'Stopped : %d', + perfdatas => [ + { label => 'stopped', value => 'stopped_absolute', template => '%d', + min => 0 }, + ], + } + }, + { label => 'crashed', set => { + key_values => [ { name => 'crashed' } ], + output_template => 'Crashed : %d', + perfdatas => [ + { label => 'crashed', value => 'crashed_absolute', template => '%d', + min => 0 }, + ], + } + }, + ]; + + $self->{maps_counters}->{instances} = [ + { label => 'state', set => { + key_values => [ { name => 'state' }, { name => 'id' } ], + closure_custom_calc => $self->can('custom_inst_state_calc'), + closure_custom_output => $self->can('custom_inst_state_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => $self->can('custom_inst_state_threshold'), + } + }, + ]; +} + +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 => + { + "app-guid:s" => { name => 'app_guid' }, + "warning-app-state:s" => { name => 'warning_app_state' }, + "critical-app-state:s" => { name => 'critical_app_state', default => '%{state} !~ /STARTED/i' }, + "warning-instance-state:s" => { name => 'warning_instance_state' }, + "critical-instance-state:s" => { name => 'critical_instance_state', default => '%{state} !~ /RUNNING/i' }, + }); + + return $self; +} + +sub change_macros { + my ($self, %options) = @_; + + foreach ('warning_app_state', 'critical_app_state', 'warning_instance_state', 'critical_instance_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); + + if (!defined($self->{option_results}->{app_guid}) || $self->{option_results}->{app_guid} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify app-guid option."); + $self->{output}->option_exit(); + } + + $instance_mode = $self; + $self->change_macros(); +} + +sub manage_selection { + my ($self, %options) = @_; + + my $app = $options{custom}->get_object(url_path => '/apps/' . $self->{option_results}->{app_guid}); + + $self->{app}->{name} = $app->{entity}->{name}; + $self->{app}->{state} = $app->{entity}->{state}; + + if ($self->{app}->{state} =~ /^STARTED$/) { + $self->{global}->{running} = 0; + $self->{global}->{stopped} = 0; + $self->{global}->{crashed} = 0; + + my $instances = $options{custom}->get_object(url_path => '/apps/' . $self->{option_results}->{app_guid} . '/stats'); + + foreach my $instance (keys %{$instances}) { + $self->{instances}->{$instance} = { + id => $instance, + state => $instances->{$instance}->{state}, + }; + $self->{global}->{lc($instances->{$instance}->{state})}++; + } + } +} + +1; + +__END__ + +=head1 MODE + +Check Cloud Foundry app state and instances usage. + +=over 8 + +=item B<--app-guid> + +App guid to look for. + +=item B<--warning-app-state> + +Threshold warning for app state. + +=item B<--critical-app-state> + +Threshold critical for app state (Default: '%{state} !~ /STARTED/i'). + +=item B<--warning-instance-state> + +Threshold warning for instances state. + +=item B<--critical-instance-state> + +Threshold critical for instances state (Default: '%{state} !~ /RUNNING/i'). + +=item B<--warning-*> + +Threshold warning for instances count based +on state (Can be: 'running', 'stopped', 'crashed') + +=item B<--critical-*> + +Threshold critical for instances count based +on state (Can be: 'running', 'stopped', 'crashed'). + +=back + +=cut diff --git a/cloud/cloudfoundry/restapi/mode/listapps.pm b/cloud/cloudfoundry/restapi/mode/listapps.pm new file mode 100644 index 000000000..92cc7a84c --- /dev/null +++ b/cloud/cloudfoundry/restapi/mode/listapps.pm @@ -0,0 +1,163 @@ +# +# Copyright 2018 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::cloudfoundry::restapi::mode::listapps; + +use base qw(centreon::plugins::templates::counter); + +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 => + { + "filter-name:s" => { name => 'filter_name' }, + "filter-space:s" => { name => 'filter_space' }, + "filter-organization:s" => { name => 'filter_organization' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); +} + +sub manage_selection { + my ($self, %options) = @_; + + my %orgs; + my %spaces; + my $result = $options{custom}->get_object(url_path => '/apps'); + + foreach my $app (@{$result}) { + if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && + $app->{entity}->{name} !~ /$self->{option_results}->{filter_name}/) { + $self->{output}->output_add(long_msg => "skipping '" . $app->{entity}->{name} . "': no matching filter name.", debug => 1); + next; + } + + next if ($app->{entity}->{space_url} !~ /^\/v2(.*)/); + my $space_url = $1; + if (!defined($spaces{$space_url})) { + my $space = $options{custom}->get_object(url_path => $space_url); + $spaces{$space_url}->{name} = $space->{entity}->{name}; + $spaces{$space_url}->{organization_url} = $space->{entity}->{organization_url}; + } + + if (defined($self->{option_results}->{filter_space}) && $self->{option_results}->{filter_space} ne '' && + $spaces{$space_url}->{name} !~ /$self->{option_results}->{filter_space}/) { + $self->{output}->output_add(long_msg => "skipping space '" . $spaces{$space_url}->{name} . "': no matching filter name.", debug => 1); + next; + } + + next if ($spaces{$space_url}->{organization_url} !~ /^\/v2(.*)/); + my $organization_url = $1; + if (!defined($orgs{$organization_url})) { + my $org = $options{custom}->get_object(url_path => $organization_url); + $orgs{$organization_url}->{name} = $org->{entity}->{name}; + } + + if (defined($self->{option_results}->{filter_organization}) && $self->{option_results}->{filter_organization} ne '' && + $orgs{$organization_url}->{name} !~ /$self->{option_results}->{filter_organization}/) { + $self->{output}->output_add(long_msg => "skipping organization '" . $orgs{$organization_url}->{name} . "': no matching filter name.", debug => 1); + next; + } + + $self->{apps}->{$app->{metadata}->{guid}} = { + name => $app->{entity}->{name}, + state => $app->{entity}->{state}, + space => $spaces{$space_url}->{name}, + organization => $orgs{$organization_url}->{name}, + }; + } +} + +sub run { + my ($self, %options) = @_; + + $self->manage_selection(%options); + foreach my $app (sort keys %{$self->{apps}}) { + $self->{output}->output_add(long_msg => sprintf("[guid = %s] [name = %s] [state = %s] [space = %s] [organization = %s]", + $app, + $self->{apps}->{$app}->{name}, + $self->{apps}->{$app}->{state}, + $self->{apps}->{$app}->{space}, + $self->{apps}->{$app}->{organization})); + } + + $self->{output}->output_add(severity => 'OK', + short_msg => 'List apps:'); + $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); + $self->{output}->exit(); +} + +sub disco_format { + my ($self, %options) = @_; + + $self->{output}->add_disco_format(elements => ['guid', 'name', 'state', 'space', 'organization']); +} + +sub disco_show { + my ($self, %options) = @_; + + $self->manage_selection(%options); + foreach my $app (sort keys %{$self->{apps}}) { + $self->{output}->add_disco_entry( + guid => $app, + name => $self->{apps}->{$app}->{name}, + state => $self->{apps}->{$app}->{state}, + space => $self->{apps}->{$app}->{space}, + organization => $self->{apps}->{$app}->{organization}, + ); + } +} + +1; + +__END__ + +=head1 MODE + +List apps. + +=over 8 + +=item B<--filter-name> + +Filter apps name (can be a regexp). + +=item B<--filter-space> + +Filter spaces name (can be a regexp). + +=item B<--filter-organization> + +Filter organizations name (can be a regexp). + +=back + +=cut diff --git a/cloud/cloudfoundry/restapi/mode/listorganizations.pm b/cloud/cloudfoundry/restapi/mode/listorganizations.pm new file mode 100644 index 000000000..1f1a01a07 --- /dev/null +++ b/cloud/cloudfoundry/restapi/mode/listorganizations.pm @@ -0,0 +1,118 @@ +# +# Copyright 2018 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::cloudfoundry::restapi::mode::listorganizations; + +use base qw(centreon::plugins::templates::counter); + +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 => + { + "filter-name:s" => { name => 'filter_name' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); +} + +sub manage_selection { + my ($self, %options) = @_; + + my $result = $options{custom}->get_object(url_path => '/organizations'); + + foreach my $organization (@{$result}) { + if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && + $organization->{entity}->{name} !~ /$self->{option_results}->{filter_name}/) { + $self->{output}->output_add(long_msg => "skipping organization '" . $organization->{entity}->{name} . "': no matching filter name.", debug => 1); + next; + } + + $self->{organizations}->{$organization->{metadata}->{guid}} = { + name => $organization->{entity}->{name}, + status => $organization->{entity}->{status}, + }; + } +} + +sub run { + my ($self, %options) = @_; + + $self->manage_selection(%options); + foreach my $organization (sort keys %{$self->{organizations}}) { + $self->{output}->output_add(long_msg => sprintf("[guid = %s] [name = %s] [status = %s]", + $organization, + $self->{organizations}->{$organization}->{name}, + $self->{organizations}->{$organization}->{status})); + } + + $self->{output}->output_add(severity => 'OK', + short_msg => 'List organizations:'); + $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); + $self->{output}->exit(); +} + +sub disco_format { + my ($self, %options) = @_; + + $self->{output}->add_disco_format(elements => ['guid', 'name', 'status']); +} + +sub disco_show { + my ($self, %options) = @_; + + $self->manage_selection(%options); + foreach my $organization (sort keys %{$self->{organizations}}) { + $self->{output}->add_disco_entry( + guid => $organization, + name => $self->{organizations}->{$organization}->{name}, + status => $self->{organizations}->{$organization}->{status}, + ); + } +} + +1; + +__END__ + +=head1 MODE + +List organizations. + +=over 8 + +=item B<--filter-name> + +Filter organizations name (can be a regexp). + +=back + +=cut diff --git a/cloud/cloudfoundry/restapi/mode/listspaces.pm b/cloud/cloudfoundry/restapi/mode/listspaces.pm new file mode 100644 index 000000000..c9e8c3012 --- /dev/null +++ b/cloud/cloudfoundry/restapi/mode/listspaces.pm @@ -0,0 +1,137 @@ +# +# Copyright 2018 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::cloudfoundry::restapi::mode::listspaces; + +use base qw(centreon::plugins::templates::counter); + +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 => + { + "filter-name:s" => { name => 'filter_name' }, + "filter-organization:s" => { name => 'filter_organization' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); +} + +sub manage_selection { + my ($self, %options) = @_; + + my %orgs; + my $result = $options{custom}->get_object(url_path => '/spaces'); + + foreach my $space (@{$result}) { + if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && + $space->{entity}->{name} !~ /$self->{option_results}->{filter_name}/) { + $self->{output}->output_add(long_msg => "skipping space '" . $space->{entity}->{name} . "': no matching filter name.", debug => 1); + next; + } + + next if ($space->{entity}->{organization_url} !~ /^\/v2(.*)/); + my $organization_url = $1; + if (!defined($orgs{$organization_url})) { + my $org = $options{custom}->get_object(url_path => $organization_url); + $orgs{$organization_url}->{name} = $org->{entity}->{name}; + } + + if (defined($self->{option_results}->{filter_organization}) && $self->{option_results}->{filter_organization} ne '' && + $orgs{$organization_url}->{name} !~ /$self->{option_results}->{filter_organization}/) { + $self->{output}->output_add(long_msg => "skipping organization '" . $orgs{$organization_url}->{name} . "': no matching filter name.", debug => 1); + next; + } + + $self->{spaces}->{$space->{metadata}->{guid}} = { + name => $space->{entity}->{name}, + organization => $orgs{$organization_url}->{name}, + }; + } +} + +sub run { + my ($self, %options) = @_; + + $self->manage_selection(%options); + foreach my $space (sort keys %{$self->{spaces}}) { + $self->{output}->output_add(long_msg => sprintf("[guid = %s] [name = %s] [organization = %s]", + $space, + $self->{spaces}->{$space}->{name}, + $self->{spaces}->{$space}->{organization})); + } + + $self->{output}->output_add(severity => 'OK', + short_msg => 'List spaces:'); + $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); + $self->{output}->exit(); +} + +sub disco_format { + my ($self, %options) = @_; + + $self->{output}->add_disco_format(elements => ['guid', 'name', 'organization']); +} + +sub disco_show { + my ($self, %options) = @_; + + $self->manage_selection(%options); + foreach my $space (sort keys %{$self->{spaces}}) { + $self->{output}->add_disco_entry( + guid => $space, + name => $self->{spaces}->{$space}->{name}, + organization => $self->{spaces}->{$space}->{organization}, + ); + } +} + +1; + +__END__ + +=head1 MODE + +List spaces. + +=over 8 + +=item B<--filter-name> + +Filter spaces name (can be a regexp). + +=item B<--filter-organization> + +Filter organizations name (can be a regexp). + +=back + +=cut diff --git a/cloud/cloudfoundry/restapi/plugin.pm b/cloud/cloudfoundry/restapi/plugin.pm new file mode 100644 index 000000000..e439b54f7 --- /dev/null +++ b/cloud/cloudfoundry/restapi/plugin.pm @@ -0,0 +1,52 @@ +# +# Copyright 2018 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::cloudfoundry::restapi::plugin; + +use strict; +use warnings; +use base qw(centreon::plugins::script_custom); + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $self->{version} = '1.0'; + %{$self->{modes}} = ( + 'apps-state' => 'cloud::cloudfoundry::restapi::mode::appsstate', + 'instances-state' => 'cloud::cloudfoundry::restapi::mode::instancesstate', + 'list-apps' => 'cloud::cloudfoundry::restapi::mode::listapps', + 'list-organizations' => 'cloud::cloudfoundry::restapi::mode::listorganizations', + 'list-spaces' => 'cloud::cloudfoundry::restapi::mode::listspaces', + ); + $self->{custom_modes}{restapi} = 'cloud::cloudfoundry::restapi::custom::api'; + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check Cloud Foundry using API. + +=cut