From 2562ffc82147784087a201091d34e64633ec4da7 Mon Sep 17 00:00:00 2001 From: Colin Gagnaire Date: Thu, 15 Nov 2018 15:33:42 +0100 Subject: [PATCH] add cloud ibm softlayer plugin, gettickets mode (#1224) --- cloud/ibm/softlayer/custom/xmlapi.pm | 251 +++++++++++++++++++++++++ cloud/ibm/softlayer/mode/gettickets.pm | 166 ++++++++++++++++ cloud/ibm/softlayer/plugin.pm | 56 ++++++ 3 files changed, 473 insertions(+) create mode 100644 cloud/ibm/softlayer/custom/xmlapi.pm create mode 100644 cloud/ibm/softlayer/mode/gettickets.pm create mode 100644 cloud/ibm/softlayer/plugin.pm diff --git a/cloud/ibm/softlayer/custom/xmlapi.pm b/cloud/ibm/softlayer/custom/xmlapi.pm new file mode 100644 index 000000000..5fc4cc112 --- /dev/null +++ b/cloud/ibm/softlayer/custom/xmlapi.pm @@ -0,0 +1,251 @@ +# +# Copyright 2018 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. +# + +package cloud::ibm::softlayer::custom::xmlapi; + +use strict; +use warnings; +use centreon::plugins::http; +use XML::Simple; + +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 => + { + "hostname:s" => { name => 'hostname' }, + "url-path:s" => { name => 'url_path' }, + "port:s" => { name => 'port' }, + "proto:s" => { name => 'proto' }, + "proxyurl:s" => { name => 'proxyurl' }, + "timeout:s" => { name => 'timeout' }, + "ssl-opt:s@" => { name => 'ssl_opt' }, + "api-username:s" => { name => 'api_username' }, + "api-key:s" => { name => 'api_key' }, + }); + } + $options{options}->add_help(package => __PACKAGE__, sections => 'XMLAPI OPTIONS', once => 1); + + $self->{output} = $options{output}; + $self->{mode} = $options{mode}; + $self->{http} = centreon::plugins::http->new(output => $self->{output}); + + return $self; +} + +sub set_options { + my ($self, %options) = @_; + + $self->{option_results} = $options{option_results}; +} + +sub set_defaults { + my ($self, %options) = @_; + + foreach (keys %{$options{default}}) { + if ($_ eq $self->{mode}) { + for (my $i = 0; $i < scalar(@{$options{default}->{$_}}); $i++) { + foreach my $opt (keys %{$options{default}->{$_}[$i]}) { + if (!defined($self->{option_results}->{$opt}[$i])) { + $self->{option_results}->{$opt}[$i] = $options{default}->{$_}[$i]->{$opt}; + } + } + } + } + } +} + +sub check_options { + my ($self, %options) = @_; + + $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : 'api.softlayer.com'; + $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443; + $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https'; + $self->{url_path} = (defined($self->{option_results}->{url_path})) ? $self->{option_results}->{url_path} : '/soap/v3'; + $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10; + $self->{proxyurl} = (defined($self->{option_results}->{proxyurl})) ? $self->{option_results}->{proxyurl} : undef; + $self->{ssl_opt} = (defined($self->{option_results}->{ssl_opt})) ? $self->{option_results}->{ssl_opt} : undef; + + if (!defined($self->{option_results}->{api_username}) || $self->{option_results}->{api_username} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --api-username option."); + $self->{output}->option_exit(); + } + if (!defined($self->{option_results}->{api_key}) || $self->{option_results}->{api_key} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --api-key option."); + $self->{output}->option_exit(); + } + + return 0; +} + +sub build_options_for_httplib { + my ($self, %options) = @_; + + $self->{option_results}->{hostname} = $self->{hostname}; + $self->{option_results}->{timeout} = $self->{timeout}; + $self->{option_results}->{port} = $self->{port}; + $self->{option_results}->{proto} = $self->{proto}; + $self->{option_results}->{proxyurl} = $self->{proxyurl}; + $self->{option_results}->{warning_status} = ''; + $self->{option_results}->{critical_status} = ''; +} + +sub settings { + my ($self, %options) = @_; + + $self->build_options_for_httplib(); + + $self->{http}->add_header(key => 'Accept', value => 'text/xml'); + $self->{http}->add_header(key => 'Accept', value => 'multipart/*'); + $self->{http}->add_header(key => 'Accept', value => 'text/xmlapplication/soap'); + $self->{http}->add_header(key => 'Content-Type', value => 'text/xml; charset=utf-8'); + $self->{http}->set_options(%{$self->{option_results}}); +} + +sub get_connection_info { + my ($self, %options) = @_; + + return $self->{hostname} . ":" . $self->{port}; +} + +sub get_hostname { + my ($self, %options) = @_; + + return $self->{hostname}; +} + +sub get_port { + my ($self, %options) = @_; + + return $self->{port}; +} + +sub get_api_username { + my ($self, %options) = @_; + + return $self->{option_results}->{api_username}; +} + +sub get_api_key { + my ($self, %options) = @_; + + return $self->{option_results}->{api_key}; +} + +sub get_endpoint { + my ($self, %options) = @_; + + $self->settings; + + $self->{http}->add_header(key => 'SOAPAction', value => 'http://api.service.softlayer.com/soap/v3/#' . $options{method}); + + my $content = ' + + + + ' . $self->get_api_key() . ' + ' . $self->get_api_username() . ' + + + + + +'; + + my $response = $self->{http}->request(url_path => $self->{url_path} . '/' . $options{service}, method => 'POST', query_form_post => $content); + + my $xml_hash = XMLin($response); + + return $xml_hash->{'SOAP-ENV:Header'}, $xml_hash->{'SOAP-ENV:Body'}; +} + +1; + +__END__ + +=head1 NAME + +IBM SoftLayer XML API + +=head1 SYNOPSIS + +IBM SoftLayer XML API + +=head1 XMLAPI OPTIONS + +=over 8 + +=item B<--hostname> + +API hostname (Default: 'api.softlayer.com'). + +=item B<--url-path> + +API url path (Default: '/soap/v3') + +=item B<--port> + +API port (Default: 443) + +=item B<--proto> + +Specify https if needed (Default: 'https') + +=item B<--api-username> + +Set API username + +=item B<--api-key> + +Set API Key + +=item B<--proxyurl> + +Proxy URL if any + +=item B<--timeout> + +Set HTTP timeout + +=item B<--ssl-opt> + +Set SSL Options (--ssl-opt="SSL_version => TLSv1" --ssl-opt="SSL_verify_mode => SSL_VERIFY_NONE"). + +=back + +=head1 DESCRIPTION + +B. + +=cut diff --git a/cloud/ibm/softlayer/mode/gettickets.pm b/cloud/ibm/softlayer/mode/gettickets.pm new file mode 100644 index 000000000..69a92f3a1 --- /dev/null +++ b/cloud/ibm/softlayer/mode/gettickets.pm @@ -0,0 +1,166 @@ +# +# Copyright 2018 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. +# + +package cloud::ibm::softlayer::mode::gettickets; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; + +sub custom_ticket_output { + my ($self, %options) = @_; + + my $msg = sprintf("Title: '%s', Group: '%s', Priority: %s, Create Date: %s", $self->{result_values}->{title}, + $self->{result_values}->{group}, $self->{result_values}->{priority}, $self->{result_values}->{createDate}); + return $msg; +} + +sub custom_ticket_calc { + my ($self, %options) = @_; + + $self->{result_values}->{id} = $options{new_datas}->{$self->{instance} . '_id'}; + $self->{result_values}->{title} = $options{new_datas}->{$self->{instance} . '_title'}; + $self->{result_values}->{priority} = $options{new_datas}->{$self->{instance} . '_priority'}; + $self->{result_values}->{createDate} = $options{new_datas}->{$self->{instance} . '_createDate'}; + $self->{result_values}->{group} = $options{new_datas}->{$self->{instance} . '_group'}; + return 0; +} + +sub prefix_tickets_output { + my ($self, %options) = @_; + + return "Ticket '" . $options{instance_value}->{id} . "' is open with "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0 }, + { name => 'tickets', type => 1, cb_prefix_output => 'prefix_tickets_output' }, + ]; + + $self->{maps_counters}->{global} = [ + { label => 'open', set => { + key_values => [ { name => 'open' } ], + output_template => 'Number of open tickets : %d', + perfdatas => [ + { label => 'open_tickets', value => 'open_absolute', template => '%d', + min => 0 }, + ], + } + }, + ]; + $self->{maps_counters}->{tickets} = [ + { label => 'ticket', threshold => 0, set => { + key_values => [ { name => 'id' }, { name => 'title' }, { name => 'priority' }, { name => 'createDate' }, + { name => 'group' } ], + closure_custom_calc => $self->can('custom_ticket_calc'), + closure_custom_output => $self->can('custom_ticket_output'), + closure_custom_perfdata => sub { return 0; }, + } + }, + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => + { + "ticket-group:s" => { name => 'ticket_group' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{global}->{open} = 0; + $self->{tickets} = {}; + + my $group_id = ''; + my %groups_hash; + my (undef, $groups) = $options{custom}->get_endpoint(service => 'SoftLayer_Ticket', method => 'getAllTicketGroups'); + foreach my $group (@{$groups->{'ns1:getAllTicketGroupsResponse'}->{'getAllTicketGroupsReturn'}->{'item'}}) { + $groups_hash{$group->{id}->{content}} = $group->{name}->{content}; + + if (defined($self->{option_results}->{ticket_group}) && $self->{option_results}->{ticket_group} ne '' && + $group->{name}->{content} =~ /$self->{option_results}->{ticket_group}/) { + $group_id = $group->{id}->{content}; + } + } + + if (defined($self->{option_results}->{ticket_group}) && $self->{option_results}->{ticket_group} ne '' && $group_id eq '') { + $self->{output}->add_option_msg(short_msg => "Ticket group ID not found from API."); + $self->{output}->option_exit(); + } + + my (undef, $tickets) = $options{custom}->get_endpoint(service => 'SoftLayer_Account', method => 'getOpenTickets'); + foreach my $ticket (@{$tickets->{'ns1:getOpenTicketsResponse'}->{'getOpenTicketsReturn'}->{'item'}}) { + next if (defined($group_id) && $group_id ne '' && $ticket->{groupId}->{content} ne $group_id); + + $self->{tickets}->{$ticket->{id}->{content}} = { + id => $ticket->{id}->{content}, + title => $ticket->{title}->{content}, + priority => $ticket->{priority}->{content}, + createDate => $ticket->{createDate}->{content}, + group => $groups_hash{$ticket->{groupId}->{content}}, + }; + + $self->{global}->{open}++; + } +} + +1; + +__END__ + +=head1 MODE + +Check if there is open tickets + +=over 8 + +=item B<--ticket-group> + +Name of the ticket group (Can be a regexp). + +=item B<--warning-open> + +Threshold warning for open tickets. + +=item B<--critical-open> + +Threshold critical for open tickets. + +=back + +=cut diff --git a/cloud/ibm/softlayer/plugin.pm b/cloud/ibm/softlayer/plugin.pm new file mode 100644 index 000000000..2b173a40c --- /dev/null +++ b/cloud/ibm/softlayer/plugin.pm @@ -0,0 +1,56 @@ +# +# Copyright 2018 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. +# + +package cloud::ibm::softlayer::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->{version} = '0.1'; + %{$self->{modes}} = ( + 'get-tickets' => 'cloud::ibm::softlayer::mode::gettickets', + ); + + $self->{custom_modes}{xmlapi} = 'cloud::ibm::softlayer::custom::xmlapi'; + + return $self; +} + +sub init { + my ($self, %options) = @_; + + $self->SUPER::init(%options); +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check IBM Softlayer + +=cut