(plugin) hardware::server::hp::ilo::xmlapi - add option --force-ilo2 option (#3527)

This commit is contained in:
qgarnier 2022-03-08 10:39:11 +01:00 committed by GitHub
parent b0b7838cd8
commit 4fd85b4eae
1 changed files with 43 additions and 27 deletions

View File

@ -39,7 +39,7 @@ sub new {
$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' },
@ -47,14 +47,18 @@ sub new {
'port:s' => { name => 'port', default => 443 },
'username:s' => { name => 'username' },
'password:s' => { name => 'password' },
'force-ilo3' => { name => 'force_ilo3' }
'force-ilo3' => { name => 'force_ilo3' },
'force-ilo2' => { name => 'force_ilo2' }
});
}
$options{options}->add_help(package => __PACKAGE__, sections => 'XML API OPTIONS', once => 1);
$self->{output} = $options{output};
$self->{http} = centreon::plugins::http->new(%options);
$self->{ilo2} = 0;
$self->{ilo3} = 0;
return $self;
}
@ -81,7 +85,7 @@ sub check_options {
$self->{output}->add_option_msg(short_msg => "Need to set password option.");
$self->{output}->option_exit();
}
$self->{ssl_opts} = '';
if (!defined($self->{option_results}->{ssl_opt})) {
$self->{option_results}->{ssl_opt} = ['SSL_verify_mode => SSL_VERIFY_NONE'];
@ -101,15 +105,16 @@ sub check_options {
sub find_ilo_version {
my ($self, %options) = @_;
($self->{ilo2}, $self->{ilo3}) = (0, 0);
my $client = new IO::Socket::SSL->new(PeerAddr => $self->{option_results}->{hostname} . ':' . $self->{option_results}->{port},
eval $self->{ssl_opts}, Timeout => $self->{option_results}->{timeout});
my $client = new IO::Socket::SSL->new(
PeerAddr => $self->{option_results}->{hostname} . ':' . $self->{option_results}->{port},
eval $self->{ssl_opts}, Timeout => $self->{option_results}->{timeout}
);
if (!$client) {
$self->{output}->add_option_msg(short_msg => "Failed to establish SSL connection: $!, ssl_error=$SSL_ERROR");
$self->{output}->option_exit();
}
print $client 'POST /ribcl HTTP/1.1' . "\r\n";
print $client "HOST: me" . "\r\n"; # Mandatory for http 1.1
print $client "User-Agent: locfg-Perl-script/3.0\r\n";
@ -129,8 +134,10 @@ sub find_ilo_version {
sub get_ilo2_data {
my ($self, %options) = @_;
my $client = new IO::Socket::SSL->new(PeerAddr => $self->{option_results}->{hostname} . ':' . $self->{option_results}->{port},
eval $self->{ssl_opts}, Timeout => $self->{option_results}->{timeout});
my $client = new IO::Socket::SSL->new(
PeerAddr => $self->{option_results}->{hostname} . ':' . $self->{option_results}->{port},
eval $self->{ssl_opts}, Timeout => $self->{option_results}->{timeout}
);
if (!$client) {
$self->{output}->add_option_msg(short_msg => "Failed to establish SSL connection: $!, ssl_error=$SSL_ERROR");
$self->{output}->option_exit();
@ -165,7 +172,7 @@ sub get_ilo3_data {
$self->{http}->add_header(key => 'TE', value => 'chunked');
$self->{http}->add_header(key => 'Connection', value => 'Close');
$self->{http}->add_header(key => 'Content-Type', value => 'text/xml');
$self->{content} = $self->{http}->request(
method => 'POST', proto => 'https', url_path => '/ribcl',
query_form_post => $xml_script,
@ -174,7 +181,7 @@ sub get_ilo3_data {
sub check_ilo_error {
my ($self, %options) = @_;
# Looking for:
# <RESPONSE
# STATUS="0x005F"
@ -261,7 +268,7 @@ sub change_shitty_xml {
sub get_ilo_response {
my ($self, %options) = @_;
# ilo result is so shitty. We get the good result from size...
my ($length, $response) = (0, '');
foreach (split /<\?xml.*?\?>/, $self->{content}) {
@ -270,14 +277,18 @@ sub get_ilo_response {
$length = length($_);
}
}
$response = $self->change_shitty_xml(response => $response);
my $xml_result;
eval {
$xml_result = XMLin($response,
ForceArray => ['FAN', 'TEMP', 'MODULE', 'SUPPLY', 'PROCESSOR', 'NIC',
'SMART_STORAGE_BATTERY', 'CONTROLLER', 'DRIVE_ENCLOSURE',
'LOGICAL_DRIVE', 'PHYSICAL_DRIVE', 'DRIVE_BAY', 'BACKPLANE']);
$xml_result = XMLin(
$response,
ForceArray => [
'FAN', 'TEMP', 'MODULE', 'SUPPLY', 'PROCESSOR', 'NIC',
'SMART_STORAGE_BATTERY', 'CONTROLLER', 'DRIVE_ENCLOSURE',
'LOGICAL_DRIVE', 'PHYSICAL_DRIVE', 'DRIVE_BAY', 'BACKPLANE'
]
);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode xml response: $@");
@ -289,24 +300,25 @@ sub get_ilo_response {
sub get_ilo_data {
my ($self, %options) = @_;
$self->{content} = '';
if (!defined($self->{option_results}->{force_ilo3})) {
if (!defined($self->{option_results}->{force_ilo3}) && !defined($self->{option_results}->{force_ilo2})) {
$self->find_ilo_version();
} else {
$self->{ilo3} = 1;
$self->{ilo3} = 1 if (defined($self->{option_results}->{force_ilo3}));
$self->{ilo2} = 1 if (defined($self->{option_results}->{force_ilo2}));
}
if ($self->{ilo3} == 1) {
$self->get_ilo3_data();
} else {
$self->get_ilo2_data();
}
$self->{content} =~ s/\r//sg;
$self->{output}->output_add(long_msg => $self->{content}, debug => 1);
$self->check_ilo_error();
return $self->get_ilo_response();
}
@ -349,7 +361,11 @@ Set timeout (Default: 30).
=item B<--force-ilo3>
Don't try to find ILO version.
No version identification. Directly check for version 3.
=item B<--force-ilo2>
No version identification. Directly check for version 2.
=back