Skip to content

Commit 3218060

Browse files
Evan-Adamsdepassio
andauthored
[CTOR-284] new Datacore plugin with rest api (centreon#4896)
Co-authored-by: Sophie Depassio <[email protected]>
1 parent 058620c commit 3218060

18 files changed

Lines changed: 1232 additions & 26 deletions

File tree

.github/scripts/stopwords.t

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
--filter-vdom
22
--force-counters32
33
Centreon
4-
Fortinet
4+
Datacore
55
Fortigate
6+
Fortinet
67
license-instances-usage-prct
78
MBean
89
OID
910
oneaccess-sys-mib
1011
perfdata
1112
powershell
13+
proto
14+
Sansymphony
1215
queue-messages-inflighted
1316
SNMP
1417
space-usage-prct

.github/workflows/tests-functional.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ jobs:
3030
- name: Install Node.js
3131
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
3232
with:
33-
node-version: 16
33+
node-version: 20
3434

3535
- name: Install Mockoon CLI
36-
run: npm install -g -D @mockoon/cli@3.1.0
36+
run: npm install -g -D @mockoon/cli@6.2.0
3737

3838
- name: Install perl dependencies
3939
uses: shogo82148/actions-setup-perl@28eae78d12c2bba1163aec45d123f6d9228bc307 # v1.29.0

doc/en/developer/plugins_advanced.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -943,9 +943,9 @@ List of methods:
943943
* **new**: class constructor. Overload if you need to add some specific options
944944
or to use a statefile.
945945
* **check_options**: overload if you need to check your specific options.
946-
* **manage_selection**: overload if *mandatory*. Method to get informations for
946+
* **manage_selection**: overload is *mandatory*. Method to get informations for
947947
the equipment.
948-
* **set_counters**: overload if **mandatory**. Method to configure counters.
948+
* **set_counters**: overload is **mandatory**. Method to configure counters.
949949

950950
**Class hardware**
951951

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"dependencies": [
3+
]
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"pkg_name": "centreon-plugin-Hardware-Storage-DataCore-Sansymphony-Restapi",
3+
"pkg_summary": "Centreon Plugin",
4+
"plugin_name": "centreon_datacore_api.pl",
5+
"files": [
6+
"centreon/plugins/script_custom.pm",
7+
"storage/datacore/api/"
8+
]
9+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"dependencies": [
3+
]
4+
}

src/centreon/plugins/misc.pm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,14 @@ sub backtick {
311311
return (0, join("\n", @output), $return_code);
312312
}
313313

314+
sub is_empty {
315+
my $value = shift;
316+
if (!defined($value) or $value eq '') {
317+
return 1;
318+
}
319+
return 0;
320+
}
321+
314322
sub trim {
315323
my ($value) = $_[0];
316324

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+
package storage::datacore::restapi::custom::api;
21+
use strict;
22+
use warnings;
23+
use centreon::plugins::http;
24+
use centreon::plugins::statefile;
25+
use JSON::XS;
26+
use centreon::plugins::misc qw(empty);
27+
28+
sub new {
29+
my ($class, %options) = @_;
30+
if (!defined($options{output})) {
31+
print "Class Custom: Need to specify 'output' argument.\n";
32+
exit 3;
33+
}
34+
if (!defined($options{options})) {
35+
$options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
36+
$options{output}->option_exit();
37+
}
38+
my $self = {};
39+
bless $self, $class;
40+
41+
42+
$options{options}->add_options(arguments => {
43+
'hostname:s' => { name => 'hostname' },
44+
'port:s' => { name => 'port', default => 443 },
45+
'proto:s' => { name => 'proto', default => 'https' },
46+
'timeout:s' => { name => 'timeout' },
47+
'username:s' => { name => 'username' },
48+
'password:s' => { name => 'password' },
49+
'unknown-http-status:s' => { name => 'unknown_http_status' },
50+
'warning-http-status:s' => { name => 'warning_http_status' },
51+
'critical-http-status:s' => { name => 'critical_http_status' }
52+
});
53+
54+
$options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
55+
56+
$self->{output} = $options{output};
57+
58+
$self->{http} = centreon::plugins::http->new(%options, default_backend => 'curl');
59+
60+
return $self;
61+
}
62+
63+
sub set_options {
64+
my ($self, %options) = @_;
65+
66+
$self->{option_results} = $options{option_results};
67+
}
68+
sub set_defaults {}
69+
70+
# hostname,username and password are required options
71+
sub check_options {
72+
my ($self, %options) = @_;
73+
$self->{http}->set_options(%{$self->{option_results}});
74+
75+
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 50;
76+
77+
if (centreon::plugins::misc::is_empty($self->{option_results}->{hostname})) {
78+
$self->{output}->add_option_msg(short_msg => 'Please set hostname option');
79+
$self->{output}->option_exit();
80+
}
81+
if (centreon::plugins::misc::is_empty($self->{option_results}->{username})) {
82+
$self->{output}->add_option_msg(short_msg => 'Please set username option to authenticate against datacore rest api');
83+
$self->{output}->option_exit();
84+
}
85+
if (centreon::plugins::misc::is_empty($self->{option_results}->{password})) {
86+
$self->{output}->add_option_msg(short_msg => 'Please set password option to authenticate against datacore rest api');
87+
$self->{output}->option_exit();
88+
}
89+
90+
}
91+
92+
sub settings {
93+
my ($self, %options) = @_;
94+
95+
return if (defined($self->{settings_done}));
96+
$self->{http}->add_header(key => 'ServerHost', value => $self->{option_results}->{hostname});
97+
$self->{http}->set_options(basic => 1, credentials => 1);
98+
$self->{settings_done} = 1;
99+
}
100+
101+
# wrapper around centreon::plugins::http::request to add authentication and decode json.
102+
# output : deserialized json from the api if not error found in http call.
103+
sub request_api {
104+
my ($self, %options) = @_;
105+
106+
# datacore api require a ServerHost header with the hostname used to query the api to respond.
107+
# authentication is http standard basic auth.
108+
my $result = $self->{http}->request(
109+
username => $self->{option_results}->{username},
110+
password => $self->{option_results}->{password},
111+
unknown_status => $self->{option_results}->{unknown_http_status},
112+
warning_status => $self->{option_results}->{warning_http_status},
113+
critical_status => $self->{option_results}->{critical_http_status},
114+
%options,
115+
);
116+
# Declare a scalar to deserialize the JSON content string into a perl data structure
117+
my $decoded_content;
118+
eval {
119+
$decoded_content = JSON::XS->new->decode($result);
120+
};
121+
# Catch the error that may arise in case the data received is not JSON
122+
if ($@) {
123+
$self->{output}->add_option_msg(short_msg => "Cannot decode JSON result");
124+
$self->{output}->option_exit();
125+
}
126+
return $decoded_content;
127+
128+
}
129+
1;
130+
131+
132+
__END__
133+
134+
=head1 NAME
135+
136+
Datacore Sansymphony Rest API
137+
138+
=head1 REST API OPTIONS
139+
140+
Datacore Sansymphony Rest API
141+
142+
=over 8
143+
144+
=item B<--hostname>
145+
146+
Address of the Datacore server that hosts the API endpoint.
147+
148+
=item B<--port>
149+
150+
Port of the resource to connect to (default: 443).
151+
152+
=item B<--proto>
153+
154+
HTTP protocol, either http or https (default: 'https')
155+
156+
=item B<--username>
157+
158+
Username to access the endpoint.
159+
160+
=item B<--password>
161+
162+
Password to access the endpoint.
163+
164+
=item B<--timeout>
165+
166+
Set timeout in seconds (default: 10).
167+
168+
=back
169+
170+
=head1 DESCRIPTION
171+
172+
B<custom>.

0 commit comments

Comments
 (0)