From 83924b35c66c336a0a17df019e238665e5d9b838 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Thu, 26 Dec 2019 16:43:28 +0100 Subject: [PATCH] Fix #1519: initial release --- .../restapi/mode/components/chassis.pm | 66 +++++ .../redfish/restapi/mode/components/device.pm | 73 +++++ .../redfish/restapi/mode/components/fan.pm | 101 +++++++ .../redfish/restapi/mode/components/psu.pm | 96 ++++++ .../restapi/mode/components/temperature.pm | 102 +++++++ .../common/redfish/restapi/mode/hardware.pm | 156 ++++++++++ hardware/server/hp/ilo/restapi/custom/api.pm | 273 ++++++++++++++++++ hardware/server/hp/ilo/restapi/plugin.pm | 49 ++++ 8 files changed, 916 insertions(+) create mode 100644 centreon/common/redfish/restapi/mode/components/chassis.pm create mode 100644 centreon/common/redfish/restapi/mode/components/device.pm create mode 100644 centreon/common/redfish/restapi/mode/components/fan.pm create mode 100644 centreon/common/redfish/restapi/mode/components/psu.pm create mode 100644 centreon/common/redfish/restapi/mode/components/temperature.pm create mode 100644 centreon/common/redfish/restapi/mode/hardware.pm create mode 100644 hardware/server/hp/ilo/restapi/custom/api.pm create mode 100644 hardware/server/hp/ilo/restapi/plugin.pm diff --git a/centreon/common/redfish/restapi/mode/components/chassis.pm b/centreon/common/redfish/restapi/mode/components/chassis.pm new file mode 100644 index 000000000..321972598 --- /dev/null +++ b/centreon/common/redfish/restapi/mode/components/chassis.pm @@ -0,0 +1,66 @@ +# +# 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 centreon::common::redfish::restapi::mode::components::chassis; + +use strict; +use warnings; + +sub check { + my ($self) = @_; + + $self->{output}->output_add(long_msg => 'checking chassis'); + $self->{components}->{chassis} = { name => 'chassis', total => 0, skip => 0 }; + return if ($self->check_filter(section => 'chassis')); + + $self->get_chassis() if (!defined($self->{chassis})); + + foreach my $chassis (@{$self->{chassis}}) { + my $instance = $chassis->{Id}; + + next if ($self->check_filter(section => 'chassis', instance => $instance)); + $self->{components}->{chassis}->{total}++; + + $self->{output}->output_add( + long_msg => sprintf( + "chassis '%s' status is '%s' [instance: %s, state: %s]", + $instance, $chassis->{Status}->{Health}, $instance, $chassis->{Status}->{State} + ) + ); + + my $exit = $self->get_severity(label => 'state', section => 'chassis.state', value => $chassis->{Status}->{State}); + if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { + $self->{output}->output_add( + severity => $exit, + short_msg => sprintf("Chassis '%s' state is '%s'", $instance, $chassis->{Status}->{State}) + ); + } + + $exit = $self->get_severity(label => 'status', section => 'chassis.status', value => $chassis->{Status}->{Health}); + if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { + $self->{output}->output_add( + severity => $exit, + short_msg => sprintf("Chassis '%s' status is '%s'", $instance, $chassis->{Status}->{Health}) + ); + } + } +} + +1; diff --git a/centreon/common/redfish/restapi/mode/components/device.pm b/centreon/common/redfish/restapi/mode/components/device.pm new file mode 100644 index 000000000..c6ed407c4 --- /dev/null +++ b/centreon/common/redfish/restapi/mode/components/device.pm @@ -0,0 +1,73 @@ +# +# 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 centreon::common::redfish::restapi::mode::components::device; + +use strict; +use warnings; + +sub check { + my ($self) = @_; + + $self->{output}->output_add(long_msg => 'checking devices'); + $self->{components}->{device} = { name => 'devices', total => 0, skip => 0 }; + return if ($self->check_filter(section => 'device')); + + $self->get_devices(); + + foreach my $chassis (@{$self->{chassis}}) { + my $chassis_name = 'chassis:' . $chassis->{Id}; + + next if (!defined($chassis->{Devices})); + + foreach my $device (@{$chassis->{Devices}}) { + my $device_name = $device->{Name}; + my $instance = $chassis->{Id} . '.' . $device->{Id}; + + next if ($self->check_filter(section => 'device', instance => $instance)); + $self->{components}->{device}->{total}++; + + $self->{output}->output_add( + long_msg => sprintf( + "device '%s/%s' status is '%s' [instance: %s, state: %s]", + $chassis_name, $device_name, $device->{Status}->{Health}, $instance, $device->{Status}->{State} + ) + ); + + my $exit = $self->get_severity(label => 'state', section => 'device.state', value => $device->{Status}->{State}); + if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { + $self->{output}->output_add( + severity => $exit, + short_msg => sprintf("Device '%s/%s' state is '%s'", $chassis_name, $device_name, $device->{Status}->{State}) + ); + } + + $exit = $self->get_severity(label => 'status', section => 'device.status', value => $device->{Status}->{Health}); + if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { + $self->{output}->output_add( + severity => $exit, + short_msg => sprintf("Device '%s/%s' status is '%s'", $chassis_name, $device_name, $device->{Status}->{Health}) + ); + } + } + } +} + +1; diff --git a/centreon/common/redfish/restapi/mode/components/fan.pm b/centreon/common/redfish/restapi/mode/components/fan.pm new file mode 100644 index 000000000..644631168 --- /dev/null +++ b/centreon/common/redfish/restapi/mode/components/fan.pm @@ -0,0 +1,101 @@ +# +# 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 centreon::common::redfish::restapi::mode::components::fan; + +use strict; +use warnings; + +my $reading_units = { + percent => { short => '%', long => 'percentage' }, + rpm => { short => 'rpm', long => 'rpm' } +}; + +sub check { + my ($self) = @_; + + $self->{output}->output_add(long_msg => 'checking fans'); + $self->{components}->{chassis} = { name => 'fans', total => 0, skip => 0 }; + return if ($self->check_filter(section => 'fan')); + + $self->get_chassis() if (!defined($self->{chassis})); + + foreach my $chassis (@{$self->{chassis}}) { + my $chassis_name = 'chassis:' . $chassis->{Id}; + + $chassis->{Thermal}->{result} = $self->get_thermal(chassis => $chassis) if (!defined($chassis->{Thermal}->{result})); + next if (!defined($chassis->{Thermal}->{result}->{Fans})); + + foreach my $fan (@{$chassis->{Thermal}->{result}->{Fans}}) { + my $fan_name = $fan->{Name}; + my $instance = $chassis->{Id} . '.' . $fan->{MemberId}; + + next if ($self->check_filter(section => 'fan', instance => $instance)); + $self->{components}->{fan}->{total}++; + + $self->{output}->output_add( + long_msg => sprintf( + "fan '%s/%s' status is '%s' [instance: %s, state: %s, speed: %s %s]", + $chassis_name, $fan_name, $fan->{Status}->{Health}, $instance, $fan->{Status}->{State}, + $fan->{Reading}, $fan->{ReadingUnits} + ) + ); + + my $exit = $self->get_severity(label => 'state', section => 'fan.state', value => $fan->{Status}->{State}); + if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { + $self->{output}->output_add( + severity => $exit, + short_msg => sprintf("Fan '%s/%s' state is '%s'", $chassis_name, $fan_name, $fan->{Status}->{State}) + ); + } + + $exit = $self->get_severity(label => 'status', section => 'fan.status', value => $fan->{Status}->{Health}); + if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { + $self->{output}->output_add( + severity => $exit, + short_msg => sprintf("Fan '%s/%s' status is '%s'", $chassis_name, $fan_name, $fan->{Status}->{Health}) + ); + } + + my ($exit2, $warn, $crit, $checked) = $self->get_severity_numeric(section => 'fan', instance => $instance, value => $fan->{Reading}); + if (!$self->{output}->is_status(value => $exit2, compare => 'ok', litteral => 1)) { + $self->{output}->output_add( + severity => $exit2, + short_msg => sprintf( + "Fan '%s/%s' speed is %s %s", + $chassis_name, $fan_name, $fan->{Reading}, $reading_units->{lc($fan->{ReadingUnits})}->{short} + ) + ); + } + $self->{output}->perfdata_add( + unit => $reading_units->{lc($fan->{ReadingUnits})}->{short}, + nlabel => 'hardware.fan.speed.' . $reading_units->{lc($fan->{ReadingUnits})}->{long}, + instances => [$chassis_name, $fan_name], + value => $fan->{Reading}, + warning => $warn, + critical => $crit, + min => 0, + max => $fan->{ReadingUnits} eq 'Percent' ? 100 : undef + ); + } + } +} + +1; diff --git a/centreon/common/redfish/restapi/mode/components/psu.pm b/centreon/common/redfish/restapi/mode/components/psu.pm new file mode 100644 index 000000000..8f3bed657 --- /dev/null +++ b/centreon/common/redfish/restapi/mode/components/psu.pm @@ -0,0 +1,96 @@ +# +# 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 centreon::common::redfish::restapi::mode::components::psu; + +use strict; +use warnings; + +sub check { + my ($self) = @_; + + $self->{output}->output_add(long_msg => 'checking power supplies'); + $self->{components}->{psu} = { name => 'psus', total => 0, skip => 0 }; + return if ($self->check_filter(section => 'psu')); + + $self->get_chassis() if (!defined($self->{chassis})); + + foreach my $chassis (@{$self->{chassis}}) { + my $chassis_name = 'chassis:' . $chassis->{Id}; + + $chassis->{Power}->{result} = $self->get_power(chassis => $chassis) if (!defined($chassis->{Power}->{result})); + next if (!defined($chassis->{Power}->{result}->{PowerSupplies})); + + foreach my $psu (@{$chassis->{Thermal}->{result}->{PowerSupplies}}) { + my $psu_name = 'psu:' . $psu->{MemberId}; + my $instance = $chassis->{Id} . '.' . $psu->{MemberId}; + + next if ($self->check_filter(section => 'psu', instance => $instance)); + $self->{components}->{psu}->{total}++; + + $self->{output}->output_add( + long_msg => sprintf( + "power supply '%s/%s' status is '%s' [instance: %s, state: %s, value: %s]", + $chassis_name, $psu_name, $psu->{Status}->{Health}, $instance, $psu->{Status}->{State}, + $psu->{LastPowerOutputWatts}, + ) + ); + + my $exit = $self->get_severity(label => 'state', section => 'psu.state', value => $psu->{Status}->{State}); + if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { + $self->{output}->output_add( + severity => $exit, + short_msg => sprintf("Power supply '%s/%s' state is '%s'", $chassis_name, $psu_name, $psu->{Status}->{State}) + ); + } + + $exit = $self->get_severity(label => 'status', section => 'psu.status', value => $psu->{Status}->{Health}); + if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { + $self->{output}->output_add( + severity => $exit, + short_msg => sprintf("Power supply '%s/%s' status is '%s'", $chassis_name, $psu_name, $psu->{Status}->{Health}) + ); + } + + my ($exit2, $warn, $crit, $checked) = $self->get_severity_numeric(section => 'psu', instance => $instance, value => $psu->{LastPowerOutputWatts}); + + if (!$self->{output}->is_status(value => $exit2, compare => 'ok', litteral => 1)) { + $self->{output}->output_add( + severity => $exit2, + short_msg => sprintf( + "Power supply '%s/%s' power is %s W", + $chassis_name, $psu_name, $psu->{LastPowerOutputWatts} + ) + ); + } + $self->{output}->perfdata_add( + unit => 'W', + nlabel => 'hardware.powersupply.power.watt', + instances => [$chassis_name, $psu_name], + value => $psu->{LastPowerOutputWatts}, + warning => $warn, + critical => $crit, + min => 0 + ); + } + } +} + +1; diff --git a/centreon/common/redfish/restapi/mode/components/temperature.pm b/centreon/common/redfish/restapi/mode/components/temperature.pm new file mode 100644 index 000000000..ef7e7ea72 --- /dev/null +++ b/centreon/common/redfish/restapi/mode/components/temperature.pm @@ -0,0 +1,102 @@ +# +# 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 centreon::common::redfish::restapi::mode::components::temperature; + +use strict; +use warnings; + +sub check { + my ($self) = @_; + + $self->{output}->output_add(long_msg => 'checking temperatures'); + $self->{components}->{temperature} = { name => 'temperatures', total => 0, skip => 0 }; + return if ($self->check_filter(section => 'temperature')); + + $self->get_chassis() if (!defined($self->{chassis})); + + foreach my $chassis (@{$self->{chassis}}) { + my $chassis_name = 'chassis:' . $chassis->{Id}; + + $chassis->{Thermal}->{result} = $self->get_thermal(chassis => $chassis) if (!defined($chassis->{Thermal}->{result})); + next if (!defined($chassis->{Thermal}->{result}->{Temperatures})); + + foreach my $temp (@{$chassis->{Thermal}->{result}->{Temperatures}}) { + my $temp_name = $temp->{Name}; + my $instance = $chassis->{Id} . '.' . $temp->{MemberId}; + + next if ($self->check_filter(section => 'temperature', instance => $instance)); + $self->{components}->{temperature}->{total}++; + + $self->{output}->output_add( + long_msg => sprintf( + "temperature '%s/%s' status is '%s' [instance: %s, state: %s, value: %s]", + $chassis_name, $temp_name, $temp->{Status}->{Health}, $instance, $temp->{Status}->{State}, + $temp->{ReadingCelsius}, + ) + ); + + my $exit = $self->get_severity(label => 'state', section => 'temperature.state', value => $temp->{Status}->{State}); + if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { + $self->{output}->output_add( + severity => $exit, + short_msg => sprintf("Temperature '%s/%s' state is '%s'", $chassis_name, $temp_name, $temp->{Status}->{State}) + ); + } + + $exit = $self->get_severity(label => 'status', section => 'temperature.status', value => $temp->{Status}->{Health}); + if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { + $self->{output}->output_add( + severity => $exit, + short_msg => sprintf("Temperature '%s/%s' status is '%s'", $chassis_name, $temp_name, $temp->{Status}->{Health}) + ); + } + + my ($exit2, $warn, $crit, $checked) = $self->get_severity_numeric(section => 'temperature', instance => $instance, value => $temp->{ReadingCelsius}); + if ($checked == 0) { + my $warn_th = ':' . $temp->{UpperThresholdCritical}; + my $crit_th = ':' . $temp->{UpperThresholdFatal}; + $self->{perfdata}->threshold_validate(label => 'warning-temperature-instance-' . $instance, value => $warn_th); + $self->{perfdata}->threshold_validate(label => 'critical-temperature-instance-' . $instance, value => $crit_th); + $crit = $self->{perfdata}->get_perfdata_for_output(label => 'critical-temperature-instance-' . $instance); + } + + if (!$self->{output}->is_status(value => $exit2, compare => 'ok', litteral => 1)) { + $self->{output}->output_add( + severity => $exit2, + short_msg => sprintf( + "Temperature '%s/%s' is %s C", + $chassis_name, $temp_name, $temp->{ReadingCelsius} + ) + ); + } + $self->{output}->perfdata_add( + unit => 'C', + nlabel => 'hardware.temperature.celsius', + instances => [$chassis_name, $temp_name], + value => $temp->{ReadingCelsius}, + warning => $warn, + critical => $crit, + ); + } + } +} + +1; diff --git a/centreon/common/redfish/restapi/mode/hardware.pm b/centreon/common/redfish/restapi/mode/hardware.pm new file mode 100644 index 000000000..86d6b4aea --- /dev/null +++ b/centreon/common/redfish/restapi/mode/hardware.pm @@ -0,0 +1,156 @@ +# +# 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 centreon::common::redfish::restapi::mode::hardware; + +use base qw(centreon::plugins::templates::hardware); + +use strict; +use warnings; + +sub set_system { + my ($self, %options) = @_; + + $self->{regexp_threshold_overload_check_section_option} = '^(?:chassis|fan|temperature|psu|device)$'; + $self->{regexp_threshold_numeric_check_section_option} = '^(?:fan|temperature|psu)$'; + + $self->{cb_hook2} = 'execute_custom'; + + $self->{thresholds} = { + status => [ + ['ok', 'OK'], + ['warning', 'WARNING'], + ['critical', 'CRITICAL'], + ], + state => [ + # can be: absent, deferring, disabled, enabled, + # inTest, quiesced, standbyOffline, standbySpare + # starting, unavailableOffline, updating + ['updating', 'WARNING'], + ['.*', 'OK'], + ], + }; + + $self->{components_exec_load} = 0; + + $self->{components_path} = 'centreon::common::redfish::restapi::mode::components'; + $self->{components_module} = ['chassis', 'device', 'fan', 'psu', 'temperature']; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, no_absent => 1, force_new_perfdata => 1); + bless $self, $class; + + $options{options}->add_options(arguments => {}); + + return $self; +} + +sub get_power { + my ($self, %options) = @_; + + return {} if (!defined($options{chassis}->{Power}->{'@odata.id'})); + return $self->{custom}->request_api(url_path => $options{chassis}->{Power}->{'@odata.id'}); +} + +sub get_thermal { + my ($self, %options) = @_; + + return {} if (!defined($options{chassis}->{Thermal}->{'@odata.id'})); + return $self->{custom}->request_api(url_path => $options{chassis}->{Thermal}->{'@odata.id'}); +} + +sub get_devices { + my ($self, %options) = @_; + + $self->get_chassis() if (!defined($self->{chassis})); + foreach my $chassis (@{$self->{chassis}}) { + $chassis->{Devices} = []; + my $result = $self->{custom}->request_api(url_path => $chassis->{'@odata.id'} . 'Devices'); + foreach (@{$result->{Members}}) { + my $device_detailed = $self->{custom}->request_api(url_path => $_->{'@odata.id'}); + push @{$chassis->{Devices}}, $device_detailed; + } + } +} + +sub get_chassis { + my ($self, %options) = @_; + + $self->{chassis} = []; + # "Members":[ + # { "@odata.id":"/redfish/v1/Chassis/1/" } + # ], + my $result = $self->{custom}->request_api(url_path => '/redfish/v1/chassis/'); + foreach (@{$result->{Members}}) { + my $chassis_detailed = $self->{custom}->request_api(url_path => $_->{'@odata.id'}); + push @{$self->{chassis}}, $chassis_detailed; + } +} + +sub execute_custom { + my ($self, %options) = @_; + + $self->{custom} = $options{custom}; +} + +1; + +=head1 MODE + +Check hardware. + +=over 8 + +=item B<--component> + +Which component to check (Default: '.*'). +Can be: 'chassis', 'device', 'fan', 'psu', 'temperature'. + +=item B<--filter> + +Exclude some parts (comma seperated list) +Can also exclude specific instance: --filter='fan,1.2' + +=item B<--no-component> + +Return an error if no compenents are checked. +If total (with skipped) is 0. (Default: 'critical' returns). + +=item B<--threshold-overload> + +Set to overload default threshold values (syntax: section,[instance,]status,regexp) +It used before default thresholds (order stays). +Example: --threshold-overload='chassis.state,WARNING,inTest' + +=item B<--warning> + +Set warning threshold for 'temperature', 'fan', 'psu' (syntax: type,regexp,threshold) +Example: --warning='temperature,.*,30' + +=item B<--critical> + +Set critical threshold for 'temperature', 'fan', 'psu' (syntax: type,regexp,threshold) +Example: --critical='temperature,.*,50' + +=back + +=cut diff --git a/hardware/server/hp/ilo/restapi/custom/api.pm b/hardware/server/hp/ilo/restapi/custom/api.pm new file mode 100644 index 000000000..bc5f55551 --- /dev/null +++ b/hardware/server/hp/ilo/restapi/custom/api.pm @@ -0,0 +1,273 @@ +# +# 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 hardware::server::hp::ilo::restapi::custom::api; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::http; +use centreon::plugins::statefile; +use JSON::XS; +use Digest::MD5 qw(md5_hex); + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + 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' }, + 'port:s' => { name => 'port'}, + 'proto:s' => { name => 'proto' }, + 'api-username:s' => { name => 'api_username' }, + 'api-password:s' => { name => 'api_password' }, + 'timeout:s' => { name => 'timeout', default => 30 }, + }); + } + + $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); + + $self->{output} = $options{output}; + $self->{mode} = $options{mode}; + $self->{http} = centreon::plugins::http->new(%options); + $self->{cache} = centreon::plugins::statefile->new(%options); + + 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} : undef; + $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->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 30; + $self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : undef; + $self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : undef; + + if (!defined($self->{hostname}) || $self->{hostname} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --hostname option."); + $self->{output}->option_exit(); + } + if (!defined($self->{api_username}) || $self->{api_username} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --api-username option."); + $self->{output}->option_exit(); + } + if (!defined($self->{api_password}) || $self->{api_password} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --api-password option."); + $self->{output}->option_exit(); + } + + $self->{cache}->check_options(option_results => $self->{option_results}); + + return 0; +} + +sub build_options_for_httplib { + my ($self, %options) = @_; + + $self->{option_results}->{hostname} = $self->{hostname}; + $self->{option_results}->{port} = $self->{port}; + $self->{option_results}->{proto} = $self->{proto}; + $self->{option_results}->{timeout} = $self->{timeout}; +} + +sub settings { + my ($self, %options) = @_; + + $self->build_options_for_httplib(); + $self->{http}->add_header(key => 'Content-Type', value => 'application/json;charset=UTF-8'); + $self->{http}->add_header(key => 'Accept', value => 'application/json;charset=UTF-8'); + $self->{http}->set_options(%{$self->{option_results}}); +} + +sub json_decode { + my ($self, %options) = @_; + + my $decoded; + eval { + $decoded = JSON::XS->new->utf8->decode($options{content}); + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); + $self->{output}->option_exit(); + } + + return $decoded; +} + +sub clean_token { + my ($self, %options) = @_; + + my $datas = { last_timestamp => time() }; + $options{statefile}->write(data => $datas); + $self->{token} = undef; + $self->{http}->add_header(key => 'X-Auth-Token', value => undef); +} + +sub authenticate { + my ($self, %options) = @_; + + my $has_cache_file = $options{statefile}->read(statefile => 'ilo_redfish_' . md5_hex($self->{option_results}->{hostname}) . '_' . md5_hex($self->{option_results}->{api_username})); + my $token = $options{statefile}->get(name => 'token'); + + if ($has_cache_file == 0 || !defined($token)) { + my $json_request = { UserName => $self->{api_username}, Password => $self->{api_password} }; + my $encoded; + eval { + $encoded = encode_json($json_request); + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot encode json request"); + $self->{output}->option_exit(); + } + + my $content = $self->{http}->request( + method => 'POST', + url_path => '/redfish/v1/SessionService/Sessions/', + query_form_post => $encoded, + warning_status => '', unknown_status => '', critical_status => '' + ); + if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) { + $self->{output}->add_option_msg(short_msg => "Login error [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']"); + $self->{output}->option_exit(); + } + + my $token = $self->{http}->get_header(name => 'X-Auth-Token'); + if (!defined($token)) { + $self->{output}->add_option_msg(short_msg => "Error retrieving token"); + $self->{output}->option_exit(); + } + + my $datas = { last_timestamp => time(), token => $token }; + $options{statefile}->write(data => $datas); + } + + $self->{token} = $token; + $self->{http}->add_header(key => 'X-Auth-Token', value => $self->{token}); +} + +sub request_api { + my ($self, %options) = @_; + + $self->settings(); + if (!defined($self->{token})) { + $self->authenticate(statefile => $self->{cache}); + } + + my $content = $self->{http}->request(%options, + warning_status => '', unknown_status => '', critical_status => '' + ); + + # Maybe there is an issue with the token. So we retry. + if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) { + $self->clean_token(statefile => $self->{cache}); + $self->authenticate(statefile => $self->{cache}); + $content = $self->{http}->request(%options, + warning_status => '', unknown_status => '', critical_status => '' + ); + } + + my $decoded = $self->json_decode(content => $content); + if (!defined($decoded)) { + $self->{output}->add_option_msg(short_msg => "Error while retrieving data (add --debug option for detailed message)"); + $self->{output}->option_exit(); + } + if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) { + $self->{output}->add_option_msg(short_msg => 'api request error: ' . (defined($decoded->{type}) ? $decoded->{type} : 'unknown')); + $self->{output}->option_exit(); + } + + return $decoded; +} + +1; + +__END__ + +=head1 NAME + +ILO Rest API + +=head1 REST API OPTIONS + +=over 8 + +=item B<--hostname> + +Set hostname or IP of ILO. + +=item B<--port> + +Set port (Default: '443'). + +=item B<--proto> + +Specify https if needed (Default: 'https'). + +=item B<--api-username> + +Set username. + +=item B<--api-password> + +Set password. + +=item B<--timeout> + +Threshold for HTTP timeout (Default: '30'). + +=back + +=cut diff --git a/hardware/server/hp/ilo/restapi/plugin.pm b/hardware/server/hp/ilo/restapi/plugin.pm new file mode 100644 index 000000000..8d90a8b95 --- /dev/null +++ b/hardware/server/hp/ilo/restapi/plugin.pm @@ -0,0 +1,49 @@ +# +# 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 hardware::server::hp::ilo::restapi::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} } = ( + 'hardware' => 'centreon::common::redfish::restapi::mode::hardware', + ); + + $self->{custom_modes}{api} = 'hardware::server::hp::ilo::restapi::custom::api'; + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check ilo 5 with Redfish Rest API. + +=cut