|
| 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 | +package storage::datacore::restapi::custom::api; |
| 21 | +use strict; |
| 22 | +use warnings; |
| 23 | +use centreon::plugins::http; |
| 24 | +use centreon::plugins::statefile; |
| 25 | +use JSON::XS; |
| 26 | +use centreon::plugins::misc qw(empty); |
| 27 | + |
| 28 | +sub new { |
| 29 | + my ($class, %options) = @_; |
| 30 | + if (!defined($options{output})) { |
| 31 | + print "Class Custom: Need to specify 'output' argument.\n"; |
| 32 | + exit 3; |
| 33 | + } |
| 34 | + if (!defined($options{options})) { |
| 35 | + $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument."); |
| 36 | + $options{output}->option_exit(); |
| 37 | + } |
| 38 | + my $self = {}; |
| 39 | + bless $self, $class; |
| 40 | + |
| 41 | + |
| 42 | + $options{options}->add_options(arguments => { |
| 43 | + 'hostname:s' => { name => 'hostname' }, |
| 44 | + 'port:s' => { name => 'port', default => 443 }, |
| 45 | + 'proto:s' => { name => 'proto', default => 'https' }, |
| 46 | + 'timeout:s' => { name => 'timeout' }, |
| 47 | + 'username:s' => { name => 'username' }, |
| 48 | + 'password:s' => { name => 'password' }, |
| 49 | + 'unknown-http-status:s' => { name => 'unknown_http_status' }, |
| 50 | + 'warning-http-status:s' => { name => 'warning_http_status' }, |
| 51 | + 'critical-http-status:s' => { name => 'critical_http_status' } |
| 52 | + }); |
| 53 | + |
| 54 | + $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); |
| 55 | + |
| 56 | + $self->{output} = $options{output}; |
| 57 | + |
| 58 | + $self->{http} = centreon::plugins::http->new(%options, default_backend => 'curl'); |
| 59 | + |
| 60 | + return $self; |
| 61 | +} |
| 62 | + |
| 63 | +sub set_options { |
| 64 | + my ($self, %options) = @_; |
| 65 | + |
| 66 | + $self->{option_results} = $options{option_results}; |
| 67 | +} |
| 68 | +sub set_defaults {} |
| 69 | + |
| 70 | +# hostname,username and password are required options |
| 71 | +sub check_options { |
| 72 | + my ($self, %options) = @_; |
| 73 | + $self->{http}->set_options(%{$self->{option_results}}); |
| 74 | + |
| 75 | + $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 50; |
| 76 | + |
| 77 | + if (centreon::plugins::misc::is_empty($self->{option_results}->{hostname})) { |
| 78 | + $self->{output}->add_option_msg(short_msg => 'Please set hostname option'); |
| 79 | + $self->{output}->option_exit(); |
| 80 | + } |
| 81 | + if (centreon::plugins::misc::is_empty($self->{option_results}->{username})) { |
| 82 | + $self->{output}->add_option_msg(short_msg => 'Please set username option to authenticate against datacore rest api'); |
| 83 | + $self->{output}->option_exit(); |
| 84 | + } |
| 85 | + if (centreon::plugins::misc::is_empty($self->{option_results}->{password})) { |
| 86 | + $self->{output}->add_option_msg(short_msg => 'Please set password option to authenticate against datacore rest api'); |
| 87 | + $self->{output}->option_exit(); |
| 88 | + } |
| 89 | + |
| 90 | +} |
| 91 | + |
| 92 | +sub settings { |
| 93 | + my ($self, %options) = @_; |
| 94 | + |
| 95 | + return if (defined($self->{settings_done})); |
| 96 | + $self->{http}->add_header(key => 'ServerHost', value => $self->{option_results}->{hostname}); |
| 97 | + $self->{http}->set_options(basic => 1, credentials => 1); |
| 98 | + $self->{settings_done} = 1; |
| 99 | +} |
| 100 | + |
| 101 | +# wrapper around centreon::plugins::http::request to add authentication and decode json. |
| 102 | +# output : deserialized json from the api if not error found in http call. |
| 103 | +sub request_api { |
| 104 | + my ($self, %options) = @_; |
| 105 | + |
| 106 | + # datacore api require a ServerHost header with the hostname used to query the api to respond. |
| 107 | + # authentication is http standard basic auth. |
| 108 | + my $result = $self->{http}->request( |
| 109 | + username => $self->{option_results}->{username}, |
| 110 | + password => $self->{option_results}->{password}, |
| 111 | + unknown_status => $self->{option_results}->{unknown_http_status}, |
| 112 | + warning_status => $self->{option_results}->{warning_http_status}, |
| 113 | + critical_status => $self->{option_results}->{critical_http_status}, |
| 114 | + %options, |
| 115 | + ); |
| 116 | + # Declare a scalar to deserialize the JSON content string into a perl data structure |
| 117 | + my $decoded_content; |
| 118 | + eval { |
| 119 | + $decoded_content = JSON::XS->new->decode($result); |
| 120 | + }; |
| 121 | + # Catch the error that may arise in case the data received is not JSON |
| 122 | + if ($@) { |
| 123 | + $self->{output}->add_option_msg(short_msg => "Cannot decode JSON result"); |
| 124 | + $self->{output}->option_exit(); |
| 125 | + } |
| 126 | + return $decoded_content; |
| 127 | + |
| 128 | +} |
| 129 | +1; |
| 130 | + |
| 131 | + |
| 132 | +__END__ |
| 133 | +
|
| 134 | +=head1 NAME |
| 135 | +
|
| 136 | +Datacore Sansymphony Rest API |
| 137 | +
|
| 138 | +=head1 REST API OPTIONS |
| 139 | +
|
| 140 | +Datacore Sansymphony Rest API |
| 141 | +
|
| 142 | +=over 8 |
| 143 | +
|
| 144 | +=item B<--hostname> |
| 145 | +
|
| 146 | +Address of the Datacore server that hosts the API endpoint. |
| 147 | +
|
| 148 | +=item B<--port> |
| 149 | +
|
| 150 | +Port of the resource to connect to (default: 443). |
| 151 | +
|
| 152 | +=item B<--proto> |
| 153 | +
|
| 154 | +HTTP protocol, either http or https (default: 'https') |
| 155 | +
|
| 156 | +=item B<--username> |
| 157 | +
|
| 158 | +Username to access the endpoint. |
| 159 | +
|
| 160 | +=item B<--password> |
| 161 | +
|
| 162 | +Password to access the endpoint. |
| 163 | +
|
| 164 | +=item B<--timeout> |
| 165 | +
|
| 166 | +Set timeout in seconds (default: 10). |
| 167 | +
|
| 168 | +=back |
| 169 | +
|
| 170 | +=head1 DESCRIPTION |
| 171 | +
|
| 172 | +B<custom>. |
0 commit comments