Debian Codegraph @

src/apps/monitoring/iplabel/ekara/restapi/mode/scenarios.pm

raw
1 #
2 # Copyright 2024 Centreon (http://www.centreon.com/)
3 #
4 # Centreon is a full-fledged industry-strength solution that meets
5 # the needs in IT infrastructure and application monitoring for
6 # service performance.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 #
20
21 package apps::monitoring::iplabel::ekara::restapi::mode::scenarios;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use POSIX;
28 use Date::Parse;
29 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
30
31 sub custom_status_output {
32 my ($self, %options) = @_;
33
34 return sprintf('status: %s (%s)', $self->{result_values}->{status}, $self->{result_values}->{num_status});
35 }
36
37 sub prefix_scenario_output {
38 my ($self, %options) = @_;
39
40 return sprintf("Scenario '%s': ", $options{instance_value}->{display});
41 }
42
43 sub prefix_steps_output {
44 my ($self, %options) = @_;
45
46 return sprintf("Step: %s, last exec: %s, ", $options{instance_value}->{display}, $options{instance_value}->{last_exec});
47 }
48
49 sub set_counters {
50 my ($self, %options) = @_;
51
52 $self->{maps_counters_type} = [
53 { 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',
54 group => [
55 { name => 'global', type => 0, skipped_code => { -10 => 1 } },
56 { name => 'steps', display_long => 1, cb_prefix_output => 'prefix_steps_output', message_multiple => 'All steps are ok', type => 1, skipped_code => { -10 => 1 } }
57 ]
58 }
59 ];
60
61 $self->{maps_counters}->{global} = [
62 { label => 'scenario-status',
63 type => 2,
64 warning_default => '%{status} =~ "Degraded"',
65 critical_default => '%{status} =~ "Failure"',
66 unknown_default => '%{status} =~ /(Unknown|No execution|Aborted|Stopped|Excluded)/',
67 set => {
68 key_values => [ { name => 'status' }, { name => 'num_status' }, { name => 'display' } ],
69 closure_custom_output => $self->can('custom_status_output'),
70 closure_custom_perfdata => sub { return 0; },
71 closure_custom_threshold_check => \&catalog_status_threshold_ng
72 }
73 },
74 { label => 'availability', nlabel => 'scenario.availability.percentage', set => {
75 key_values => [ { name => 'availability' }, { name => 'display' } ],
76 output_template => 'availability: %s%%',
77 perfdatas => [
78 { template => '%s', min => 0, max => 100, unit => '%', label_extra_instance => 1 }
79 ]
80 }
81 },
82 { label => 'time-total-allsteps', nlabel => 'scenario.time.allsteps.total.milliseconds', set => {
83 key_values => [ { name => 'time_total_allsteps' }, { name => 'display' } ],
84 output_template => 'time total all steps: %sms',
85 perfdatas => [
86 { template => '%s', min => 0, unit => 'ms', label_extra_instance => 1 }
87 ]
88 }
89 },
90 { label => 'time-interaction', nlabel => 'scenario.time.interaction.milliseconds', set => {
91 key_values => [ { name => 'time_interaction' }, { name => 'display' } ],
92 output_template => 'time interaction: %sms',
93 perfdatas => [
94 { template => '%s', min => 0, unit => 'ms', label_extra_instance => 1 }
95 ]
96 }
97 }
98 ];
99 $self->{maps_counters}->{steps} = [
100 { label => 'time-step', nlabel => 'scenario.step.time.milliseconds', set => {
101 key_values => [ { name => 'time_step' }, { name => 'display' }, { name => 'last_exec' } ],
102 output_template => 'time step: %s ms',
103 perfdatas => [
104 { template => '%s', unit => 'ms', min => 0, label_extra_instance => 1 }
105 ]
106 }
107 },
108 { label => 'time-total', nlabel => 'scenario.steps.time.total.milliseconds', set => {
109 key_values => [ { name => 'time_total' }, { name => 'display' }, { name => 'last_exec' } ],
110 output_template => 'time total: %s ms',
111 perfdatas => [
112 { template => '%s', unit => 'ms', min => 0, label_extra_instance => 1 }
113 ]
114 }
115 }
116 ];
117 }
118
119 sub new {
120 my ($class, %options) = @_;
121 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
122 bless $self, $class;
123
124 $options{options}->add_options(arguments => {
125
126 'filter-type:s' => { name => 'filter_type' },
127 'timeframe:s' => { name => 'timeframe'}
128 });
129
130 return $self;
131 }
132
133 sub check_options {
134 my ($self, %options) = @_;
135 $self->SUPER::check_options(%options);
136
137 $self->{timeframe} = defined($self->{option_results}->{timeframe}) && $self->{option_results}->{timeframe} ne '' ? $self->{option_results}->{timeframe} : '7500';
138 }
139
140 my $status_mapping = {
141 0 => 'Unknown',
142 1 => 'Success',
143 2 => 'Failure',
144 3 => 'Aborted',
145 4 => 'No execution',
146 5 => 'No execution',
147 6 => 'Stopped',
148 7 => 'Excluded',
149 8 => 'Degraded'
150 };
151
152 sub manage_selection {
153 my ($self, %options) = @_;
154 my $results = $options{custom}->request_scenarios_status();
155
156 my $time = time();
157 my $start_date = POSIX::strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($time - $self->{timeframe}));
158 my $end_date = POSIX::strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($time));
159 foreach my $scenario (@$results) {
160 my $scenario_detail = $options{custom}->request_api(
161 endpoint => '/results-api/results/' . $scenario->{scenarioId},
162 method => 'POST',
163 get_param => [
164 'from=' . $start_date,
165 'to=' . $end_date
166 ]
167 );
168
169 if (defined($self->{option_results}->{filter_type}) && $self->{option_results}->{filter_type} ne '' &&
170 $scenario_detail->{infos}->{plugin_id} !~ /$self->{option_results}->{filter_type}/i) {
171 $self->{output}->output_add(long_msg => "skipping scenario '" . $scenario->{scenarioName} . "': no matching filter.", debug => 1);
172 next;
173 }
174
175 $self->{scenarios}->{ $scenario->{scenarioName} } = {
176 display => $scenario->{scenarioName},
177 global => {
178 display => $scenario->{scenarioName},
179 id => $scenario->{scenarioId},
180 num_status => $scenario->{currentStatus},
181 status => $status_mapping->{$scenario->{currentStatus}} // 'Unknown',
182 }
183 };
184 if (!defined $scenario_detail->{results} or scalar(@{$scenario_detail->{results}}) <= 0) {
185 $self->{output}->add_option_msg(short_msg => "No execution, please try again with a bigger timeframe");
186 next;
187 }
188 foreach my $kpi (@{$scenario_detail->{kpis}}) {
189 $self->{scenarios}->{ $scenario->{scenarioName} }->{global}->{$kpi->{label}} = $kpi->{value};
190 }
191 $self->{scenarios}->{ $scenario->{scenarioName} }->{steps_index}->{0} = 'Default';
192 if ($scenario_detail->{infos}->{info}->{hasStep}) {
193 foreach my $steps (@{$scenario_detail->{steps}}) {
194 $self->{scenarios}->{ $scenario->{scenarioName} }->{steps_index}->{$steps->{index} - 1} = $steps->{name};
195 }
196 }
197 # The API is expected to sort the output to get the most recent data at the end of the array.
198 # We store the last execution date, and check it for every data point sent back by the api.
199 # If a step has failed, no data is sent by the api for this step, but the results of the previous executions are present.
200 # This allows to get perfdata for the last execution with a successful first step.
201 # If the first step fails, the script will take older data.
202 my $last_execution = @{$scenario_detail->{results}}[-1]->{planningTime};
203 foreach my $step_metrics (@{$scenario_detail->{results}}) {
204 if ($step_metrics->{planningTime} ne $last_execution){
205 $self->{output}->add_option_msg(long_msg => "Execution $step_metrics->{planningTime} of step $step_metrics->{stepId} is older than $last_execution, not taking it into account.", debug => 1);
206 next;
207 }
208 my $exec_time = str2time($step_metrics->{planningTime}, 'GMT');
209 $self->{scenarios}->{ $scenario->{scenarioName} }->{steps}->{ $self->{scenarios}->{ $scenario->{scenarioName} }->{steps_index}->{ $step_metrics->{stepId} } }->{ $step_metrics->{metric} } = $step_metrics->{value};
210 $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));
211 $self->{scenarios}->{ $scenario->{scenarioName} }->{steps}->{ $self->{scenarios}->{ $scenario->{scenarioName} }->{steps_index}->{ $step_metrics->{stepId} } }->{display} = $self->{scenarios}->{ $scenario->{scenarioName} }->{steps_index}->{ $step_metrics->{stepId} };
212 }
213 }
214
215 if (scalar(keys %{$self->{scenarios}}) <= 0) {
216 $self->{output}->add_option_msg(short_msg => "No scenario found");
217 $self->{output}->option_exit();
218 }
219 }
220
221 1;
222
223 __END__
224
225 =head1 MODE
226
227 Check ip-label Ekara scenarios.
228
229 =over 8
230
231 =item B<--timeframe>
232
233 Set timeframe period in seconds. (default: 7500)
234 Example: C<--timeframe='3600'> will check the last hour.
235 Note: If the API/Poller is overloaded, it is preferable to refine
236 this value according to the highest check frequency in the scenario.
237
238 =item B<--filter-type>
239
240 Filter by scenario type.
241 Can be: 'WEB', 'HTTPR', 'BROWSER PAGE LOAD'
242
243 =item B<--unknown-scenario-status>
244 Unknown threshold for scenario status (default: C<%{status} !~ /(Unknown|No execution)/>).
245 Syntax: C<--unknown-scenario-status='%{status} =~ "xxx"'>
246
247 =item B<--warning-scenario-status>
248
249 Warning threshold for scenario status (default: C<%{status} !~ /(Aborted|Stopped|Excluded|Degraded)/>).
250 Syntax: C<--warning-scenario-status='%{status} =~ "xxx"'>
251
252 =item B<--critical-scenario-status>
253
254 Critical threshold for scenario status (default: '%{status} =~ "Failure"').
255 Syntax: --critical-scenario-status='%{status} =~ "xxx"'
256
257 =item B<--warning-*> B<--critical-*>
258
259 Thresholds.
260 Common: 'availability' (%),
261 For WEB scenarios: 'time-total-allsteps' (ms), 'time-step' (ms),
262 For HTTPR scenarios: 'time-total' (ms),
263 FOR BPL scenarios: 'time-interaction' (ms), 'time-total' (ms).
264
265
266 =back
267
268 =cut