Debian Codegraph @

src/cloud/microsoft/office365/skype/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::skype::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',
44 nlabel => 'skype.devices.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 => 'devices', 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 devices 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 "Users count by device type : ";
97 }
98
99 sub set_counters {
100 my ($self, %options) = @_;
101
102 $self->{maps_counters_type} = [
103 { name => 'active', type => 0 },
104 { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' },
105 ];
106
107 $self->{maps_counters}->{active} = [
108 { label => 'active-devices', set => {
109 key_values => [ { name => 'active' }, { name => 'total' }, { name => 'report_date' } ],
110 closure_custom_calc => $self->can('custom_active_calc'),
111 closure_custom_output => $self->can('custom_active_output'),
112 closure_custom_threshold_check => $self->can('custom_active_threshold'),
113 closure_custom_perfdata => $self->can('custom_active_perfdata')
114 }
115 },
116 ];
117
118 $self->{maps_counters}->{global} = [
119 { label => 'windows', nlabel => 'skype.devices.windows.count', set => {
120 key_values => [ { name => 'windows' } ],
121 output_template => 'Windows: %d',
122 perfdatas => [
123 { label => 'windows', template => '%d',
124 min => 0 }
125 ]
126 }
127 },
128 { label => 'ipad', nlabel => 'skype.devices.ipad.count', set => {
129 key_values => [ { name => 'ipad' } ],
130 output_template => 'iPad: %d',
131 perfdatas => [
132 { label => 'ipad', template => '%d',
133 min => 0 }
134 ]
135 }
136 },
137 { label => 'iphone', nlabel => 'skype.devices.iphone.count', set => {
138 key_values => [ { name => 'iphone' } ],
139 output_template => 'iPhone: %d',
140 perfdatas => [
141 { label => 'iphone', template => '%d',
142 min => 0 }
143 ]
144 }
145 },
146 { label => 'android-phone', nlabel => 'skype.devices.android.count', set => {
147 key_values => [ { name => 'android_phone' } ],
148 output_template => 'Android Phone: %d',
149 perfdatas => [
150 { label => 'android_phone', template => '%d',
151 min => 0 }
152 ]
153 }
154 },
155 { label => 'windows-phone', nlabel => 'skype.devices.windowsphone.count', set => {
156 key_values => [ { name => 'windows_phone' } ],
157 output_template => 'Windows Phone: %d',
158 perfdatas => [
159 { label => 'windows_phone', template => '%d',
160 min => 0 }
161 ]
162 }
163 }
164 ];
165 }
166
167 sub new {
168 my ($class, %options) = @_;
169 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
170 bless $self, $class;
171
172 $options{options}->add_options(arguments => {
173 'filter-user:s' => { name => 'filter_user' },
174 'units:s' => { name => 'units', default => '%' }
175 });
176
177 return $self;
178 }
179
180 sub manage_selection {
181 my ($self, %options) = @_;
182
183 $self->{active} = { active => 0, total => 0, report_date => '' };
184 $self->{global} = { windows => 0, ipad => 0, iphone => 0, android_phone => 0, windows_phone => 0 };
185
186 my $results = $options{custom}->office_get_skype_device_usage(param => "period='D7'");
187 my $results_daily = [];
188 if (scalar(@{$results})) {
189 $self->{active}->{report_date} = @{$results}[0]->{'Report Refresh Date'};
190 $results_daily = $options{custom}->office_get_skype_device_usage(param => "date=" . $self->{active}->{report_date});
191 }
192
193 foreach my $user (@{$results}, @{$results_daily}) {
194 if (defined($self->{option_results}->{filter_user}) && $self->{option_results}->{filter_user} ne '' &&
195 $user->{'User Principal Name'} !~ /$self->{option_results}->{filter_user}/) {
196 $self->{output}->output_add(long_msg => "skipping '" . $user->{'User Principal Name'} . "': no matching filter name.", debug => 1);
197 next;
198 }
199
200 my $used_devices = 0;
201 $used_devices++ if ($user->{'Used Windows'} =~ /Yes/);
202 $used_devices++ if ($user->{'Used iPad'} =~ /Yes/);
203 $used_devices++ if ($user->{'Used iPhone'} =~ /Yes/);
204 $used_devices++ if ($user->{'Used Android Phone'} =~ /Yes/);
205 $used_devices++ if ($user->{'Used Windows Phone'} =~ /Yes/);
206
207 if ($user->{'Report Period'} != 1) {
208 if (!defined($user->{'Last Activity Date'}) || ($user->{'Last Activity Date'} ne $self->{active}->{report_date})) {
209 $self->{output}->output_add(long_msg => "skipping '" . $user->{'User Principal Name'} . "': no activity.", debug => 1);
210 }
211 $self->{active}->{total} += $used_devices;
212 next;
213 }
214
215 $self->{active}->{active} += $used_devices;
216
217 $self->{global}->{windows}++ if ($user->{'Used Windows'} =~ /Yes/);
218 $self->{global}->{ipad}++ if ($user->{'Used iPad'} =~ /Yes/);
219 $self->{global}->{iphone}++ if ($user->{'Used iPhone'} =~ /Yes/);
220 $self->{global}->{android_phone}++ if ($user->{'Used Android Phone'} =~ /Yes/);
221 $self->{global}->{windows_phone}++ if ($user->{'Used Windows Phone'} =~ /Yes/);
222 }
223 }
224
225 1;
226
227 __END__
228
229 =head1 MODE
230
231 Check devices usage (reporting period over the last refreshed day).
232
233 (see link for details about metrics :
234 https://docs.microsoft.com/en-us/SkypeForBusiness/skype-for-business-online-reporting/device-usage-report)
235
236 =over 8
237
238 =item B<--filter-user>
239
240 Filter users.
241
242 =item B<--warning-*>
243
244 Warning threshold.
245 Can be: 'active-devices', 'windows' (count), 'ipad' (count),
246 'iphone' (count), 'android-phone' (count),
247 'windows-phone' (count).
248
249 =item B<--critical-*>
250
251 Critical threshold.
252 Can be: 'active-devices', 'windows' (count), 'ipad' (count),
253 'iphone' (count), 'android-phone' (count),
254 'windows-phone' (count).
255
256 =item B<--filter-counters>
257
258 Only display some counters (regexp can be used).
259 Example to hide per user counters: --filter-counters='windows'
260
261 =item B<--units>
262
263 Unit of thresholds (default: '%') ('%', 'count').
264
265 =back
266
267 =cut