(plugin) notification::centreon::opentickets::api (#3620)

This commit is contained in:
qgarnier 2022-04-26 16:12:35 +02:00 committed by GitHub
parent bcb3393e56
commit 276b37eaaf
4 changed files with 719 additions and 0 deletions

View File

@ -0,0 +1,306 @@
#
# Copyright 2022 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Authors : Roman Morandell - ivertix
#
package notification::centreon::opentickets::api::custom::api;
use strict;
use warnings;
use centreon::plugins::http;
use centreon::plugins::statefile;
use JSON::XS;
use Digest::MD5 qw(md5_hex);
sub new {
my ($class, %options) = @_;
my $self = {};
bless $self, $class;
if (!defined($options{output})) {
print "Class Custom: Need to specify 'output' argument.\n";
exit 3;
}
if (!defined($options{options})) {
$options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
$options{output}->option_exit();
}
if (!defined($options{noptions})) {
$options{options}->add_options(arguments => {
'api-hostname:s' => { name => 'api_hostname' },
'api-port:s' => { name => 'api_port' },
'api-proto:s' => { name => 'api_proto' },
'url-path:s' => { name => 'url_path' },
'api-username:s' => { name => 'api_username' },
'api-password:s' => { name => 'api_password' },
'api-timeout:s' => { name => 'api_timeout' }
});
}
$options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
$self->{output} = $options{output};
$self->{http} = centreon::plugins::http->new(%options);
$self->{cache} = centreon::plugins::statefile->new(%options);
return $self;
}
sub set_options {
my ($self, %options) = @_;
$self->{option_results} = $options{option_results};
}
sub set_defaults {}
sub check_options {
my ($self, %options) = @_;
$self->{api_hostname} = (defined($self->{option_results}->{api_hostname})) ? $self->{option_results}->{api_hostname} : '';
$self->{api_proto} = (defined($self->{option_results}->{api_proto})) ? $self->{option_results}->{api_proto} : 'http';
$self->{api_port} = (defined($self->{option_results}->{api_port})) ? $self->{option_results}->{api_port} : 80;
$self->{url_path} = (defined($self->{option_results}->{url_path})) ? $self->{option_results}->{url_path} : '/centreon/api/';
$self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : '';
$self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : '';
$self->{api_timeout} = (defined($self->{option_results}->{api_timeout})) ? $self->{option_results}->{api_timeout} : 10;
if ($self->{api_hostname} eq '') {
$self->{output}->add_option_msg(short_msg => 'Need to specify api-hostname option.');
$self->{output}->option_exit();
}
if ($self->{api_username} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --api-username option.");
$self->{output}->option_exit();
}
if ($self->{api_password} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --api-password option.");
$self->{output}->option_exit();
}
$self->{cache}->check_options(option_results => $self->{option_results});
return 0;
}
sub get_connection_infos {
my ($self, %options) = @_;
return $self->{hostname} . '_' . $self->{http}->get_port();
}
sub get_hostname {
my ($self, %options) = @_;
return $self->{hostname};
}
sub get_port {
my ($self, %options) = @_;
return $self->{port};
}
sub json_decode {
my ($self, %options) = @_;
my $decoded;
eval {
$decoded = JSON::XS->new->utf8->decode($options{content});
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
$self->{output}->option_exit();
}
return $decoded;
}
sub settings {
my ($self, %options) = @_;
$self->{http}->add_header(key => 'Accept', value => 'application/json');
$self->{http}->set_options(
hostname => $self->{api_hostname},
port => $self->{api_port},
proto => $self->{api_proto}
);
}
sub clean_token {
my ($self, %options) = @_;
my $datas = {};
$self->{cache}->write(data => $datas);
}
sub get_token {
my ($self, %options) = @_;
my $has_cache_file = $self->{cache}->read(statefile => 'centreon_opentickets_' . md5_hex($self->{api_hostname}) . '_' . md5_hex($self->{api_username}));
my $token = $self->{cache}->get(name => 'token');
if ($has_cache_file == 0 || !defined($token)) {
my ($content) = $self->{http}->request(
method => 'POST',
url_path => $self->{url_path} . 'index.php',
get_param => ['action=authenticate'],
post_param => [
'username=' . $self->{api_username},
'password=' . $self->{api_password}
],
warning_status => '', unknown_status => '', critical_status => ''
);
if ($self->{http}->get_code() != 200) {
$self->{output}->add_option_msg(short_msg => "Authentication error [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
$self->{output}->option_exit();
}
my $decoded = $self->json_decode(content => $content);
if (!defined($decoded->{authToken})) {
$self->{output}->add_option_msg(short_msg => "Cannot get token");
$self->{output}->option_exit();
}
$token = $decoded->{authToken};
my $datas = { token => $token };
$self->{cache}->write(data => $datas);
}
return $token;
}
sub request_api {
my ($self, %options) = @_;
$self->settings();
my $token = $self->get_token();
my $encoded;
eval {
$encoded = JSON::XS->new->utf8->encode($options{data});
};
if ($@) {
$self->{output}->add_option_msg(short_msg => 'cannot encode json request');
$self->{output}->option_exit();
}
my $content = $self->{http}->request(
method => 'POST',
url_path => $self->{url_path} . 'index.php',
get_param => [
'object=centreon_openticket',
'action=' . $options{action},
],
header => [
'Content-Type: application/json',
'centreon-auth-token: ' . $token
],
query_form_post => $encoded,
warning_status => '',
unknown_status => '',
critical_status => ''
);
if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) {
$self->clean_token();
$token = $self->get_token();
$content = $self->{http}->request(
method => 'POST',
url_path => $self->{url_path} . 'index.php',
get_param => [
'object=centreon_openticket',
'action=' . $options{action},
],
header => [
'Content-Type: application/json',
'centreon-auth-token: ' . $token
],
query_form_post => $encoded,
warning_status => '',
unknown_status => '',
critical_status => ''
);
}
if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) {
$self->{output}->add_option_msg(short_msg => "Authentication error [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
$self->{output}->option_exit();
}
my $decoded = $self->json_decode(content => $content);
if (!defined($decoded)) {
$self->{output}->add_option_msg(short_msg => 'Error while retrieving data (add --debug option for detailed message)');
$self->{output}->option_exit();
}
return $decoded;
}
1;
__END__
=head1 NAME
Centreon Open-Tickets API
=head1 SYNOPSIS
Centreon open-tickets api
=head1 REST API OPTIONS
=over 8
=item B<--api-hostname>
Centreon address.
=item B<--url-path>
API url path (Default: '/centreon/api/')
=item B<--api-port>
API port (Default: 80)
=item B<--api-proto>
Specify https if needed (Default: 'http')
=item B<--api-username>
API username
=item B<--api-password>
API password
=item B<--timeout>
HTTP timeout
=back
=head1 DESCRIPTION
B<custom>.
=cut

View File

@ -0,0 +1,174 @@
#
# Copyright 2022 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Authors : Roman Morandell - ivertix
#
package notification::centreon::opentickets::api::mode::openhost;
use base qw(centreon::plugins::mode);
use strict;
use warnings;
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments => {
'rule-name:s' => { name => 'rule_name' },
'contact-name:s' => { name => 'contact_name' },
'contact-alias:s' => { name => 'contact_alias' },
'contact-email:s' => { name => 'contact_email' },
'host-id:s' => { name => 'host_id' },
'host-output:s' => { name => 'host_output' },
'host-name:s' => { name => 'host_name' },
'host-alias:s' => { name => 'host_alias' },
'host-state:s' => { name => 'host_state' },
'last-host-state-change:s' => { name => 'last_service_state_change' },
'extra-property:s%' => { name => 'extra_property' },
'select:s%' => { name => 'select' }
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
if (!defined($self->{option_results}->{rule_name}) || $self->{option_results}->{rule_name} eq '') {
$self->{output}->add_option_msg(short_msg => 'Set --rule-name option');
$self->{output}->option_exit();
}
if (!defined($self->{option_results}->{host_id}) || $self->{option_results}->{host_id} eq '') {
$self->{output}->add_option_msg(short_msg => 'Set --host-id option');
$self->{output}->option_exit();
}
if (!defined($self->{option_results}->{host_state}) || $self->{option_results}->{host_state} eq '') {
$self->{output}->add_option_msg(short_msg => 'Set --host-state option');
$self->{output}->option_exit();
}
if (!defined($self->{option_results}->{host_output})) {
$self->{output}->add_option_msg(short_msg => 'Set --host-output option');
$self->{output}->option_exit();
}
}
sub run {
my ($self, %options) = @_;
my $extra_properties = {};
foreach (keys %{$self->{option_results}->{extra_property}}) {
$extra_properties->{$_} = $self->{option_results}->{extra_property}->{$_};
}
my $select = {};
foreach (keys %{$self->{option_results}->{select}}) {
$select->{$_} = $self->{option_results}->{select}->{$_};
}
my $properties = {};
foreach ('contact_name', 'contact_alias', 'contact_email', 'host_name', 'host_alias', 'last_host_state_change') {
if (defined($self->{option_results}->{$_}) && $self->{option_results}->{$_} ne '') {
$properties->{$_} = $self->{option_results}->{$_};
}
}
my $response = $options{custom}->request_api(
action => 'openHost',
data => {
rule_name => $self->{option_results}->{rule_name},
host_id => $self->{option_results}->{host_id},
host_state => $self->{option_results}->{host_state},
host_output => $self->{option_results}->{host_output},
extra_properties => $extra_properties,
select => $select,
%$properties
}
);
$self->{output}->output_add(short_msg => $response->{message});
$self->{output}->display(force_ignore_perfdata => 1, force_long_output => 1);
$self->{output}->exit();
}
1;
__END__
=head1 MODE
Open a host ticket.
=over 8
=item B<--rule-name>
Rule name used (Required).
=item B<--host-id>
Centreon host ID (Required).
=item B<--host-state>
Host state (Eg: UP, DOWN, UNREACHABLE) (Required).
=item B<--host-output>
Host output (Required).
=item B<--contact-name>
Contact name (default: --api-username contact information).
=item B<--contact-alias>
Contact alias (default: --api-username contact information).
=item B<--contact-email>
Contact email (default: --api-username contact information).
=item B<--host-name>
Host name.
=item B<--host-alias>
Host alias.
=item B<--last-host-state-change>
Last host state change.
=item B<--extra-property>
Add a extra property.
Eg: --extra-property='custom_message=test my message'
=item B<--select>
Add a select property (open-ticket list).
Eg: --select='list-id=value'
=back
=cut

View File

@ -0,0 +1,189 @@
#
# Copyright 2022 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Authors : Roman Morandell - ivertix
#
package notification::centreon::opentickets::api::mode::openservice;
use base qw(centreon::plugins::mode);
use strict;
use warnings;
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments => {
'rule-name:s' => { name => 'rule_name' },
'contact-name:s' => { name => 'contact_name' },
'contact-alias:s' => { name => 'contact_alias' },
'contact-email:s' => { name => 'contact_email' },
'host-id:s' => { name => 'host_id' },
'host-name:s' => { name => 'host_name' },
'host-alias:s' => { name => 'host_alias' },
'service-id:s' => { name => 'service_id' },
'service-description:s' => { name => 'service_description' },
'service-output:s' => { name => 'service_output' },
'service-state:s' => { name => 'service_state' },
'last-service-state-change:s' => { name => 'last_service_state_change' },
'extra-property:s%' => { name => 'extra_property' },
'select:s%' => { name => 'select' }
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
if (!defined($self->{option_results}->{rule_name}) || $self->{option_results}->{rule_name} eq '') {
$self->{output}->add_option_msg(short_msg => 'Set --rule-name option');
$self->{output}->option_exit();
}
if (!defined($self->{option_results}->{host_id}) || $self->{option_results}->{host_id} eq '') {
$self->{output}->add_option_msg(short_msg => 'Set --host-id option');
$self->{output}->option_exit();
}
if (!defined($self->{option_results}->{service_id}) || $self->{option_results}->{service_id} eq '') {
$self->{output}->add_option_msg(short_msg => 'Set --service-id option');
$self->{output}->option_exit();
}
if (!defined($self->{option_results}->{service_state}) || $self->{option_results}->{service_state} eq '') {
$self->{output}->add_option_msg(short_msg => 'Set --service-state option');
$self->{output}->option_exit();
}
if (!defined($self->{option_results}->{service_output})) {
$self->{output}->add_option_msg(short_msg => 'Set --service-output option');
$self->{output}->option_exit();
}
}
sub run {
my ($self, %options) = @_;
my $extra_properties = {};
foreach (keys %{$self->{option_results}->{extra_property}}) {
$extra_properties->{$_} = $self->{option_results}->{extra_property}->{$_};
}
my $select = {};
foreach (keys %{$self->{option_results}->{select}}) {
$select->{$_} = $self->{option_results}->{select}->{$_};
}
my $properties = {};
foreach ('contact_name', 'contact_alias', 'contact_email', 'host_name', 'host_alias', 'service_description', 'last_service_state_change') {
if (defined($self->{option_results}->{$_}) && $self->{option_results}->{$_} ne '') {
$properties->{$_} = $self->{option_results}->{$_};
}
}
my $response = $options{custom}->request_api(
action => 'openService',
data => {
rule_name => $self->{option_results}->{rule_name},
host_id => $self->{option_results}->{host_id},
service_id => $self->{option_results}->{service_id},
service_state => $self->{option_results}->{service_state},
service_output => $self->{option_results}->{service_output},
extra_properties => $extra_properties,
select => $select,
%$properties
}
);
$self->{output}->output_add(short_msg => $response->{message});
$self->{output}->display(force_ignore_perfdata => 1, force_long_output => 1);
$self->{output}->exit();
}
1;
__END__
=head1 MODE
Open a service ticket.
=over 8
=item B<--rule-name>
Rule name used (Required).
=item B<--host-id>
Centreon host ID (Required).
=item B<--service-id>
Centreon service ID (Required).
=item B<--service-state>
Service state (Eg: CRITICAL, UNKNOWN, WARNING, OK) (Required).
=item B<--service-output>
Service output (Required).
=item B<--contact-name>
Contact name (default: --api-username contact information).
=item B<--contact-alias>
Contact alias (default: --api-username contact information).
=item B<--contact-email>
Contact email (default: --api-username contact information).
=item B<--host-name>
Host name.
=item B<--host-alias>
Host alias.
=item B<--service-description>
Service description.
=item B<--last-service-state-change>
Last service state change.
=item B<--extra-property>
Add a extra property.
Eg: --extra-property='custom_message=test my message'
=item B<--select>
Add a select property (open-ticket list).
Eg: --select='list-id=value'
=back
=cut

View File

@ -0,0 +1,50 @@
#
# Copyright 2022 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Authors : Roman Morandell - ivertix
#
package notification::centreon::opentickets::api::plugin;
use strict;
use warnings;
use base qw(centreon::plugins::script_custom);
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$self->{modes} = {
'open-host' => 'notification::centreon::opentickets::api::mode::openhost',
'open-service' => 'notification::centreon::opentickets::api::mode::openservice'
};
$self->{custom_modes}->{api} = 'notification::centreon::opentickets::api::custom::api';
return $self;
}
1;
__END__
=head1 PLUGIN DESCRIPTION
Open tickets with centreon-open-tickets module.
=cut