| 1 |
# |
| 2 |
# Copyright 2024 Centreon (http://www.centreon.com/) |
| 3 |
# |
| 4 |
# Centreon is a full-fledged industry-strength solution that meets |
| 5 |
# the needs in IT infrastructure and application monitoring for |
| 6 |
# service performance. |
| 7 |
# |
| 8 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 |
# you may not use this file except in compliance with the License. |
| 10 |
# You may obtain a copy of the License at |
| 11 |
# |
| 12 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
# |
| 14 |
# Unless required by applicable law or agreed to in writing, software |
| 15 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
# See the License for the specific language governing permissions and |
| 18 |
# limitations under the License. |
| 19 |
# |
| 20 |
|
| 21 |
package apps::mq::rabbitmq::restapi::custom::api; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::mode); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use centreon::plugins::http; |
| 28 |
use JSON::XS; |
| 29 |
|
| 30 |
sub new { |
| 31 |
my ($class, %options) = @_; |
| 32 |
my $self = {}; |
| 33 |
bless $self, $class; |
| 34 |
|
| 35 |
if (!defined($options{output})) { |
| 36 |
print "Class Custom: Need to specify 'output' argument.\n"; |
| 37 |
exit 3; |
| 38 |
} |
| 39 |
if (!defined($options{options})) { |
| 40 |
$options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument."); |
| 41 |
$options{output}->option_exit(); |
| 42 |
} |
| 43 |
|
| 44 |
if (!defined($options{noptions})) { |
| 45 |
$options{options}->add_options(arguments => { |
| 46 |
'hostname:s' => { name => 'hostname' }, |
| 47 |
'port:s' => { name => 'port' }, |
| 48 |
'proto:s' => { name => 'proto' }, |
| 49 |
'username:s' => { name => 'username' }, |
| 50 |
'password:s' => { name => 'password' }, |
| 51 |
'timeout:s' => { name => 'timeout' } |
| 52 |
}); |
| 53 |
} |
| 54 |
$options{options}->add_help(package => __PACKAGE__, sections => 'CUSTOM MODE OPTIONS', once => 1); |
| 55 |
|
| 56 |
$self->{output} = $options{output}; |
| 57 |
$self->{http} = centreon::plugins::http->new(%options); |
| 58 |
|
| 59 |
return $self; |
| 60 |
} |
| 61 |
|
| 62 |
sub set_options { |
| 63 |
my ($self, %options) = @_; |
| 64 |
|
| 65 |
$self->{option_results} = $options{option_results}; |
| 66 |
} |
| 67 |
|
| 68 |
sub set_defaults {} |
| 69 |
|
| 70 |
sub check_options { |
| 71 |
my ($self, %options) = @_; |
| 72 |
|
| 73 |
$self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : ''; |
| 74 |
$self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 15672; |
| 75 |
$self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'http'; |
| 76 |
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10; |
| 77 |
$self->{username} = (defined($self->{option_results}->{username})) ? $self->{option_results}->{username} : undef; |
| 78 |
$self->{password} = (defined($self->{option_results}->{password})) ? $self->{option_results}->{password} : undef; |
| 79 |
|
| 80 |
if (!defined($self->{hostname}) || $self->{hostname} eq '') { |
| 81 |
$self->{output}->add_option_msg(short_msg => 'Need to specify --hostname option.'); |
| 82 |
$self->{output}->option_exit(); |
| 83 |
} |
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
sub get_hostname { |
| 88 |
my ($self, %options) = @_; |
| 89 |
|
| 90 |
return $self->{hostname}; |
| 91 |
} |
| 92 |
|
| 93 |
sub get_port { |
| 94 |
my ($self, %options) = @_; |
| 95 |
|
| 96 |
return $self->{port}; |
| 97 |
} |
| 98 |
|
| 99 |
sub build_options_for_httplib { |
| 100 |
my ($self, %options) = @_; |
| 101 |
|
| 102 |
$self->{option_results}->{hostname} = $self->{hostname}; |
| 103 |
$self->{option_results}->{timeout} = $self->{timeout}; |
| 104 |
$self->{option_results}->{port} = $self->{port}; |
| 105 |
$self->{option_results}->{proto} = $self->{proto}; |
| 106 |
$self->{option_results}->{timeout} = $self->{timeout}; |
| 107 |
|
| 108 |
if (defined($self->{username}) && $self->{username} ne '') { |
| 109 |
$self->{option_results}->{credentials} = 1; |
| 110 |
$self->{option_results}->{basic} = 1; |
| 111 |
$self->{option_results}->{username} = $self->{username}; |
| 112 |
$self->{option_results}->{password} = $self->{password}; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
sub settings { |
| 117 |
my ($self, %options) = @_; |
| 118 |
|
| 119 |
$self->build_options_for_httplib(); |
| 120 |
$self->{http}->set_options(%{$self->{option_results}}); |
| 121 |
} |
| 122 |
|
| 123 |
sub request { |
| 124 |
my ($self, %options) = @_; |
| 125 |
|
| 126 |
$self->settings(); |
| 127 |
|
| 128 |
$self->{output}->output_add(long_msg => "URL: '" . $self->{proto} . '://' . $self->{hostname} . ':' . $self->{port} . $options{url_path} . "'", debug => 1); |
| 129 |
$self->{output}->output_add(long_msg => "Parameters: '" . join(', ', @{$options{post_param}}) . "'", debug => 1) if (defined($options{post_param})); |
| 130 |
|
| 131 |
my $content = $self->{http}->request( |
| 132 |
%options, |
| 133 |
unknown_status => '%{http_code} < 200 or %{http_code} >= 300', |
| 134 |
critical_status => '', |
| 135 |
); |
| 136 |
|
| 137 |
if (!defined($content) || $content eq '') { |
| 138 |
$self->{output}->add_option_msg(short_msg => "API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']"); |
| 139 |
$self->{output}->option_exit(); |
| 140 |
} |
| 141 |
|
| 142 |
my $decoded; |
| 143 |
eval { |
| 144 |
$decoded = JSON::XS->new->utf8->decode($content); |
| 145 |
}; |
| 146 |
if ($@) { |
| 147 |
$self->{output}->output_add(long_msg => $content, debug => 1); |
| 148 |
$self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)"); |
| 149 |
$self->{output}->option_exit(); |
| 150 |
} |
| 151 |
|
| 152 |
return $decoded; |
| 153 |
} |
| 154 |
|
| 155 |
sub query { |
| 156 |
my ($self, %options) = @_; |
| 157 |
|
| 158 |
return $self->request(url_path => $options{url_path}); |
| 159 |
} |
| 160 |
|
| 161 |
1; |
| 162 |
|
| 163 |
__END__ |
| 164 |
|
| 165 |
=head1 NAME |
| 166 |
|
| 167 |
RabbitMQ Rest API |
| 168 |
|
| 169 |
=head1 CUSTOM MODE OPTIONS |
| 170 |
|
| 171 |
RabbitMQ Rest API |
| 172 |
|
| 173 |
=over 8 |
| 174 |
|
| 175 |
=item B<--hostname> |
| 176 |
|
| 177 |
Remote hostname or IP address. |
| 178 |
|
| 179 |
=item B<--port> |
| 180 |
|
| 181 |
Port used (default: 15672) |
| 182 |
|
| 183 |
=item B<--proto> |
| 184 |
|
| 185 |
Specify https if needed (default: 'http') |
| 186 |
|
| 187 |
=item B<--username> |
| 188 |
|
| 189 |
Specify the username for authentication |
| 190 |
|
| 191 |
=item B<--password> |
| 192 |
|
| 193 |
Specify the password for authentication |
| 194 |
|
| 195 |
=item B<--timeout> |
| 196 |
|
| 197 |
Set timeout in seconds (default: 10). |
| 198 |
|
| 199 |
=back |
| 200 |
|
| 201 |
=head1 DESCRIPTION |
| 202 |
|
| 203 |
B<custom>. |
| 204 |
|
| 205 |
=cut |