Debian Codegraph @

src/cloud/microsoft/office365/exchange/mode/mailboxusage.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::exchange::mode::mailboxusage;
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 Time::Local;
29
30 sub custom_active_perfdata {
31 my ($self, %options) = @_;
32
33 my %total_options = ();
34 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
35 $total_options{total} = $self->{result_values}->{total};
36 $total_options{cast_int} = 1;
37 }
38
39 if ($self->{result_values}->{report_date} =~ /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/) {
40 $self->{output}->perfdata_add(label => 'perfdate', value => timelocal(0,0,12,$3,$2-1,$1-1900));
41 }
42
43 $self->{output}->perfdata_add(
44 label => 'active_mailboxes', nlabel => 'exchange.mailboxes.active.count',
45 value => $self->{result_values}->{active},
46 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options),
47 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options),
48 unit => 'mailboxes', min => 0, max => $self->{result_values}->{total}
49 );
50 }
51
52 sub custom_active_threshold {
53 my ($self, %options) = @_;
54
55 my $threshold_value = $self->{result_values}->{active};
56 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
57 $threshold_value = $self->{result_values}->{prct_active};
58 }
59 my $exit = $self->{perfdata}->threshold_check(
60 value => $threshold_value,
61 threshold => [
62 { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' },
63 { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' }
64 ]
65 );
66 return $exit;
67
68 }
69
70 sub custom_active_output {
71 my ($self, %options) = @_;
72
73 return sprintf(
74 'Active mailboxes on %s : %d/%d (%.2f%%)',
75 $self->{result_values}->{report_date},
76 $self->{result_values}->{active},
77 $self->{result_values}->{total},
78 $self->{result_values}->{prct_active}
79 );
80 }
81
82 sub custom_active_calc {
83 my ($self, %options) = @_;
84
85 $self->{result_values}->{active} = $options{new_datas}->{$self->{instance} . '_active'};
86 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
87 $self->{result_values}->{report_date} = $options{new_datas}->{$self->{instance} . '_report_date'};
88 $self->{result_values}->{prct_active} = ($self->{result_values}->{total} != 0) ? $self->{result_values}->{active} * 100 / $self->{result_values}->{total} : 0;
89
90 return 0;
91 }
92
93 sub custom_usage_perfdata {
94 my ($self, %options) = @_;
95
96 my $extra_label = '';
97 $extra_label = '_' . $self->{result_values}->{display} if (!defined($options{extra_instance}) || $options{extra_instance} != 0);
98
99 $self->{output}->perfdata_add(
100 label => 'used' . $extra_label,
101 nlabel => $self->{result_values}->{display} . '#exchange.mailboxes.usage.bytes',
102 unit => 'B',
103 value => $self->{result_values}->{used},
104 min => 0
105 );
106 }
107
108 sub custom_status_output {
109 my ($self, %options) = @_;
110
111 my ($used_value, $used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
112 my ($issue_warning_quota_value, $issue_warning_quota_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{issue_warning_quota});
113 my ($prohibit_send_quota_value, $prohibit_send_quota_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{prohibit_send_quota});
114 my ($prohibit_send_receive_quota_value, $prohibit_send_receive_quota_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{prohibit_send_receive_quota});
115
116 return sprintf(
117 "Used: %s Issue Warning Quota: %s Prohibit Send Quota: %s Prohibit Send/Receive Quota: %s",
118 $used_value . " " . $used_unit,
119 $issue_warning_quota_value . " " . $issue_warning_quota_unit,
120 $prohibit_send_quota_value . " " . $prohibit_send_quota_unit,
121 $prohibit_send_receive_quota_value . " " . $prohibit_send_receive_quota_unit
122 );
123 }
124
125 sub custom_status_calc {
126 my ($self, %options) = @_;
127
128 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_name'};
129 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_storage_used'};
130 $self->{result_values}->{issue_warning_quota} = $options{new_datas}->{$self->{instance} . '_issue_warning_quota'};
131 $self->{result_values}->{prohibit_send_quota} = $options{new_datas}->{$self->{instance} . '_prohibit_send_quota'};
132 $self->{result_values}->{prohibit_send_receive_quota} = $options{new_datas}->{$self->{instance} . '_prohibit_send_receive_quota'};
133 return 0;
134 }
135
136 sub prefix_global_output {
137 my ($self, %options) = @_;
138
139 return "Total ";
140 }
141
142 sub prefix_mailbox_output {
143 my ($self, %options) = @_;
144
145 return "Mailbox '" . $options{instance_value}->{name} . "' ";
146 }
147
148 sub set_counters {
149 my ($self, %options) = @_;
150
151 $self->{maps_counters_type} = [
152 { name => 'active', type => 0 },
153 { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' },
154 { name => 'mailboxes', type => 1, cb_prefix_output => 'prefix_mailbox_output', message_multiple => 'All mailboxes usage are ok' },
155 ];
156
157 $self->{maps_counters}->{active} = [
158 { label => 'active-mailboxes', set => {
159 key_values => [ { name => 'active' }, { name => 'total' }, { name => 'report_date' } ],
160 closure_custom_calc => $self->can('custom_active_calc'),
161 closure_custom_output => $self->can('custom_active_output'),
162 closure_custom_threshold_check => $self->can('custom_active_threshold'),
163 closure_custom_perfdata => $self->can('custom_active_perfdata')
164 }
165 },
166 ];
167 $self->{maps_counters}->{global} = [
168 { label => 'total-usage-active', nlabel => 'exchange.mailboxes.active.usage.total.bytes', set => {
169 key_values => [ { name => 'storage_used_active' } ],
170 output_template => 'Usage (active mailboxes): %s %s',
171 output_change_bytes => 1,
172 perfdatas => [
173 { label => 'total_usage_active', value => 'storage_used_active', template => '%d',
174 min => 0, unit => 'B' },
175 ],
176 }
177 },
178 { label => 'total-usage-inactive', nlabel => 'exchange.mailboxes.inactive.usage.total.bytes', set => {
179 key_values => [ { name => 'storage_used_inactive' } ],
180 output_template => 'Usage (inactive mailboxes): %s %s',
181 output_change_bytes => 1,
182 perfdatas => [
183 { label => 'total_usage_inactive', template => '%d', min => 0, unit => 'B' }
184 ]
185 }
186 }
187 ];
188
189 $self->{maps_counters}->{mailboxes} = [
190 {
191 label => 'status',
192 type => 2,
193 warning_default => '%{used} > %{issue_warning_quota}',
194 critical_default => '%{used} > %{prohibit_send_quota}',
195 set => {
196 key_values => [
197 { name => 'storage_used' }, { name => 'issue_warning_quota' },
198 { name => 'prohibit_send_quota' }, { name => 'prohibit_send_receive_quota' },
199 { name => 'name' }
200 ],
201 closure_custom_calc => $self->can('custom_status_calc'),
202 closure_custom_output => $self->can('custom_status_output'),
203 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
204 closure_custom_threshold_check => \&catalog_status_threshold_ng
205 }
206 },
207 { label => 'items', nlabel => 'exchange.mailboxes.items.count', set => {
208 key_values => [ { name => 'items' }, { name => 'name' } ],
209 output_template => 'Items: %d',
210 perfdatas => [
211 { label => 'items', template => '%d',
212 min => 0, label_extra_instance => 1, instance_use => 'name' }
213 ]
214 }
215 }
216 ];
217 }
218
219 sub new {
220 my ($class, %options) = @_;
221 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
222 bless $self, $class;
223
224 $options{options}->add_options(arguments => {
225 'filter-mailbox:s' => { name => 'filter_mailbox' },
226 'units:s' => { name => 'units', default => '%' },
227 'filter-counters:s' => { name => 'filter_counters', default => 'active|total' }
228 });
229
230 return $self;
231 }
232
233 sub manage_selection {
234 my ($self, %options) = @_;
235
236 $self->{active} = { active => 0, total => 0, report_date => '' };
237 $self->{global} = { storage_used_active => 0, storage_used_inactive => 0 };
238 $self->{mailboxes} = {};
239
240 my $results = $options{custom}->office_get_exchange_mailbox_usage(param => "period='D7'");
241 my $results_daily = [];
242 if (scalar(@{$results})) {
243 $self->{active}->{report_date} = @{$results}[0]->{'Report Refresh Date'};
244 #$results_daily = $options{custom}->office_get_exchange_mailbox_usage(param => "date=" . $self->{active}->{report_date});
245 }
246
247 foreach my $mailbox (@{$results}, @{$results_daily}) {
248 if (defined($self->{option_results}->{filter_mailbox}) && $self->{option_results}->{filter_mailbox} ne '' &&
249 $mailbox->{'User Principal Name'} !~ /$self->{option_results}->{filter_mailbox}/) {
250 $self->{output}->output_add(long_msg => "skipping '" . $mailbox->{'User Principal Name'} . "': no matching filter name.", debug => 1);
251 next;
252 }
253
254 $self->{active}->{total}++;
255
256 if (!defined($mailbox->{'Last Activity Date'}) || ($mailbox->{'Last Activity Date'} ne $self->{active}->{report_date})) {
257 $self->{global}->{storage_used_inactive} += ($mailbox->{'Storage Used (Byte)'} ne '') ? $mailbox->{'Storage Used (Byte)'} : 0;
258 $self->{output}->output_add(long_msg => "skipping '" . $mailbox->{'User Principal Name'} . "': no activity.", debug => 1);
259 next;
260 }
261
262 $self->{active}->{active}++;
263
264 $self->{global}->{storage_used_active} += ($mailbox->{'Storage Used (Byte)'} ne '') ? $mailbox->{'Storage Used (Byte)'} : 0;
265
266 $self->{mailboxes}->{$mailbox->{'User Principal Name'}}->{name} = $mailbox->{'User Principal Name'};
267 $self->{mailboxes}->{$mailbox->{'User Principal Name'}}->{storage_used} = ($mailbox->{'Storage Used (Byte)'} ne '') ? $mailbox->{'Storage Used (Byte)'} : 0;
268 $self->{mailboxes}->{$mailbox->{'User Principal Name'}}->{issue_warning_quota} = $mailbox->{'Issue Warning Quota (Byte)'};
269 $self->{mailboxes}->{$mailbox->{'User Principal Name'}}->{prohibit_send_quota} = $mailbox->{'Prohibit Send Quota (Byte)'};
270 $self->{mailboxes}->{$mailbox->{'User Principal Name'}}->{prohibit_send_receive_quota} = $mailbox->{'Prohibit Send/Receive Quota (Byte)'};
271 $self->{mailboxes}->{$mailbox->{'User Principal Name'}}->{items} = ($mailbox->{'Item Count'} ne '') ? $mailbox->{'Item Count'} : 0;
272 }
273 }
274
275 1;
276
277 __END__
278
279 =head1 MODE
280
281 Check mailbox usage (reporting period over the last refreshed day).
282
283 (see link for details about metrics :
284 https://docs.microsoft.com/en-us/office365/admin/activity-reports/mailbox-usage?view=o365-worldwide)
285
286 =over 8
287
288 =item B<--filter-mailbox>
289
290 Filter mailboxes.
291
292 =item B<--warning-*>
293
294 Warning threshold.
295 Can be: 'active-mailboxes', 'total-usage-active' (count),
296 'total-usage-inactive' (count).
297
298 =item B<--critical-*>
299
300 Critical threshold.
301 Can be: 'active-mailboxes', 'total-usage-active' (count),
302 'total-usage-inactive' (count).
303
304 =item B<--warning-status>
305
306 Define the conditions to match for the status to be WARNING (default: '%{used} > %{issue_warning_quota}').
307 You can use the following variables: %{used}, %{issue_warning_quota},
308 %{prohibit_send_quota}, %{prohibit_send_receive_quota}
309
310 =item B<--critical-status>
311
312 Define the conditions to match for the status to be CRITICAL (default: '%{used} > %{prohibit_send_quota}').
313 You can use the following variables: %{used}, %{issue_warning_quota},
314 %{prohibit_send_quota}, %{prohibit_send_receive_quota}
315
316 =item B<--filter-counters>
317
318 Only display some counters (regexp can be used).
319 Example to hide per user counters: --filter-counters='active|total'
320 (default: 'active|total')
321
322 =item B<--units>
323
324 Unit of thresholds (default: '%') ('%', 'count').
325
326 =back
327
328 =cut