| 1 |
# |
| 2 |
# Copyright 2022 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::azure::policyinsights::policystates::mode::compliance; |
| 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_compliance_state_output { |
| 30 |
my ($self, %options) = @_; |
| 31 |
return "Compliance state for policy '$self->{result_values}->{policy_name}' on resource '$self->{result_values}->{resource_name}' is '$self->{result_values}->{compliance_state}'"; |
| 32 |
} |
| 33 |
|
| 34 |
sub set_counters { |
| 35 |
my ($self, %options) = @_; |
| 36 |
|
| 37 |
$self->{maps_counters_type} = [ |
| 38 |
{ name => 'non_compliant_policies', type => 0 }, |
| 39 |
{ name => 'compliance_state', type => 1, message_multiple => 'All compliances states are ok' } |
| 40 |
]; |
| 41 |
|
| 42 |
$self->{maps_counters}->{non_compliant_policies} = [ |
| 43 |
{ label => 'non-compliant-policies', nlabel => 'policies.non_compliant.count', set => { |
| 44 |
key_values => [ { name => 'non_compliant_policies' } ], |
| 45 |
output_template => 'Number of non compliant policies: %d', |
| 46 |
perfdatas => [ |
| 47 |
{ label => 'total_non_compliant_policies', template => '%d', min => 0, unit => '' } |
| 48 |
] |
| 49 |
} |
| 50 |
} |
| 51 |
]; |
| 52 |
|
| 53 |
$self->{maps_counters}->{compliance_state} = [ |
| 54 |
{ label => 'compliance-state', |
| 55 |
type => 2, |
| 56 |
critical_default => '%{compliance_state} eq "NonCompliant"', |
| 57 |
set => { |
| 58 |
key_values => [ { name => 'compliance_state' }, { name => 'policy_name' }, { name => 'resource_name' }, { name => 'display' } ], |
| 59 |
closure_custom_output => $self->can('custom_compliance_state_output'), |
| 60 |
closure_custom_threshold_check => \&catalog_status_threshold_ng |
| 61 |
} |
| 62 |
} |
| 63 |
]; |
| 64 |
} |
| 65 |
|
| 66 |
sub new { |
| 67 |
my ($class, %options) = @_; |
| 68 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 69 |
bless $self, $class; |
| 70 |
|
| 71 |
$options{options}->add_options(arguments => { |
| 72 |
'api-version:s' => { name => 'api_version', default => '2019-10-01'}, |
| 73 |
'policy-states:s' => { name => 'policy_states', default => 'default' }, |
| 74 |
'policy-name:s' => { name => 'policy_name' }, |
| 75 |
'resource-group:s' => { name => 'resource_group' }, |
| 76 |
'resource-location:s' => { name => 'resource_location' }, |
| 77 |
'resource-type:s' => { name => 'resource_type' } |
| 78 |
}); |
| 79 |
return $self; |
| 80 |
} |
| 81 |
|
| 82 |
sub check_options { |
| 83 |
my ($self, %options) = @_; |
| 84 |
$self->SUPER::check_options(%options); |
| 85 |
|
| 86 |
$self->{policy_states} = (defined($self->{option_results}->{policy_states}) && $self->{option_results}->{policy_states} ne "") ? $self->{option_results}->{policy_states} : "default"; |
| 87 |
$self->{api_version} = (defined($self->{option_results}->{api_version}) && $self->{option_results}->{api_version} ne "") ? $self->{option_results}->{api_version} : "2019-10-01"; |
| 88 |
} |
| 89 |
|
| 90 |
sub manage_selection { |
| 91 |
my ($self, %options) = @_; |
| 92 |
|
| 93 |
my $policy_states = $options{custom}->azure_list_policystates( |
| 94 |
providers => 'Microsoft.PolicyInsights/policyStates', |
| 95 |
resource => $self->{policy_states}, |
| 96 |
resource_group => $self->{option_results}->{resource_group}, |
| 97 |
query_name => 'queryResults', |
| 98 |
resource_location => $self->{option_results}->{resource_location}, |
| 99 |
resource_type => $self->{option_results}->{resource_type}, |
| 100 |
policy_name => $self->{option_results}->{policy_name} |
| 101 |
); |
| 102 |
|
| 103 |
my $non_compliant_policies = 0; |
| 104 |
$self->{compliance_state} = {}; |
| 105 |
foreach my $policy_state (@{ $policy_states }) { |
| 106 |
my $resource_name = $policy_state->{resourceId}; |
| 107 |
$resource_name =~ /(\w*)$/; |
| 108 |
$resource_name = $1; |
| 109 |
my $display = $policy_state->{policyDefinitionName} . "_" . $resource_name; |
| 110 |
$self->{compliance_state}->{ $display } = { |
| 111 |
display => $display, |
| 112 |
compliance_state => $policy_state->{complianceState}, |
| 113 |
policy_name => $policy_state->{policyDefinitionName}, |
| 114 |
resource_name => $resource_name |
| 115 |
}; |
| 116 |
$non_compliant_policies = $non_compliant_policies + 1 if $policy_state->{complianceState} eq 'NonCompliant'; |
| 117 |
}; |
| 118 |
$self->{non_compliant_policies} = { non_compliant_policies => $non_compliant_policies }; |
| 119 |
} |
| 120 |
|
| 121 |
1; |
| 122 |
|
| 123 |
__END__ |
| 124 |
|
| 125 |
=head1 MODE |
| 126 |
|
| 127 |
Check Azure policies compliance. |
| 128 |
|
| 129 |
Example: |
| 130 |
|
| 131 |
perl centreon_plugins.pl --plugin=cloud::azure::policyinsights::policystates::plugin --mode=compliance --policy-states=default |
| 132 |
[--resource-group='MYRESOURCEGROUP'] --api-version=2019-10-01 |
| 133 |
|
| 134 |
|
| 135 |
=over 8 |
| 136 |
|
| 137 |
=item B<--policy-states> |
| 138 |
|
| 139 |
The virtual resource under PolicyStates resource type. In a given time range, 'latest' represents the latest policy state(s), whereas 'default' represents all policy state(s). |
| 140 |
|
| 141 |
=item B<--resource-group> |
| 142 |
|
| 143 |
Set resource group (optional). |
| 144 |
|
| 145 |
=item B<--resource-location> |
| 146 |
|
| 147 |
Set resource location (optional). |
| 148 |
|
| 149 |
=item B<--resource-type> |
| 150 |
|
| 151 |
Set resource type (optional). |
| 152 |
|
| 153 |
=item B<--policy-name> |
| 154 |
|
| 155 |
Set policy name (optional). |
| 156 |
|
| 157 |
=item B<--warning-*> B<--critical-*> |
| 158 |
|
| 159 |
Thresholds. |
| 160 |
Can be: 'non-compliant-policies' ,'compliance-state'. |
| 161 |
|
| 162 |
=back |
| 163 |
|
| 164 |
=cut |