diff --git a/centreon-plugins/cloud/microsoft/office365/custom/graphapi.pm b/centreon-plugins/cloud/microsoft/office365/custom/graphapi.pm index 9b72cf697..fd9a98c37 100644 --- a/centreon-plugins/cloud/microsoft/office365/custom/graphapi.pm +++ b/centreon-plugins/cloud/microsoft/office365/custom/graphapi.pm @@ -477,6 +477,15 @@ sub get_services_health { return $response; } +sub list_subscribed_skus { + my ($self, %options) = @_; + + my $full_url = $self->{graph_endpoint} . '/v1.0/subscribedSkus'; + my $response = $self->request_api_json(method => 'GET', full_url => $full_url, hostname => ''); + + return $response; +} + sub get_applications { my ($self, %options) = @_; diff --git a/centreon-plugins/cloud/microsoft/office365/management/mode/listsubscriptions.pm b/centreon-plugins/cloud/microsoft/office365/management/mode/listsubscriptions.pm new file mode 100644 index 000000000..be694a4a0 --- /dev/null +++ b/centreon-plugins/cloud/microsoft/office365/management/mode/listsubscriptions.pm @@ -0,0 +1,103 @@ +# +# Copyright 2022 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::management::mode::listsubscriptions; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $options{options}->add_options(arguments => {}); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); +} + +sub manage_selection { + my ($self, %options) = @_; + + return $options{custom}->list_subscribed_skus(); +} + +sub run { + my ($self, %options) = @_; + + my $results = $self->manage_selection(%options); + foreach (@$results) { + $self->{output}->output_add( + long_msg => sprintf( + "[skuPartNumber: %s] [capabilityStatus: %s] [appliesTo: %s]", + $_->{skuPartNumber}, + lc($_->{capabilityStatus}), + $_->{appliesTo} + ) + ); + } + + $self->{output}->output_add( + severity => 'OK', + short_msg => 'List subscriptions:' + ); + $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); + $self->{output}->exit(); +} + +sub disco_format { + my ($self, %options) = @_; + + $self->{output}->add_disco_format(elements => ['skuPartNumber', 'capabilityStatus', 'appliesTo']); +} + +sub disco_show { + my ($self, %options) = @_; + + my $results = $self->manage_selection(%options); + foreach (@$results) { + $self->{output}->add_disco_entry( + skuPartNumber => $_->{skuPartNumber}, + capabilityStatus => lc($_->{capabilityStatus}), + appliesTo => $_->{appliesTo} + ); + } +} + +1; + +__END__ + +=head1 MODE + +List subscriptions. + +=over 8 + +=back + +=cut diff --git a/centreon-plugins/cloud/microsoft/office365/management/mode/subscriptions.pm b/centreon-plugins/cloud/microsoft/office365/management/mode/subscriptions.pm new file mode 100644 index 000000000..f42fc500b --- /dev/null +++ b/centreon-plugins/cloud/microsoft/office365/management/mode/subscriptions.pm @@ -0,0 +1,184 @@ +# +# Copyright 2022 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::management::mode::subscriptions; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); + +sub custom_status_output { + my ($self, %options) = @_; + + return 'capability status: ' . $self->{result_values}->{capabilityStatus}; +} + +sub custom_subscription_usage_output { + my ($self, %options) = @_; + + return sprintf( + 'usage total: %s used: %s (%.2f%%) free: %s (%.2f%%)', + $self->{result_values}->{total}, + $self->{result_values}->{used}, + $self->{result_values}->{prct_used}, + $self->{result_values}->{free}, + $self->{result_values}->{prct_free} + ); +} + +sub prefix_subscription_output { + my ($self, %options) = @_; + + return "Subscriptions '" . $options{instance_value}->{skuPartNumber} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'subscriptions', type => 1, cb_prefix_output => 'prefix_subscription_output', message_multiple => 'All subscriptions are ok', skipped_code => { -10 => 1 } } + ]; + + $self->{maps_counters}->{subscriptions} = [ + { + label => 'status', + type => 2, + warning_default => '%{capabilityStatus} =~ /warning/i', + set => { + key_values => [ + { name => 'capabilityStatus' }, { name => 'skuPartNumber' } + ], + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + }, + { label => 'subscription-usage', nlabel => 'subscription.usage.count', set => { + key_values => [ + { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, + { name => 'skuPartNumber' } + ], + closure_custom_output => $self->can('custom_subscription_usage_output'), + perfdatas => [ + { template => '%d', min => 0, max => 'total', label_extra_instance => 1, instance_use => 'skuPartNumber' } + ] + } + }, + { label => 'subscription-usage-free', display_ok => 0, nlabel => 'subscription.free.count', set => { + key_values => [ + { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, + { name => 'skuPartNumber' } + ], + closure_custom_output => $self->can('custom_subscription_usage_output'), + perfdatas => [ + { template => '%d', min => 0, max => 'total', label_extra_instance => 1, instance_use => 'skuPartNumber' } + ] + } + }, + { label => 'subscription-usage-prct', display_ok => 0, nlabel => 'subscription.usage.percentage', set => { + key_values => [ + { name => 'prct_used' }, { name => 'used' }, { name => 'free' }, { name => 'prct_free' }, { name => 'total' }, + { name => 'skuPartNumber' } + ], + closure_custom_output => $self->can('custom_subscription_usage_output'), + perfdatas => [ + { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'skuPartNumber' } + ] + } + } + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'filter-sku-part-number:s' => { name => 'filter_sku_part_number' } + }); + + return $self; +} + +sub manage_selection { + my ($self, %options) = @_; + + my $results = $options{custom}->list_subscribed_skus(); + + $self->{subscriptions} = {}; + foreach my $subscription (@$results) { + next if (defined($self->{option_results}->{filter_sku_part_number}) && $self->{option_results}->{filter_sku_part_number} ne '' && + $subscription->{skuPartNumber} !~ /$self->{option_results}->{filter_sku_part_number}/); + + $self->{subscriptions}->{ $subscription->{id} } = { + skuPartNumber => $subscription->{skuPartNumber}, + capabilityStatus => lc($subscription->{capabilityStatus}) + }; + my $total = $subscription->{prepaidUnits}->{enabled} > 0 ? $subscription->{prepaidUnits}->{enabled} : $subscription->{prepaidUnits}->{warning}; + next if ($total == 0); + + $self->{subscriptions}->{ $subscription->{id} }->{total} = $total; + $self->{subscriptions}->{ $subscription->{id} }->{used} = $subscription->{consumedUnits}; + $self->{subscriptions}->{ $subscription->{id} }->{free} = $total - $subscription->{consumedUnits}; + $self->{subscriptions}->{ $subscription->{id} }->{prct_used} = $subscription->{consumedUnits} * 100 / $total; + $self->{subscriptions}->{ $subscription->{id} }->{prct_free} = 100 - $self->{subscriptions}->{ $subscription->{id} }->{prct_used}; + } + + if (scalar(keys %{$self->{subscriptions}}) <= 0) { + $self->{output}->add_option_msg(short_msg => 'No subscriptions found.'); + $self->{output}->option_exit(); + } +} + +1; + +__END__ + +=head1 MODE + +Check SKU subcriptions. + +=over 8 + +=item B<--filter-sku-part-number> + +Filter subscriptions by SKU part number (can be a regexp). + +=item B<--warning-status> + +Set warning threshold for status (Default: '%{status} =~ /warning/i'). +Can used special variables like: %{capabilityStatus}, %{skuPartNumber} + +=item B<--critical-status> + +Set critical threshold for status. +Can used special variables like: %{capabilityStatus}, %{skuPartNumber} + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'subscription-usage', 'subscription-usage-free', 'subscription-usage-prct'. + +=back + +=cut diff --git a/centreon-plugins/cloud/microsoft/office365/management/plugin.pm b/centreon-plugins/cloud/microsoft/office365/management/plugin.pm index 538d63af4..d01ac2aab 100644 --- a/centreon-plugins/cloud/microsoft/office365/management/plugin.pm +++ b/centreon-plugins/cloud/microsoft/office365/management/plugin.pm @@ -30,9 +30,11 @@ sub new { bless $self, $class; $self->{modes} = { - 'app-credentials' => 'cloud::microsoft::office365::management::mode::appcredentials', - 'list-services' => 'cloud::microsoft::office365::management::mode::listservices', - 'service-status' => 'cloud::microsoft::office365::management::mode::servicestatus' + 'app-credentials' => 'cloud::microsoft::office365::management::mode::appcredentials', + 'list-services' => 'cloud::microsoft::office365::management::mode::listservices', + 'list-subscriptions' => 'cloud::microsoft::office365::management::mode::listsubscriptions', + 'service-status' => 'cloud::microsoft::office365::management::mode::servicestatus', + 'subscriptions' => 'cloud::microsoft::office365::management::mode::subscriptions' }; $self->{custom_modes}->{graphapi} = 'cloud::microsoft::office365::custom::graphapi';