Debian Codegraph @

src/cloud/microsoft/office365/teams/mode/devicesusage.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::teams::mode::devicesusage;
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_devices', nlabel => 'teams.devices.active.count',
44 value => $self->{result_values}->{active},
45 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options),
46 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options),
47 unit => 'devices', min => 0, max => $self->{result_values}->{total}
48 );
49 }
50
51 sub custom_active_threshold {
52 my ($self, %options) = @_;
53
54 my $threshold_value = $self->{result_values}->{active};
55 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
56 $threshold_value = $self->{result_values}->{prct_active};
57 }
58 my $exit = $self->{perfdata}->threshold_check(
59 value => $threshold_value,
60 threshold => [
61 { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' },
62 { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' }
63 ]
64 );
65 return $exit;
66 }
67
68 sub custom_active_output {
69 my ($self, %options) = @_;
70
71 my $msg = sprintf(
72 "Active devices on %s : %d/%d (%.2f%%)",
73 $self->{result_values}->{report_date},
74 $self->{result_values}->{active},
75 $self->{result_values}->{total},
76 $self->{result_values}->{prct_active}
77 );
78 return $msg;
79 }
80
81 sub custom_active_calc {
82 my ($self, %options) = @_;
83
84 $self->{result_values}->{active} = $options{new_datas}->{$self->{instance} . '_active'};
85 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
86 $self->{result_values}->{report_date} = $options{new_datas}->{$self->{instance} . '_report_date'};
87 $self->{result_values}->{prct_active} = ($self->{result_values}->{total} != 0) ? $self->{result_values}->{active} * 100 / $self->{result_values}->{total} : 0;
88
89 return 0;
90 }
91
92 sub prefix_global_output {
93 my ($self, %options) = @_;
94
95 return "Users count by device type : ";
96 }
97
98 sub set_counters {
99 my ($self, %options) = @_;
100
101 $self->{maps_counters_type} = [
102 { name => 'active', type => 0 },
103 { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' },
104 ];
105
106 $self->{maps_counters}->{active} = [
107 { label => 'active-devices', set => {
108 key_values => [ { name => 'active' }, { name => 'total' }, { name => 'report_date' } ],
109 closure_custom_calc => $self->can('custom_active_calc'),
110 closure_custom_output => $self->can('custom_active_output'),
111 closure_custom_threshold_check => $self->can('custom_active_threshold'),
112 closure_custom_perfdata => $self->can('custom_active_perfdata')
113 }
114 }
115 ];
116
117 $self->{maps_counters}->{global} = [
118 { label => 'windows', nlabel => 'teams.devices.windows.count', set => {
119 key_values => [ { name => 'windows' } ],
120 output_template => 'Windows: %d',
121 perfdatas => [
122 { label => 'windows', template => '%d', min => 0 }
123 ]
124 }
125 },
126 { label => 'mac', nlabel => 'teams.devices.mac.count', set => {
127 key_values => [ { name => 'mac' } ],
128 output_template => 'Mac: %d',
129 perfdatas => [
130 { label => 'mac', template => '%d', min => 0 }
131 ]
132 }
133 },
134 { label => 'web', nlabel => 'teams.devices.web.count', set => {
135 key_values => [ { name => 'web' } ],
136 output_template => 'Web: %d',
137 perfdatas => [
138 { label => 'web', template => '%d', min => 0 }
139 ]
140 }
141 },
142 { label => 'ios', nlabel => 'teams.devices.ios.count', set => {
143 key_values => [ { name => 'ios' } ],
144 output_template => 'iOS: %d',
145 perfdatas => [
146 { label => 'ios', template => '%d', min => 0 }
147 ]
148 }
149 },
150 { label => 'android-phone', nlabel => 'teams.devices.android.count', set => {
151 key_values => [ { name => 'android_phone' } ],
152 output_template => 'Android Phone: %d',
153 perfdatas => [
154 { label => 'android_phone', template => '%d', min => 0 }
155 ]
156 }
157 },
158 { label => 'windows-phone', nlabel => 'teams.devices.windowsphone.count', set => {
159 key_values => [ { name => 'windows_phone' } ],
160 output_template => 'Windows Phone: %d',
161 perfdatas => [
162 { label => 'windows_phone', template => '%d', min => 0 }
163 ]
164 }
165 }
166 ];
167 }
168
169 sub new {
170 my ($class, %options) = @_;
171 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
172 bless $self, $class;
173
174 $options{options}->add_options(arguments => {
175 "filter-user:s" => { name => 'filter_user' },
176 "units:s" => { name => 'units', default => '%' },
177 });
178
179 return $self;
180 }
181
182 sub manage_selection {
183 my ($self, %options) = @_;
184
185 $self->{active} = { active => 0, total => 0, report_date => '' };
186 $self->{global} = { windows => 0, mac => 0, web => 0, ios => 0, android_phone => 0, windows_phone => 0 };
187
188 my $results = $options{custom}->office_get_teams_device_usage(param => "period='D7'");
189 my $results_daily = [];
190 if (scalar(@{$results})) {
191 $self->{active}->{report_date} = @{$results}[0]->{'Report Refresh Date'};
192 $results_daily = $options{custom}->office_get_teams_device_usage(param => "date=" . $self->{active}->{report_date});
193 }
194
195 foreach my $user (@{$results}, @{$results_daily}) {
196 if (defined($self->{option_results}->{filter_user}) && $self->{option_results}->{filter_user} ne '' &&
197 $user->{'User Principal Name'} !~ /$self->{option_results}->{filter_user}/) {
198 $self->{output}->output_add(long_msg => "skipping '" . $user->{'User Principal Name'} . "': no matching filter name.", debug => 1);
199 next;
200 }
201
202 my $used_devices = 0;
203 $used_devices++ if ($user->{'Used Windows'} =~ /Yes/);
204 $used_devices++ if ($user->{'Used Mac'} =~ /Yes/);
205 $used_devices++ if ($user->{'Used Web'} =~ /Yes/);
206 $used_devices++ if ($user->{'Used iOS'} =~ /Yes/);
207 $used_devices++ if ($user->{'Used Android Phone'} =~ /Yes/);
208 $used_devices++ if ($user->{'Used Windows Phone'} =~ /Yes/);
209
210 if ($user->{'Report Period'} != 1) {
211 if (!defined($user->{'Last Activity Date'}) || ($user->{'Last Activity Date'} ne $self->{active}->{report_date})) {
212 $self->{output}->output_add(long_msg => "skipping '" . $user->{'User Principal Name'} . "': no activity.", debug => 1);
213 }
214 $self->{active}->{total} += $used_devices;
215 next;
216 }
217
218 $self->{active}->{active} += $used_devices;
219
220 $self->{global}->{windows}++ if ($user->{'Used Windows'} =~ /Yes/);
221 $self->{global}->{mac}++ if ($user->{'Used Mac'} =~ /Yes/);
222 $self->{global}->{web}++ if ($user->{'Used Web'} =~ /Yes/);
223 $self->{global}->{ios}++ if ($user->{'Used iOS'} =~ /Yes/);
224 $self->{global}->{android_phone}++ if ($user->{'Used Android Phone'} =~ /Yes/);
225 $self->{global}->{windows_phone}++ if ($user->{'Used Windows Phone'} =~ /Yes/);
226 }
227 }
228
229 1;
230
231 __END__
232
233 =head1 MODE
234
235 Check devices usage (reporting period over the last refreshed day).
236
237 (see link for details about metrics :
238 https://docs.microsoft.com/en-us/office365/admin/activity-reports/microsoft-teams-device-usage?view=o365-worldwide)
239
240 =over 8
241
242 =item B<--filter-user>
243
244 Filter users.
245
246 =item B<--warning-*>
247
248 Warning threshold.
249 Can be: 'active-devices', 'windows' (count), 'mac' (count),
250 'web' (count), 'ios' (count), 'android-phone' (count),
251 'windows-phone' (count).
252
253 =item B<--critical-*>
254
255 Critical threshold.
256 Can be: 'active-devices', 'windows' (count), 'mac' (count),
257 'web' (count), 'ios' (count), 'android-phone' (count),
258 'windows-phone' (count).
259
260 =item B<--filter-counters>
261
262 Only display some counters (regexp can be used).
263 Example to hide per user counters: --filter-counters='windows'
264
265 =item B<--units>
266
267 Unit of thresholds (default: '%') ('%', 'count').
268
269 =back
270
271 =cut