From 8739e2ff39ac218f0bc549013bf8bd6737c43870 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20Pilpr=C3=A9?= Date: Mon, 10 Dec 2018 13:16:03 +0100 Subject: [PATCH] OVH SMS Notification (#1263) * Minor Correction for getting 'get_params' by $request_options during GET Method Access. * First Release of OVH SMS Notification Use of HTTP CGI function (https://docs.ovh.com/fr/sms/envoyer_des_sms_depuis_une_url_-_http2sms/) * Minor Esthetical Code modification * changes after code review * Change of smsId in Response * fix rights, remove data::dumper --- centreon-plugins/centreon/plugins/http.pm | 6 +- .../notification/ovhsms/mode/alert.pm | 215 ++++++++++++++++++ .../notification/ovhsms/plugin.pm | 48 ++++ 3 files changed, 266 insertions(+), 3 deletions(-) create mode 100644 centreon-plugins/notification/ovhsms/mode/alert.pm create mode 100644 centreon-plugins/notification/ovhsms/plugin.pm diff --git a/centreon-plugins/centreon/plugins/http.pm b/centreon-plugins/centreon/plugins/http.pm index c64ac06bb..4bade0b60 100644 --- a/centreon-plugins/centreon/plugins/http.pm +++ b/centreon-plugins/centreon/plugins/http.pm @@ -121,7 +121,7 @@ sub check_options { $options{request}->{$_} =~ s/%\{http_code\}/\$response->code/g; } } - + $self->{ssl_context} = ''; if (!defined($options{request}->{ssl_opt})) { $options{request}->{ssl_opt} = []; @@ -238,8 +238,8 @@ sub request { } my $uri = URI->new($url); - if (defined($self->{get_params})) { - $uri->query_form($self->{get_params}); + if (defined($request_options->{get_params})) { + $uri->query_form($request_options->{get_params}); } $req = HTTP::Request->new($request_options->{method}, $uri); diff --git a/centreon-plugins/notification/ovhsms/mode/alert.pm b/centreon-plugins/notification/ovhsms/mode/alert.pm new file mode 100644 index 000000000..9e80a50a8 --- /dev/null +++ b/centreon-plugins/notification/ovhsms/mode/alert.pm @@ -0,0 +1,215 @@ +# +# 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 notification::ovhsms::mode::alert; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::http; +use JSON::XS; + +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 => + { + "hostname:s" => { name => 'hostname', default => 'www.ovh.com' }, + "port:s" => { name => 'port', default => 443 }, + "proto:s" => { name => 'proto', default => 'https' }, + "urlpath:s" => { name => 'url_path', default => "/cgi-bin/sms/http2sms.cgi" }, + "account:s" => { name => 'account' }, + "login:s" => { name => 'login' }, + "password:s" => { name => 'password' }, + "from:s" => { name => 'from'}, + "to:s" => { name => 'to' }, + "message:s" => { name => 'message' }, + "class:s" => { name => 'class', default => 1 }, + "nostop:s" => { name => 'nostop', default => 1 }, + "smscoding:s" => { name => 'smscoding', default => 1 }, + "proxyurl:s" => { name => 'proxyurl' }, + "proxypac:s" => { name => 'proxypac' }, + "timeout:s" => { name => 'timeout' }, + "ssl-opt:s@" => { name => 'ssl_opt' }, + }); + + $self->{http} = centreon::plugins::http->new(output => $self->{output}); + + return $self; +} + + +sub check_options { + my ($self, %options) = @_; + + $self->SUPER::init(%options); + if (!defined($self->{option_results}->{account})) { + $self->{output}->add_option_msg(short_msg => "You need to set --account option"); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{login})) { + $self->{output}->add_option_msg(short_msg => "You need to set --login option"); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{password})) { + $self->{output}->add_option_msg(short_msg => "You need to set --password option"); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{from})) { + $self->{output}->add_option_msg(short_msg => "You need to set --from option"); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{to})) { + $self->{output}->add_option_msg(short_msg => "You need to set --to option"); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{message})) { + $self->{output}->add_option_msg(short_msg => "You need to set --message option"); + $self->{output}->option_exit(); + } + + $self->{http}->set_options(%{$self->{option_results}}); +} + +sub run { + my ($self, %options) = @_; + + $self->{http}->add_header(key => 'Content-Type', value => 'text/plain'); + $self->{http}->add_header(key => 'Accept', value => 'text/plain'); + + my $sms_arg = {}; + + $sms_arg->{account} = $self->{option_results}->{account}; + $sms_arg->{login} = $self->{option_results}->{login}; + $sms_arg->{password} = $self->{option_results}->{password}; + $sms_arg->{to} = $self->{option_results}->{to}; + $sms_arg->{from} = $self->{option_results}->{from}; + $sms_arg->{message} = $self->{option_results}->{message}; + $sms_arg->{class} = $self->{option_results}->{class}; + $sms_arg->{noStop} = $self->{option_results}->{nostop}; + $sms_arg->{contentType} = 'application/json'; + $sms_arg->{smsCoding} = $self->{option_results}->{smscoding}; + + my $response = $self->{http}->request(method => 'GET', get_params => $sms_arg); + + my $decoded; + eval { + $decoded = JSON::XS->new->utf8->decode($response); + }; + if ($@) { + $self->{output}->output_add(long_msg => $response, debug => 1); + $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); + $self->{output}->option_exit(); + } + if (defined($decoded->{status}) && ($decoded->{status} < 100 || $decoded->{status} >= 200)) { + $self->{output}->add_option_msg(short_msg => "API returned status '" . $decoded->{status} . "' and message '" . $decoded->{message} . "'"); + $self->{output}->option_exit(); + } + + $self->{output}->output_add(short_msg => 'smsIds : ' . join(', ', @{$decoded->{smsIds}})); + $self->{output}->display(force_ignore_perfdata => 1); + $self->{output}->exit(); +} + +1; + +__END__ + +=head1 MODE + +Send SMS with OVH API (https://docs.ovh.com/fr/sms/envoyer_des_sms_depuis_une_url_-_http2sms/) + +=over 6 + +=item B<--hostname> + +Hostname of the OVH SMS API (Default: 'www.ovh.com') + +=item B<--port> + +Port used by API (Default: '443') + +=item B<--proto> + +Specify https if needed (Default: 'https'). + +=item B<--urlpath> + +Set path to the SMS API (Default: '/cgi-bin/sms/http2sms.cgi'). + +=item B<--proxyurl> + +Proxy URL if any. + +=item B<--proxypac> + +Proxy pac file (can be an url or local file). + +=item B<--account> + +Specify SMS Account for API authentification. + +=item B<--login> + +Specify login for API authentification. + +=item B<--password> + +Specify password for API authentification. + +=item B<--from> + +Specify sender linked to account. + +=item B<--to> + +Specify receiver phone number (format 00336xxxx for French Number). + +=item B<--message> + +Specify the message to send. + +=item B<--class> + +Specify the class of message. (Default : '1'). + +=item B<--nostop> + +Specify the nostop option. (Default : '1'). + +=item B<--smsdoding> + +Specify the coding of message. (Default : '1'). + +=item B<--timeout> + +Threshold for HTTP timeout + +=item B<--ssl-opt> + +Set SSL options (--ssl-opt="SSL_version => TLSv1" --ssl-opt="SSL_verify_mode => SSL_VERIFY_NONE"). + +=back + +=cut diff --git a/centreon-plugins/notification/ovhsms/plugin.pm b/centreon-plugins/notification/ovhsms/plugin.pm new file mode 100644 index 000000000..3ba1a4a21 --- /dev/null +++ b/centreon-plugins/notification/ovhsms/plugin.pm @@ -0,0 +1,48 @@ +# +# 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 notification::ovhsms::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} = '1.0'; + %{$self->{modes}} = ( + 'alert' => 'notification::ovhsms::mode::alert', + ); + + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Send SMS via OVH SMS API. + +=cut