From d8d9d19790bf8d25074675a192c1473b98d2e1dc Mon Sep 17 00:00:00 2001 From: Laurent Kappel Date: Wed, 5 Jul 2017 16:51:56 +0200 Subject: [PATCH 1/5] foxbox sms --- .../notification/foxbox/mode/alert.pm | 148 ++++++++++++++++++ .../notification/foxbox/plugin.pm | 50 ++++++ 2 files changed, 198 insertions(+) create mode 100644 centreon-plugins/notification/foxbox/mode/alert.pm create mode 100644 centreon-plugins/notification/foxbox/plugin.pm diff --git a/centreon-plugins/notification/foxbox/mode/alert.pm b/centreon-plugins/notification/foxbox/mode/alert.pm new file mode 100644 index 000000000..a07efdba5 --- /dev/null +++ b/centreon-plugins/notification/foxbox/mode/alert.pm @@ -0,0 +1,148 @@ +# +# Copyright 2017 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::foxbox::mode::alert; + +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 => + { + "username:s" => { name => 'username', default => 'centreon' }, + "password:s" => { name => 'password' }, + "from:s" => { name => 'from', default => 'Centreon' }, + "proto:s" => { name => 'proto', default => 'http' }, + "phonenumber:s" => { name => 'phonenumber' }, + "hostname:s" => { name => 'hostname' }, + "testo:s" => { name => 'testo' }, + "timeout:s" => { name => 'timeout', default => 10 }, + }); + + $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}->{username}) && !defined($self->{option_results}->{password}))) { + $self->{output}->add_option_msg(short_msg => "You need to set --username= and --password= option"); + $self->{output}->option_exit(); + } + + if (!defined($self->{option_results}->{hostname})) { + $self->{output}->add_option_msg(short_msg => "Please set the --hostname option"); + $self->{output}->option_exit(); + } + + if (!defined($self->{option_results}->{phonenumber})) { + $self->{output}->add_option_msg(short_msg => "Please set the --phonenumber option"); + $self->{output}->option_exit(); + } + + if (!defined($self->{option_results}->{testo})) { + $self->{output}->add_option_msg(short_msg => "Please set the --testo 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/xml'); + $self->{http}->add_header(key => 'Accept', value => 'text/xml'); + + my $api_path = '/source/send_sms.php'; + my $url = $self->{option_results}->{proto} . '://' . $self->{option_results}->{hostname} . $api_path; + + my $arg = [ + "username" => $self->{option_results}->{username}, + "pwd" => $self->{option_results}->{password}, + "from" => $self->{option_results}->{from}, + "nphone" => $self->{option_results}->{phonenum}, + "testo" => $self->{option_results}->{testo}, + "nc" => $url, + ]; + + my $response = $self->{http}->request(full_url => $url, method => 'POST', query_form_post => $arg); + + $self->{output}->display(force_ignore_perfdata => 1); + $self->{output}->exit(); +} + +1; + +__END__ + +=head1 MODE + +Send SMS with Foxbox API. + +=over 6 + +=item B<--hostname> + +url of the Foxbox Server. + +=item B<--username> + +Specify username for API authentification. + +=item B<--password> + +Specify password for API authentification. + +=item B<--phonenumber> + +Specify phone number. + +=item B<--testo> + +Specify the testo to send. + +=item B<--from> + +Specify the sender. It should NOT start with a number and have a max of 11 charracter. + +=item B<--timeout> + +Timeout in seconds for the command (Default: 10). + +=back + +=cut diff --git a/centreon-plugins/notification/foxbox/plugin.pm b/centreon-plugins/notification/foxbox/plugin.pm new file mode 100644 index 000000000..f15676c98 --- /dev/null +++ b/centreon-plugins/notification/foxbox/plugin.pm @@ -0,0 +1,50 @@ +# +# Copyright 2017 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::foxbox::plugin; + +use strict; +use warnings; +use base qw(centreon::plugins::script_simple); + +# use Data::Dumper; + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $self->{version} = '0.1'; + %{$self->{modes}} = ( + 'alert' => 'notification::foxbox::mode::alert', + ); + + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Send Foxbox notifications. + +=cut From 69124d96544428c0785d458a53d47863b505749c Mon Sep 17 00:00:00 2001 From: Laurent Kappel Date: Mon, 17 Jul 2017 19:18:03 +0200 Subject: [PATCH 2/5] update --- .../notification/foxbox/mode/alert.pm | 108 +++++++++++------- 1 file changed, 69 insertions(+), 39 deletions(-) diff --git a/centreon-plugins/notification/foxbox/mode/alert.pm b/centreon-plugins/notification/foxbox/mode/alert.pm index a07efdba5..cc6304eac 100644 --- a/centreon-plugins/notification/foxbox/mode/alert.pm +++ b/centreon-plugins/notification/foxbox/mode/alert.pm @@ -25,41 +25,47 @@ use base qw(centreon::plugins::mode); use strict; use warnings; use centreon::plugins::http; -use JSON; + +use constant OK => 0; +use constant WARNING => 1; +use constant CRITICAL => 2; +use constant UNKNOWN => 3; +use constant SEND_PAGE => '/source/send_sms.php'; # use Data::Dumper; sub new { my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); + my $self = $class->SUPER::new(package => __PACKAGE__, %options, no_performance => 1); bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "username:s" => { name => 'username', default => 'centreon' }, - "password:s" => { name => 'password' }, - "from:s" => { name => 'from', default => 'Centreon' }, - "proto:s" => { name => 'proto', default => 'http' }, - "phonenumber:s" => { name => 'phonenumber' }, - "hostname:s" => { name => 'hostname' }, - "testo:s" => { name => 'testo' }, - "timeout:s" => { name => 'timeout', default => 10 }, - }); - + $options{options}->add_options( + arguments => { + "username:s" => { name => 'username', default => 'centreon' }, + "password:s" => { name => 'password' }, + "from:s" => { name => 'from', default => 'centreon' }, + "proto:s" => { name => 'proto', default => 'http' }, + "phonenumber:s" => { name => 'phonenumber' }, + "hostname:s" => { name => 'hostname' }, + "testo:s" => { name => 'testo' }, + "timeout:s" => { name => 'timeout', default => 10 }, + } + ); + $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}->{username}) && !defined($self->{option_results}->{password}))) { - $self->{output}->add_option_msg(short_msg => "You need to set --username= and --password= option"); + if (!defined($self->{option_results}->{password})) { + $self->{output} + ->add_option_msg(short_msg => "You need to set --username= and --password= option"); $self->{output}->option_exit(); } @@ -78,31 +84,55 @@ sub check_options { $self->{output}->option_exit(); } - $self->{http}->set_options(%{$self->{option_results}}); + $self->{http}->set_options(%{ $self->{option_results} }); } sub run { my ($self, %options) = @_; - $self->{http}->add_header(key => 'Content-Type', value => 'text/xml'); - $self->{http}->add_header(key => 'Accept', value => 'text/xml'); + my ($ua, $html_page, $response, $status_code); - my $api_path = '/source/send_sms.php'; - my $url = $self->{option_results}->{proto} . '://' . $self->{option_results}->{hostname} . $api_path; + my $url + = $self->{option_results}->{proto} . '://' + . $self->{option_results}->{hostname} + . SEND_PAGE; - my $arg = [ - "username" => $self->{option_results}->{username}, - "pwd" => $self->{option_results}->{password}, - "from" => $self->{option_results}->{from}, - "nphone" => $self->{option_results}->{phonenum}, - "testo" => $self->{option_results}->{testo}, - "nc" => $url, - ]; + $ua = LWP::UserAgent->new; + $ua->timeout($self->{option_results}->{timeout}); + + $response = $ua->post( + $url, + [ "username" => $self->{option_results}->{username}, + "pwd" => $self->{option_results}->{password}, + "from" => $self->{option_results}->{from}, + "nphone" => $self->{option_results}->{phonenumber}, + "testo" => $self->{option_results}->{testo}, + "nc" => $url, + ] + ); + + if (!$response->is_success) { + print("ERROR: " . $response->status_line . "\n"); + $status_code = UNKNOWN; + } + + $html_page = $response->content; + if ($html_page =~ /p class="(\w+)"/g) { + if ($1 eq "confneg") { + print("ERROR: Unable to send SMS\n"); + $status_code = UNKNOWN; + } + else { + $status_code = OK; + } + } + else { + print("ERROR: Unknown page output\n"); + $status_code = UNKNOWN; + } + + return $status_code; - my $response = $self->{http}->request(full_url => $url, method => 'POST', query_form_post => $arg); - - $self->{output}->display(force_ignore_perfdata => 1); - $self->{output}->exit(); } 1; @@ -113,7 +143,7 @@ __END__ Send SMS with Foxbox API. -=over 6 +=over 8 =item B<--hostname> @@ -121,19 +151,19 @@ url of the Foxbox Server. =item B<--username> -Specify username for API authentification. +Specify username for API authentification (Default: centreon). =item B<--password> -Specify password for API authentification. +Specify password for API authentification (Required). =item B<--phonenumber> -Specify phone number. +Specify phone number (Required). =item B<--testo> -Specify the testo to send. +Specify the testo to send (Required). =item B<--from> From f8bb8257c409ccd4b7fe3ec57c4fc7348f792431 Mon Sep 17 00:00:00 2001 From: Laurent Kappel Date: Tue, 18 Jul 2017 11:53:04 +0200 Subject: [PATCH 3/5] update exit code --- .../notification/foxbox/mode/alert.pm | 49 ++++++++----------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/centreon-plugins/notification/foxbox/mode/alert.pm b/centreon-plugins/notification/foxbox/mode/alert.pm index cc6304eac..e16f977fd 100644 --- a/centreon-plugins/notification/foxbox/mode/alert.pm +++ b/centreon-plugins/notification/foxbox/mode/alert.pm @@ -26,10 +26,6 @@ use strict; use warnings; use centreon::plugins::http; -use constant OK => 0; -use constant WARNING => 1; -use constant CRITICAL => 2; -use constant UNKNOWN => 3; use constant SEND_PAGE => '/source/send_sms.php'; # use Data::Dumper; @@ -45,7 +41,6 @@ sub new { "username:s" => { name => 'username', default => 'centreon' }, "password:s" => { name => 'password' }, "from:s" => { name => 'from', default => 'centreon' }, - "proto:s" => { name => 'proto', default => 'http' }, "phonenumber:s" => { name => 'phonenumber' }, "hostname:s" => { name => 'hostname' }, "testo:s" => { name => 'testo' }, @@ -90,12 +85,9 @@ sub check_options { sub run { my ($self, %options) = @_; - my ($ua, $html_page, $response, $status_code); + my ($ua, $html_page, $response, $status_code, $litteral_code); - my $url - = $self->{option_results}->{proto} . '://' - . $self->{option_results}->{hostname} - . SEND_PAGE; + my $url = 'http' . '://' . $self->{option_results}->{hostname} . SEND_PAGE; $ua = LWP::UserAgent->new; $ua->timeout($self->{option_results}->{timeout}); @@ -111,28 +103,29 @@ sub run { ] ); - if (!$response->is_success) { - print("ERROR: " . $response->status_line . "\n"); - $status_code = UNKNOWN; - } - - $html_page = $response->content; - if ($html_page =~ /p class="(\w+)"/g) { - if ($1 eq "confneg") { - print("ERROR: Unable to send SMS\n"); - $status_code = UNKNOWN; - } - else { - $status_code = OK; - } + if ($response->{_rc} != 200) { + print("ERROR: " . $response->{_msg} . "\n"); + $status_code = $self->{output}->{errors}->{UNKNOWN}; } else { - print("ERROR: Unknown page output\n"); - $status_code = UNKNOWN; + $html_page = $response->{_content}; + if ($html_page =~ /p class="(\w+)"/g) { + if ($1 eq "confneg") { + print("ERROR: Unable to send SMS\n"); + $status_code = $self->{output}->{errors}->{UNKNOWN}; + } + else { + $status_code = $self->{output}->{errors}->{OK}; + } + } + else { + print("ERROR: Unknown page output\n"); + $status_code = $self->{output}->{errors}->{UNKNOWN}; + } } - return $status_code; - + $litteral_code = $self->{output}->{errors_num}->{$status_code}; + $self->{output}->exit(exit_litteral => $litteral_code); } 1; From a01f58190a43100c8152eabc1200ed23a0d8c74c Mon Sep 17 00:00:00 2001 From: Laurent Kappel Date: Tue, 18 Jul 2017 11:56:49 +0200 Subject: [PATCH 4/5] fix typo --- centreon-plugins/notification/foxbox/mode/alert.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/centreon-plugins/notification/foxbox/mode/alert.pm b/centreon-plugins/notification/foxbox/mode/alert.pm index e16f977fd..a72b722c5 100644 --- a/centreon-plugins/notification/foxbox/mode/alert.pm +++ b/centreon-plugins/notification/foxbox/mode/alert.pm @@ -160,7 +160,7 @@ Specify the testo to send (Required). =item B<--from> -Specify the sender. It should NOT start with a number and have a max of 11 charracter. +Specify the sender. It should NOT start with a number and have a max of 11 characters. =item B<--timeout> From 5ea7ef6ba2bf360cf45616d62965f673d9bd82ab Mon Sep 17 00:00:00 2001 From: Laurent Kappel Date: Thu, 20 Jul 2017 22:15:04 +0200 Subject: [PATCH 5/5] testo is replaced by texto, output_add is now used, SEND_PAGE replaced by option --send_page --- .../notification/foxbox/mode/alert.pm | 61 +++++++++++++------ 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/centreon-plugins/notification/foxbox/mode/alert.pm b/centreon-plugins/notification/foxbox/mode/alert.pm index a72b722c5..12625f208 100644 --- a/centreon-plugins/notification/foxbox/mode/alert.pm +++ b/centreon-plugins/notification/foxbox/mode/alert.pm @@ -26,8 +26,6 @@ use strict; use warnings; use centreon::plugins::http; -use constant SEND_PAGE => '/source/send_sms.php'; - # use Data::Dumper; sub new { @@ -41,9 +39,11 @@ sub new { "username:s" => { name => 'username', default => 'centreon' }, "password:s" => { name => 'password' }, "from:s" => { name => 'from', default => 'centreon' }, + "proto:s" => { name => 'proto', default => 'http' }, + "sendpage:s" => { name => 'sendpage', default => '/source/send_sms.php' }, "phonenumber:s" => { name => 'phonenumber' }, "hostname:s" => { name => 'hostname' }, - "testo:s" => { name => 'testo' }, + "texto:s" => { name => 'texto' }, "timeout:s" => { name => 'timeout', default => 10 }, } ); @@ -74,8 +74,8 @@ sub check_options { $self->{output}->option_exit(); } - if (!defined($self->{option_results}->{testo})) { - $self->{output}->add_option_msg(short_msg => "Please set the --testo option"); + if (!defined($self->{option_results}->{texto})) { + $self->{output}->add_option_msg(short_msg => "Please set the --texto option"); $self->{output}->option_exit(); } @@ -87,7 +87,10 @@ sub run { my ($ua, $html_page, $response, $status_code, $litteral_code); - my $url = 'http' . '://' . $self->{option_results}->{hostname} . SEND_PAGE; + my $url + = $self->{option_results}->{proto} . '://' + . $self->{option_results}->{hostname} + . $self->{option_results}->{sendpage}; $ua = LWP::UserAgent->new; $ua->timeout($self->{option_results}->{timeout}); @@ -98,33 +101,45 @@ sub run { "pwd" => $self->{option_results}->{password}, "from" => $self->{option_results}->{from}, "nphone" => $self->{option_results}->{phonenumber}, - "testo" => $self->{option_results}->{testo}, + "testo" => $self->{option_results}->{texto}, "nc" => $url, ] ); if ($response->{_rc} != 200) { - print("ERROR: " . $response->{_msg} . "\n"); - $status_code = $self->{output}->{errors}->{UNKNOWN}; + $status_code = $self->{output}->{errors}->{UNKNOWN}; + $litteral_code = $self->{output}->{errors_num}->{$status_code}; + $self->{output}->output_add( + severity => $litteral_code, + short_msg => 'ERROR: ' . $response->{_msg} + ); } else { $html_page = $response->{_content}; if ($html_page =~ /p class="(\w+)"/g) { if ($1 eq "confneg") { - print("ERROR: Unable to send SMS\n"); - $status_code = $self->{output}->{errors}->{UNKNOWN}; + $status_code = $self->{output}->{errors}->{UNKNOWN}; + $litteral_code = $self->{output}->{errors_num}->{$status_code}; + $self->{output}->output_add( + severity => $litteral_code, + short_msg => 'ERROR: Unable to send SMS' + ); } else { - $status_code = $self->{output}->{errors}->{OK}; + $status_code = $self->{output}->{errors}->{OK}; + $litteral_code = $self->{output}->{errors_num}->{$status_code}; } } else { - print("ERROR: Unknown page output\n"); - $status_code = $self->{output}->{errors}->{UNKNOWN}; + $status_code = $self->{output}->{errors}->{UNKNOWN}; + $litteral_code = $self->{output}->{errors_num}->{$status_code}; + $self->{output}->output_add( + severity => $litteral_code, + short_msg => 'ERROR: Unknown page output' + ); } } - - $litteral_code = $self->{output}->{errors_num}->{$status_code}; + $self->{output}->display(); $self->{output}->exit(exit_litteral => $litteral_code); } @@ -142,6 +157,10 @@ Send SMS with Foxbox API. url of the Foxbox Server. +=item B<--sendpage> + +The path describes a specific resource. (Default: /source/send_sms.php) + =item B<--username> Specify username for API authentification (Default: centreon). @@ -150,17 +169,21 @@ Specify username for API authentification (Default: centreon). Specify password for API authentification (Required). +=item B<--proto> + +Specify http or https protocol. (Default: http) + =item B<--phonenumber> Specify phone number (Required). -=item B<--testo> +=item B<--texto> -Specify the testo to send (Required). +Specify the content of your SMS message (Required). =item B<--from> -Specify the sender. It should NOT start with a number and have a max of 11 characters. +Specify the sender. It should NOT start with a number and have a max of 11 characters (Default: centreon). =item B<--timeout>