Debian Codegraph @

src/apps/infor/ion/grid/custom/restapi.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 apps::infor::ion::grid::custom::restapi;
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 = $class->SUPER::new(package => __PACKAGE__, %options);
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 'api-username:s' => { name => 'api_username' },
50 'api-password:s' => { name => 'api_password' },
51 'timeout:s' => { name => 'timeout' },
52 'unknown-http-status:s' => { name => 'unknown_http_status' },
53 'warning-http-status:s' => { name => 'warning_http_status' },
54 'critical-http-status:s' => { name => 'critical_http_status' },
55 'cookies-file:s' => { name => 'cookies_file' }
56 });
57 }
58
59 $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
60
61 $self->{output} = $options{output};
62 $self->{http} = centreon::plugins::http->new(%options, default_backend => 'curl');
63
64 return $self;
65 }
66
67 sub set_options {
68 my ($self, %options) = @_;
69
70 $self->{option_results} = $options{option_results};
71 }
72
73 sub set_defaults {}
74
75 sub check_options {
76 my ($self, %options) = @_;
77
78 $self->{hostname} = (defined($self->{option_results}->{hostname}))
79 ? $self->{option_results}->{hostname}
80 : '';
81 $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443;
82 $self->{proto} = (defined($self->{option_results}->{proto}))
83 ? $self->{option_results}->{proto}
84 : 'https';
85 $self->{timeout} = (defined($self->{option_results}->{timeout}))
86 ? $self->{option_results}->{timeout}
87 : 20;
88 $self->{api_username} = (defined($self->{option_results}->{api_username}))
89 ? $self->{option_results}->{api_username}
90 : '';
91 $self->{api_password} = (defined($self->{option_results}->{api_password}))
92 ? $self->{option_results}->{api_password}
93 : '';
94 $self->{unknown_http_status} = (defined($self->{option_results}->{unknown_http_status}))
95 ? $self->{option_results}->{unknown_http_status}
96 : '%{http_code} < 200 or %{http_code} >= 300';
97 $self->{warning_http_status} = (defined($self->{option_results}->{warning_http_status}))
98 ? $self->{option_results}->{warning_http_status}
99 : '';
100 $self->{critical_http_status} = (defined($self->{option_results}->{critical_http_status}))
101 ? $self->{option_results}->{critical_http_status}
102 : '';
103 $self->{cookies_file} = (defined($self->{option_results}->{cookies_file}))
104 ? $self->{option_results}->{cookies_file}
105 : '';
106
107 if ($self->{hostname} eq '') {
108 $self->{output}->add_option_msg(short_msg => "Need to specify --hostname option.");
109 $self->{output}->option_exit();
110 }
111 if ($self->{api_username} ne '' && $self->{api_password} eq '') {
112 $self->{output}->add_option_msg(short_msg => "Need to specify --api-password option.");
113 $self->{output}->option_exit();
114 }
115
116 return 0;
117 }
118
119 sub build_options_for_httplib {
120 my ($self, %options) = @_;
121
122 $self->{option_results}->{hostname} = $self->{hostname};
123 $self->{option_results}->{port} = $self->{port};
124 $self->{option_results}->{proto} = $self->{proto};
125 $self->{option_results}->{timeout} = $self->{timeout};
126 if ($self->{api_username} ne '') {
127 $self->{option_results}->{credentials} = 1;
128 $self->{option_results}->{basic} = 1;
129 $self->{option_results}->{username} = $self->{api_username};
130 $self->{option_results}->{password} = $self->{api_password};
131 }
132 $self->{option_results}->{cookies_file} = $self->{cookies_file};
133 }
134
135 sub settings {
136 my ($self, %options) = @_;
137
138 return if (defined($self->{settings_done}));
139 $self->build_options_for_httplib();
140 $self->{http}->add_header(key => 'Accept', value => 'application/json');
141 $self->{http}->set_options(%{$self->{option_results}});
142 $self->{settings_done} = 1;
143 }
144
145 sub request_api {
146 my ($self, %options) = @_;
147
148 $self->settings();
149
150 my $content = $self->{http}->request(
151 %options,
152 unknown_status => $self->{unknown_http_status},
153 warning_status => $self->{warning_http_status},
154 critical_status => $self->{critical_http_status}
155 );
156
157 if (!defined($content) || $content eq '') {
158 $self->{output}->add_option_msg(short_msg => "API returns empty content [code: '"
159 . $self->{http}->get_code()
160 . "'] [message: '"
161 . $self->{http}->get_message()
162 . "']");
163 $self->{output}->option_exit();
164 }
165
166 my $decoded;
167 eval {
168 $decoded = JSON::XS->new->utf8->decode($content);
169 };
170 if ($@) {
171 $self->{output}->add_option_msg(short_msg =>
172 "Cannot decode response (add --debug option to display returned content)");
173 $self->{output}->option_exit();
174 }
175
176 return $decoded;
177 }
178
179 1;
180
181 __END__
182
183 =head1 NAME
184
185 ION Grid Rest API
186
187 =head1 REST API OPTIONS
188
189 =over 8
190
191 =item B<--hostname>
192
193 Define the name or the address of the host.
194
195 =item B<--port>
196
197 Define the port to connect to (default: '443').
198
199 =item B<--proto>
200
201 Select the right protocol between 'http' and 'https' (default: 'https').
202
203 =item B<--api-username>
204
205 Username to access the API.
206
207 =item B<--api-password>
208
209 Password to access the API.
210
211 =item B<--timeout>
212
213 Maximum time to wait (in seconds) for a response to HTTP requests before timeout (default: '20').
214
215 =back
216
217 =cut