| 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::siteusage; |
| 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_sites', nlabel => 'onedrive.sites.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 => 'sites', 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 |
|
| 69 |
sub custom_active_output { |
| 70 |
my ($self, %options) = @_; |
| 71 |
|
| 72 |
return sprintf( |
| 73 |
"Active sites on %s : %d/%d (%.2f%%)", |
| 74 |
$self->{result_values}->{report_date}, |
| 75 |
$self->{result_values}->{active}, |
| 76 |
$self->{result_values}->{total}, |
| 77 |
$self->{result_values}->{prct_active} |
| 78 |
); |
| 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 custom_usage_perfdata { |
| 93 |
my ($self, %options) = @_; |
| 94 |
|
| 95 |
my $extra_label = ''; |
| 96 |
$extra_label = '_' . $self->{result_values}->{display} if (!defined($options{extra_instance}) || $options{extra_instance} != 0); |
| 97 |
|
| 98 |
$self->{output}->perfdata_add( |
| 99 |
label => 'used' . $extra_label, |
| 100 |
nlabel => $self->{result_values}->{display} . '#onedrive.sites.usage.bytes', |
| 101 |
unit => 'B', |
| 102 |
value => $self->{result_values}->{used}, |
| 103 |
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1), |
| 104 |
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1), |
| 105 |
min => 0, max => $self->{result_values}->{total} |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
sub custom_usage_threshold { |
| 110 |
my ($self, %options) = @_; |
| 111 |
|
| 112 |
my ($exit, $threshold_value); |
| 113 |
$threshold_value = $self->{result_values}->{used}; |
| 114 |
$threshold_value = $self->{result_values}->{free} if (defined($self->{instance_mode}->{option_results}->{free})); |
| 115 |
if ($self->{instance_mode}->{option_results}->{units} eq '%') { |
| 116 |
$threshold_value = $self->{result_values}->{prct_used}; |
| 117 |
$threshold_value = $self->{result_values}->{prct_free} if (defined($self->{instance_mode}->{option_results}->{free})); |
| 118 |
} |
| 119 |
$exit = $self->{perfdata}->threshold_check( |
| 120 |
value => $threshold_value, |
| 121 |
threshold => [ |
| 122 |
{ label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, |
| 123 |
{ label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } |
| 124 |
] |
| 125 |
); |
| 126 |
return $exit; |
| 127 |
} |
| 128 |
|
| 129 |
sub custom_usage_output { |
| 130 |
my ($self, %options) = @_; |
| 131 |
|
| 132 |
my ($used_value, $used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}); |
| 133 |
my ($free_value, $free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}); |
| 134 |
my ($total_value, $total_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total}); |
| 135 |
|
| 136 |
return sprintf( |
| 137 |
"Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", |
| 138 |
$total_value . " " . $total_unit, |
| 139 |
$used_value . " " . $used_unit, $self->{result_values}->{prct_used}, |
| 140 |
$free_value . " " . $free_unit, $self->{result_values}->{prct_free} |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
sub custom_usage_calc { |
| 145 |
my ($self, %options) = @_; |
| 146 |
|
| 147 |
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_url'}; |
| 148 |
$self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_storage_allocated'}; |
| 149 |
$self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_storage_used'}; |
| 150 |
|
| 151 |
if ($self->{result_values}->{total} != 0) { |
| 152 |
$self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used}; |
| 153 |
$self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total}; |
| 154 |
$self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used}; |
| 155 |
} else { |
| 156 |
$self->{result_values}->{free} = '0'; |
| 157 |
$self->{result_values}->{prct_used} = '0'; |
| 158 |
$self->{result_values}->{prct_free} = '0'; |
| 159 |
} |
| 160 |
|
| 161 |
return 0; |
| 162 |
} |
| 163 |
|
| 164 |
sub prefix_global_output { |
| 165 |
my ($self, %options) = @_; |
| 166 |
|
| 167 |
return "Total "; |
| 168 |
} |
| 169 |
|
| 170 |
sub prefix_site_output { |
| 171 |
my ($self, %options) = @_; |
| 172 |
|
| 173 |
return "Site '" . $options{instance_value}->{url} . "' [Owner: " . $options{instance_value}->{owner} . "] "; |
| 174 |
} |
| 175 |
|
| 176 |
sub set_counters { |
| 177 |
my ($self, %options) = @_; |
| 178 |
|
| 179 |
$self->{maps_counters_type} = [ |
| 180 |
{ name => 'active', type => 0 }, |
| 181 |
{ name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' }, |
| 182 |
{ name => 'sites', type => 1, cb_prefix_output => 'prefix_site_output', message_multiple => 'All sites usage are ok' } |
| 183 |
]; |
| 184 |
|
| 185 |
$self->{maps_counters}->{active} = [ |
| 186 |
{ label => 'active-sites', set => { |
| 187 |
key_values => [ { name => 'active' }, { name => 'total' }, { name => 'report_date' } ], |
| 188 |
closure_custom_calc => $self->can('custom_active_calc'), |
| 189 |
closure_custom_output => $self->can('custom_active_output'), |
| 190 |
closure_custom_threshold_check => $self->can('custom_active_threshold'), |
| 191 |
closure_custom_perfdata => $self->can('custom_active_perfdata') |
| 192 |
} |
| 193 |
} |
| 194 |
]; |
| 195 |
|
| 196 |
$self->{maps_counters}->{global} = [ |
| 197 |
{ label => 'total-usage-active', nlabel => 'onedrive.sites.active.usage.total.bytes', set => { |
| 198 |
key_values => [ { name => 'storage_used_active' } ], |
| 199 |
output_template => 'Usage (active sites): %s %s', |
| 200 |
output_change_bytes => 1, |
| 201 |
perfdatas => [ |
| 202 |
{ label => 'total_usage_active', template => '%d', |
| 203 |
min => 0, unit => 'B' } |
| 204 |
] |
| 205 |
} |
| 206 |
}, |
| 207 |
{ label => 'total-usage-inactive', nlabel => 'onedrive.sites.inactive.usage.total.bytes', set => { |
| 208 |
key_values => [ { name => 'storage_used_inactive' } ], |
| 209 |
output_template => 'Usage (inactive sites): %s %s', |
| 210 |
output_change_bytes => 1, |
| 211 |
perfdatas => [ |
| 212 |
{ label => 'total_usage_inactive', template => '%d', |
| 213 |
min => 0, unit => 'B' } |
| 214 |
] |
| 215 |
} |
| 216 |
}, |
| 217 |
{ label => 'total-file-count-active', nlabel => 'onedrive.sites.active.files.total.count', set => { |
| 218 |
key_values => [ { name => 'file_count_active' } ], |
| 219 |
output_template => 'File Count (active sites): %d', |
| 220 |
perfdatas => [ |
| 221 |
{ label => 'total_file_count_active', template => '%d', |
| 222 |
min => 0 } |
| 223 |
] |
| 224 |
} |
| 225 |
}, |
| 226 |
{ label => 'total-file-count-inactive', nlabel => 'onedrive.sites.inactive.files.total.count', set => { |
| 227 |
key_values => [ { name => 'file_count_inactive' } ], |
| 228 |
output_template => 'File Count (inactive sites): %d', |
| 229 |
perfdatas => [ |
| 230 |
{ label => 'total_file_count_inactive', template => '%d', |
| 231 |
min => 0 } |
| 232 |
] |
| 233 |
} |
| 234 |
}, |
| 235 |
{ label => 'total-active-file-count', nlabel => 'onedrive.sites.files.active.total.count', set => { |
| 236 |
key_values => [ { name => 'active_file_count' } ], |
| 237 |
output_template => 'Active File Count (active sites): %d', |
| 238 |
perfdatas => [ |
| 239 |
{ label => 'total_active_file_count', template => '%d', |
| 240 |
min => 0 } |
| 241 |
] |
| 242 |
} |
| 243 |
} |
| 244 |
]; |
| 245 |
|
| 246 |
$self->{maps_counters}->{sites} = [ |
| 247 |
{ label => 'usage', set => { |
| 248 |
key_values => [ { name => 'storage_used' }, { name => 'storage_allocated' }, { name => 'url' }, { name => 'owner' } ], |
| 249 |
closure_custom_calc => $self->can('custom_usage_calc'), |
| 250 |
closure_custom_output => $self->can('custom_usage_output'), |
| 251 |
closure_custom_perfdata => $self->can('custom_usage_perfdata'), |
| 252 |
closure_custom_threshold_check => $self->can('custom_usage_threshold') |
| 253 |
} |
| 254 |
}, |
| 255 |
{ label => 'file-count', nlabel => 'onedrive.sites.files.count', set => { |
| 256 |
key_values => [ { name => 'file_count' }, { name => 'url' }, { name => 'owner' } ], |
| 257 |
output_template => 'File Count: %d', |
| 258 |
perfdatas => [ |
| 259 |
{ label => 'file_count', template => '%d', |
| 260 |
min => 0, label_extra_instance => 1, instance_use => 'url' } |
| 261 |
] |
| 262 |
} |
| 263 |
}, |
| 264 |
{ label => 'active-file-count', nlabel => 'onedrive.sites.files.active.count', set => { |
| 265 |
key_values => [ { name => 'active_file_count' }, { name => 'url' }, { name => 'owner' } ], |
| 266 |
output_template => 'Active File Count: %d', |
| 267 |
perfdatas => [ |
| 268 |
{ label => 'active_file_count', template => '%d', |
| 269 |
min => 0, label_extra_instance => 1, instance_use => 'url' } |
| 270 |
] |
| 271 |
} |
| 272 |
} |
| 273 |
]; |
| 274 |
} |
| 275 |
|
| 276 |
sub new { |
| 277 |
my ($class, %options) = @_; |
| 278 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options); |
| 279 |
bless $self, $class; |
| 280 |
|
| 281 |
$options{options}->add_options(arguments => { |
| 282 |
"filter-url:s" => { name => 'filter_url' }, |
| 283 |
"filter-owner:s" => { name => 'filter_owner' }, |
| 284 |
"units:s" => { name => 'units', default => '%' }, |
| 285 |
"free" => { name => 'free' }, |
| 286 |
"filter-counters:s" => { name => 'filter_counters', default => 'active-sites|total' } |
| 287 |
}); |
| 288 |
|
| 289 |
return $self; |
| 290 |
} |
| 291 |
|
| 292 |
sub manage_selection { |
| 293 |
my ($self, %options) = @_; |
| 294 |
|
| 295 |
$self->{active} = { active => 0, total => 0, report_date => '' }; |
| 296 |
$self->{global} = { |
| 297 |
storage_used_active => 0, storage_used_inactive => 0, |
| 298 |
file_count_active => 0, file_count_inactive => 0, active_file_count => 0 |
| 299 |
}; |
| 300 |
$self->{sites} = {}; |
| 301 |
|
| 302 |
my $results = $options{custom}->office_get_onedrive_usage(param => "period='D7'"); |
| 303 |
my $results_daily = []; |
| 304 |
if (scalar(@{$results})) { |
| 305 |
$self->{active}->{report_date} = @{$results}[0]->{'Report Refresh Date'}; |
| 306 |
$results_daily = $options{custom}->office_get_onedrive_usage(param => "date=" . $self->{active}->{report_date}); |
| 307 |
} |
| 308 |
|
| 309 |
foreach my $site (@{$results}, @{$results_daily}) { |
| 310 |
if (defined($self->{option_results}->{filter_url}) && $self->{option_results}->{filter_url} ne '' && |
| 311 |
$site->{'Site URL'} !~ /$self->{option_results}->{filter_url}/) { |
| 312 |
$self->{output}->output_add(long_msg => "skipping '" . $site->{'Site URL'} . "': no matching filter name.", debug => 1); |
| 313 |
next; |
| 314 |
} |
| 315 |
if (defined($self->{option_results}->{filter_owner}) && $self->{option_results}->{filter_owner} ne '' && |
| 316 |
$site->{'Owner Display Name'} !~ /$self->{option_results}->{filter_owner}/) { |
| 317 |
$self->{output}->output_add(long_msg => "skipping '" . $site->{'Owner Display Name'} . "': no matching filter name.", debug => 1); |
| 318 |
next; |
| 319 |
} |
| 320 |
|
| 321 |
if ($site->{'Report Period'} != 1) { |
| 322 |
if (!defined($site->{'Last Activity Date'}) || ($site->{'Last Activity Date'} ne $self->{active}->{report_date})) { |
| 323 |
$self->{global}->{storage_used_inactive} += ($site->{'Storage Used (Byte)'} ne '') ? $site->{'Storage Used (Byte)'} : 0; |
| 324 |
$self->{global}->{file_count_inactive} += ($site->{'File Count'} ne '') ? $site->{'File Count'} : 0; |
| 325 |
$self->{output}->output_add(long_msg => "skipping '" . $site->{'Site URL'} . "': no activity.", debug => 1); |
| 326 |
} |
| 327 |
$self->{active}->{total}++; |
| 328 |
next; |
| 329 |
} |
| 330 |
|
| 331 |
$self->{active}->{active}++; |
| 332 |
|
| 333 |
$self->{global}->{storage_used_active} += ($site->{'Storage Used (Byte)'} ne '') ? $site->{'Storage Used (Byte)'} : 0; |
| 334 |
$self->{global}->{file_count_active} += ($site->{'File Count'} ne '') ? $site->{'File Count'} : 0; |
| 335 |
$self->{global}->{active_file_count} += ($site->{'Active File Count'} ne '') ? $site->{'Active File Count'} : 0; |
| 336 |
|
| 337 |
$self->{sites}->{$site->{'Site URL'}}->{url} = $site->{'Site URL'}; |
| 338 |
$self->{sites}->{$site->{'Site URL'}}->{owner} = $site->{'Owner Display Name'}; |
| 339 |
$self->{sites}->{$site->{'Site URL'}}->{file_count} = $site->{'File Count'}; |
| 340 |
$self->{sites}->{$site->{'Site URL'}}->{active_file_count} = $site->{'Active File Count'}; |
| 341 |
$self->{sites}->{$site->{'Site URL'}}->{storage_used} = $site->{'Storage Used (Byte)'}; |
| 342 |
$self->{sites}->{$site->{'Site URL'}}->{storage_allocated} = $site->{'Storage Allocated (Byte)'}; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
1; |
| 347 |
|
| 348 |
__END__ |
| 349 |
|
| 350 |
=head1 MODE |
| 351 |
|
| 352 |
Check sites usage (reporting period over the last refreshed day). |
| 353 |
|
| 354 |
(see link for details about metrics : |
| 355 |
https://docs.microsoft.com/en-us/microsoft-365/admin/activity-reports/onedrive-for-business-usage?view=o365-worldwide) |
| 356 |
|
| 357 |
=over 8 |
| 358 |
|
| 359 |
=item B<--filter-*> |
| 360 |
|
| 361 |
Filter sites. |
| 362 |
Can be: 'url', 'owner' (can be a regexp). |
| 363 |
|
| 364 |
=item B<--warning-*> |
| 365 |
|
| 366 |
Warning threshold. |
| 367 |
Can be: 'active-sites', |
| 368 |
'total-usage-active' (count), 'total-usage-inactive' (count), |
| 369 |
'total-file-count-active' (count), 'total-file-count-inactive' (count), |
| 370 |
'total-active-file-count' (count), |
| 371 |
'usage' (count), 'file-count' (count), 'active-file-count' (count). |
| 372 |
|
| 373 |
=item B<--critical-*> |
| 374 |
|
| 375 |
Critical threshold. |
| 376 |
Can be: 'active-sites', |
| 377 |
'total-usage-active' (count), 'total-usage-inactive' (count), |
| 378 |
'total-file-count-active' (count), 'total-file-count-inactive' (count), |
| 379 |
'total-active-file-count' (count), |
| 380 |
'usage' (count), 'file-count' (count), 'active-file-count' (count). |
| 381 |
|
| 382 |
=item B<--filter-counters> |
| 383 |
|
| 384 |
Only display some counters (regexp can be used). |
| 385 |
Example to hide per user counters: --filter-counters='active-sites|total' |
| 386 |
(default: 'active-sites|total') |
| 387 |
|
| 388 |
=item B<--units> |
| 389 |
|
| 390 |
Unit of thresholds (default: '%') ('%', 'count'). |
| 391 |
|
| 392 |
=back |
| 393 |
|
| 394 |
=cut |