From 3cd433a4e9b10fd3f03f4e71fd0c4567da3d3fdd Mon Sep 17 00:00:00 2001 From: Quentin Garnier Date: Mon, 10 Feb 2014 17:21:14 +0100 Subject: [PATCH] Refs #5187 --- centreon/plugins/wsman.pm | 60 +++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/centreon/plugins/wsman.pm b/centreon/plugins/wsman.pm index cc61e9b3a..a04d60aa0 100644 --- a/centreon/plugins/wsman.pm +++ b/centreon/plugins/wsman.pm @@ -143,43 +143,21 @@ sub request { ###### # ClientOptions object - my $client_options = new openwsman::ClientOptions::(); - if (!defined($client_options)) { - if ($dont_quit == 1) { - $self->set_error(error_status => -1, error_msg => 'Could not create client options handler'); - return undef; - } - $self->{output}->add_option_msg(short_msg => 'Could not create client options handler'); - $self->{output}->option_exit(exit_litteral => $self->{wsman_errors_exit}); - } + my $client_options = new openwsman::ClientOptions::() + or $self->internal_exit(msg => 'Could not create client options handler'); + # Optimization $client_options->set_flags($openwsman::FLAG_ENUMERATION_OPTIMIZATION); $client_options->set_max_elements(999); ###### # Filter/Enumerate - my $filter = new openwsman::Filter::(); - if (!defined($filter)) { - if ($dont_quit == 1) { - $self->set_error(error_status => -1, error_msg => 'Could not create filter'); - return undef; - } - $self->{output}->add_option_msg(short_msg => 'Could not create filter.'); - $self->{output}->option_exit(exit_litteral => $self->{wsman_errors_exit}); - } + my $filter = new openwsman::Filter::() + or $self->internal_exit(msg => 'Could not create filter'); $filter->wql($options{wql_filter}); my $result = $self->{client}->enumerate($client_options, $filter, $options{uri}); - unless($result && $result->is_fault eq 0) { - my $fault_string = $self->{client}->fault_string(); - my $msg = 'Could not enumerate instances: ' . ((defined($fault_string)) ? $fault_string : 'use debug option to have details'); - if ($dont_quit == 1) { - $self->set_error(error_status => -1, error_msg => $msg ); - return undef; - } - $self->{output}->add_option_msg(short_msg => $msg); - $self->{output}->option_exit(exit_litteral => $self->{wsman_errors_exit}); - } + return undef if ($self->handle_dialog_fault(result => $result, msg => 'Could not enumerate instances: ', dont_quit => $dont_quit)); ###### # Fetch values @@ -270,6 +248,32 @@ sub check_options { $self->{wsman_params}->{wsman_debug} = $options{option_results}->{wsman_debug}; } +sub handle_dialog_fault { + my ($self, %options) = @_; + my $result = $options{result}; + my $msg = $options{msg}; + + unless($result && $result->is_fault eq 0) { + my $fault_string = $self->{client}->fault_string(); + my $msg = 'Could not enumerate instances: ' . ((defined($fault_string)) ? $fault_string : 'use debug option to have details'); + if ($options{dont_quit} == 1) { + $self->set_error(error_status => -1, error_msg => $msg); + return 1; + } + $self->{output}->add_option_msg(short_msg => $msg); + $self->{output}->option_exit(exit_litteral => $self->{wsman_errors_exit}); + } + + return 0; +} + +sub internal_exit { + my ($self, %options) = @_; + + $self->{output}->add_option_msg(short_msg => $options{msg}); + $self->{output}->option_exit(exit_litteral => $self->{wsman_errors_exit}); +} + sub set_error { my ($self, %options) = @_; # $options{error_msg} = string error