Debian Codegraph @

src/network/aruba/orchestrator/restapi/custom/api.pm

raw
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 network::aruba::orchestrator::restapi::custom::api;
22
23 use strict;
24 use warnings;
25 use centreon::plugins::http;
26 use JSON::XS;
27
28 sub new {
29 my ($class, %options) = @_;
30 my $self = {};
31 bless $self, $class;
32
33 if (!defined($options{output})) {
34 print "Class Custom: Need to specify 'output' argument.\n";
35 exit 3;
36 }
37 if (!defined($options{options})) {
38 $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
39 $options{output}->option_exit();
40 }
41
42 if (!defined($options{noptions})) {
43 $options{options}->add_options(arguments => {
44 'access-token:s' => { name => 'access_token' },
45 'hostname:s' => { name => 'hostname' },
46 'port:s' => { name => 'port' },
47 'proto:s' => { name => 'proto' },
48 'timeout:s' => { name => 'timeout' },
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 $self->{http} = centreon::plugins::http->new(%options, default_backend => 'curl');
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} : 443;
75 $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
76 $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 50;
77 $self->{access_token} = (defined($self->{option_results}->{access_token})) ? $self->{option_results}->{access_token} : '';
78 $self->{unknown_http_status} = (defined($self->{option_results}->{unknown_http_status})) ? $self->{option_results}->{unknown_http_status} : '%{http_code} < 200 or %{http_code} >= 300';
79 $self->{warning_http_status} = (defined($self->{option_results}->{warning_http_status})) ? $self->{option_results}->{warning_http_status} : '';
80 $self->{critical_http_status} = (defined($self->{option_results}->{critical_http_status})) ? $self->{option_results}->{critical_http_status} : '';
81
82 if ($self->{hostname} eq '') {
83 $self->{output}->add_option_msg(short_msg => "Need to specify --hostname option.");
84 $self->{output}->option_exit();
85 }
86 if ($self->{access_token} eq '') {
87 $self->{output}->add_option_msg(short_msg => "Need to specify --access-token option.");
88 $self->{output}->option_exit();
89 }
90
91 return 0;
92 }
93
94 sub build_options_for_httplib {
95 my ($self, %options) = @_;
96
97 $self->{option_results}->{hostname} = $self->{hostname};
98 $self->{option_results}->{timeout} = $self->{timeout};
99 $self->{option_results}->{port} = $self->{port};
100 $self->{option_results}->{proto} = $self->{proto};
101 $self->{option_results}->{timeout} = $self->{timeout};
102 }
103
104 sub settings {
105 my ($self, %options) = @_;
106
107 return if (defined($self->{settings_done}));
108 $self->build_options_for_httplib();
109 $self->{http}->add_header(key => 'Content-Type', value => 'application/json');
110 $self->{http}->add_header(key => 'Accept', value => 'application/json');
111 $self->{http}->set_options(%{$self->{option_results}});
112 $self->{settings_done} = 1;
113 }
114
115 sub get_hostname {
116 my ($self, %options) = @_;
117
118 return $self->{hostname};
119 }
120
121 sub get_port {
122 my ($self, %options) = @_;
123
124 return $self->{port};
125 }
126
127 sub request_api {
128 my ($self, %options) = @_;
129
130 if (defined($options{query_form_post})) {
131 $options{query_form_post} = JSON::XS->new->encode($options{query_form_post});
132 }
133 $self->settings();
134 my $content = $self->{http}->request(
135 method => defined($options{method}) ? $options{method} : 'GET',
136 url_path => '/gms/rest' . $options{endpoint},
137 get_param => $options{get_param},
138 header => ['X-Auth-Token: ' . $self->{access_token}],
139 query_form_post => $options{query_form_post},
140 unknown_status => $self->{unknown_http_status},
141 warning_status => $self->{warning_http_status},
142 critical_status => $self->{critical_http_status}
143 );
144
145 if (!defined($content) || $content eq '') {
146 $self->{output}->add_option_msg(short_msg => "API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
147 $self->{output}->option_exit();
148 }
149
150 my $decoded;
151 eval {
152 $decoded = JSON::XS->new->decode($content);
153 };
154 if ($@) {
155 $self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)");
156 $self->{output}->option_exit();
157 }
158
159 return $decoded;
160 }
161
162 1;
163
164 __END__
165
166 =head1 NAME
167
168 Aruba Orchestrator Rest API
169
170 =head1 REST API OPTIONS
171
172 Aruba Orchestrator Rest API
173
174 =over 8
175
176 =item B<--hostname>
177
178 Set hostname.
179
180 =item B<--port>
181
182 Port used (default: 443)
183
184 =item B<--proto>
185
186 Specify https if needed (default: 'https')
187
188 =item B<--access-token>
189
190 API token.
191
192 =item B<--timeout>
193
194 Set timeout in seconds (default: 50).
195
196 =back
197
198 =head1 DESCRIPTION
199
200 B<custom>.
201
202 =cut