Debian Codegraph @

src/storage/hp/storeonce/3/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 storage::hp::storeonce::3::restapi::custom::api;
22
23 use strict;
24 use warnings;
25 use centreon::plugins::http;
26 use XML::Simple;
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 'username:s' => { name => 'username' },
46 'password:s' => { name => 'password' },
47 'timeout:s' => { name => 'timeout' }
48 });
49 }
50 $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
51
52 $self->{output} = $options{output};
53 $self->{http} = centreon::plugins::http->new(%options);
54
55 return $self;
56 }
57
58 sub set_options {
59 my ($self, %options) = @_;
60
61 $self->{option_results} = $options{option_results};
62 }
63
64 sub set_defaults {}
65
66 sub check_options {
67 my ($self, %options) = @_;
68
69 $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : '';
70 $self->{username} = (defined($self->{option_results}->{username})) ? $self->{option_results}->{username} : '';
71 $self->{password} = (defined($self->{option_results}->{password})) ? $self->{option_results}->{password} : '';
72 $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
73
74 if ($self->{hostname} eq '') {
75 $self->{output}->add_option_msg(short_msg => "Need to specify hostname option.");
76 $self->{output}->option_exit();
77 }
78
79 return 0;
80 }
81
82 sub build_options_for_httplib {
83 my ($self, %options) = @_;
84
85 $self->{option_results}->{hostname} = $self->{hostname};
86 $self->{option_results}->{timeout} = $self->{timeout};
87 $self->{option_results}->{port} = 443;
88 $self->{option_results}->{proto} = 'https';
89 $self->{option_results}->{credentials} = 1;
90 $self->{option_results}->{basic} = 1;
91 $self->{option_results}->{username} = $self->{username};
92 $self->{option_results}->{password} = $self->{password};
93 }
94
95 sub settings {
96 my ($self, %options) = @_;
97
98 $self->build_options_for_httplib();
99 $self->{http}->add_header(key => 'Accept', value => 'text/xml');
100 $self->{http}->set_options(%{$self->{option_results}});
101 }
102
103 sub get {
104 my ($self, %options) = @_;
105
106 $self->settings();
107 my $response = $self->{http}->request(
108 url_path => '/storeonceservices' . $options{path},
109 critical_status => '',
110 warning_status => ''
111 );
112
113 my $content;
114 eval {
115 $content = XMLin($response, ForceArray => $options{ForceArray}, KeyAttr => []);
116 };
117 if ($@) {
118 $self->{output}->add_option_msg(short_msg => "Cannot decode xml response: $@");
119 $self->{output}->option_exit();
120 }
121
122 return $content;
123 }
124
125 1;
126
127 __END__
128
129 =head1 NAME
130
131 STOREONCE REST API
132
133 =head1 SYNOPSIS
134
135 Storeonce Rest API custom mode
136
137 =head1 REST API OPTIONS
138
139 =over 8
140
141 =item B<--hostname>
142
143 Storeonce hostname.
144
145 =item B<--username>
146
147 Storeonce username.
148
149 =item B<--password>
150
151 Storeonce password.
152
153 =item B<--timeout>
154
155 Set HTTP timeout
156
157 =back
158
159 =head1 DESCRIPTION
160
161 B<custom>.
162
163 =cut