Debian Codegraph @

src/blockchain/parity/ethpoller/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 blockchain::parity::ethpoller::custom::api;
22
23 use strict;
24 use warnings;
25 use centreon::plugins::http;
26 use DateTime;
27 use JSON::XS;
28
29 sub new {
30 my ($class, %options) = @_;
31 my $self = {};
32 bless $self, $class;
33
34 if (!defined($options{output})) {
35 print "Class Custom: Need to specify 'output' argument.\n";
36 exit 3;
37 }
38 if (!defined($options{options})) {
39 $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
40 $options{output}->option_exit();
41 }
42
43 if (!defined($options{noptions})) {
44 $options{options}->add_options(arguments => {
45 "hostname:s" => { name => 'hostname' },
46 "port:s" => { name => 'port' },
47 "proto:s" => { name => 'proto' },
48 "url-path:s" => { name => 'url_path' },
49 "timeout:s" => { name => 'timeout' },
50 });
51 }
52 $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
53
54 $self->{output} = $options{output};
55 $self->{mode} = $options{mode};
56 $self->{http} = centreon::plugins::http->new(%options);
57
58 return $self;
59 }
60
61 sub set_options {
62 my ($self, %options) = @_;
63
64 $self->{option_results} = $options{option_results};
65 }
66
67 sub set_defaults {
68 my ($self, %options) = @_;
69
70 foreach (keys %{$options{default}}) {
71 if ($_ eq $self->{mode}) {
72 for (my $i = 0; $i < scalar(@{$options{default}->{$_}}); $i++) {
73 foreach my $opt (keys %{$options{default}->{$_}[$i]}) {
74 if (!defined($self->{option_results}->{$opt}[$i])) {
75 $self->{option_results}->{$opt}[$i] = $options{default}->{$_}[$i]->{$opt};
76 }
77 }
78 }
79 }
80 }
81 }
82
83 sub check_options {
84 my ($self, %options) = @_;
85
86 $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : undef;
87 $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 8000;
88 $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'http';
89 $self->{url_path} = (defined($self->{option_results}->{url_path})) ? $self->{option_results}->{url_path} : '';
90 $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
91
92 if (!defined($self->{hostname}) || $self->{hostname} eq '') {
93 $self->{output}->add_option_msg(short_msg => "Need to specify --hostname option.");
94 $self->{output}->option_exit();
95 }
96
97 return 0;
98 }
99
100 sub build_options_for_httplib {
101 my ($self, %options) = @_;
102
103 $self->{option_results}->{hostname} = $self->{hostname};
104 $self->{option_results}->{timeout} = $self->{timeout};
105 $self->{option_results}->{port} = $self->{port};
106 $self->{option_results}->{proto} = $self->{proto};
107 $self->{option_results}->{url_path} = $self->{url_path};
108 $self->{option_results}->{warning_status} = '';
109 $self->{option_results}->{critical_status} = '';
110 }
111
112 sub settings {
113 my ($self, %options) = @_;
114
115 $self->build_options_for_httplib();
116 $self->{http}->add_header(key => 'Accept', value => 'application/json');
117 $self->{http}->set_options(%{$self->{option_results}});
118 }
119
120 sub get_connection_info {
121 my ($self, %options) = @_;
122
123 return $self->{hostname} . ":" . $self->{port};
124 }
125
126 sub get_hostname {
127 my ($self, %options) = @_;
128
129 return $self->{hostname};
130 }
131
132 sub get_port {
133 my ($self, %options) = @_;
134
135 return $self->{port};
136 }
137
138 sub request_api {
139 my ($self, %options) = @_;
140
141 $self->settings;
142
143 $self->{output}->output_add(long_msg => "Query URL: '" . $self->{proto} . "://" . $self->{hostname} .
144 $self->{url_path} . $options{url_path} . "'", debug => 1);
145
146 my $content = $self->{http}->request(url_path => $self->{url_path} . $options{url_path});
147
148 my $decoded;
149 eval {
150 $decoded = JSON::XS->new->utf8->decode($content);
151 };
152 if ($@) {
153 $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
154 $self->{output}->option_exit();
155 }
156
157 return $decoded;
158 }
159
160 1;
161
162 __END__
163
164 =head1 NAME
165
166 Parity eth-poller Rest API
167
168 =head1 SYNOPSIS
169
170 Parity eth-poller Rest API custom mode
171
172 =head1 REST API OPTIONS
173
174 Parity eth-poller Rest API
175
176 =over 8
177
178 =item B<--hostname>
179
180 Parity eth-poller API hostname.
181
182 =item B<--port>
183
184 API port (default: 8000)
185
186 =item B<--proto>
187
188 Specify https if needed (default: 'http')
189
190 =item B<--url-path>
191
192 API URL path (default: '')
193
194 =item B<--timeout>
195
196 Set HTTP timeout
197
198 =back
199
200 =head1 DESCRIPTION
201
202 B<custom>.
203
204 =cut