diff --git a/apps/ericsson/enm/api/custom/api.pm b/apps/ericsson/enm/api/custom/api.pm new file mode 100644 index 000000000..b3e5c40cc --- /dev/null +++ b/apps/ericsson/enm/api/custom/api.pm @@ -0,0 +1,499 @@ +# +# Copyright 2021 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 apps::ericsson::enm::api::custom::api; + +use strict; +use warnings; +use centreon::plugins::http; +use centreon::plugins::statefile; +use JSON::XS; +use Time::HiRes; +use Digest::MD5 qw(md5_hex); +use centreon::plugins::misc; + +sub new { + my ($class, %options) = @_; + my $self = {}; + 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' }, + 'cache-use' => { name => 'cache_use' } + }); + } + $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); + + $self->{output} = $options{output}; + $self->{http} = centreon::plugins::http->new(%options); + $self->{cache_token} = centreon::plugins::statefile->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 {} + +sub check_options { + my ($self, %options) = @_; + + $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : ''; + $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} : 50; + $self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : ''; + $self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : ''; + if ($self->{hostname} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --hostname option."); + $self->{output}->option_exit(); + } + if ($self->{api_username} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --api-username option."); + $self->{output}->option_exit(); + } + if ($self->{api_password} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --api-password option."); + $self->{output}->option_exit(); + } + + # we force to use storable module + $self->{option_results}->{statefile_storable} = 1; + $self->{cache_token}->check_options(option_results => $self->{option_results}); + $self->{cache}->check_options(option_results => $self->{option_results}); + return 0; +} + +sub get_hostname { + my ($self, %options) = @_; + + return $self->{hostname}; +} + +sub build_options_for_httplib { + my ($self, %options) = @_; + + $self->{option_results}->{hostname} = $self->{hostname}; + $self->{option_results}->{timeout} = $self->{timeout}; + $self->{option_results}->{port} = $self->{port}; + $self->{option_results}->{proto} = $self->{proto}; +} + +sub settings { + my ($self, %options) = @_; + + return if (defined($self->{settings_done})); + $self->build_options_for_httplib(); + $self->{http}->set_options(%{$self->{option_results}}); + $self->{settings_done} = 1; +} + +sub login { + my ($self, %options) = @_; + + my $has_cache_file = $self->{cache_token}->read(statefile => 'ericsson_enm_' . md5_hex($self->{option_results}->{hostname} . '_' . $self->{api_username})); + my $session_id = $self->{cache_token}->get(name => 'session_id'); + my $md5_secret_cache = $self->{cache_token}->get(name => 'md5_secret'); + my $md5_secret = md5_hex($self->{api_username} . $self->{api_password}); + + if ($has_cache_file == 0 || + !defined($session_id) || + (defined($md5_secret_cache) && $md5_secret_cache ne $md5_secret) + ) { + $self->settings(); + my $content = $self->{http}->request( + method => 'POST', + url_path => '/login', + post_param => ['IDToken1=' . $self->{api_username}, 'IDToken2=' . $self->{api_password}], + critical_status => '', + warning_status => '', + unknown_status => '' + ); + + # 401 for failed auth + if ($self->{http}->get_code() != 200) { + $self->{output}->add_option_msg(short_msg => "Authentication error [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']"); + $self->{output}->option_exit(); + } + + my (@cookies) = $self->{http}->get_first_header(name => 'Set-Cookie'); + $session_id = ''; + foreach my $cookie (@cookies) { + $session_id = $1 if ($cookie =~ /^iPlanetDirectoryPro=(.+?);/); + } + + if (!defined($session_id) || $session_id eq '') { + $self->{output}->add_option_msg(short_msg => 'Cannot get session id'); + $self->{output}->option_exit(); + } + + my $datas = { + updated => time(), + session_id => $session_id, + md5_secret => $md5_secret + }; + $self->{cache_token}->write(data => $datas); + } + + return $session_id; +} + +sub clean_session { + my ($self, %options) = @_; + + my $datas = { updated => time() }; + $self->{cache_token}->write(data => $datas); +} + +sub credentials { + my ($self, %options) = @_; + + if (!defined($self->{session_id})) { + $self->{session_id} = $self->login(); + } +} + +sub execute_command { + my ($self, %options) = @_; + + my $response = $self->{http}->request( + method => 'POST', + url_path => '/server-scripting/services/command', + header => [ + 'Accept: application/vnd.com.ericsson.oss.scripting+text;VERSION="1"', + 'X-Requested-With: XMLHttpRequest', + 'Cookie: iPlanetDirectoryPro=' . $self->{session_id} + ], + form => [ + { copyname => 'name', copycontents => 'command' }, + { copyname => 'command', copycontents => $options{command} }, + { copyname => 'stream_output', copycontents => 'true' }, + { copyname => 'requestSequence', copycontents => Time::HiRes::time() } + ], + critical_status => '', + warning_status => '', + unknown_status => '' + ); + + # Maybe token is invalid. so we retry + if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) { + $self->clean_session(); + $self->credentials(); + $response = $self->{http}->request( + method => 'POST', + url_path => '/server-scripting/services/command', + header => [ + 'Accept: application/vnd.com.ericsson.oss.scripting+text;VERSION="1"', + 'X-Requested-With: XMLHttpRequest', + 'Cookie: iPlanetDirectoryPro=' . $self->{session_id} + ], + form => [ + { copyname => 'name', copycontents => 'command' }, + { copyname => 'command', copycontents => $options{command} }, + { copyname => 'stream_output', copycontents => 'true' }, + { copyname => 'requestSequence', copycontents => Time::HiRes::time() } + ], + critical_status => '', + warning_status => '', + unknown_status => '' + ); + } + + if ($self->{http}->get_code() != 201) { + $self->{output}->add_option_msg(short_msg => "execute-command issue"); + $self->{output}->option_exit(); + } + + return $response; +} + +sub get_command_output { + my ($self, %options) = @_; + + my $decoded; + my $elements = []; + while (1) { + my $response = $self->{http}->request( + method => 'GET', + url_path => '/server-scripting/services/command/output/' . $options{command_id} . '/stream?', + header => [ + 'Accept: application/vnd.com.ericsson.oss.scripting.terminal+json;VERSION="3"', + 'X-Requested-With: XMLHttpRequest', + 'Cookie: iPlanetDirectoryPro=' . $self->{session_id} + ], + get_param => => ['_wait_milli=1000'], + critical_status => '', + warning_status => '', + unknown_status => '' + ); + # Maybe token is invalid. so we retry + if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) { + $self->clean_session(); + $self->credentials(); + $response = $self->{http}->request( + method => 'GET', + url_path => '/server-scripting/services/command/output/' . $options{command_id} . '/stream?', + header => [ + 'Accept: application/vnd.com.ericsson.oss.scripting.terminal+json;VERSION="3"', + 'X-Requested-With: XMLHttpRequest', + 'Cookie: iPlanetDirectoryPro=' . $self->{session_id} + ], + get_param => => ['_wait_milli=1000'], + critical_status => '', + warning_status => '', + unknown_status => '' + ); + } + + if ($self->{http}->get_code() < 200 || $self->{http}->get_code() >= 300) { + $self->{output}->add_option_msg(short_msg => "get-command-output issue"); + $self->{output}->option_exit(); + } + + my $json; + eval { + $json = JSON::XS->new->utf8->decode($response); + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => 'get-command-output: cannot decode response'); + $self->{output}->option_exit(); + } + #_response_status = FETCHING, COMPLETE + if ($json->{_response_status} eq 'COMPLETE') { + $decoded = $json; + unshift @{$decoded->{output}->{_elements}}, @$elements; + last; + } else { + push @$elements, @{$json->{output}->{_elements}}; + } + } + + return $decoded; +} + +sub execute { + my ($self, %options) = @_; + + $self->settings(); + $self->credentials(); + my $command_id = $self->execute_command(command => $options{command}); + return $self->get_command_output(command_id => $command_id); +} + +sub parse_result { + my ($self, %options) = @_; + + my $results = []; + foreach my $group (@{$options{result}->{output}->{_elements}}) { + foreach my $entry (@{$group->{_elements}}) { + my $h = {}; + foreach (@{$entry->{_elements}}) { + $h->{ $_->{_label}->[0] } = $_->{value}; + } + push @$results, $h; + } + } + + return $results; +} + +sub call_fruState { + my ($self, %options) = @_; + + my $datas = $self->execute( + command => 'cmedit get * FieldReplaceableUnit.(administrativeState,availabilityStatus,faultIndicator,hwTestResult,maintenanceIndicator,operationalIndicator,operationalState,specialIndicator,statusIndicator,userLabel) -t' + ); + + return $self->parse_result(result => $datas); +} + +sub call_nodeSyncState { + my ($self, %options) = @_; + + my $datas = $self->execute( + command => 'cmedit get * CmFunction.syncStatus -t' + ); + + return $self->parse_result(result => $datas); +} + +sub call_EUtranCellTDD { + my ($self, %options) = @_; + + my $datas = $self->execute( + command => 'cmedit get * EUtranCellTDD.(operationalstate,administrativestate,availabilityStatus,userlabel) -t' + ); + + return $self->parse_result(result => $datas); +} + +sub cache_fruState { + my ($self, %options) = @_; + + my $datas = $self->call_fruState(); + $self->write_cache_file( + statefile => 'fruState', + response => $datas + ); + + return $datas; +} + +sub cache_nodeSyncState { + my ($self, %options) = @_; + + my $datas = $self->call_nodeSyncState(); + $self->write_cache_file( + statefile => 'nodeSyncState', + response => $datas + ); + + return $datas; +} + +sub cache_EUtranCellTDD { + my ($self, %options) = @_; + + my $datas = $self->call_EUtranCellTDD(); + $self->write_cache_file( + statefile => 'EUtranCellTDD', + response => $datas + ); + + return $datas; +} + +sub write_cache_file { + my ($self, %options) = @_; + + $self->{cache}->read(statefile => 'cache_ericsson_enm_' . $self->get_hostname() . '_' . $options{statefile}); + $self->{cache}->write(data => { + update_time => time(), + response => $options{response} + }); +} + +sub get_cache_file_response { + my ($self, %options) = @_; + + $self->{cache}->read(statefile => 'cache_ericsson_enm_' . $self->get_hostname() . '_' . $options{statefile}); + my $response = $self->{cache}->get(name => 'response'); + if (!defined($response)) { + $self->{output}->add_option_msg(short_msg => 'Cache file missing'); + $self->{output}->option_exit(); + } + return $response; +} + +sub get_fruState { + my ($self, %options) = @_; + + return $self->get_cache_file_response(statefile => 'fruState') + if (defined($self->{option_results}->{cache_use})); + return $self->call_fruState(); +} + +sub get_nodeSyncState { + my ($self, %options) = @_; + + return $self->get_cache_file_response(statefile => 'nodeSyncState') + if (defined($self->{option_results}->{cache_use})); + return $self->call_nodeSyncState(); +} + +sub get_EUtranCellTDD { + my ($self, %options) = @_; + + return $self->get_cache_file_response(statefile => 'EUtranCellTDD') + if (defined($self->{option_results}->{cache_use})); + return $self->call_EUtranCellTDD(); +} + +1; + +__END__ + +=head1 NAME + +ENM REST API + +=head1 SYNOPSIS + +Rest API custom mode + +=head1 REST API OPTIONS + +=over 8 + +=item B<--hostname> + +ENM hostname (Required) + +=item B<--port> + +Port used (Default: 443) + +=item B<--proto> + +Specify https if needed (Default: 'https') + +=item B<--api-username> + +API username. + +=item B<--api-password> + +API password. + +=item B<--timeout> + +Set HTTP timeout + +=item B<--cache-use> + +Use the cache file (created with cache mode). + +=back + +=head1 DESCRIPTION + +B. + +=cut diff --git a/apps/ericsson/enm/api/mode/cache.pm b/apps/ericsson/enm/api/mode/cache.pm new file mode 100644 index 000000000..da63b8e33 --- /dev/null +++ b/apps/ericsson/enm/api/mode/cache.pm @@ -0,0 +1,62 @@ +# +# Copyright 2021 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 apps::ericsson::enm::api::mode::cache; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $options{options}->add_options(arguments => {}); + + return $self; +} + +sub manage_selection { + my ($self, %options) = @_; + + $options{custom}->cache_fruState(); + $options{custom}->cache_nodeSyncState(); + $options{custom}->cache_EUtranCellTDD(); + $self->{output}->output_add( + severity => 'OK', + short_msg => 'Cache files created successfully' + ); +} + +1; + +__END__ + +=head1 MODE + +Create cache files (other modes could use it with --cache-use option). + +=over 8 + +=back + +=cut diff --git a/apps/ericsson/enm/api/mode/discovery.pm b/apps/ericsson/enm/api/mode/discovery.pm new file mode 100644 index 000000000..a0cdcd65a --- /dev/null +++ b/apps/ericsson/enm/api/mode/discovery.pm @@ -0,0 +1,120 @@ +# +# Copyright 2021 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 apps::ericsson::enm::api::mode::discovery; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use JSON::XS; + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'resource-type:s' => { name => 'resource_type' }, + 'prettify' => { name => 'prettify' } + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + if (!defined($self->{option_results}->{resource_type}) || $self->{option_results}->{resource_type} eq '') { + $self->{option_results}->{resource_type} = 'nodes'; + } + if ($self->{option_results}->{resource_type} !~ /^nodes$/) { + $self->{output}->add_option_msg(short_msg => 'unknown resource type'); + $self->{output}->option_exit(); + } +} + +sub discovery_nodes { + my ($self, %options) = @_; + + my $nodes = $options{custom}->call_nodeSyncState(); + + my $disco_data = []; + foreach (@$nodes) { + my $node = {}; + $node->{id} = $_->{NodeId}; + $node->{sync_status} = $_->{syncStatus}; + + push @$disco_data, $node; + } + + return $disco_data; +} + +sub run { + my ($self, %options) = @_; + + my $disco_stats; + $disco_stats->{start_time} = time(); + + my $results = $self->discovery_nodes( + custom => $options{custom} + ); + + $disco_stats->{end_time} = time(); + $disco_stats->{duration} = $disco_stats->{end_time} - $disco_stats->{start_time}; + $disco_stats->{discovered_items} = scalar(@$results); + $disco_stats->{results} = $results; + + my $encoded_data; + eval { + if (defined($self->{option_results}->{prettify})) { + $encoded_data = JSON::XS->new->utf8->pretty->encode($disco_stats); + } else { + $encoded_data = JSON::XS->new->utf8->encode($disco_stats); + } + }; + if ($@) { + $encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}'; + } + + $self->{output}->output_add(short_msg => $encoded_data); + $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1); + $self->{output}->exit(); +} + +1; + +__END__ + +=head1 MODE + +Resources discovery. + +=over 8 + +=item B<--resource-type> + +Choose the type of resources to discover (Can be: 'nodes'). + +=back + +=cut diff --git a/apps/ericsson/enm/api/mode/listnodescelltdd.pm b/apps/ericsson/enm/api/mode/listnodescelltdd.pm new file mode 100644 index 000000000..8966353ea --- /dev/null +++ b/apps/ericsson/enm/api/mode/listnodescelltdd.pm @@ -0,0 +1,126 @@ +# +# Copyright 2021 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 apps::ericsson::enm::api::mode::listnodescelltdd; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'add-extra-attrs' => { name => 'add_extra_attrs' } + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); +} + +sub manage_selection { + my ($self, %options) = @_; + + my $cells = $options{custom}->call_EUtranCellTDD(); + my $results = []; + foreach my $cell (@$cells) { + my $attr = { node_id => $cell->{NodeId}, cell_tdd_id => $cell->{EUtranCellTDDId} }; + if (defined($self->{option_results}->{add_extra_attrs})) { + $attr->{label} = defined($cell->{userLabel}) && $cell->{userLabel} ne 'null' ? $cell->{userLabel} : ''; + $attr->{administrative_state} = lc($cell->{administrativeState}); + $attr->{availability_status} = $cell->{availabilityStatus} ne 'null' ? lc($cell->{availabilityStatus}) : ''; + $attr->{operational_state} = lc($cell->{operationalState}); + } + push @$results, $attr; + } + + return $results; +} + +sub run { + my ($self, %options) = @_; + + my $results = $self->manage_selection(%options); + foreach (@$results) { + my $msg = sprintf('[node_id: %s][cell_tdd_id: %s]', $_->{node_id}, $_->{cell_tdd_id}); + if (defined($self->{option_results}->{add_extra_attrs})) { + $msg .= sprintf( + '[label: %s][administrative state: %s][availability status: %s][operational state: %s]', + $_->{label}, + $_->{administrative_state}, + $_->{availability_status}, + $_->{operational_state} + ) + } + $self->{output}->output_add(long_msg => $msg); + } + $self->{output}->output_add( + severity => 'OK', + short_msg => 'List cells tdd:' + ); + + $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); + $self->{output}->exit(); +} + +sub disco_format { + my ($self, %options) = @_; + + my $attrs = ['node_id', 'cell_tdd_id']; + push @$attrs, ('label', 'administrative_state', 'availability_status', 'operational_state') + if (defined($self->{option_results}->{add_extra_attrs})); + $self->{output}->add_disco_format( + elements => $attrs + ); +} + +sub disco_show { + my ($self, %options) = @_; + + my $results = $self->manage_selection(%options); + foreach (@$results) { + $self->{output}->add_disco_entry(%$_); + } +} + +1; + +__END__ + +=head1 MODE + +List nodes cells tdd. + +=over 8 + +=item B<--add-extra-attrs> + +Display label/administrative_state/availability_status/operational_state. + +=back + +=cut diff --git a/apps/ericsson/enm/api/mode/listnodesfru.pm b/apps/ericsson/enm/api/mode/listnodesfru.pm new file mode 100644 index 000000000..78f7d12db --- /dev/null +++ b/apps/ericsson/enm/api/mode/listnodesfru.pm @@ -0,0 +1,126 @@ +# +# Copyright 2021 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 apps::ericsson::enm::api::mode::listnodesfru; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'add-extra-attrs' => { name => 'add_extra_attrs' } + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); +} + +sub manage_selection { + my ($self, %options) = @_; + + my $frus = $options{custom}->call_fruState(); + my $results = []; + foreach my $fru (@$frus) { + my $attr = { node_id => $fru->{NodeId}, fru_id => $fru->{FieldReplaceableUnitId} }; + if (defined($self->{option_results}->{add_extra_attrs})) { + $attr->{label} = defined($fru->{userLabel}) && $fru->{userLabel} ne 'null' ? $fru->{userLabel} : ''; + $attr->{administrative_state} = lc($fru->{administrativeState}); + $attr->{availability_status} = $fru->{availabilityStatus} ne 'null' ? lc($fru->{availabilityStatus}) : ''; + $attr->{operational_state} = lc($fru->{operationalState}); + } + push @$results, $attr; + } + + return $results; +} + +sub run { + my ($self, %options) = @_; + + my $results = $self->manage_selection(%options); + foreach (@$results) { + my $msg = sprintf('[node_id: %s][fru_id: %s]', $_->{node_id}, $_->{fru_id}); + if (defined($self->{option_results}->{add_extra_attrs})) { + $msg .= sprintf( + '[label: %s][administrative state: %s][availability status: %s][operational state: %s]', + $_->{label}, + $_->{administrative_state}, + $_->{availability_status}, + $_->{operational_state} + ) + } + $self->{output}->output_add(long_msg => $msg); + } + $self->{output}->output_add( + severity => 'OK', + short_msg => 'List field replaceable units:' + ); + + $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); + $self->{output}->exit(); +} + +sub disco_format { + my ($self, %options) = @_; + + my $attrs = ['node_id', 'fru_id']; + push @$attrs, ('label', 'administrative_state', 'availability_status', 'operational_state') + if (defined($self->{option_results}->{add_extra_attrs})); + $self->{output}->add_disco_format( + elements => $attrs + ); +} + +sub disco_show { + my ($self, %options) = @_; + + my $results = $self->manage_selection(%options); + foreach (@$results) { + $self->{output}->add_disco_entry(%$_); + } +} + +1; + +__END__ + +=head1 MODE + +List nodes field replaceable units. + +=over 8 + +=item B<--add-extra-attrs> + +Display label/administrative_state/availability_status/operational_state. + +=back + +=cut diff --git a/apps/ericsson/enm/api/mode/nodes.pm b/apps/ericsson/enm/api/mode/nodes.pm new file mode 100644 index 000000000..aa08532aa --- /dev/null +++ b/apps/ericsson/enm/api/mode/nodes.pm @@ -0,0 +1,313 @@ +# +# Copyright 2021 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 apps::ericsson::enm::api::mode::nodes; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); + +sub custom_fru_status_output { + my ($self, %options) = @_; + + return sprintf( + 'operational state: %s, admin state: %s%s', + $self->{result_values}->{operational_state}, + $self->{result_values}->{administrative_state}, + $self->{result_values}->{availability_status} ne 'null' ? ', availability status: ' . $self->{result_values}->{availability_status}: '' + ); +} + +sub custom_celltdd_status_output { + my ($self, %options) = @_; + + return sprintf( + 'operational state: %s, admin state: %s%s', + $self->{result_values}->{operational_state}, + $self->{result_values}->{administrative_state}, + $self->{result_values}->{availability_status} ne 'null' ? ', availability status: ' . $self->{result_values}->{availability_status}: '' + ); +} + +sub prefix_fru_output { + my ($self, %options) = @_; + + return sprintf( + "field replaceable unit '%s'%s ", + $options{instance}, + $options{instance_value}->{label} ne '' ? ' [label: ' . $options{instance_value}->{label} . ']' : '' + ); +} + +sub prefix_celltdd_output { + my ($self, %options) = @_; + + return sprintf( + "tdd cell '%s'%s ", + $options{instance}, + $options{instance_value}->{label} ne '' ? ' [label: ' . $options{instance_value}->{label} . ']' : '' + ); +} + +sub prefix_global_output { + my ($self, %options) = @_; + + return 'nodes '; +} + +sub node_long_output { + my ($self, %options) = @_; + + return "checking node '" . $options{instance} . "'"; +} + +sub prefix_node_output { + my ($self, %options) = @_; + + return "Node '" . $options{instance} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output', skipped_code => { -10 => 1 } }, + { name => 'nodes', type => 3, cb_prefix_output => 'prefix_node_output', cb_long_output => 'node_long_output', indent_long_output => ' ', message_multiple => 'All nodes are ok', + group => [ + { name => 'node_global', type => 0, skipped_code => { -10 => 1 } }, + { name => 'fru', display_long => 1, cb_prefix_output => 'prefix_fru_output', message_multiple => 'All field replaceable units are ok', type => 1, skipped_code => { -10 => 1 } }, + { name => 'celltdd', display_long => 1, cb_prefix_output => 'prefix_celltdd_output', message_multiple => 'All tdd cells are ok', type => 1, skipped_code => { -10 => 1 } }, + ] + } + ]; + + $self->{maps_counters}->{global} = [ + { label => 'nodes-total', nlabel => 'nodes.total.count', display_ok => 0, set => { + key_values => [ { name => 'total' } ], + output_template => 'total: %s', + perfdatas => [ + { template => '%s', min => 0 } + ] + } + } + ]; + + $self->{maps_counters}->{node_global} = [ + { label => 'node-sync-status', type => 2, critical_default => '%{sync_status} =~ /unsynchronized/i', set => { + key_values => [ { name => 'sync_status' }, { name => 'node_id' } ], + output_template => 'synchronization status: %s', + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + } + ]; + + $self->{maps_counters}->{fru} = [ + { label => 'fru-status', type => 2, set => { + key_values => [ + { name => 'node_id' }, { name => 'fru_id' }, { name => 'label' }, + { name => 'administrative_state' }, { name => 'availability_status' }, + { name => 'operational_state' } + ], + closure_custom_output => $self->can('custom_fru_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + } + ]; + + $self->{maps_counters}->{celltdd} = [ + { label => 'cell-tdd-status', type => 2, set => { + key_values => [ + { name => 'node_id' }, { name => 'cell_tdd_id' }, { name => 'label' }, + { name => 'administrative_state' }, { name => 'availability_status' }, + { name => 'operational_state' } + ], + closure_custom_output => $self->can('custom_celltdd_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + } + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'filter-node-id:s' => { name => 'filter_node_id' }, + 'exclude-node-id:s' => { name => 'exclude_node_id' }, + 'filter-fru-id:s' => { name => 'filter_fru_id' }, + 'exclude-fru-id:s' => { name => 'exclude_fru_id' }, + 'filter-cell-tdd-id:s' => { name => 'filter_cell_tdd_id' }, + 'exclude-cell-tdd-id:s' => { name => 'exclude_cell_tdd_id' } + }); + + return $self; +} + +sub manage_selection { + my ($self, %options) = @_; + + my $nodes = $options{custom}->get_nodeSyncState(); + + $self->{global} = { total => 0 }; + $self->{nodes} = {}; + foreach my $node (@$nodes) { + next if (defined($self->{option_results}->{filter_node_id}) && $self->{option_results}->{filter_node_id} ne '' && + $node->{NodeId} !~ /$self->{option_results}->{filter_node_id}/); + next if (defined($self->{option_results}->{exclude_node_id}) && $self->{option_results}->{exclude_node_id} ne '' && + $node->{NodeId} =~ /$self->{option_results}->{exclude_node_id}/); + + $self->{global}->{total}++; + $self->{nodes}->{ $node->{NodeId} } = { + node_global => { node_id => $node->{NodeId}, sync_status => lc($node->{syncStatus}) }, + fru => {}, + celltdd => {} + }; + } + + my $frus = $options{custom}->get_fruState(); + foreach my $fru (@$frus) { + next if (defined($self->{option_results}->{filter_node_id}) && $self->{option_results}->{filter_node_id} ne '' && + $fru->{NodeId} !~ /$self->{option_results}->{filter_node_id}/); + next if (defined($self->{option_results}->{exclude_node_id}) && $self->{option_results}->{exclude_node_id} ne '' && + $fru->{NodeId} =~ /$self->{option_results}->{exclude_node_id}/); + next if (defined($self->{option_results}->{filter_fru_id}) && $self->{option_results}->{filter_fru_id} ne '' && + $fru->{FieldReplaceableUnitId} !~ /$self->{option_results}->{filter_fru_id}/); + next if (defined($self->{option_results}->{exclude_fru_id}) && $self->{option_results}->{exclude_fru_id} ne '' && + $fru->{FieldReplaceableUnitId} =~ /$self->{option_results}->{exclude_fru_id}/); + + $self->{nodes}->{ $fru->{NodeId} }->{fru}->{ $fru->{FieldReplaceableUnitId} } = { + node_id => $fru->{NodeId}, + fru_id => $fru->{FieldReplaceableUnitId}, + label => defined($fru->{userLabel}) && $fru->{userLabel} ne 'null' ? $fru->{userLabel} : '', + administrative_state => lc($fru->{administrativeState}), + availability_status => lc($fru->{availabilityStatus}), + operational_state => lc($fru->{operationalState}) + }; + } + + my $cells = $options{custom}->get_EUtranCellTDD(); + foreach my $cell (@$cells) { + next if (defined($self->{option_results}->{filter_node_id}) && $self->{option_results}->{filter_node_id} ne '' && + $cell->{NodeId} !~ /$self->{option_results}->{filter_node_id}/); + next if (defined($self->{option_results}->{exclude_node_id}) && $self->{option_results}->{exclude_node_id} ne '' && + $cell->{NodeId} =~ /$self->{option_results}->{exclude_node_id}/); + next if (defined($self->{option_results}->{filter_cell_tdd_id}) && $self->{option_results}->{filter_cell_tdd_id} ne '' && + $cell->{EUtranCellTDDId} !~ /$self->{option_results}->{filter_cell_tdd_id}/); + next if (defined($self->{option_results}->{exclude_cell_tdd_id}) && $self->{option_results}->{exclude_cell_tdd_id} ne '' && + $cell->{EUtranCellTDDId} =~ /$self->{option_results}->{exclude_cell_tdd_id}/); + + $self->{nodes}->{ $cell->{NodeId} }->{celltdd}->{ $cell->{EUtranCellTDDId} } = { + node_id => $cell->{NodeId}, + cell_tdd_id => $cell->{EUtranCellTDDId}, + label => defined($cell->{userLabel}) && $cell->{userLabel} ne 'null' ? $cell->{userLabel} : '', + administrative_state => lc($cell->{administrativeState}), + availability_status => lc($cell->{availabilityStatus}), + operational_state => lc($cell->{operationalState}) + }; + } +} + +1; + +__END__ + +=head1 MODE + +Check nodes. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='total' + +=item B<--filter-node-id> + +Filter nodes by id (can be a regexp). + +=item B<--filter-fru-id> + +Filter tdd cells by id (can be a regexp). + +=item B<--filter-cell-tdd-id> + +Filter field replaceable units by id (can be a regexp). + +=item B<--unknown-node-sync-status> + +Set unknown threshold for synchronization status. +Can used special variables like: %{node_id}, %{sync_status} + +=item B<--warning-node-sync-status> + +Set warning threshold for synchronization status. +Can used special variables like: %{node_id}, %{sync_status} + +=item B<--critical-node-sync-status> + +Set critical threshold for synchronization status (Default: '%{sync_status} =~ /unsynchronized/i'). +Can used special variables like: %{node_id}, %{sync_status} + +=item B<--unknown-fru-status> + +Set unknown threshold for field replaceable unit status. +Can used special variables like: %{node_id}, %{fru_id}, %{label}, %{administrative_state}, %{availability_status}, %{operational_state} + +=item B<--warning-fru-status> + +Set warning threshold for field replaceable unit status. +Can used special variables like: %{node_id}, %{fru_id}, %{label}, %{administrative_state}, %{availability_status}, %{operational_state} + +=item B<--critical-fru-status> + +Set critical threshold for field replaceable unit status. +Can used special variables like: %{node_id}, %{fru_id}, %{label}, %{administrative_state}, %{availability_status}, %{operational_state} + +=item B<--unknown-cell-tdd-status> + +Set unknown threshold for cell tdd status. +Can used special variables like: %{node_id}, %{cell_tdd_id}, %{label}, %{administrative_state}, %{availability_status}, %{operational_state} + +=item B<--warning-cell-tdd-status> + +Set warning threshold for cell tdd status. +Can used special variables like: %{node_id}, %{cell_tdd_id}, %{label}, %{administrative_state}, %{availability_status}, %{operational_state} + +=item B<--critical-cell-tdd-status> + +Set critical threshold for cell tdd status. +Can used special variables like: %{node_id}, %{cell_tdd_id}, %{label}, %{administrative_state}, %{availability_status}, %{operational_state} + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'nodes-total'. + +=back + +=cut diff --git a/apps/ericsson/enm/api/plugin.pm b/apps/ericsson/enm/api/plugin.pm new file mode 100644 index 000000000..4ffb91f7a --- /dev/null +++ b/apps/ericsson/enm/api/plugin.pm @@ -0,0 +1,53 @@ +# +# Copyright 2021 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 apps::ericsson::enm::api::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} = '1.0'; + $self->{modes} = { + 'cache' => 'apps::ericsson::enm::api::mode::cache', + 'discovery' => 'apps::ericsson::enm::api::mode::discovery', + 'list-nodes-celltdd' => 'apps::ericsson::enm::api::mode::listnodescelltdd', + 'list-nodes-fru' => 'apps::ericsson::enm::api::mode::listnodesfru', + 'nodes' => 'apps::ericsson::enm::api::mode::nodes' + }; + + $self->{custom_modes}->{api} = 'apps::ericsson::enm::api::custom::api'; + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check Ericsson Network Manager (ENM) using API. + +=cut diff --git a/centreon/plugins/backend/http/curl.pm b/centreon/plugins/backend/http/curl.pm index 93c4ed7a7..c16506d8b 100644 --- a/centreon/plugins/backend/http/curl.pm +++ b/centreon/plugins/backend/http/curl.pm @@ -211,6 +211,31 @@ sub set_auth { } } +sub set_form { + my ($self, %options) = @_; + + if (!defined($self->{form_loaded})) { + centreon::plugins::misc::mymodule_load( + output => $self->{output}, + module => 'Net::Curl::Form', + error_msg => "Cannot load module 'Net::Curl::Form'." + ); + $self->{form_loaded} = 1; + } + + my $form = Net::Curl::Form->new(); + foreach (@{$options{form}}) { + my %args = (); + $args{ $self->{constant_cb}->(name => 'CURLFORM_COPYNAME()') } = $_->{copyname} + if (defined($_->{copyname})); + $args{ $self->{constant_cb}->(name => 'CURLFORM_COPYCONTENTS()') } = $_->{copycontents} + if (defined($_->{copycontents})); + $form->add(%args); + } + + $self->curl_setopt(option => $self->{constant_cb}->(name => 'CURLOPT_HTTPPOST()'), parameter => $form); +} + sub set_proxy { my ($self, %options) = @_; @@ -344,6 +369,10 @@ sub request { $self->set_method(%options, content_type_forced => $content_type_forced, headers => $headers); + if (defined($options{request}->{form})) { + $self->set_form(form => $options{request}->{form}); + } + if (scalar(@$headers) > 0) { $self->{curl_easy}->setopt($self->{constant_cb}->(name => 'CURLOPT_HTTPHEADER'), $headers); } diff --git a/centreon/plugins/backend/http/curlconstants.pm b/centreon/plugins/backend/http/curlconstants.pm index df80894d8..20abea1d1 100644 --- a/centreon/plugins/backend/http/curlconstants.pm +++ b/centreon/plugins/backend/http/curlconstants.pm @@ -23,6 +23,7 @@ package centreon::plugins::backend::http::curlconstants; use strict; use warnings; use Net::Curl::Easy qw(:constants); +use Net::Curl::Form qw(:constants); sub get_constant_value { my (%options) = @_; diff --git a/centreon/plugins/backend/http/lwp.pm b/centreon/plugins/backend/http/lwp.pm index 76763a64e..1108f9952 100644 --- a/centreon/plugins/backend/http/lwp.pm +++ b/centreon/plugins/backend/http/lwp.pm @@ -202,6 +202,7 @@ sub request { if (defined($request_options->{get_params})) { $uri->query_form($request_options->{get_params}); } + $req = HTTP::Request->new($request_options->{method}, $uri); my $content_type_forced = 0; @@ -221,6 +222,11 @@ sub request { $req->content($uri_post->query); } + if (defined($request_options->{form})) { + $self->{output}->add_option_msg(short_msg => 'unsupported form param'); + $self->{output}->option_exit(); + } + if (defined($request_options->{credentials}) && defined($request_options->{ntlmv2})) { centreon::plugins::misc::mymodule_load( output => $self->{output},