Debian Codegraph @

src/cloud/microsoft/office365/onedrive/mode/usersactivity.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::onedrive::mode::usersactivity;
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 => 'onedrive.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 sites) ";
97 }
98
99 sub prefix_user_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_user_output', message_multiple => 'All users 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-viewed-edited-file-count', nlabel => 'onedrive.users.files.viewed.total.count', set => {
127 key_values => [ { name => 'viewed_edited_file_count' } ],
128 output_template => 'Viewed or Edited File Count: %d',
129 perfdatas => [
130 { label => 'total_viewed_edited_file_count', template => '%d',
131 min => 0 }
132 ]
133 }
134 },
135 { label => 'total-synced-file-count', nlabel => 'onedrive.users.files.synced.total.count', set => {
136 key_values => [ { name => 'synced_file_count' } ],
137 output_template => 'Synced File Count: %d',
138 perfdatas => [
139 { label => 'total_synced_file_count', template => '%d',
140 min => 0 }
141 ]
142 }
143 },
144 { label => 'total-shared-int-file-count', nlabel => 'onedrive.users.files.shared.internally.total.count', set => {
145 key_values => [ { name => 'shared_int_file_count' } ],
146 output_template => 'Shared Internally File Count: %d',
147 perfdatas => [
148 { label => 'total_shared_int_file_count', template => '%d',
149 min => 0 }
150 ]
151 }
152 },
153 { label => 'total-shared-ext-file-count', nlabel => 'onedrive.users.files.shared.externally.total.count', set => {
154 key_values => [ { name => 'shared_ext_file_count' } ],
155 output_template => 'Shared Externally File Count: %d',
156 perfdatas => [
157 { label => 'total_shared_ext_file_count', template => '%d',
158 min => 0 }
159 ]
160 }
161 }
162 ];
163
164 $self->{maps_counters}->{users} = [
165 { label => 'viewed-edited-file-count', nlabel => 'onedrive.users.files.viewed.count', set => {
166 key_values => [ { name => 'viewed_edited_file_count' }, { name => 'name' } ],
167 output_template => 'Viewed or Edited File Count: %d',
168 perfdatas => [
169 { label => 'viewed_edited_file_count', template => '%d',
170 min => 0, label_extra_instance => 1, instance_use => 'name' }
171 ]
172 }
173 },
174 { label => 'synced-file-count', nlabel => 'onedrive.users.files.synced.count', set => {
175 key_values => [ { name => 'synced_file_count' }, { name => 'name' } ],
176 output_template => 'Synced File Count: %d',
177 perfdatas => [
178 { label => 'synced_file_count', template => '%d',
179 min => 0, label_extra_instance => 1, instance_use => 'name' }
180 ]
181 }
182 },
183 { label => 'shared-int-file-count', nlabel => 'onedrive.users.files.shared.internally.count', set => {
184 key_values => [ { name => 'shared_int_file_count' }, { name => 'name' } ],
185 output_template => 'Shared Internally File Count: %d',
186 perfdatas => [
187 { label => 'shared_int_file_count', template => '%d',
188 min => 0, label_extra_instance => 1, instance_use => 'name' }
189 ]
190 }
191 },
192 { label => 'shared-ext-file-count', nlabel => 'onedrive.users.files.shared.externally.count', set => {
193 key_values => [ { name => 'shared_ext_file_count' }, { name => 'name' } ],
194 output_template => 'Shared Externally File Count: %d',
195 perfdatas => [
196 { label => 'shared_ext_file_count', template => '%d',
197 min => 0, label_extra_instance => 1, instance_use => 'name' }
198 ]
199 }
200 }
201 ];
202 }
203
204 sub new {
205 my ($class, %options) = @_;
206 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
207 bless $self, $class;
208
209 $options{options}->add_options(arguments => {
210 "filter-user:s" => { name => 'filter_user' },
211 "units:s" => { name => 'units', default => '%' },
212 "filter-counters:s" => { name => 'filter_counters', default => 'active|total' }
213 });
214
215 return $self;
216 }
217
218 sub manage_selection {
219 my ($self, %options) = @_;
220
221 $self->{active} = { active => 0, total => 0, report_date => '' };
222 $self->{global} = {
223 viewed_edited_file_count => 0, synced_file_count => 0,
224 shared_int_file_count => 0, shared_ext_file_count => 0
225 };
226 $self->{users} = {};
227
228 my $results = $options{custom}->office_get_onedrive_activity(param => "period='D7'");
229 my $results_daily = [];
230 if (scalar(@{$results})) {
231 $self->{active}->{report_date} = @{$results}[0]->{'Report Refresh Date'};
232 $results_daily = $options{custom}->office_get_onedrive_activity(param => "date=" . $self->{active}->{report_date});
233 }
234
235 foreach my $user (@{$results}, @{$results_daily}) {
236 if (defined($self->{option_results}->{filter_user}) && $self->{option_results}->{filter_user} ne '' &&
237 $user->{'User Principal Name'} !~ /$self->{option_results}->{filter_user}/) {
238 $self->{output}->output_add(long_msg => "skipping '" . $user->{'User Principal Name'} . "': no matching filter name.", debug => 1);
239 next;
240 }
241
242 if ($user->{'Report Period'} != 1) {
243 if (!defined($user->{'Last Activity Date'}) || ($user->{'Last Activity Date'} ne $self->{active}->{report_date})) {
244 $self->{output}->output_add(long_msg => "skipping '" . $user->{'User Principal Name'} . "': no activity.", debug => 1);
245 }
246 $self->{active}->{total}++;
247 next;
248 }
249
250 $self->{active}->{active}++;
251
252 $self->{global}->{viewed_edited_file_count} += ($user->{'Viewed Or Edited File Count'} ne '') ? $user->{'Viewed Or Edited File Count'} : 0;
253 $self->{global}->{synced_file_count} += ($user->{'Synced File Count'} ne '') ? $user->{'Synced File Count'} : 0;
254 $self->{global}->{shared_int_file_count} += ($user->{'Shared Internally File Count'} ne '') ? $user->{'Shared Internally File Count'} : 0;
255 $self->{global}->{shared_ext_file_count} += ($user->{'Shared Externally File Count'} ne '') ? $user->{'Shared Externally File Count'} : 0;
256
257 $self->{users}->{$user->{'User Principal Name'}}->{name} = $user->{'User Principal Name'};
258 $self->{users}->{$user->{'User Principal Name'}}->{viewed_edited_file_count} = $user->{'Viewed Or Edited File Count'};
259 $self->{users}->{$user->{'User Principal Name'}}->{synced_file_count} = $user->{'Synced File Count'};
260 $self->{users}->{$user->{'User Principal Name'}}->{shared_int_file_count} = $user->{'Shared Internally File Count'};
261 $self->{users}->{$user->{'User Principal Name'}}->{shared_ext_file_count} = $user->{'Shared Externally File Count'};
262 }
263 }
264
265 1;
266
267 __END__
268
269 =head1 MODE
270
271 Check users activity (reporting period over the last refreshed day).
272
273 (see link for details about metrics :
274 https://docs.microsoft.com/en-us/microsoft-365/admin/activity-reports/onedrive-for-business-activity?view=o365-worldwide)
275
276 =over 8
277
278 =item B<--filter-user>
279
280 Filter users.
281
282 =item B<--warning-*>
283
284 Warning threshold.
285 Can be: 'active-users',
286 'total-viewed-edited-file-count' (count), 'total-synced-file-count' (count),
287 'total-shared-int-file-count' (count), 'total-shared-ext-file-count' (count),
288 'viewed-edited-file-count' (count), 'synced-file-count' (count),
289 'shared-int-file-count' (count), 'shared-ext-file-count' (count).
290
291 =item B<--critical-*>
292
293 Critical threshold.
294 Can be: 'active-users',
295 'total-viewed-edited-file-count' (count), 'total-synced-file-count' (count),
296 'total-shared-int-file-count' (count), 'total-shared-ext-file-count' (count),
297 'viewed-edited-file-count' (count), 'synced-file-count' (count),
298 'shared-int-file-count' (count), 'shared-ext-file-count' (count).
299
300 =item B<--filter-counters>
301
302 Only display some counters (regexp can be used).
303 Example to hide per user counters: --filter-counters='active|total'
304 (default: 'active|total')
305
306 =item B<--units>
307
308 Unit of thresholds (default: '%') ('%', 'count').
309
310 =back
311
312 =cut