Skip to content

Commit

Permalink
Merge pull request #1 from centreon/master
Browse files Browse the repository at this point in the history
maj upstream
  • Loading branch information
BenoitPoulet authored Feb 6, 2019
2 parents 0538afa + ea78f21 commit 626da32
Show file tree
Hide file tree
Showing 71 changed files with 7,149 additions and 1,601 deletions.
149 changes: 112 additions & 37 deletions apps/vmware/connector/custom/connector.pm
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ sub new {
"timeout:s@" => { name => 'timeout' },
"sampling-period:s@" => { name => 'sampling_period' },
"time-shift:s@" => { name => 'time_shift' },
"case-insensitive" => { name => 'case_insensitive' },
"unknown-connector-status:s" => { name => 'unknown_connector_status' },
"warning-connector-status:s" => { name => 'warning_connector_status' },
"critical-connector-status:s" => { name => 'critical_connector_status' },
});
}
$options{options}->add_help(package => __PACKAGE__, sections => 'CONNECTOR OPTIONS', once => 1);
Expand All @@ -65,6 +69,7 @@ sub new {
$self->{mode} = $options{mode};

$self->{json_send} = {};
$self->{result} = undef;
return $self;
}

Expand Down Expand Up @@ -109,6 +114,10 @@ sub check_options {
$self->{vsphere_password} = (defined($self->{option_results}->{vsphere_password})) ? shift(@{$self->{option_results}->{vsphere_password}}) : undef;
$self->{sampling_period} = (defined($self->{option_results}->{sampling_period})) ? shift(@{$self->{option_results}->{sampling_period}}) : undef;
$self->{time_shift} = (defined($self->{option_results}->{sampling_period})) ? shift(@{$self->{option_results}->{time_shift}}) : 0;
$self->{unknown_connector_status} = (defined($self->{option_results}->{unknown_connector_status})) ? $self->{option_results}->{unknown_connector_status} : '%{code} < 0 || (%{code} > 0 && %{code} < 200)';
$self->{warning_connector_status} = (defined($self->{option_results}->{warning_connector_status})) ? $self->{option_results}->{warning_connector_status} : '';
$self->{critical_connector_status} = (defined($self->{option_results}->{critical_connector_status})) ? $self->{option_results}->{critical_connector_status} : '';
$self->{case_insensitive} = (defined($self->{option_results}->{case_insensitive})) ? $self->{option_results}->{case_insensitive} : undef;

$self->{connector_port} = 5700 if ($self->{connector_port} eq '');
$self->{container} = 'default' if ($self->{container} eq '');
Expand All @@ -130,12 +139,6 @@ sub check_options {
return 1;
}

sub set_discovery {
my ($self, %options) = @_;

$self->{discovery} = 1;
}

sub add_params {
my ($self, %options) = @_;

Expand All @@ -159,38 +162,102 @@ sub connector_response {
$self->{output}->option_exit();
}

my $json = $1;
my $result;

my $json = $1;
eval {
$result = JSON->new->utf8->decode($json);
$self->{result} = JSON->new->utf8->decode($json);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json result: $@");
$self->{output}->option_exit();
}
}

sub connector_response_status {
my ($self, %options) = @_;

if (!defined($self->{result})) {
$self->{output}->add_option_msg(short_msg => "Cannot get response (timeout received)");
$self->{output}->option_exit();
}
if (!defined($self->{result}->{code})) {
$self->{output}->add_option_msg(short_msg => "response format incorrect");
$self->{output}->option_exit();
}

foreach my $output (@{$result->{plugin}->{outputs}}) {
if ($output->{type} == 1) {
$self->{output}->output_add(severity => $output->{exit},
short_msg => $output->{msg});
} elsif ($output->{type} == 2) {
$self->{output}->output_add(long_msg => $output->{msg});
foreach (('unknown_connector_status', 'warning_connector_status', 'critical_connector_status')) {
$self->{$_} =~ s/%\{(.*?)\}/\$self->{result}->{$1}/g;
}

# Check response
my $status = 'ok';
my $message;
eval {
local $SIG{__WARN__} = sub { $message = $_[0]; };
local $SIG{__DIE__} = sub { $message = $_[0]; };

if (defined($self->{critical_connector_status}) && $self->{critical_connector_status} ne '' &&
eval "$self->{critical_connector_status}") {
$status = 'critical';
} elsif (defined($self->{warning_connector_status}) && $self->{warning_connector_status} ne '' &&
eval "$self->{warning_connector_status}") {
$status = 'warning';
} elsif (defined($self->{unknown_connector_status}) && $self->{unknown_connector_status} ne '' &&
eval "$self->{unknown_connector_status}") {
$status = 'unknown';
}
};
if (defined($message)) {
$self->{output}->add_option_msg(short_msg => 'filter connector status issue: ' . $message);
$self->{output}->option_exit();
}

if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) {
$self->{output}->output_add(long_msg => $self->{result}->{extra_message}, debug => 1);
$self->{output}->output_add(severity => $status,
short_msg => $self->{result}->{short_message});
$self->{output}->display();
$self->{output}->exit();
}
}

sub entity_is_connected {
my ($self, %options) = @_;

if ($options{state} !~ /^connected$/i) {
return 0;
}
return 1;
}

sub vm_is_running {
my ($self, %options) = @_;

foreach my $perf (@{$result->{plugin}->{perfdatas}}) {
$self->{output}->perfdata_add(label => $perf->{label}, unit => $perf->{unit},
value => $perf->{value},
warning => $perf->{warning},
critical => $perf->{critical},
min => $perf->{min}, max => $perf->{max});
if ($options{power} !~ /^poweredOn$/i) {
return 0;
}
return 1;
}

sub run {
sub get_id {
my ($self, %options) = @_;

return $self->{connector_hostname} . '.' . $self->{connector_port} . '.' . $self->{container};
}

sub strip_cr {
my ($self, %options) = @_;

$options{value} =~ s/^\s+.*\s+$//mg;
$options{value} =~ s/\r//mg;
$options{value} =~ s/\n/ -- /mg;
return $options{value};
}

sub execute {
my ($self, %options) = @_;

$self->add_params(%options);

# Build request
my $uuid;
UUID::generate($uuid);
Expand Down Expand Up @@ -227,27 +294,16 @@ sub run {
my $response = zmq_recvmsg($self->{requester});
zmq_close($self->{requester});
$self->connector_response(response => $response);
# We need to remove xml output
if (defined($self->{output}->{option_results}->{output_xml})) {
delete $self->{output}->{option_results}->{output_xml};
}
if (!defined($self->{discovery})) {
$self->{output}->display();
} else {
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
}
$self->{output}->exit();
},
},
);

zmq_poll(\@poll, $self->{timeout} * 1000);
zmq_close($self->{requester});

$self->{output}->output_add(severity => 'UNKNOWN',
short_msg => sprintf("Cannot get response (timeout received)"));
$self->{output}->display();
$self->{output}->exit();
$self->connector_response_status();

return $self->{result};
}

1;
Expand Down Expand Up @@ -303,6 +359,25 @@ Should be not different than 300 or 20.
Can shift the time. We the following option you can average X counters values (default: 0).
=item B<--case-insensitive>
Searchs are case insensitive.
=item B<--unknown-connector-status>
Set unknown threshold for connector status (Default: '%{code} < 0 || (%{code} > 0 && %{code} < 200)').
Can used special variables like: %{code}, %{short_message}, %{extra_message}.
=item B<--warning-connector-status>
Set warning threshold for connector status (Default: '').
Can used special variables like: %{code}, %{short_message}, %{extra_message}.
=item B<--critical-connector-status>
Set critical threshold for connector status (Default: '').
Can used special variables like: %{code}, %{short_message}, %{extra_message}.
=back
=head1 DESCRIPTION
Expand Down
Loading

0 comments on commit 626da32

Please sign in to comment.