From 9766eb23110685301f1b53572ad629f9c1b95ad7 Mon Sep 17 00:00:00 2001 From: Colin Gagnaire Date: Wed, 6 Feb 2019 15:25:39 +0100 Subject: [PATCH] add office 365 skype for business --- cloud/microsoft/office365/custom/graphapi.pm | 84 ++++-- .../office365/skype/mode/devicesusage.pm | 255 ++++++++++++++++ .../office365/skype/mode/usersactivity.pm | 280 ++++++++++++++++++ cloud/microsoft/office365/skype/plugin.pm | 50 ++++ 4 files changed, 651 insertions(+), 18 deletions(-) create mode 100644 cloud/microsoft/office365/skype/mode/devicesusage.pm create mode 100644 cloud/microsoft/office365/skype/mode/usersactivity.pm create mode 100644 cloud/microsoft/office365/skype/plugin.pm diff --git a/cloud/microsoft/office365/custom/graphapi.pm b/cloud/microsoft/office365/custom/graphapi.pm index 7c920a484..a78f0f29c 100644 --- a/cloud/microsoft/office365/custom/graphapi.pm +++ b/cloud/microsoft/office365/custom/graphapi.pm @@ -186,7 +186,7 @@ sub get_access_token { return $access_token; } -sub request_api_json { +sub request_api_json { #so lame for now my ($self, %options) = @_; if (!defined($self->{access_token})) { @@ -195,26 +195,40 @@ sub request_api_json { $self->settings(); - $self->{output}->output_add(long_msg => "URL: '" . $options{full_url} . "'", debug => 1); + my @results; + my %local_options = %options; - my $content = $self->{http}->request(%options); - - my $decoded; - eval { - $decoded = JSON::XS->new->utf8->decode($content); - }; - if ($@) { - $self->{output}->output_add(long_msg => $content, debug => 1); - $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); - $self->{output}->option_exit(); - } - if (defined($decoded->{error})) { - $self->{output}->output_add(long_msg => "Error message : " . $decoded->{error}->{message}, debug => 1); - $self->{output}->add_option_msg(short_msg => "Graph endpoint API return error code '" . $decoded->{error}->{code} . "' (add --debug option for detailed message)"); - $self->{output}->option_exit(); + while (1) { + $self->{output}->output_add(long_msg => "URL: '" . $local_options{full_url} . "'", debug => 1); + + my $content = $self->{http}->request(%local_options); + my $response = $self->{http}->get_response(); + + if ($response->code() == 429) { + last; + } + + my $decoded; + eval { + $decoded = JSON::XS->new->utf8->decode($content); + }; + if ($@) { + $self->{output}->output_add(long_msg => $content, debug => 1); + $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); + $self->{output}->option_exit(); + } + if (defined($decoded->{error})) { + $self->{output}->output_add(long_msg => "Error message : " . $decoded->{error}->{message}, debug => 1); + $self->{output}->add_option_msg(short_msg => "Graph endpoint API return error code '" . $decoded->{error}->{code} . "' (add --debug option for detailed message)"); + $self->{output}->option_exit(); + } + push @results, @{$decoded->{value}}; + + last if (!defined($decoded->{'@odata.nextLink'})); + $local_options{full_url} = $decoded->{'@odata.nextLink'}; } - return $decoded; + return @results; } sub request_api_csv { @@ -396,6 +410,40 @@ sub office_get_teams_device_usage { return $response; } +sub office_get_skype_activity_set_url { + my ($self, %options) = @_; + + my $url = $self->{graph_endpoint} . "/v1.0/reports/getSkypeForBusinessActivityUserDetail(period='D7')"; + + return $url; +} + +sub office_get_skype_activity { + my ($self, %options) = @_; + + my $full_url = $self->office_get_skype_activity_set_url(%options); + my $response = $self->request_api_csv(method => 'GET', full_url => $full_url, hostname => ''); + + return $response; +} + +sub office_get_skype_device_usage_set_url { + my ($self, %options) = @_; + + my $url = $self->{graph_endpoint} . "/v1.0/reports/getSkypeForBusinessDeviceUsageUserDetail(period='D7')"; + + return $url; +} + +sub office_get_skype_device_usage { + my ($self, %options) = @_; + + my $full_url = $self->office_get_skype_device_usage_set_url(%options); + my $response = $self->request_api_csv(method => 'GET', full_url => $full_url, hostname => ''); + + return $response; +} + 1; __END__ diff --git a/cloud/microsoft/office365/skype/mode/devicesusage.pm b/cloud/microsoft/office365/skype/mode/devicesusage.pm new file mode 100644 index 000000000..ba372a89e --- /dev/null +++ b/cloud/microsoft/office365/skype/mode/devicesusage.pm @@ -0,0 +1,255 @@ +# +# Copyright 2019 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package cloud::microsoft::office365::skype::mode::devicesusage; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; + +my $instance_mode; + +sub custom_active_perfdata { + my ($self, %options) = @_; + + my %total_options = (); + if ($instance_mode->{option_results}->{units} eq '%') { + $total_options{total} = $self->{result_values}->{total}; + $total_options{cast_int} = 1; + } + + $self->{output}->perfdata_add(label => 'active_users', + value => $self->{result_values}->{active}, + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{label}, %total_options), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{label}, %total_options), + unit => 'users', min => 0, max => $self->{result_values}->{total}); +} + +sub custom_active_threshold { + my ($self, %options) = @_; + + my $threshold_value = $self->{result_values}->{active}; + if ($instance_mode->{option_results}->{units} eq '%') { + $threshold_value = $self->{result_values}->{prct_active}; + } + my $exit = $self->{perfdata}->threshold_check(value => $threshold_value, + threshold => [ { label => 'critical-' . $self->{label}, exit_litteral => 'critical' }, + { label => 'warning-' . $self->{label}, exit_litteral => 'warning' } ]); + return $exit; + +} + +sub custom_active_output { + my ($self, %options) = @_; + + my $msg = sprintf("Active users on %s : %d/%d (%.2f%%)", + $self->{result_values}->{report_date}, + $self->{result_values}->{active}, + $self->{result_values}->{total}, + $self->{result_values}->{prct_active}); + return $msg; +} + +sub custom_active_calc { + my ($self, %options) = @_; + + $self->{result_values}->{active} = $options{new_datas}->{$self->{instance} . '_active'}; + $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'}; + $self->{result_values}->{report_date} = $options{new_datas}->{$self->{instance} . '_report_date'}; + $self->{result_values}->{prct_active} = ($self->{result_values}->{total} != 0) ? $self->{result_values}->{active} * 100 / $self->{result_values}->{total} : 0; + + return 0; +} + +sub prefix_global_output { + my ($self, %options) = @_; + + return "Users count by device type : "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'active', type => 0 }, + { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' }, + ]; + + $self->{maps_counters}->{active} = [ + { label => 'active-users', set => { + key_values => [ { name => 'active' }, { name => 'total' }, { name => 'report_date' } ], + closure_custom_calc => $self->can('custom_active_calc'), + closure_custom_output => $self->can('custom_active_output'), + closure_custom_threshold_check => $self->can('custom_active_threshold'), + closure_custom_perfdata => $self->can('custom_active_perfdata') + } + }, + ]; + $self->{maps_counters}->{global} = [ + { label => 'windows', set => { + key_values => [ { name => 'windows' } ], + output_template => 'Windows: %d', + perfdatas => [ + { label => 'windows', value => 'windows_absolute', template => '%d', + min => 0 }, + ], + } + }, + { label => 'ipad', set => { + key_values => [ { name => 'ipad' } ], + output_template => 'iPad: %d', + perfdatas => [ + { label => 'ipad', value => 'ipad_absolute', template => '%d', + min => 0 }, + ], + } + }, + { label => 'iphone', set => { + key_values => [ { name => 'iphone' } ], + output_template => 'iPhone: %d', + perfdatas => [ + { label => 'iphone', value => 'iphone_absolute', template => '%d', + min => 0 }, + ], + } + }, + { label => 'android-phone', set => { + key_values => [ { name => 'android_phone' } ], + output_template => 'Android Phone: %d', + perfdatas => [ + { label => 'android_phone', value => 'android_phone_absolute', template => '%d', + min => 0 }, + ], + } + }, + { label => 'windows-phone', set => { + key_values => [ { name => 'windows_phone' } ], + output_template => 'Windows Phone: %d', + perfdatas => [ + { label => 'windows_phone', value => 'windows_phone_absolute', template => '%d', + min => 0 }, + ], + } + }, + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => + { + "filter-user:s" => { name => 'filter_user' }, + "units:s" => { name => 'units', default => '%' }, + "filter-counters:s" => { name => 'filter_counters' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $instance_mode = $self; +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{active} = { active => 0, total => 0, report_date => '' }; + $self->{global} = { windows => 0, ipad => 0, iphone => 0, android_phone => 0, windows_phone => 0 }; + + my $results = $options{custom}->office_get_skype_device_usage(); + + foreach my $user (@{$results}) { + $self->{active}->{report_date} = $user->{'Report Refresh Date'} if ($self->{active}->{report_date} eq ''); + + if (defined($self->{option_results}->{filter_user}) && $self->{option_results}->{filter_user} ne '' && + $user->{'User Principal Name'} !~ /$self->{option_results}->{filter_user}/) { + $self->{output}->output_add(long_msg => "skipping '" . $user->{'User Principal Name'} . "': no matching filter name.", debug => 1); + next; + } + + $self->{active}->{total}++; + + if (!defined($user->{'Last Activity Date'}) || $user->{'Last Activity Date'} eq '' || + ($user->{'Last Activity Date'} ne $user->{'Report Refresh Date'})) { + $self->{output}->output_add(long_msg => "skipping '" . $user->{'User Principal Name'} . "': no activity.", debug => 1); + next; + } + + $self->{active}->{active}++; + + $self->{global}->{windows}++ if ($user->{'Used Windows'} =~ /Yes/); + $self->{global}->{ipad}++ if ($user->{'Used iPad'} =~ /Yes/); + $self->{global}->{iphone}++ if ($user->{'Used iPhone'} =~ /Yes/); + $self->{global}->{android_phone}++ if ($user->{'Used Android Phone'} =~ /Yes/); + $self->{global}->{windows_phone}++ if ($user->{'Used Windows Phone'} =~ /Yes/); + } +} + +1; + +__END__ + +=head1 MODE + +Check devices usage (reporting period over the last 7 days). + +(See link for details about metrics : +https://docs.microsoft.com/en-us/office365/admin/activity-reports/microsoft-teams-device-usage?view=o365-worldwide) + +=over 8 + +=item B<--filter-user> + +Filter users. + +=item B<--warning-*> + +Threshold warning. +Can be: 'active-users', 'windows' (count), 'ipad' (count), +'iphone' (count), 'android-phone' (count), +'windows-phone' (count). + +=item B<--critical-*> + +Threshold critical. +Can be: 'active-users', 'windows' (count), 'ipad' (count), +'iphone' (count), 'android-phone' (count), +'windows-phone' (count). + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example to hide per user counters: --filter-counters='windows' + +=item B<--units> + +Unit of thresholds (Default: '%') ('%', 'count'). + +=back + +=cut diff --git a/cloud/microsoft/office365/skype/mode/usersactivity.pm b/cloud/microsoft/office365/skype/mode/usersactivity.pm new file mode 100644 index 000000000..996d67afc --- /dev/null +++ b/cloud/microsoft/office365/skype/mode/usersactivity.pm @@ -0,0 +1,280 @@ +# +# Copyright 2019 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package cloud::microsoft::office365::skype::mode::usersactivity; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; + +my $instance_mode; + +sub custom_active_perfdata { + my ($self, %options) = @_; + + my %total_options = (); + if ($instance_mode->{option_results}->{units} eq '%') { + $total_options{total} = $self->{result_values}->{total}; + $total_options{cast_int} = 1; + } + + $self->{output}->perfdata_add(label => 'active_users', + value => $self->{result_values}->{active}, + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{label}, %total_options), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{label}, %total_options), + unit => 'users', min => 0, max => $self->{result_values}->{total}); +} + +sub custom_active_threshold { + my ($self, %options) = @_; + + my $threshold_value = $self->{result_values}->{active}; + if ($instance_mode->{option_results}->{units} eq '%') { + $threshold_value = $self->{result_values}->{prct_active}; + } + my $exit = $self->{perfdata}->threshold_check(value => $threshold_value, + threshold => [ { label => 'critical-' . $self->{label}, exit_litteral => 'critical' }, + { label => 'warning-' . $self->{label}, exit_litteral => 'warning' } ]); + return $exit; + +} + +sub custom_active_output { + my ($self, %options) = @_; + + my $msg = sprintf("Active users on %s : %d/%d (%.2f%%)", + $self->{result_values}->{report_date}, + $self->{result_values}->{active}, + $self->{result_values}->{total}, + $self->{result_values}->{prct_active}); + return $msg; +} + +sub custom_active_calc { + my ($self, %options) = @_; + + $self->{result_values}->{active} = $options{new_datas}->{$self->{instance} . '_active'}; + $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'}; + $self->{result_values}->{report_date} = $options{new_datas}->{$self->{instance} . '_report_date'}; + $self->{result_values}->{prct_active} = ($self->{result_values}->{total} != 0) ? $self->{result_values}->{active} * 100 / $self->{result_values}->{total} : 0; + + return 0; +} + +sub prefix_global_output { + my ($self, %options) = @_; + + return "Total "; +} + +sub prefix_user_output { + my ($self, %options) = @_; + + return "User '" . $options{instance_value}->{name} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'active', type => 0 }, + { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' }, + { name => 'users', type => 1, cb_prefix_output => 'prefix_user_output', message_multiple => 'All users activity are ok' }, + ]; + + $self->{maps_counters}->{active} = [ + { label => 'active-users', set => { + key_values => [ { name => 'active' }, { name => 'total' }, { name => 'report_date' } ], + closure_custom_calc => $self->can('custom_active_calc'), + closure_custom_output => $self->can('custom_active_output'), + closure_custom_threshold_check => $self->can('custom_active_threshold'), + closure_custom_perfdata => $self->can('custom_active_perfdata') + } + }, + ]; + $self->{maps_counters}->{global} = [ + { label => 'total-peer-to-peer-sessions', set => { + key_values => [ { name => 'peer_to_peer_sessions' } ], + output_template => 'Peer-to-peer Sessions Count: %d', + perfdatas => [ + { label => 'total_peer_to_peer_sessions', value => 'peer_to_peer_sessions_absolute', template => '%d', + min => 0 }, + ], + } + }, + { label => 'total-organized-conference', set => { + key_values => [ { name => 'organized_conference' } ], + output_template => 'Organized Conference Count: %d', + perfdatas => [ + { label => 'total_organized_conference', value => 'organized_conference_absolute', template => '%d', + min => 0 }, + ], + } + }, + { label => 'total-participated-conference', set => { + key_values => [ { name => 'participated_conference' } ], + output_template => 'Participated Conference Count: %d', + perfdatas => [ + { label => 'total_participated_conference', value => 'participated_conference_absolute', template => '%d', + min => 0 }, + ], + } + }, + ]; + $self->{maps_counters}->{users} = [ + { label => 'peer-to-peer-sessions', set => { + key_values => [ { name => 'peer_to_peer_sessions' }, { name => 'name' } ], + output_template => 'Peer-to-peer Sessions Count: %d', + perfdatas => [ + { label => 'peer_to_peer_sessions', value => 'peer_to_peer_sessions_absolute', template => '%d', + min => 0, label_extra_instance => 1, instance_use => 'name_absolute' }, + ], + } + }, + { label => 'organized-conference', set => { + key_values => [ { name => 'organized_conference' }, { name => 'name' } ], + output_template => 'Organized Conference Count: %d', + perfdatas => [ + { label => 'organized_conference', value => 'organized_conference_absolute', template => '%d', + min => 0, label_extra_instance => 1, instance_use => 'name_absolute' }, + ], + } + }, + { label => 'participated-conference', set => { + key_values => [ { name => 'participated_conference' }, { name => 'name' } ], + output_template => 'Participated Conference Count: %d', + perfdatas => [ + { label => 'participated_conference', value => 'participated_conference_absolute', template => '%d', + min => 0, label_extra_instance => 1, instance_use => 'name_absolute' }, + ], + } + }, + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => + { + "filter-user:s" => { name => 'filter_user' }, + "units:s" => { name => 'units', default => '%' }, + "filter-counters:s" => { name => 'filter_counters', default => 'active|total' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $instance_mode = $self; +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{active} = { active => 0, total => 0, report_date => '' }; + $self->{global} = { peer_to_peer_sessions => 0, organized_conference => 0, participated_conference => 0 }; + $self->{users} = {}; + + my $results = $options{custom}->office_get_skype_activity(); + + foreach my $user (@{$results}) { + $self->{active}->{report_date} = $user->{'Report Refresh Date'} if ($self->{active}->{report_date} eq ''); + + if (defined($self->{option_results}->{filter_user}) && $self->{option_results}->{filter_user} ne '' && + $user->{'User Principal Name'} !~ /$self->{option_results}->{filter_user}/) { + $self->{output}->output_add(long_msg => "skipping '" . $user->{'User Principal Name'} . "': no matching filter name.", debug => 1); + next; + } + + $self->{active}->{total}++; + + if (!defined($user->{'Last Activity Date'}) || $user->{'Last Activity Date'} eq '' || + ($user->{'Last Activity Date'} ne $user->{'Report Refresh Date'})) { + $self->{output}->output_add(long_msg => "skipping '" . $user->{'User Principal Name'} . "': no activity.", debug => 1); + next; + } + + $self->{active}->{active}++; + + $self->{global}->{peer_to_peer_sessions} += $user->{'Total Peer-to-peer Session Count'}; + $self->{global}->{organized_conference} += $user->{'Total Organized Conference Count'}; + $self->{global}->{participated_conference} += $user->{'Total Participated Conference Count'}; + + $self->{users}->{$user->{'User Principal Name'}}->{name} = $user->{'User Principal Name'}; + $self->{users}->{$user->{'User Principal Name'}}->{peer_to_peer_sessions} = $user->{'Total Peer-to-peer Session Count'}; + $self->{users}->{$user->{'User Principal Name'}}->{organized_conference} = $user->{'Total Organized Conference Count'}; + $self->{users}->{$user->{'User Principal Name'}}->{participated_conference} = $user->{'Total Participated Conference Count'}; + } +} + +1; + +__END__ + +=head1 MODE + +Check users activity (reporting period over the last 7 days). + +(See link for details about metrics : +https://docs.microsoft.com/en-us/SkypeForBusiness/skype-for-business-online-reporting/activity-report) + +=over 8 + +=item B<--filter-user> + +Filter users. + +=item B<--warning-*> + +Threshold warning. +Can be: 'active-users', 'total-peer-to-peer-sessions' (count), +'total-organized-conference' (count), 'total-participated-conference' (count), +'peer-to-peer-sessions' (count), 'organized-conference' (count), +'participated-conference' (count). + +=item B<--critical-*> + +Threshold critical. +Can be: 'active-users', 'total-peer-to-peer-sessions' (count), +'total-organized-conference' (count), 'total-participated-conference' (count), +'peer-to-peer-sessions' (count), 'organized-conference' (count), +'participated-conference' (count). + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example to hide per user counters: --filter-counters='total' +(Default: 'active|total') + +=item B<--units> + +Unit of thresholds (Default: '%') ('%', 'count'). + +=back + +=cut diff --git a/cloud/microsoft/office365/skype/plugin.pm b/cloud/microsoft/office365/skype/plugin.pm new file mode 100644 index 000000000..865d51d1f --- /dev/null +++ b/cloud/microsoft/office365/skype/plugin.pm @@ -0,0 +1,50 @@ +# +# Copyright 2019 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package cloud::microsoft::office365::skype::plugin; + +use strict; +use warnings; +use base qw(centreon::plugins::script_custom); + +sub new { + my ( $class, %options ) = @_; + my $self = $class->SUPER::new( package => __PACKAGE__, %options ); + bless $self, $class; + + $self->{version} = '0.1'; + %{ $self->{modes} } = ( + 'devices-usage' => 'cloud::microsoft::office365::skype::mode::devicesusage', + 'users-activity' => 'cloud::microsoft::office365::skype::mode::usersactivity', + ); + + $self->{custom_modes}{graphapi} = 'cloud::microsoft::office365::custom::graphapi'; + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check Microsoft Office 365 Skype for Business. + +=cut