Debian Codegraph @

src/apps/mq/ibmmq/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 apps::mq::ibmmq::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 'api-username:s' => { name => 'api_username' },
45 'api-password:s' => { name => 'api_password' },
46 'hostname:s' => { name => 'hostname' },
47 'port:s' => { name => 'port' },
48 'proto:s' => { name => 'proto' },
49 'timeout:s' => { name => 'timeout' },
50 'url-path:s' => { name => 'url_path' },
51 'unknown-http-status:s' => { name => 'unknown_http_status' },
52 'warning-http-status:s' => { name => 'warning_http_status' },
53 'critical-http-status:s' => { name => 'critical_http_status' }
54 });
55 }
56 $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
57
58 $self->{output} = $options{output};
59 $self->{http} = centreon::plugins::http->new(%options);
60
61 return $self;
62 }
63
64 sub set_options {
65 my ($self, %options) = @_;
66
67 $self->{option_results} = $options{option_results};
68 }
69
70 sub set_defaults {}
71
72 sub check_options {
73 my ($self, %options) = @_;
74
75 $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : '';
76 $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 9443;
77 $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
78 $self->{url_path} = (defined($self->{option_results}->{url_path})) ? $self->{option_results}->{url_path} : '/ibmmq/rest/v1/admin';
79 $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 50;
80 $self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : '';
81 $self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : '';
82 $self->{unknown_http_status} = (defined($self->{option_results}->{unknown_http_status})) ? $self->{option_results}->{unknown_http_status} : '%{http_code} < 200 or %{http_code} >= 300';
83 $self->{warning_http_status} = (defined($self->{option_results}->{warning_http_status})) ? $self->{option_results}->{warning_http_status} : '';
84 $self->{critical_http_status} = (defined($self->{option_results}->{critical_http_status})) ? $self->{option_results}->{critical_http_status} : '';
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
95 return 0;
96 }
97
98 sub build_options_for_httplib {
99 my ($self, %options) = @_;
100
101 $self->{option_results}->{hostname} = $self->{hostname};
102 $self->{option_results}->{timeout} = $self->{timeout};
103 $self->{option_results}->{port} = $self->{port};
104 $self->{option_results}->{proto} = $self->{proto};
105 $self->{option_results}->{timeout} = $self->{timeout};
106 $self->{option_results}->{credentials} = 1;
107 $self->{option_results}->{basic} = 1;
108 $self->{option_results}->{username} = $self->{api_username};
109 $self->{option_results}->{password} = $self->{api_password};
110 }
111
112 sub settings {
113 my ($self, %options) = @_;
114
115 return if (defined($self->{settings_done}));
116 $self->build_options_for_httplib();
117 $self->{http}->add_header(key => 'Accept', value => 'application/json');
118 $self->{http}->set_options(%{$self->{option_results}});
119 $self->{settings_done} = 1;
120 }
121
122 sub get_hostname {
123 my ($self, %options) = @_;
124
125 return $self->{hostname};
126 }
127
128 sub request_api {
129 my ($self, %options) = @_;
130
131 $self->settings();
132 my $content = $self->{http}->request(
133 url_path => $self->{url_path} . $options{endpoint},
134 unknown_status => $self->{unknown_http_status},
135 warning_status => $self->{warning_http_status},
136 critical_status => $self->{critical_http_status},
137 get_param => $options{get_param}
138 );
139
140 if (!defined($content) || $content eq '') {
141 $self->{output}->add_option_msg(short_msg => "API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
142 $self->{output}->option_exit();
143 }
144
145 my $decoded;
146 eval {
147 $decoded = JSON::XS->new->utf8->decode($content);
148 };
149 if ($@) {
150 $self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)");
151 $self->{output}->option_exit();
152 }
153
154 return $decoded;
155 }
156
157 1;
158
159 __END__
160
161 =head1 NAME
162
163 IBM MQ Rest API
164
165 =head1 REST API OPTIONS
166
167 IBM MQ Rest API
168
169 =over 8
170
171 =item B<--hostname>
172
173 Administrative server hostname.
174
175 =item B<--port>
176
177 Port used (default: 9443)
178
179 =item B<--proto>
180
181 Specify https if needed (default: 'https')
182
183 =item B<--url-path>
184
185 API url path (default: '/ibmmq/rest/v1/admin')
186
187 =item B<--api-username>
188
189 API username.
190
191 =item B<--api-password>
192
193 API password.
194
195 =item B<--timeout>
196
197 Set timeout in seconds (default: 50).
198
199 =back
200
201 =head1 DESCRIPTION
202
203 B<custom>.
204
205 =cut