Debian Codegraph @

src/apps/monitoring/iplabel/ekara/restapi/mode/incidents.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::incidents;
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', $self->{result_values}->{status});
35 }
36
37 sub custom_duration_output {
38 my ($self, %options) = @_;
39
40 if ($self->{result_values}->{status} =~ 'Open') {
41 return sprintf(
42 'start time: %s, duration: %s',
43 $self->{result_values}->{start_time},
44 centreon::plugins::misc::change_seconds(value => $self->{result_values}->{duration})
45 );
46 } else {
47 return sprintf(
48 'start time: %s, end time: %s, duration: %s',
49 $self->{result_values}->{start_time},
50 $self->{result_values}->{end_time},
51 centreon::plugins::misc::change_seconds(value => $self->{result_values}->{duration})
52 );
53 }
54 }
55
56 sub prefix_global_output {
57 my ($self, %options) = @_;
58
59 return 'Incidents ';
60 }
61
62 sub prefix_incidents_output {
63 my ($self, %options) = @_;
64
65 return sprintf("Incident #%s, Scenario '%s' ", $options{instance_value}->{display}, $options{instance_value}->{scenario_name});
66 }
67
68 sub prefix_triggers_output {
69 my ($self, %options) = @_;
70
71 return sprintf(" Site: '%s', last exec: %s, ", $options{instance_value}->{display}, $options{instance_value}->{last_exec});
72 }
73
74
75 sub set_counters {
76 my ($self, %options) = @_;
77
78 $self->{maps_counters_type} = [
79 { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output', skipped_code => { -10 => 1 } },
80 { name => 'incidents', type => 3, cb_prefix_output => 'prefix_incidents_output', cb_long_output => 'prefix_incidents_output', indent_long_output => ' ', message_multiple => 'No current incidents',
81 group => [
82 { name => 'incident', type => 0, skipped_code => { -10 => 1 } },
83 { name => 'triggers', display_long => 1, cb_prefix_output => 'prefix_triggers_output', message_multiple => 'All steps are ok', type => 1, skipped_code => { -10 => 1 } }
84 ]
85 }
86 ];
87
88 $self->{maps_counters}->{global} = [
89 { label => 'incidents-total', nlabel => 'ekara.incidents.current.total.count', set => {
90 key_values => [ { name => 'total' } ],
91 output_template => 'total current: %s',
92 perfdatas => [ { template => '%d', min => 0 } ]
93 }
94 }
95 ];
96
97 $self->{maps_counters}->{incident} = [
98 { label => 'incident-status',
99 type => 2,
100 warning_default => '',
101 critical_default => '%{status} =~ "Open"',
102 set => {
103 key_values => [ { name => 'status' }, { name => 'display' }, { name => 'scenario_name' } ],
104 closure_custom_output => $self->can('custom_status_output'),
105 closure_custom_perfdata => sub { return 0; },
106 closure_custom_threshold_check => \&catalog_status_threshold_ng
107 }
108 },
109 { label => 'incident-severity',
110 type => 2,
111 warning_default => '',
112 critical_default => '%{severity} =~ "Critical"',
113 set => {
114 key_values => [ { name => 'severity' }, { name => 'display' } ],
115 output_template => 'severity: %s',
116 closure_custom_perfdata => sub { return 0; },
117 closure_custom_threshold_check => \&catalog_status_threshold_ng
118 }
119 },
120 { label => 'incident-duration', nlabel => 'ekara.incident.duration.seconds', set => {
121 key_values => [ { name => 'duration' }, { name => 'start_time' }, { name => 'end_time' }, { name => 'status'} ],
122 closure_custom_output => $self->can('custom_duration_output'),
123 closure_custom_perfdata => sub { return 0; }
124 }
125 }
126 ];
127
128 $self->{maps_counters}->{triggers} = [
129 { label => 'trigger-status',
130 type => 2,
131 warning_default => '',
132 critical_default => '%{severity} =~ "Failure"',
133 set => {
134 key_values => [ { name => 'status' }, { name => 'display' } ],
135 closure_custom_output => $self->can('custom_status_output'),
136 closure_custom_perfdata => sub { return 0; },
137 closure_custom_threshold_check => \&catalog_status_threshold_ng
138 }
139 }
140 ];
141 }
142
143 sub new {
144 my ($class, %options) = @_;
145 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
146 bless $self, $class;
147
148 $options{options}->add_options(arguments => {
149 'ignore-closed' => { name => 'ignore_closed' },
150 'timeframe:s' => { name => 'timeframe'}
151 });
152
153 return $self;
154 }
155
156 sub check_options {
157 my ($self, %options) = @_;
158 $self->SUPER::check_options(%options);
159
160 $self->{timeframe} = defined($self->{option_results}->{timeframe}) && $self->{option_results}->{timeframe} ne '' ? $self->{option_results}->{timeframe} : '900';
161 }
162
163 my $status_mapping = {
164 0 => 'Unknown',
165 1 => 'Success',
166 2 => 'Failure',
167 3 => 'Aborted',
168 4 => 'No execution',
169 5 => 'No execution',
170 6 => 'Stopped',
171 7 => 'Excluded',
172 8 => 'Degraded'
173 };
174
175 sub manage_selection {
176 my ($self, %options) = @_;
177
178 my $results = $options{custom}->request_scenarios_status();
179
180 my $scenarios_list = {};
181 foreach (@$results) {
182 push @{$scenarios_list->{scenarioIds}}, $_->{scenarioId};
183 }
184
185 my $time = time();
186 my $end_date = POSIX::strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($time));
187 my $start_date = POSIX::strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($time - $self->{timeframe}));
188 my $incidents;
189 if (defined($scenarios_list->{scenarioIds})) {
190 $incidents = $options{custom}->request_api(
191 endpoint => '/results-api/incidents',
192 method => 'POST',
193 get_param => [
194 'from=' . $start_date,
195 'to=' . $end_date
196 ],
197 post_body => $scenarios_list
198 );
199
200 }
201 else{
202 $self->{output}->add_option_msg(short_msg => "No scenarios found, can't search for incidents. Please check filters.");
203 $self->{output}->option_exit();
204 }
205
206 $self->{global}->{total} = 0;
207
208 if (ref($incidents) eq 'ARRAY' ) {
209 foreach my $incident (@$incidents) {
210 my $start_time = str2time($incident->{startTime}, 'GMT');
211 my $end_time = defined($incident->{endTime}) ? str2time($incident->{endTime}, 'GMT') : time();
212 next if (defined($self->{option_results}->{ignore_closed}) && defined($incident->{endTime}));
213
214 my $incident_id = $incident->{ssr_id} . '_' . $incident->{scnName};
215 $self->{incidents}->{$incident_id} = {
216 display => $incident->{ssr_id},
217 scenario_name => $incident->{scnName},
218 incident => {
219 display => $incident->{ssr_id},
220 scenario_name => $incident->{scnName},
221 scenario_id => $incident->{scn_id},
222 status => defined($incident->{endTime}) ? 'Closed' : 'Open',
223 severity => $incident->{severity},
224 start_time => POSIX::strftime('%d-%m-%Y %H:%M:%S %Z', localtime($start_time)),
225 end_time => POSIX::strftime('%d-%m-%Y %H:%M:%S %Z', localtime($end_time)),
226 duration => $end_time - $start_time
227 }
228 };
229
230 $self->{global}->{total}++;
231
232 foreach my $trigger (@{$incident->{execList}}) {
233 my $exec_time = str2time($trigger->{execTime}, 'GMT');
234 $self->{incidents}->{$incident_id}->{triggers}->{ $trigger->{executionId} } = {
235 display => $trigger->{siteName},
236 status => $status_mapping->{$trigger->{status}},
237 last_exec => POSIX::strftime('%d-%m-%Y %H:%M:%S %Z', localtime($exec_time))
238 }
239 }
240 }
241 }
242 }
243
244 1;
245
246 __END__
247
248 =head1 MODE
249
250 Check Ekara incidents.
251
252 =over 8
253
254 =item B<--timeframe>
255
256 Set timeframe period in seconds. (default: 900)
257 Example: --timeframe='3600' will check the last hour
258
259 =item B<--ignore-closed>
260
261 Ignore solved incidents within the timeframe.
262
263 =item B<--warning-incident-status>
264
265 Warning threshold for incident status (default: none).
266 Syntax: --warning-incident-status='%{status} =~ "xxx"'
267 Can be 'Open' or 'Closed'
268
269 =item B<--critical-incident-status>
270
271 Critical threshold for incident status (default: '%{status} =~ "Open"').
272 Syntax: --critical-incident-status='%{status} =~ "xxx"'
273 Can be 'Open' or 'Closed'
274
275 =item B<--warning-incident-severity>
276
277 Warning threshold for incident severity (default: none).
278 Syntax: --warning-incident-severity='%{severity} =~ "xxx"'
279
280 =item B<--critical-incident-severity>
281
282 Critical threshold for incident severity (default: '%{severity} =~ "Critical"').
283 Syntax: --critical-incident-severity='%{severity} =~ "xxx"'
284
285 =item B<--warning-trigger-status>
286
287 Warning threshold for trigger status (default: none).
288 Syntax: --warning-trigger-status='%{status} =~ "xxx"'
289 Can be 'Unknown', 'Success', 'Failure', 'Aborted', 'No execution',
290 'Stopped', 'Excluded', 'Degraded'
291
292 =item B<--critical-trigger-status>
293
294 Critical threshold for trigger status (default: '%{severity} =~ "Failure"').
295 Syntax: --critical-trigger-status='%{status} =~ "xxx"'
296 Can be 'Unknown', 'Success', 'Failure', 'Aborted', 'No execution',
297 'Stopped', 'Excluded', 'Degraded'
298
299 =item B<--warning-*> B<--critical-*>
300
301 Thresholds.
302 Can be: 'warning-incidents-total' (count) 'critical-incidents-total' (count),
303 'warning-incident-duration' (s), 'critical-incident-duration' (s).
304
305 =back
306
307 =cut