Debian Codegraph @

src/apps/video/zixi/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::video::zixi::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 'hostname:s@' => { name => 'hostname' },
45 'port:s@' => { name => 'port' },
46 'proto:s@' => { name => 'proto' },
47 'username:s@' => { name => 'username' },
48 'password:s@' => { name => 'password' },
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->{http} = centreon::plugins::http->new(%options);
56
57 return $self;
58 }
59
60 sub set_options {
61 my ($self, %options) = @_;
62
63 $self->{option_results} = $options{option_results};
64 }
65
66 sub set_defaults {}
67
68 sub check_options {
69 my ($self, %options) = @_;
70
71 $self->{hostname} = (defined($self->{option_results}->{hostname})) ? shift(@{$self->{option_results}->{hostname}}) : undef;
72 $self->{port} = (defined($self->{option_results}->{port})) ? shift(@{$self->{option_results}->{port}}) : 4444;
73 $self->{proto} = (defined($self->{option_results}->{proto})) ? shift(@{$self->{option_results}->{proto}}) : 'http';
74 $self->{username} = (defined($self->{option_results}->{username})) ? shift(@{$self->{option_results}->{username}}) : '';
75 $self->{password} = (defined($self->{option_results}->{password})) ? shift(@{$self->{option_results}->{password}}) : '';
76 $self->{timeout} = (defined($self->{option_results}->{timeout})) ? shift(@{$self->{option_results}->{timeout}}) : 10;
77
78 if (!defined($self->{hostname})) {
79 $self->{output}->add_option_msg(short_msg => "Need to specify hostname option.");
80 $self->{output}->option_exit();
81 }
82
83 if (!defined($self->{hostname}) ||
84 scalar(@{$self->{option_results}->{hostname}}) == 0) {
85 return 0;
86 }
87 return 1;
88 }
89
90 sub build_options_for_httplib {
91 my ($self, %options) = @_;
92
93 $self->{option_results}->{hostname} = $self->{hostname};
94 $self->{option_results}->{timeout} = $self->{timeout};
95 $self->{option_results}->{port} = $self->{port};
96 $self->{option_results}->{proto} = $self->{proto};
97 $self->{option_results}->{credentials} = 1;
98 $self->{option_results}->{basic} = 1;
99 $self->{option_results}->{username} = $self->{username};
100 $self->{option_results}->{password} = $self->{password};
101 }
102
103 sub settings {
104 my ($self, %options) = @_;
105
106 $self->build_options_for_httplib();
107 $self->{http}->set_options(%{$self->{option_results}});
108 }
109
110 sub get {
111 my ($self, %options) = @_;
112
113 $self->settings();
114
115 my $response = $self->{http}->request(
116 url_path => $options{path},
117 critical_status => '', warning_status => ''
118 );
119 my $content;
120 eval {
121 $content = JSON::XS->new->utf8->decode($response);
122 };
123 if ($@) {
124 $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
125 $self->{output}->option_exit();
126 }
127 if (defined($content->{errmsg})) {
128 $self->{output}->add_option_msg(short_msg => "Cannot get data: " . $content->{errmsg});
129 $self->{output}->option_exit();
130 }
131
132 return $content;
133 }
134
135 1;
136
137 __END__
138
139 =head1 NAME
140
141 Zixi REST API
142
143 =head1 SYNOPSIS
144
145 Zixi Rest API custom mode
146
147 =head1 REST API OPTIONS
148
149 =over 8
150
151 =item B<--hostname>
152
153 Zixi hostname.
154
155 =item B<--port>
156
157 Port used (default: 4444)
158
159 =item B<--proto>
160
161 Specify https if needed (default: 'http')
162
163 =item B<--username>
164
165 Zixi username.
166
167 =item B<--password>
168
169 Zixi password.
170
171 =item B<--timeout>
172
173 Set HTTP timeout
174
175 =back
176
177 =head1 DESCRIPTION
178
179 B<custom>.
180
181 =cut