Debian Codegraph @

src/cloud/microsoft/office365/exchange/mode/emailactivity.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::emailactivity;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use Time::Local;
28
29 sub custom_active_perfdata {
30 my ($self, %options) = @_;
31
32 my %total_options = ();
33 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
34 $total_options{total} = $self->{result_values}->{total};
35 $total_options{cast_int} = 1;
36 }
37
38 if ($self->{result_values}->{report_date} =~ /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/) {
39 $self->{output}->perfdata_add(label => 'perfdate', value => timelocal(0,0,12,$3,$2-1,$1-1900));
40 }
41
42 $self->{output}->perfdata_add(
43 label => 'active_users',
44 nlabel => 'exchange.users.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 => 'users', 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 users 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 prefix_global_output {
94 my ($self, %options) = @_;
95
96 return "Total (active users) ";
97 }
98
99 sub prefix_mailbox_output {
100 my ($self, %options) = @_;
101
102 return "User '" . $options{instance_value}->{name} . "' ";
103 }
104
105 sub set_counters {
106 my ($self, %options) = @_;
107
108 $self->{maps_counters_type} = [
109 { name => 'active', type => 0 },
110 { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' },
111 { name => 'users', type => 1, cb_prefix_output => 'prefix_mailbox_output', message_multiple => 'All email activity are ok' }
112 ];
113
114 $self->{maps_counters}->{active} = [
115 { label => 'active-users', set => {
116 key_values => [ { name => 'active' }, { name => 'total' }, { name => 'report_date' } ],
117 closure_custom_calc => $self->can('custom_active_calc'),
118 closure_custom_output => $self->can('custom_active_output'),
119 closure_custom_threshold_check => $self->can('custom_active_threshold'),
120 closure_custom_perfdata => $self->can('custom_active_perfdata')
121 }
122 },
123 ];
124
125 $self->{maps_counters}->{global} = [
126 { label => 'total-send-count', nlabel => 'exchange.users.emails.sent.total.count', set => {
127 key_values => [ { name => 'send_count' } ],
128 output_template => 'Send Count: %d',
129 perfdatas => [
130 { label => 'total_send_count', template => '%d',
131 min => 0 }
132 ]
133 }
134 },
135 { label => 'total-receive-count', nlabel => 'exchange.users.emails.received.total.count', set => {
136 key_values => [ { name => 'receive_count' } ],
137 output_template => 'Receive Count: %d',
138 perfdatas => [
139 { label => 'total_receive_count', template => '%d',
140 min => 0 }
141 ]
142 }
143 },
144 { label => 'total-read-count', nlabel => 'exchange.users.emails.read.total.count', set => {
145 key_values => [ { name => 'read_count' } ],
146 output_template => 'Read Count: %d',
147 perfdatas => [
148 { label => 'total_read_count', template => '%d',
149 min => 0 }
150 ]
151 }
152 }
153 ];
154
155 $self->{maps_counters}->{users} = [
156 { label => 'send-count', nlabel => 'exchange.users.emails.sent.count', set => {
157 key_values => [ { name => 'send_count' }, { name => 'name' } ],
158 output_template => 'Send Count: %d',
159 perfdatas => [
160 { label => 'send_count', template => '%d',
161 min => 0, label_extra_instance => 1, instance_use => 'name' }
162 ]
163 }
164 },
165 { label => 'receive-count', nlabel => 'exchange.users.emails.received.count', set => {
166 key_values => [ { name => 'receive_count' }, { name => 'name' } ],
167 output_template => 'Receive Count: %d',
168 perfdatas => [
169 { label => 'receive_count', template => '%d',
170 min => 0, label_extra_instance => 1, instance_use => 'name' }
171 ]
172 }
173 },
174 { label => 'read-count', nlabel => 'exchange.users.emails.read.count', set => {
175 key_values => [ { name => 'read_count' }, { name => 'name' } ],
176 output_template => 'Read Count: %d',
177 perfdatas => [
178 { label => 'read_count', template => '%d',
179 min => 0, label_extra_instance => 1, instance_use => 'name' }
180 ]
181 }
182 }
183 ];
184 }
185
186 sub new {
187 my ($class, %options) = @_;
188 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
189 bless $self, $class;
190
191 $options{options}->add_options(arguments => {
192 "filter-user:s" => { name => 'filter_user' },
193 "units:s" => { name => 'units', default => '%' },
194 "filter-counters:s" => { name => 'filter_counters', default => 'active|total' }
195 });
196
197 return $self;
198 }
199
200 sub manage_selection {
201 my ($self, %options) = @_;
202
203 $self->{active} = { active => 0, total => 0, report_date => '' };
204 $self->{global} = { send_count => 0, receive_count => 0 , read_count => 0 };
205 $self->{users} = {};
206
207 my $results = $options{custom}->office_get_exchange_activity(param => "period='D7'");
208 my $results_daily = [];
209 if (scalar(@{$results})) {
210 $self->{active}->{report_date} = @{$results}[0]->{'Report Refresh Date'};
211 $results_daily = $options{custom}->office_get_exchange_activity(param => "date=" . $self->{active}->{report_date});
212 }
213
214 foreach my $user (@{$results}, @{$results_daily}) {
215 if (defined($self->{option_results}->{filter_user}) && $self->{option_results}->{filter_user} ne '' &&
216 $user->{'User Principal Name'} !~ /$self->{option_results}->{filter_user}/) {
217 $self->{output}->output_add(long_msg => "skipping '" . $user->{'User Principal Name'} . "': no matching filter name.", debug => 1);
218 next;
219 }
220
221 if ($user->{'Report Period'} != 1) {
222 if (!defined($user->{'Last Activity Date'}) || ($user->{'Last Activity Date'} ne $self->{active}->{report_date})) {
223 $self->{output}->output_add(long_msg => "skipping '" . $user->{'User Principal Name'} . "': no activity.", debug => 1);
224 }
225 $self->{active}->{total}++;
226 next;
227 }
228
229 $self->{active}->{active}++;
230
231 $self->{global}->{send_count} += $user->{'Send Count'};
232 $self->{global}->{receive_count} += $user->{'Receive Count'};
233 $self->{global}->{read_count} += $user->{'Read Count'};
234
235 $self->{users}->{$user->{'User Principal Name'}}->{name} = $user->{'User Principal Name'};
236 $self->{users}->{$user->{'User Principal Name'}}->{send_count} = $user->{'Send Count'};
237 $self->{users}->{$user->{'User Principal Name'}}->{receive_count} = $user->{'Receive Count'};
238 $self->{users}->{$user->{'User Principal Name'}}->{read_count} = $user->{'Read Count'};
239 }
240 }
241
242 1;
243
244 __END__
245
246 =head1 MODE
247
248 Check email activity (reporting period over the last refreshed day).
249
250 (see link for details about metrics :
251 https://docs.microsoft.com/en-us/office365/admin/activity-reports/email-activity?view=o365-worldwide)
252
253 =over 8
254
255 =item B<--filter-user>
256
257 Filter users.
258
259 =item B<--warning-*>
260
261 Warning threshold.
262 Can be: 'active-users', 'total-send-count' (count),
263 'total-receive-count' (count), 'total-read-count' (count),
264 'send-count' (count), 'receive-count' (count), 'read-count' (count).
265
266 =item B<--critical-*>
267
268 Critical threshold.
269 Can be: 'active-users', 'total-send-count' (count),
270 'total-receive-count' (count), 'total-read-count' (count),
271 'send-count' (count), 'receive-count' (count), 'read-count' (count).
272
273 =item B<--filter-counters>
274
275 Only display some counters (regexp can be used).
276 Example to hide per user counters: --filter-counters='active|total'
277 (default: 'active|total')
278
279 =item B<--units>
280
281 Unit of thresholds (default: '%') ('%', 'count').
282
283 =back
284
285 =cut