| 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::management::mode::subscriptions; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); |
| 28 |
|
| 29 |
sub custom_status_output { |
| 30 |
my ($self, %options) = @_; |
| 31 |
|
| 32 |
return 'capability status: ' . $self->{result_values}->{capabilityStatus}; |
| 33 |
} |
| 34 |
|
| 35 |
sub custom_subscription_usage_output { |
| 36 |
my ($self, %options) = @_; |
| 37 |
|
| 38 |
return sprintf( |
| 39 |
'usage total: %s used: %s (%.2f%%) free: %s (%.2f%%)', |
| 40 |
$self->{result_values}->{total}, |
| 41 |
$self->{result_values}->{used}, |
| 42 |
$self->{result_values}->{prct_used}, |
| 43 |
$self->{result_values}->{free}, |
| 44 |
$self->{result_values}->{prct_free} |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
sub prefix_subscription_output { |
| 49 |
my ($self, %options) = @_; |
| 50 |
|
| 51 |
return "Subscriptions '" . $options{instance_value}->{skuPartNumber} . "' "; |
| 52 |
} |
| 53 |
|
| 54 |
sub set_counters { |
| 55 |
my ($self, %options) = @_; |
| 56 |
|
| 57 |
$self->{maps_counters_type} = [ |
| 58 |
{ name => 'subscriptions', type => 1, cb_prefix_output => 'prefix_subscription_output', message_multiple => 'All subscriptions are ok', skipped_code => { -10 => 1 } } |
| 59 |
]; |
| 60 |
|
| 61 |
$self->{maps_counters}->{subscriptions} = [ |
| 62 |
{ |
| 63 |
label => 'status', |
| 64 |
type => 2, |
| 65 |
warning_default => '%{capabilityStatus} =~ /warning/i', |
| 66 |
set => { |
| 67 |
key_values => [ |
| 68 |
{ name => 'capabilityStatus' }, { name => 'skuPartNumber' } |
| 69 |
], |
| 70 |
closure_custom_output => $self->can('custom_status_output'), |
| 71 |
closure_custom_perfdata => sub { return 0; }, |
| 72 |
closure_custom_threshold_check => \&catalog_status_threshold_ng |
| 73 |
} |
| 74 |
}, |
| 75 |
{ label => 'subscription-usage', nlabel => 'subscription.usage.count', set => { |
| 76 |
key_values => [ |
| 77 |
{ name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, |
| 78 |
{ name => 'skuPartNumber' } |
| 79 |
], |
| 80 |
closure_custom_output => $self->can('custom_subscription_usage_output'), |
| 81 |
perfdatas => [ |
| 82 |
{ template => '%d', min => 0, max => 'total', label_extra_instance => 1, instance_use => 'skuPartNumber' } |
| 83 |
] |
| 84 |
} |
| 85 |
}, |
| 86 |
{ label => 'subscription-usage-free', display_ok => 0, nlabel => 'subscription.free.count', set => { |
| 87 |
key_values => [ |
| 88 |
{ name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, |
| 89 |
{ name => 'skuPartNumber' } |
| 90 |
], |
| 91 |
closure_custom_output => $self->can('custom_subscription_usage_output'), |
| 92 |
perfdatas => [ |
| 93 |
{ template => '%d', min => 0, max => 'total', label_extra_instance => 1, instance_use => 'skuPartNumber' } |
| 94 |
] |
| 95 |
} |
| 96 |
}, |
| 97 |
{ label => 'subscription-usage-prct', display_ok => 0, nlabel => 'subscription.usage.percentage', set => { |
| 98 |
key_values => [ |
| 99 |
{ name => 'prct_used' }, { name => 'used' }, { name => 'free' }, { name => 'prct_free' }, { name => 'total' }, |
| 100 |
{ name => 'skuPartNumber' } |
| 101 |
], |
| 102 |
closure_custom_output => $self->can('custom_subscription_usage_output'), |
| 103 |
perfdatas => [ |
| 104 |
{ template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'skuPartNumber' } |
| 105 |
] |
| 106 |
} |
| 107 |
} |
| 108 |
]; |
| 109 |
} |
| 110 |
|
| 111 |
sub new { |
| 112 |
my ($class, %options) = @_; |
| 113 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 114 |
bless $self, $class; |
| 115 |
|
| 116 |
$options{options}->add_options(arguments => { |
| 117 |
'filter-sku-part-number:s' => { name => 'filter_sku_part_number' } |
| 118 |
}); |
| 119 |
|
| 120 |
return $self; |
| 121 |
} |
| 122 |
|
| 123 |
sub manage_selection { |
| 124 |
my ($self, %options) = @_; |
| 125 |
|
| 126 |
my $results = $options{custom}->list_subscribed_skus(); |
| 127 |
|
| 128 |
$self->{subscriptions} = {}; |
| 129 |
foreach my $subscription (@$results) { |
| 130 |
next if (defined($self->{option_results}->{filter_sku_part_number}) && $self->{option_results}->{filter_sku_part_number} ne '' && |
| 131 |
$subscription->{skuPartNumber} !~ /$self->{option_results}->{filter_sku_part_number}/); |
| 132 |
|
| 133 |
$self->{subscriptions}->{ $subscription->{id} } = { |
| 134 |
skuPartNumber => $subscription->{skuPartNumber}, |
| 135 |
capabilityStatus => lc($subscription->{capabilityStatus}) |
| 136 |
}; |
| 137 |
my $total = $subscription->{prepaidUnits}->{enabled} > 0 ? $subscription->{prepaidUnits}->{enabled} : $subscription->{prepaidUnits}->{warning}; |
| 138 |
next if ($total == 0); |
| 139 |
|
| 140 |
$self->{subscriptions}->{ $subscription->{id} }->{total} = $total; |
| 141 |
$self->{subscriptions}->{ $subscription->{id} }->{used} = $subscription->{consumedUnits}; |
| 142 |
$self->{subscriptions}->{ $subscription->{id} }->{free} = $total - $subscription->{consumedUnits}; |
| 143 |
$self->{subscriptions}->{ $subscription->{id} }->{prct_used} = $subscription->{consumedUnits} * 100 / $total; |
| 144 |
$self->{subscriptions}->{ $subscription->{id} }->{prct_free} = 100 - $self->{subscriptions}->{ $subscription->{id} }->{prct_used}; |
| 145 |
} |
| 146 |
|
| 147 |
if (scalar(keys %{$self->{subscriptions}}) <= 0) { |
| 148 |
$self->{output}->add_option_msg(short_msg => 'No subscriptions found.'); |
| 149 |
$self->{output}->option_exit(); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
1; |
| 154 |
|
| 155 |
__END__ |
| 156 |
|
| 157 |
=head1 MODE |
| 158 |
|
| 159 |
Check SKU subcriptions. |
| 160 |
|
| 161 |
=over 8 |
| 162 |
|
| 163 |
=item B<--filter-sku-part-number> |
| 164 |
|
| 165 |
Filter subscriptions by SKU part number (can be a regexp). |
| 166 |
|
| 167 |
=item B<--warning-status> |
| 168 |
|
| 169 |
Define the conditions to match for the status to be WARNING (default: '%{status} =~ /warning/i'). |
| 170 |
You can use the following variables: %{capabilityStatus}, %{skuPartNumber} |
| 171 |
|
| 172 |
=item B<--critical-status> |
| 173 |
|
| 174 |
Define the conditions to match for the status to be CRITICAL. |
| 175 |
You can use the following variables: %{capabilityStatus}, %{skuPartNumber} |
| 176 |
|
| 177 |
=item B<--warning-*> B<--critical-*> |
| 178 |
|
| 179 |
Thresholds. |
| 180 |
Can be: 'subscription-usage', 'subscription-usage-free', 'subscription-usage-prct'. |
| 181 |
|
| 182 |
=back |
| 183 |
|
| 184 |
=cut |