Debian Codegraph @

src/cloud/microsoft/office365/management/mode/appcredentials.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 cloud::microsoft::office365::management::mode::appcredentials;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
28 use centreon::plugins::misc;
29 use POSIX;
30 use DateTime;
31
32 my $unitdiv = { s => 1, w => 604800, d => 86400, h => 3600, m => 60 };
33 my $unitdiv_long = { s => 'seconds', w => 'weeks', d => 'days', h => 'hours', m => 'minutes' };
34
35 sub custom_expires_perfdata {
36 my ($self, %options) = @_;
37
38 $self->{output}->perfdata_add(
39 nlabel => $self->{nlabel} . '.' . $unitdiv_long->{ $self->{instance_mode}->{option_results}->{unit} },
40 unit => $self->{instance_mode}->{option_results}->{unit},
41 instances => [$self->{result_values}->{app_name}, $self->{result_values}->{id}],
42 value => floor($self->{result_values}->{expires_seconds} / $unitdiv->{ $self->{instance_mode}->{option_results}->{unit} }),
43 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
44 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
45 min => 0
46 );
47 }
48
49 sub custom_expires_threshold {
50 my ($self, %options) = @_;
51
52 return $self->{perfdata}->threshold_check(
53 value => floor($self->{result_values}->{expires_seconds} / $unitdiv->{ $self->{instance_mode}->{option_results}->{unit} }),
54 threshold => [
55 { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' },
56 { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' },
57 { label => 'unknown-'. $self->{thlabel}, exit_litteral => 'unknown' }
58 ]
59 );
60 }
61
62 sub custom_status_output {
63 my ($self, %options) = @_;
64
65 return 'status: ' . $self->{result_values}->{status};
66 }
67
68 sub application_long_output {
69 my ($self, %options) = @_;
70
71 return "checking application '" . $options{instance_value}->{name} . "'";
72 }
73
74 sub prefix_application_output {
75 my ($self, %options) = @_;
76
77 return "application '" . $options{instance_value}->{name} . "' ";
78 }
79
80 sub prefix_key_output {
81 my ($self, %options) = @_;
82
83 return "key credential '" . $options{instance} . "' ";
84 }
85
86 sub prefix_password_output {
87 my ($self, %options) = @_;
88
89 return "password credential '" . $options{instance} . "' ";
90 }
91
92 sub set_counters {
93 my ($self, %options) = @_;
94
95 $self->{maps_counters_type} = [
96 { name => 'applications', type => 3, cb_prefix_output => 'prefix_application_output', cb_long_output => 'application_long_output', indent_long_output => ' ', message_multiple => 'All application credentials are ok',
97 group => [
98 { name => 'password', display_long => 1, cb_prefix_output => 'prefix_password_output', message_multiple => 'All password credentials are ok', type => 1, skipped_code => { -10 => 1 } },
99 { name => 'key', display_long => 1, cb_prefix_output => 'prefix_key_output', message_multiple => 'All key credentials are ok', type => 1, skipped_code => { -10 => 1 } }
100 ]
101 }
102 ];
103
104 $self->{maps_counters}->{password} = [
105 { label => 'password-status', type => 2, critical_default => '%{status} =~ /expired/i', set => {
106 key_values => [ { name => 'status' }, { name => 'app_name' }, { name => 'id' } ],
107 closure_custom_output => $self->can('custom_status_output'),
108 closure_custom_perfdata => sub { return 0; },
109 closure_custom_threshold_check => \&catalog_status_threshold_ng
110 }
111 },
112 { label => 'password-expires', nlabel => 'application.password.expires', set => {
113 key_values => [
114 { name => 'expires_seconds' }, { name => 'expires_human' },
115 { name => 'app_name' }, { name => 'id' }
116 ],
117 output_template => 'expires in %s',
118 output_use => 'expires_human',
119 closure_custom_perfdata => $self->can('custom_expires_perfdata'),
120 closure_custom_threshold_check => $self->can('custom_expires_threshold')
121 }
122 }
123 ];
124
125 $self->{maps_counters}->{key} = [
126 { label => 'key-status', type => 2, critical_default => '%{status} =~ /expired/i', set => {
127 key_values => [ { name => 'status' }, { name => 'app_name' }, { name => 'id' } ],
128 closure_custom_output => $self->can('custom_status_output'),
129 closure_custom_perfdata => sub { return 0; },
130 closure_custom_threshold_check => \&catalog_status_threshold_ng
131 }
132 },
133 { label => 'key-expires', nlabel => 'application.key.expires', set => {
134 key_values => [
135 { name => 'expires_seconds' }, { name => 'expires_human' },
136 { name => 'app_name' }, { name => 'id' }
137 ],
138 output_template => 'expires in %s',
139 output_use => 'expires_human',
140 closure_custom_perfdata => $self->can('custom_expires_perfdata'),
141 closure_custom_threshold_check => $self->can('custom_expires_threshold')
142 }
143 }
144 ];
145 }
146
147 sub new {
148 my ($class, %options) = @_;
149 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
150 bless $self, $class;
151
152 $options{options}->add_options(arguments => {
153 'filter-app-name:s' => { name => 'filter_app_name' },
154 'unit:s' => { name => 'unit', default => 's' }
155 });
156
157 return $self;
158 }
159
160 sub check_options {
161 my ($self, %options) = @_;
162 $self->SUPER::check_options(%options);
163
164 if ($self->{option_results}->{unit} eq '' || !defined($unitdiv->{$self->{option_results}->{unit}})) {
165 $self->{option_results}->{unit} = 's';
166 }
167 }
168
169 sub add_credential {
170 my ($self, %options) = @_;
171
172 # 2022-03-17T17:11:43.932Z
173 return if (!defined($options{end}) || $options{end} !~ /^(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)/);
174
175 my $dt = DateTime->new(
176 year => $1,
177 month => $2,
178 day => $3,
179 hour => $4,
180 minute => $5,
181 second => $6,
182 );
183 my $elapsed = $dt->epoch() - time();
184 $elapsed = 0 if ($elapsed < 0);
185
186 $self->{applications}->{ $options{app} }->{ $options{type} }->{ $options{id} } = {
187 app_name => $self->{applications}->{ $options{app} }->{name},
188 id => $options{id},
189 status => $elapsed == 0 ? 'expired' : 'valid',
190 expires_seconds => $elapsed,
191 expires_human => centreon::plugins::misc::change_seconds(
192 value => $elapsed
193 )
194 }
195 }
196
197 sub manage_selection {
198 my ($self, %options) = @_;
199
200 my $results = $options{custom}->get_applications();
201
202 $self->{applications} = {};
203 foreach my $app (@$results) {
204 if (defined($self->{option_results}->{filter_app_name}) && $self->{option_results}->{filter_app_name} ne '' &&
205 $app->{displayName} !~ /$self->{option_results}->{filter_app_name}/) {
206 $self->{output}->output_add(long_msg => "skipping '" . $app->{displayName} . "': no matching filter name.", debug => 1);
207 next;
208 }
209
210 $self->{applications}->{ $app->{id} } = {
211 name => $app->{displayName},
212 key => {},
213 password => {}
214 };
215
216 foreach (@{$app->{keyCredentials}}) {
217 $self->add_credential(app => $app->{id}, type => 'key', id => $_->{keyId}, end => $_->{endDateTime});
218 }
219 foreach (@{$app->{passwordCredentials}}) {
220 $self->add_credential(app => $app->{id}, type => 'password', id => $_->{keyId}, end => $_->{endDateTime});
221 }
222 }
223 }
224
225 1;
226
227 __END__
228
229 =head1 MODE
230
231 Check application credentials.
232
233 =over 8
234
235 =item B<--filter-app-name>
236
237 Filter applications (can be a regexp).
238
239 =item B<--warning-key-status>
240
241 Define the conditions to match for the status to be WARNING.
242 You can use the following variables: %{status}, %{id}, %{app_name}.
243
244 =item B<--critical-key-status>
245
246 Define the conditions to match for the status to be CRITICAL (default: '%{status} =~ /expired/i').
247 You can use the following variables: %{status}, %{id}, %{app_name}.
248
249 =item B<--warning-password-status>
250
251 Define the conditions to match for the status to be WARNING.
252 You can use the following variables: %{status}, %{id}, %{app_name}.
253
254 =item B<--critical-password-status>
255
256 Define the conditions to match for the status to be CRITICAL (default: '%{status} =~ /expired/i').
257 You can use the following variables: %{status}, %{id}, %{app_name}.
258
259 =item B<--unit>
260
261 Select the time unit for the expiration thresholds. May be 's' for seconds, 'm' for minutes, 'h' for hours, 'd' for days, 'w' for weeks. Default is secondss.
262
263 =item B<--warning-*> B<--critical-*>
264
265 Thresholds.
266 Can be: 'key-expires', 'password-expires'.
267
268 =back
269
270 =cut