Debian Codegraph @

src/hardware/devices/cisco/ces/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 hardware::devices::cisco::ces::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 centreon::plugins::statefile;
29 use XML::LibXML::Simple;
30 use Digest::MD5 qw(md5_hex);
31
32 sub new {
33 my ($class, %options) = @_;
34 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
35 bless $self, $class;
36
37 if (!defined($options{output})) {
38 print "Class Custom: Need to specify 'output' argument.\n";
39 exit 3;
40 }
41 if (!defined($options{options})) {
42 $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
43 $options{output}->option_exit();
44 }
45
46 if (!defined($options{noptions})) {
47 $options{options}->add_options(arguments => {
48 'hostname:s' => { name => 'hostname' },
49 'port:s' => { name => 'port'},
50 'proto:s' => { name => 'proto' },
51 'api-username:s' => { name => 'api_username' },
52 'api-password:s' => { name => 'api_password' },
53 'timeout:s' => { name => 'timeout', default => 30 },
54 'no-session' => { name => 'no_session' }
55 });
56 }
57
58 $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
59
60 $self->{output} = $options{output};
61 $self->{http} = centreon::plugins::http->new(%options);
62 $self->{cache} = centreon::plugins::statefile->new(%options);
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})) ? $self->{option_results}->{hostname} : '';
79 $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443;
80 $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
81 $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 30;
82 $self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : '';
83 $self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : '';
84 $self->{no_session} = (defined($self->{option_results}->{no_session})) ? 1 : 0;
85
86 if ($self->{hostname} eq '') {
87 $self->{output}->add_option_msg(short_msg => "Need to specify --hostname option.");
88 $self->{output}->option_exit();
89 }
90 if ($self->{api_username} eq '') {
91 $self->{output}->add_option_msg(short_msg => "Need to specify --api-username option.");
92 $self->{output}->option_exit();
93 }
94 if ($self->{api_password} eq '') {
95 $self->{output}->add_option_msg(short_msg => "Need to specify --api-password option.");
96 $self->{output}->option_exit();
97 }
98
99 $self->{cache}->check_options(option_results => $self->{option_results}) if ($self->{no_session} == 0);
100 return 0;
101 }
102
103 sub build_options_for_httplib {
104 my ($self, %options) = @_;
105
106 $self->{option_results}->{hostname} = $self->{hostname};
107 $self->{option_results}->{port} = $self->{port};
108 $self->{option_results}->{proto} = $self->{proto};
109 $self->{option_results}->{timeout} = $self->{timeout};
110 if ($self->{no_session} == 1) {
111 $self->{option_results}->{credentials} = 1;
112 $self->{option_results}->{basic} = 1;
113 $self->{option_results}->{username} = $self->{api_username};
114 $self->{option_results}->{password} = $self->{api_password};
115 }
116 }
117
118 sub settings {
119 my ($self, %options) = @_;
120
121 $self->build_options_for_httplib();
122 $self->{http}->add_header(key => 'Content-Type', value => 'text/xml');
123 $self->{http}->add_header(key => 'Accept', value => 'text/xml');
124 $self->{http}->set_options(%{$self->{option_results}});
125 }
126
127 sub clean_session_cookie {
128 my ($self, %options) = @_;
129
130 my $datas = { last_timestamp => time() };
131 $options{statefile}->write(data => $datas);
132 $self->{session_cookie} = undef;
133 $self->{http}->add_header(key => 'Cookie', value => undef);
134 }
135
136 sub authenticate {
137 my ($self, %options) = @_;
138
139 my $has_cache_file = $options{statefile}->read(statefile => 'cces_api_' . md5_hex($self->{option_results}->{hostname}) . '_' . md5_hex($self->{option_results}->{api_username}));
140 my $session_cookie = $options{statefile}->get(name => 'session_cookie');
141
142 if ($has_cache_file == 0 || !defined($session_cookie)) {
143 my $content = $self->{http}->request(
144 method => 'POST',
145 url_path => '/xmlapi/session/begin',
146 credentials => 1, basic => 1,
147 username => $self->{api_username},
148 password => $self->{api_password},
149 warning_status => '', unknown_status => '', critical_status => '',
150 curl_backend_options => { header => ['Content-Length: 0'] },
151 );
152 if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) {
153 $self->{output}->add_option_msg(short_msg => "Login error [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
154 $self->{output}->option_exit();
155 }
156
157 ($session_cookie) = $self->{http}->get_header(name => 'Set-Cookie');
158 if (!defined($session_cookie)) {
159 $self->{output}->add_option_msg(short_msg => "Error retrieving cookie");
160 $self->{output}->option_exit();
161 }
162
163 my $datas = { last_timestamp => time(), session_cookie => $session_cookie };
164 $options{statefile}->write(data => $datas);
165 }
166
167 $self->{session_cookie} = $session_cookie;
168 $self->{http}->add_header(key => 'Cookie', value => $self->{session_cookie});
169 }
170
171 sub request_api {
172 my ($self, %options) = @_;
173
174 $self->settings();
175 if ($self->{no_session} == 0 && !defined($self->{session_cookie})) {
176 $self->authenticate(statefile => $self->{cache});
177 }
178
179 my $content = $self->{http}->request(
180 %options,
181 warning_status => '', unknown_status => '', critical_status => ''
182 );
183
184 # Maybe there is an issue with the session_cookie. So we retry.
185 if ($self->{no_session} == 0 && ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300)) {
186 $self->clean_session_cookie(statefile => $self->{cache});
187 $self->authenticate(statefile => $self->{cache});
188 $content = $self->{http}->request(
189 %options,
190 warning_status => '', unknown_status => '', critical_status => ''
191 );
192 }
193
194 if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) {
195 $self->{output}->add_option_msg(short_msg => 'api request error. use --debug.');
196 $self->{output}->option_exit();
197 }
198
199 my $result;
200 eval {
201 $SIG{__WARN__} = sub {};
202 $result = XMLin($content, ForceArray => $options{ForceArray}, KeyAttr => []);
203 };
204 if ($@) {
205 $self->{output}->add_option_msg(short_msg => "Cannot decode xml response: $@");
206 $self->{output}->option_exit();
207 }
208
209 return $result;
210 }
211
212 1;
213
214 __END__
215
216 =head1 NAME
217
218 CCES Rest API
219
220 =head1 REST API OPTIONS
221
222 =over 8
223
224 =item B<--hostname>
225
226 Set hostname.
227
228 =item B<--port>
229
230 Set port (default: '443').
231
232 =item B<--proto>
233
234 Specify https if needed (default: 'https').
235
236 =item B<--api-username>
237
238 Set username.
239
240 =item B<--api-password>
241
242 Set password.
243
244 =item B<--timeout>
245
246 Threshold for HTTP timeout (default: '30').
247
248 =item B<--no-session>
249
250 To be used for legacy version (before CE 8.0).
251
252 =back
253
254 =cut