diff --git a/apps/monitoring/iplabel/ekara/restapi/custom/api.pm b/apps/monitoring/iplabel/ekara/restapi/custom/api.pm new file mode 100644 index 000000000..00ec63d1a --- /dev/null +++ b/apps/monitoring/iplabel/ekara/restapi/custom/api.pm @@ -0,0 +1,250 @@ +# +# Copyright 2022 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::monitoring::iplabel::ekara::restapi::custom::api; + +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 = {}; + 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 => { + 'api-username:s' => { name => 'api_username' }, + 'api-password:s' => { name => 'api_password' }, + 'hostname:s' => { name => 'hostname' }, + 'port:s' => { name => 'port' }, + 'proto:s' => { name => 'proto' }, + 'timeout:s' => { name => 'timeout' }, + 'url-path:s' => { name => 'url_path' }, + 'authent-endpoint' => { name => 'authent_endpoint' } + }); + } + $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} = 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} : 'api.ekara.ip-label.net'; + $self->{url_path} = (defined($self->{option_results}->{url_path})) ? $self->{option_results}->{url_path} : ''; + $self->{authent_endpoint} = (defined($self->{option_results}->{authent_endpoint})) ? $self->{option_results}->{authent_endpoint} : '/auth/login'; + $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} : 10; + $self->{unknown_http_status} = (defined($self->{option_results}->{unknown_http_status})) ? $self->{option_results}->{unknown_http_status} : '%{http_code} < 200 or %{http_code} >= 300'; + $self->{warning_http_status} = (defined($self->{option_results}->{warning_http_status})) ? $self->{option_results}->{warning_http_status} : ''; + $self->{critical_http_status} = (defined($self->{option_results}->{critical_http_status})) ? $self->{option_results}->{critical_http_status} : ''; + $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; + $self->{cache}->check_options(option_results => $self->{option_results}); + + 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(); + } + + return 0; +} + +sub build_options_for_httplib { + my ($self, %options) = @_; + + $self->{option_results}->{hostname} = $self->{hostname}; + $self->{option_results}->{timeout} = $self->{timeout}; + $self->{option_results}->{proto} = $self->{proto}; + $self->{option_results}->{warning_status} = ''; + $self->{option_results}->{critical_status} = ''; + $self->{option_results}->{unknown_status} = '%{http_code} < 200 or %{http_code} >= 500'; +} + +sub settings { + my ($self, %options) = @_; + + $self->build_options_for_httplib(); + $self->{http}->add_header(key => 'Accept', value => 'application/json'); + $self->{http}->add_header(key => 'Content-Type', value => 'application/json'); + $self->{http}->add_header(key => 'Authorization', value => 'Bearer ' . $self->{access_token}) if (defined($self->{access_token})); + $self->{http}->set_options(%{$self->{option_results}}); +} + +sub get_access_token { + my ($self, %options) = @_; + + my $has_cache_file = $options{statefile}->read(statefile => 'iplabel_ekara_api_' . md5_hex($self->{hostname}) . '_' . md5_hex($self->{api_username})); + my $expires_on = $options{statefile}->get(name => 'expires_on'); + my $access_token = $options{statefile}->get(name => 'access_token'); + if ( $has_cache_file == 0 || !defined($access_token) || (($expires_on - time()) < 10) ) { + my $login = { email => $self->{api_username}, password => $self->{api_password} }; + my $post_json = JSON::XS->new->utf8->encode($login); + + $self->settings(); + + my $content = $self->{http}->request( + method => 'POST', + header => ['Content-type: application/json'], + query_form_post => $post_json, + url_path => $self->{authent_endpoint} + ); + + if (!defined($content) || $content eq '' || $self->{http}->get_header(name => 'content-length') == 0) { + $self->{output}->add_option_msg(short_msg => "Authentication endpoint returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']"); + $self->{output}->option_exit(); + } + + my $decoded; + eval { + $decoded = JSON::XS->new->utf8->decode($content); + }; + if ($@) { + $self->{output}->output_add(long_msg => $content, debug => 1); + $self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)"); + $self->{output}->option_exit(); + } + if (!($decoded->{success})) { + $self->{output}->output_add(long_msg => "Error message : " . $decoded->{message}, debug => 1); + $self->{output}->add_option_msg(short_msg => "Authentication endpoint returns error code '" . $decoded->{message} . "' (add --debug option for detailed message)"); + $self->{output}->option_exit(); + } + + $access_token = $decoded->{token}; + my $datas = { last_timestamp => time(), access_token => $decoded->{token}, expires_on => time() + 3600 }; + $options{statefile}->write(data => $datas); + } + + return $access_token; +} + +sub request_api { + my ($self, %options) = @_; + + if (!defined($self->{access_token})) { + $self->{access_token} = $self->get_access_token(statefile => $self->{cache}); + } + + $self->settings(); + + my ($json, $response); + + my $post_json = defined($options{post_body}) ? $options{post_body} : {}; + + $response = $self->{http}->request( + get_param => $options{get_param}, + method => $options{method}, + url_path => $self->{url_path} . $options{endpoint}, + query_form_post => JSON::XS->new->utf8->encode($post_json) + ); + $self->{output}->output_add(long_msg => $response, debug => 1); + + eval { + $json = JSON::XS->new->utf8->decode($response); + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode Vault JSON response: $@"); + $self->{output}->option_exit(); + }; + + return $json; +} + + +1; + +__END__ + +=head1 NAME + +Ip-Label Ekara Rest API + +=head1 REST API OPTIONS + +Ip-Label Ekara Rest API + +=over 8 + +=item B<--hostname> + +Set hostname (Default: 'api.ip-label.net'). + +=item B<--port> + +Port used (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> + +Set timeout in seconds (Default: 10). + +=back + +=head1 DESCRIPTION + +B. + +=cut diff --git a/apps/monitoring/iplabel/ekara/restapi/mode/discovery.pm b/apps/monitoring/iplabel/ekara/restapi/mode/discovery.pm new file mode 100644 index 000000000..e3dd88249 --- /dev/null +++ b/apps/monitoring/iplabel/ekara/restapi/mode/discovery.pm @@ -0,0 +1,88 @@ +# +# Copyright 2022 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::monitoring::iplabel::ekara::restapi::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 => { + 'prettify' => { name => 'prettify' } + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); +} + +sub run { + my ($self, %options) = @_; + + my $disco_stats; + $disco_stats->{start_time} = time(); + + my $results = $options{custom}->request_api(endpoint => '/results-api/scenarios/status', method => 'GET'); + + $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 + +IP Label Ekara scenarios discovery. + +=over 8 + +=back + +=cut diff --git a/apps/monitoring/iplabel/ekara/restapi/mode/incidents.pm b/apps/monitoring/iplabel/ekara/restapi/mode/incidents.pm new file mode 100644 index 000000000..59c29e530 --- /dev/null +++ b/apps/monitoring/iplabel/ekara/restapi/mode/incidents.pm @@ -0,0 +1,327 @@ +# +# Copyright 2022 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::monitoring::iplabel::ekara::restapi::mode::incidents; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use POSIX; +use Date::Parse; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); + +sub custom_status_output { + my ($self, %options) = @_; + + return sprintf('status: %s', $self->{result_values}->{status}); +} + +sub custom_duration_output { + my ($self, %options) = @_; + if ($self->{result_values}->{status} =~ 'Open') { + return sprintf( + 'start time: %s, duration: %s', + $self->{result_values}->{start_time}, + centreon::plugins::misc::change_seconds(value => $self->{result_values}->{duration}) + ); + } else { + return sprintf( + 'start time: %s, end time: %s, duration: %s', + $self->{result_values}->{start_time}, + $self->{result_values}->{end_time}, + centreon::plugins::misc::change_seconds(value => $self->{result_values}->{duration}) + ); + } +} + +sub prefix_global_output { + my ($self, %options) = @_; + + return 'Incidents '; +} + +sub prefix_incidents_output { + my ($self, %options) = @_; + + return sprintf("Incident #%s, Scenario '%s' ", $options{instance_value}->{display}, $options{instance_value}->{scenario_name}); +} + +sub prefix_triggers_output { + my ($self, %options) = @_; + + return sprintf(" Site: '%s', last exec: %s, ", $options{instance_value}->{display}, $options{instance_value}->{last_exec}); +} + + +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 => 'incidents', type => 3, cb_prefix_output => 'prefix_incidents_output', cb_long_output => 'prefix_incidents_output', indent_long_output => ' ', message_multiple => 'No current incidents', + group => [ + { name => 'incident', type => 0, skipped_code => { -10 => 1 } }, + { name => 'triggers', display_long => 1, cb_prefix_output => 'prefix_triggers_output', message_multiple => 'All steps are ok', type => 1, skipped_code => { -10 => 1 } } + ] + } + ]; + + $self->{maps_counters}->{global} = [ + { label => 'incidents-total', nlabel => 'ekara.incidents.current.total.count', set => { + key_values => [ { name => 'total' } ], + output_template => 'total current: %s', + perfdatas => [ { template => '%d', min => 0 } ] + } + } + ]; + + $self->{maps_counters}->{incident} = [ + { label => 'incident-status', + type => 2, + warning_default => '', + critical_default => '%{status} =~ "Open"', + set => { + key_values => [ { name => 'status' }, { name => 'display' }, { name => 'scenario_name' } ], + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + }, + { label => 'incident-severity', + type => 2, + warning_default => '', + critical_default => '%{severity} =~ "Critical"', + set => { + key_values => [ { name => 'severity' }, { name => 'display' } ], + output_template => 'severity: %s', + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + }, + { label => 'incident-duration', nlabel => 'ekara.incident.duration.seconds', set => { + key_values => [ { name => 'duration' }, { name => 'start_time' }, { name => 'end_time' }, { name => 'status'} ], + closure_custom_output => $self->can('custom_duration_output'), + perfdatas => [ + { template => '%s', min => 0, unit => 's', label_extra_instance => 1 } + ] + } + } + ]; + + $self->{maps_counters}->{triggers} = [ + { label => 'trigger-status', + type => 2, + warning_default => '', + critical_default => '%{severity} =~ "Failure"', + set => { + key_values => [ { name => 'status' }, { name => 'display' } ], + closure_custom_output => $self->can('custom_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-id:s' => { name => 'filter_id' }, + 'filter-name:s' => { name => 'filter_name' }, + 'ignore-closed' => { name => 'ignore_closed' }, + 'timeframe:s' => { name => 'timeframe'} + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $self->{timeframe} = defined($self->{option_results}->{timeframe}) && $self->{option_results}->{timeframe} ne '' ? $self->{option_results}->{timeframe} : '900'; +} + +my $status_mapping = { + 0 => 'Unknown', + 1 => 'Success', + 2 => 'Failure', + 3 => 'Aborted', + 4 => 'No execution', + 5 => 'No execution', + 6 => 'Stopped', + 7 => 'Excluded', + 8 => 'Degraded' +}; + +sub manage_selection { + my ($self, %options) = @_; + + my $results = $options{custom}->request_api( + endpoint => '/results-api/scenarios/status', + method => 'POST', + ); + + my $scenarios_list = {}; + foreach (@$results) { + if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && + $_->{scenarioName} !~ /$self->{option_results}->{filter_name}/) { + $self->{output}->output_add(long_msg => "skipping scenario '" . $_->{scenarioName} . "': no matching filter.", debug => 1); + next; + } + if (defined($self->{option_results}->{filter_id}) && $self->{option_results}->{filter_id} ne '' && + $_->{scenarioId} !~ /$self->{option_results}->{filter_id}/) { + $self->{output}->output_add(long_msg => "skipping scenario '" . $_->{scenarioName} . "': no matching filter.", debug => 1); + next; + } + push @{$scenarios_list->{scenarioIds}}, $_->{scenarioId}; + } + + my $time = time(); + my $end_date = POSIX::strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($time)); + my $start_date = POSIX::strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($time - $self->{timeframe})); + my $incidents; + if (defined($scenarios_list->{scenarioIds})) { + $incidents = $options{custom}->request_api( + endpoint => '/results-api/incidents', + method => 'POST', + get_param => [ + 'from=' . $start_date, + 'to=' . $end_date + ], + post_body => $scenarios_list + ); + + } + + $self->{global}->{total} = 0; + + if (ref($incidents) eq 'ARRAY' ) { + foreach my $incident (@$incidents) { + my $start_time = str2time($incident->{startTime}, 'GMT'); + my $end_time = defined($incident->{endTime}) ? str2time($incident->{endTime}, 'GMT') : time(); + next if (defined($self->{option_results}->{ignore_closed}) && defined($incident->{endTime})); + + my $incident_id = $incident->{ssr_id} . '_' . $incident->{scnName}; + $self->{incidents}->{$incident_id} = { + display => $incident->{ssr_id}, + scenario_name => $incident->{scnName}, + incident => { + display => $incident->{ssr_id}, + scenario_name => $incident->{scnName}, + scenario_id => $incident->{scn_id}, + status => defined($incident->{endTime}) ? 'Closed' : 'Open', + severity => $incident->{severity}, + start_time => POSIX::strftime('%d-%m-%Y %H:%M:%S %Z', localtime($start_time)), + end_time => POSIX::strftime('%d-%m-%Y %H:%M:%S %Z', localtime($end_time)), + duration => $end_time - $start_time + } + }; + + $self->{global}->{total}++; + + foreach my $trigger (@{$incident->{execList}}) { + my $exec_time = str2time($trigger->{execTime}, 'GMT'); + $self->{incidents}->{$incident_id}->{triggers}->{ $trigger->{executionId} } = { + display => $trigger->{siteName}, + status => $status_mapping->{$trigger->{status}}, + last_exec => POSIX::strftime('%d-%m-%Y %H:%M:%S %Z', localtime($exec_time)) + } + } + } + } +} + +1; + +__END__ + +=head1 MODE + +Check IP Label Ekara incidents. + +=over 8 + +=item B<--timeframe> + +Set timeframe period in seconds. (Default: 900) +Example: --timeframe='3600' will check the last hour + +=item B<--filter-id> + +Filter by monitor id (can be a regexp). + +=item B<--filter-name> + +Filter by monitor name (can be a regexp). + +=item B<--ignore-closed> + +Ignore solved incidents within the timeframe. + +=item B<--warning-incident-status> + +Warning threshold for incident status (Default: none). +Syntax: --warning-incident-status='%{status} =~ "xxx"' +Can be 'Open' or 'Closed' + +=item B<--critical-incident-status> + +Critical threshold for incident status (Default: '%{status} =~ "Open"'). +Syntax: --critical-incident-status='%{status} =~ "xxx"' +Can be 'Open' or 'Closed' + +=item B<--warning-incident-severity> + +Warning threshold for incident severity (Default: none). +Syntax: --warning-incident-severity='%{severity} =~ "xxx"' + +=item B<--critical-incident-severity> + +Critical threshold for incident severity (Default: '%{severity} =~ "Critical"'). +Syntax: --critical-incident-severity='%{severity} =~ "xxx"' + +=item B<--warning-trigger-status> + +Warning threshold for trigger status (Default: none). +Syntax: --warning-trigger-status='%{status} =~ "xxx"' +Can be 'Unknown', 'Success', 'Failure', 'Aborted', 'No execution', +'Stopped', 'Excluded', 'Degraded' + +=item B<--critical-trigger-status> + +Critical threshold for trigger status (Default: '%{severity} =~ "Failure"'). +Syntax: --critical-trigger-status='%{status} =~ "xxx"' +Can be 'Unknown', 'Success', 'Failure', 'Aborted', 'No execution', +'Stopped', 'Excluded', 'Degraded' + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'warning-incidents-total' (count) 'critical-incidents-total' (count), +'warning-incident-duration' (s), 'critical-incident-duration' (s). + +=back + +=cut diff --git a/apps/monitoring/iplabel/ekara/restapi/mode/scenarios.pm b/apps/monitoring/iplabel/ekara/restapi/mode/scenarios.pm new file mode 100644 index 000000000..6a161cffb --- /dev/null +++ b/apps/monitoring/iplabel/ekara/restapi/mode/scenarios.pm @@ -0,0 +1,305 @@ +# +# Copyright 2022 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::monitoring::iplabel::ekara::restapi::mode::scenarios; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use POSIX; +use Date::Parse; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); + +sub custom_status_output { + my ($self, %options) = @_; + + return sprintf('status: %s (%s)', $self->{result_values}->{status}, $self->{result_values}->{num_status}); +} + +sub custom_date_output { + my ($self, %options) = @_; + + return sprintf( + 'last execution: %s (%s ago)', + $self->{result_values}->{lastexec}, + centreon::plugins::misc::change_seconds(value => $self->{result_values}->{freshness}) + ); +} + +sub prefix_scenario_output { + my ($self, %options) = @_; + + return sprintf("Scenario '%s': ", $options{instance_value}->{display}); +} + +sub prefix_steps_output { + my ($self, %options) = @_; + + return sprintf(" Step: %s, last exec: %s, ", $options{instance_value}->{display}, $options{instance_value}->{last_exec}); +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'scenarios', type => 3, cb_prefix_output => 'prefix_scenario_output', cb_long_output => 'prefix_scenario_output', indent_long_output => ' ', message_multiple => 'All scenarios are ok', + group => [ + { name => 'global', type => 0, skipped_code => { -10 => 1 } }, + { name => 'steps', display_long => 1, cb_prefix_output => 'prefix_steps_output', message_multiple => 'All steps are ok', type => 1, skipped_code => { -10 => 1 } } + ] + } + ]; + + $self->{maps_counters}->{global} = [ + { label => 'scenario-status', + type => 2, + warning_default => '%{status} !~ "Success"', + critical_default => '%{status} =~ "Failure"', + set => { + key_values => [ { name => 'status' }, { name => 'num_status' }, { name => 'display' } ], + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + }, + { label => 'availability', nlabel => 'scenario.availability.percentage', set => { + key_values => [ { name => 'availability' }, { name => 'display' } ], + output_template => 'availability: %s%%', + perfdatas => [ + { template => '%s', min => 0, max => 100, unit => '%', label_extra_instance => 1 } + ] + } + }, + { label => 'time-total-allsteps', nlabel => 'scenario.time.allsteps.total.milliseconds', set => { + key_values => [ { name => 'time_total_allsteps' }, { name => 'display' } ], + output_template => 'time total all steps: %sms', + perfdatas => [ + { template => '%s', min => 0, unit => 'ms', label_extra_instance => 1 } + ] + } + }, + { label => 'time-interaction', nlabel => 'scenario.time.interaction.milliseconds', set => { + key_values => [ { name => 'time_interaction' }, { name => 'display' } ], + output_template => 'time interaction: %sms', + perfdatas => [ + { template => '%s', min => 0, unit => 'ms', label_extra_instance => 1 } + ] + } + } + ]; + $self->{maps_counters}->{steps} = [ + { label => 'time-step', nlabel => 'scenario.step.time.milliseconds', set => { + key_values => [ { name => 'time_step' }, { name => 'display' }, { name => 'last_exec' } ], + output_template => 'time step: %s ms', + perfdatas => [ + { template => '%s', unit => 'ms', min => 0, label_extra_instance => 1 } + ] + } + }, + { label => 'time-total', nlabel => 'scenario.steps.time.total.milliseconds', set => { + key_values => [ { name => 'time_total' }, { name => 'display' }, { name => 'last_exec' } ], + output_template => 'time total: %s ms', + perfdatas => [ + { template => '%s', unit => 'ms', min => 0, label_extra_instance => 1 } + ] + } + } + ]; +} + +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-id:s' => { name => 'filter_id' }, + 'filter-name:s' => { name => 'filter_name' }, + 'filter-status:s@' => { name => 'filter_status' }, + 'filter-type:s' => { name => 'filter_type' }, + 'timeframe:s' => { name => 'timeframe'} + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $self->{timeframe} = defined($self->{option_results}->{timeframe}) && $self->{option_results}->{timeframe} ne '' ? $self->{option_results}->{timeframe} : '900'; +} + +my $status_mapping = { + 0 => 'Unknown', + 1 => 'Success', + 2 => 'Failure', + 3 => 'Aborted', + 4 => 'No execution', + 5 => 'No execution', + 6 => 'Stopped', + 7 => 'Excluded', + 8 => 'Degraded' +}; + +sub manage_selection { + my ($self, %options) = @_; + + my $status_filter = {}; + if (defined($self->{option_results}->{filter_status}) && $self->{option_results}->{filter_status}[0] ne '') { + $status_filter->{statusFilter} = $self->{option_results}->{filter_status}; + } + + my $results = $options{custom}->request_api( + endpoint => '/results-api/scenarios/status', + method => 'POST', + post_body => $status_filter + ); + + my $time = time(); + my $start_date = POSIX::strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($time - $self->{timeframe})); + my $end_date = POSIX::strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($time)); + foreach (@$results) { + if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && + $_->{scenarioName} !~ /$self->{option_results}->{filter_name}/) { + $self->{output}->output_add(long_msg => "skipping scenario '" . $_->{scenarioName} . "': no matching filter.", debug => 1); + next; + } + if (defined($self->{option_results}->{filter_id}) && $self->{option_results}->{filter_id} ne '' && + $_->{scenarioId} !~ /$self->{option_results}->{filter_id}/) { + $self->{output}->output_add(long_msg => "skipping scenario '" . $_->{scenarioName} . "': no matching filter.", debug => 1); + next; + } + + my $scenario_detail = $options{custom}->request_api( + endpoint => '/results-api/results/' . $_->{scenarioId}, + method => 'POST', + get_param => [ + 'from=' . $start_date, + 'to=' . $end_date + ] + ); + + if (defined($self->{option_results}->{filter_type}) && $self->{option_results}->{filter_type} ne '' && + $scenario_detail->{infos}->{plugin_id} !~ /$self->{option_results}->{filter_type}/i) { + $self->{output}->output_add(long_msg => "skipping scenario '" . $_->{scenarioName} . "': no matching filter.", debug => 1); + next; + } + + $self->{scenarios}->{ $_->{scenarioName} } = { + display => $_->{scenarioName}, + global => { + display => $_->{scenarioName}, + id => $_->{scenarioId}, + num_status => $_->{currentStatus}, + status => $status_mapping->{$_->{currentStatus}}, + } + }; + + foreach my $kpi (@{$scenario_detail->{kpis}}) { + $self->{scenarios}->{ $_->{scenarioName} }->{global}->{$kpi->{label}} = $kpi->{value}; + } + $self->{scenarios}->{ $_->{scenarioName} }->{steps_index}->{0} = 'Default'; + if ($scenario_detail->{infos}->{info}->{hasStep}) { + foreach my $steps (@{$scenario_detail->{steps}}) { + $self->{scenarios}->{ $_->{scenarioName} }->{steps_index}->{$steps->{index}} = $steps->{name}; + } + } + + foreach my $step_metrics (@{$scenario_detail->{results}}) { + my $exec_time = str2time($step_metrics->{planningTime}, 'GMT'); + $self->{scenarios}->{ $_->{scenarioName} }->{steps}->{ $self->{scenarios}->{ $_->{scenarioName} }->{steps_index}->{ $step_metrics->{stepId} } }->{ $step_metrics->{metric} } = $step_metrics->{value}; + $self->{scenarios}->{ $_->{scenarioName} }->{steps}->{ $self->{scenarios}->{ $_->{scenarioName} }->{steps_index}->{ $step_metrics->{stepId} } }->{last_exec} = POSIX::strftime('%d-%m-%Y %H:%M:%S %Z', localtime($exec_time)); + $self->{scenarios}->{ $_->{scenarioName} }->{steps}->{ $self->{scenarios}->{ $_->{scenarioName} }->{steps_index}->{ $step_metrics->{stepId} } }->{display} = $self->{scenarios}->{ $_->{scenarioName} }->{steps_index}->{ $step_metrics->{stepId} }; + } + } + + if (scalar(keys %{$self->{scenarios}}) <= 0) { + $self->{output}->add_option_msg(short_msg => "No scenario found"); + $self->{output}->option_exit(); + } +} + +1; + +__END__ + +=head1 MODE + +Check IP Label Ekara scenarios. + +=over 8 + +=item B<--timeframe> + +Set timeframe period in seconds. (Default: 900) +Example: --timeframe='3600' will check the last hour + +=item B<--filter-id> + +Filter by monitor id (can be a regexp). + +=item B<--filter-name> + +Filter by monitor name (can be a regexp). + +=item B<--filter-status> + +Filter by numeric status (can be multiple). +0 => 'Unknown', +1 => 'Success', +2 => 'Failure', +3 => 'Aborted', +4 => 'No execution', +5 => 'No execution', +6 => 'Stopped', +7 => 'Excluded', +8 => 'Degraded' + +Example: --filter-status='1,2' + +=item B<--filter-type> + +Filter by scenario type. +Can be: 'WEB', 'HTTPR', 'BROWSER PAGE LOAD' + +=item B<--warning-scenario-status> + +Warning threshold for scenario status (Default: '%{status} !~ "Success"'). +Syntax: --warning-scenario-status='%{status} =~ "xxx"' + +=item B<--critical-scenario-status> + +Critical threshold for scenario status (Default: '%{status} =~ "Failure"'). +Syntax: --critical-scenario-status='%{status} =~ "xxx"' + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Common: 'availability' (%), +For WEB scenarios: 'time-total-allsteps' (ms), 'time-step' (ms), +For HTTPR scenarios: 'time-total' (ms), +FOR BPL scenarios: 'time-interaction' (ms), 'time-total' (ms). + + +=back + +=cut diff --git a/apps/monitoring/iplabel/ekara/restapi/plugin.pm b/apps/monitoring/iplabel/ekara/restapi/plugin.pm new file mode 100644 index 000000000..432e629c6 --- /dev/null +++ b/apps/monitoring/iplabel/ekara/restapi/plugin.pm @@ -0,0 +1,51 @@ +# +# Copyright 2022 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::monitoring::iplabel::ekara::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} = '1.0'; + $self->{modes} = { + 'discovery' => 'apps::monitoring::iplabel::ekara::restapi::mode::discovery', + 'incidents' => 'apps::monitoring::iplabel::ekara::restapi::mode::incidents', + 'scenarios' => 'apps::monitoring::iplabel::ekara::restapi::mode::scenarios' + }; + + $self->{custom_modes}->{api} = 'apps::monitoring::iplabel::ekara::restapi::custom::api'; + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check IP-Label Ekara using Rest API. + +=cut