centreon-plugins/cloud/ibm/softlayer/mode/opentickets.pm

241 lines
8.2 KiB
Perl
Raw Normal View History

#
2019-01-09 09:57:11 +01:00
# Copyright 2019 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::opentickets;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use DateTime;
my $instance_mode;
sub custom_status_threshold {
my ($self, %options) = @_;
my $status = 'ok';
my $message;
eval {
local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; };
if (defined($instance_mode->{option_results}->{critical_status}) && $instance_mode->{option_results}->{critical_status} ne '' &&
eval "$instance_mode->{option_results}->{critical_status}") {
$status = 'critical';
} elsif (defined($instance_mode->{option_results}->{warning_status}) && $instance_mode->{option_results}->{warning_status} ne '' &&
eval "$instance_mode->{option_results}->{warning_status}") {
$status = 'warning';
}
};
if (defined($message)) {
$self->{output}->output_add(long_msg => 'filter status issue: ' . $message);
}
return $status;
}
sub custom_ticket_output {
my ($self, %options) = @_;
my $msg = sprintf("Title: '%s', Group: '%s', Priority: %s, Create Date: %s (%s ago)",
$self->{result_values}->{title},
$self->{result_values}->{group},
$self->{result_values}->{priority},
$self->{result_values}->{create_date},
centreon::plugins::misc::change_seconds(value => $self->{result_values}->{since}));
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}->{create_date} = $options{new_datas}->{$self->{instance} . '_create_date'};
$self->{result_values}->{group} = $options{new_datas}->{$self->{instance} . '_group'};
$self->{result_values}->{since} = $options{new_datas}->{$self->{instance} . '_since'};
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 => 'create_date' },
{ name => 'group' }, { name => 'since' } ],
closure_custom_calc => $self->can('custom_ticket_calc'),
closure_custom_output => $self->can('custom_ticket_output'),
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => $self->can('custom_status_threshold'),
}
},
];
}
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' },
"warning-status:s" => { name => 'warning_status', default => '' },
"critical-status:s" => { name => 'critical_status', default => '' },
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
$instance_mode = $self;
$self->change_macros();
}
sub change_macros {
my ($self, %options) = @_;
foreach (('warning_status', 'critical_status')) {
if (defined($self->{option_results}->{$_})) {
$self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{result_values}->{$1}/g;
}
}
}
sub manage_selection {
my ($self, %options) = @_;
$self->{global}->{open} = 0;
$self->{tickets} = {};
my $group_id = '';
my %groups_hash;
2018-12-11 16:29:25 +01:00
my $groups = $options{custom}->get_endpoint(service => 'SoftLayer_Ticket', method => 'getAllTicketGroups', extra_content => '');
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 '' &&
2018-12-18 14:25:22 +01:00
$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 $current_time = time();
2018-12-11 16:29:25 +01:00
my $tickets = $options{custom}->get_endpoint(service => 'SoftLayer_Account', method => 'getOpenTickets', extra_content => '');
foreach my $ticket (@{$tickets->{'ns1:getOpenTicketsResponse'}->{'getOpenTicketsReturn'}->{'item'}}) {
next if (defined($group_id) && $group_id ne '' && $ticket->{groupId}->{content} ne $group_id);
next if ($ticket->{createDate}->{content} !~ /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(.*)$/); # 2018-10-18T15:36:54+00:00
my $dt = DateTime->new(
year => $1,
month => $2,
day => $3,
hour => $4,
minute => $5,
second => $6,
time_zone => $7
);
$self->{tickets}->{$ticket->{id}->{content}} = {
id => $ticket->{id}->{content},
title => $ticket->{title}->{content},
priority => $ticket->{priority}->{content},
create_date => $ticket->{createDate}->{content},
group => $groups_hash{$ticket->{groupId}->{content}},
since => $current_time - $dt->epoch,
};
$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-status>
Set warning threshold for status (Default: '')
Can used special variables like: %{id}, %{title},
%{priority}, %{create_date}, %{group}, %{since}.
=item B<--critical-status>
Set critical threshold for status (Default: '').
Can used special variables like: %{id}, %{title},
%{priority}, %{create_date}, %{group}, %{since}.
=item B<--warning-open>
Threshold warning for open tickets.
=item B<--critical-open>
Threshold critical for open tickets.
=back
=cut