Skip to content

Commit

Permalink
enh(iplabel): Add new filters to scenarios selection and fix identifi…
Browse files Browse the repository at this point in the history
…ers management


Refs:CTOR-1107
  • Loading branch information
Evan-Adam authored Jan 13, 2025
1 parent 942c0b7 commit 47cf7c9
Show file tree
Hide file tree
Showing 7 changed files with 1,218 additions and 119 deletions.
109 changes: 98 additions & 11 deletions src/apps/monitoring/iplabel/ekara/restapi/custom/api.pm
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,19 @@ sub new {

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' }
'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' },
'filter-id:s' => { name => 'filter_id' },
'filter-name:s' => { name => 'filter_name' },
'filter-workspaceid:s' => { name => 'filter_workspaceid' },
'filter-siteid:s' => { name => 'filter_siteid' },
'filter-status:s@' => { name => 'filter_status' },
'authent-endpoint' => { name => 'authent_endpoint' }
});
}
$options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
Expand Down Expand Up @@ -170,6 +175,57 @@ sub get_access_token {
return $access_token;
}

sub request_scenarios_status{
my ($self, %options) = @_;

my $status_filter = {};
my @get_param;

if (defined($self->{option_results}->{filter_status}) && $self->{option_results}->{filter_status}[0] ne '') {
$status_filter->{statusFilter} = $self->{option_results}->{filter_status};
}
if (defined($self->{option_results}->{filter_workspaceid}) && $self->{option_results}->{filter_workspaceid} ne '') {
push(@get_param, "workspaceId=$self->{option_results}->{filter_workspaceid}");
}
if (defined($self->{option_results}->{filter_siteid}) && $self->{option_results}->{filter_siteid} ne '') {
push(@get_param, "siteId=$self->{option_results}->{filter_siteid}");
}
my $results = $self->request_api(
endpoint => '/results-api/scenarios/status',
method => 'POST',
post_body => $status_filter,
get_param => \@get_param,
);
if (ref($results) eq "HASH" ) {
if (defined($results->{message})) {
$self->{output}->add_option_msg(short_msg => "Cannot get scenarios : " . $results->{message});
$self->{output}->option_exit();
}
if (defined($results->{error})) {
$self->{output}->add_option_msg(short_msg => "Cannot get scenarios : " . $results->{error});
$self->{output}->option_exit();
}
$self->{output}->add_option_msg(short_msg => "Cannot get scenarios due to an unknown error, please use the --debug option to find more information");
$self->{output}->option_exit();
}

my @scenarios;
for my $scenario (@$results) {
if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
$scenario->{scenarioName} !~ /$self->{option_results}->{filter_name}/) {
$self->{output}->output_add(long_msg => "skipping scenario '" . $scenario->{scenarioName} . "': no matching filter.", debug => 1);
next;
}
if (defined($self->{option_results}->{filter_id}) && $self->{option_results}->{filter_id} ne '' &&
$scenario->{scenarioId} !~ /$self->{option_results}->{filter_id}/) {
$self->{output}->output_add(long_msg => "skipping scenario '" . $scenario->{scenarioName} . "': no matching filter.", debug => 1);
next;
}
push(@scenarios, $scenario);
}
return \@scenarios;
}

sub request_api {
my ($self, %options) = @_;

Expand All @@ -195,7 +251,7 @@ sub request_api {
$json = JSON::XS->new->utf8->decode($response);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode Vault JSON response: $@");
$self->{output}->add_option_msg(short_msg => "Cannot decode ekara JSON response: $@");
$self->{output}->option_exit();
};

Expand All @@ -209,11 +265,11 @@ __END__
=head1 NAME
Ip-Label Ekara Rest API
ip-label Ekara Rest API
=head1 REST API OPTIONS
Ip-Label Ekara Rest API
ip-label Ekara Rest API
=over 8
Expand All @@ -237,6 +293,37 @@ Set username.
Set password.
=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-workspaceid>
Filter scenario to check by workspace id.
=item B<--filter-siteid>
Filter scenario to check by site id.
=item B<--timeout>
Set timeout in seconds (default: 10).
Expand Down
32 changes: 7 additions & 25 deletions src/apps/monitoring/iplabel/ekara/restapi/mode/incidents.pm
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ sub custom_status_output {

sub custom_duration_output {
my ($self, %options) = @_;

if ($self->{result_values}->{status} =~ 'Open') {
return sprintf(
'start time: %s, duration: %s',
Expand Down Expand Up @@ -145,8 +146,6 @@ sub new {
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'}
});
Expand Down Expand Up @@ -176,23 +175,10 @@ my $status_mapping = {
sub manage_selection {
my ($self, %options) = @_;

my $results = $options{custom}->request_api(
endpoint => '/results-api/scenarios/status',
method => 'POST',
);
my $results = $options{custom}->request_scenarios_status();

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};
}

Expand All @@ -212,6 +198,10 @@ sub manage_selection {
);

}
else{
$self->{output}->add_option_msg(short_msg => "No scenarios found, can't search for incidents. Please check filters.");
$self->{output}->option_exit();
}

$self->{global}->{total} = 0;

Expand Down Expand Up @@ -257,7 +247,7 @@ __END__
=head1 MODE
Check IP Label Ekara incidents.
Check Ekara incidents.
=over 8
Expand All @@ -266,14 +256,6 @@ Check IP Label Ekara incidents.
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.
Expand Down
95 changes: 22 additions & 73 deletions src/apps/monitoring/iplabel/ekara/restapi/mode/scenarios.pm
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,6 @@ sub custom_status_output {
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) = @_;

Expand Down Expand Up @@ -131,9 +121,7 @@ sub new {
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'}
});
Expand Down Expand Up @@ -162,35 +150,14 @@ my $status_mapping = {

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 $results = $options{custom}->request_scenarios_status();

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;
}

foreach my $scenario (@$results) {
my $scenario_detail = $options{custom}->request_api(
endpoint => '/results-api/results/' . $_->{scenarioId},
endpoint => '/results-api/results/' . $scenario->{scenarioId},
method => 'POST',
get_param => [
'from=' . $start_date,
Expand All @@ -200,35 +167,38 @@ sub manage_selection {

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);
$self->{output}->output_add(long_msg => "skipping scenario '" . $scenario->{scenarioName} . "': no matching filter.", debug => 1);
next;
}

$self->{scenarios}->{ $_->{scenarioName} } = {
display => $_->{scenarioName},
$self->{scenarios}->{ $scenario->{scenarioName} } = {
display => $scenario->{scenarioName},
global => {
display => $_->{scenarioName},
id => $_->{scenarioId},
num_status => $_->{currentStatus},
status => $status_mapping->{$_->{currentStatus}},
display => $scenario->{scenarioName},
id => $scenario->{scenarioId},
num_status => $scenario->{currentStatus},
status => $status_mapping->{$scenario->{currentStatus}} // 'Unknown',
}
};

if (!defined $scenario_detail->{results} or scalar(@{$scenario_detail->{results}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "Scenario '" . $scenario->{scenarioName} . "' Don't have any performance data, please try to add a bigger timeframe");
next;
}
foreach my $kpi (@{$scenario_detail->{kpis}}) {
$self->{scenarios}->{ $_->{scenarioName} }->{global}->{$kpi->{label}} = $kpi->{value};
$self->{scenarios}->{ $scenario->{scenarioName} }->{global}->{$kpi->{label}} = $kpi->{value};
}
$self->{scenarios}->{ $_->{scenarioName} }->{steps_index}->{0} = 'Default';
$self->{scenarios}->{ $scenario->{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};
$self->{scenarios}->{ $scenario->{scenarioName} }->{steps_index}->{$steps->{index} - 1} = $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} };
$self->{scenarios}->{ $scenario->{scenarioName} }->{steps}->{ $self->{scenarios}->{ $scenario->{scenarioName} }->{steps_index}->{ $step_metrics->{stepId} } }->{ $step_metrics->{metric} } = $step_metrics->{value};
$self->{scenarios}->{ $scenario->{scenarioName} }->{steps}->{ $self->{scenarios}->{ $scenario->{scenarioName} }->{steps_index}->{ $step_metrics->{stepId} } }->{last_exec} = POSIX::strftime('%d-%m-%Y %H:%M:%S %Z', localtime($exec_time));
$self->{scenarios}->{ $scenario->{scenarioName} }->{steps}->{ $self->{scenarios}->{ $scenario->{scenarioName} }->{steps_index}->{ $step_metrics->{stepId} } }->{display} = $self->{scenarios}->{ $scenario->{scenarioName} }->{steps_index}->{ $step_metrics->{stepId} };
}
}

Expand All @@ -244,7 +214,7 @@ __END__
=head1 MODE
Check IP Label Ekara scenarios.
Check ip-label Ekara scenarios.
=over 8
Expand All @@ -253,28 +223,7 @@ Check IP Label Ekara scenarios.
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>
Expand Down
Loading

0 comments on commit 47cf7c9

Please sign in to comment.