| 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::azure::management::resource::mode::deploymentsstatus; |
| 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); |
| 28 |
|
| 29 |
sub custom_status_output { |
| 30 |
my ($self, %options) = @_; |
| 31 |
|
| 32 |
my $msg = sprintf("Status '%s' [Duration: %s] [Last modified: %s]", $self->{result_values}->{status}, |
| 33 |
$self->{result_values}->{duration}, |
| 34 |
$self->{result_values}->{last_modified}); |
| 35 |
return $msg; |
| 36 |
} |
| 37 |
|
| 38 |
sub custom_status_calc { |
| 39 |
my ($self, %options) = @_; |
| 40 |
|
| 41 |
$self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'}; |
| 42 |
$self->{result_values}->{duration} = centreon::plugins::misc::change_seconds(value => $options{new_datas}->{$self->{instance} . '_duration'}); |
| 43 |
$self->{result_values}->{last_modified} = $options{new_datas}->{$self->{instance} . '_last_modified'}; |
| 44 |
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; |
| 45 |
return 0; |
| 46 |
} |
| 47 |
|
| 48 |
sub prefix_global_output { |
| 49 |
my ($self, %options) = @_; |
| 50 |
|
| 51 |
return "Deployments "; |
| 52 |
} |
| 53 |
|
| 54 |
sub prefix_deployment_output { |
| 55 |
my ($self, %options) = @_; |
| 56 |
|
| 57 |
return "Deployment '" . $options{instance_value}->{display} . "' "; |
| 58 |
} |
| 59 |
|
| 60 |
sub set_counters { |
| 61 |
my ($self, %options) = @_; |
| 62 |
|
| 63 |
$self->{maps_counters_type} = [ |
| 64 |
{ name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' }, |
| 65 |
{ name => 'deployments', type => 1, cb_prefix_output => 'prefix_deployment_output', message_multiple => 'All deployments are ok' }, |
| 66 |
]; |
| 67 |
|
| 68 |
$self->{maps_counters}->{global} = [ |
| 69 |
{ label => 'total-succeeded', set => { |
| 70 |
key_values => [ { name => 'succeeded' } ], |
| 71 |
output_template => "succeeded : %s", |
| 72 |
perfdatas => [ |
| 73 |
{ label => 'total_succeeded', value => 'succeeded', template => '%d', min => 0 }, |
| 74 |
], |
| 75 |
} |
| 76 |
}, |
| 77 |
{ label => 'total-failed', set => { |
| 78 |
key_values => [ { name => 'failed' } ], |
| 79 |
output_template => "failed : %s", |
| 80 |
perfdatas => [ |
| 81 |
{ label => 'total_failed', value => 'failed', template => '%d', min => 0 }, |
| 82 |
], |
| 83 |
} |
| 84 |
}, |
| 85 |
]; |
| 86 |
|
| 87 |
$self->{maps_counters}->{deployments} = [ |
| 88 |
{ label => 'status', threshold => 0, set => { |
| 89 |
key_values => [ { name => 'status' }, { name => 'duration' }, { name => 'last_modified' }, { name => 'display' } ], |
| 90 |
closure_custom_calc => $self->can('custom_status_calc'), |
| 91 |
closure_custom_output => $self->can('custom_status_output'), |
| 92 |
closure_custom_perfdata => sub { return 0; }, |
| 93 |
closure_custom_threshold_check => \&catalog_status_threshold, |
| 94 |
} |
| 95 |
}, |
| 96 |
]; |
| 97 |
} |
| 98 |
|
| 99 |
sub new { |
| 100 |
my ($class, %options) = @_; |
| 101 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options); |
| 102 |
bless $self, $class; |
| 103 |
|
| 104 |
$options{options}->add_options(arguments => |
| 105 |
{ |
| 106 |
"resource-group:s" => { name => 'resource_group' }, |
| 107 |
"filter-counters:s" => { name => 'filter_counters' }, |
| 108 |
"warning-status:s" => { name => 'warning_status', default => '' }, |
| 109 |
"critical-status:s" => { name => 'critical_status', default => '%{status} ne "Succeeded"' }, |
| 110 |
}); |
| 111 |
|
| 112 |
return $self; |
| 113 |
} |
| 114 |
|
| 115 |
sub check_options { |
| 116 |
my ($self, %options) = @_; |
| 117 |
$self->SUPER::check_options(%options); |
| 118 |
|
| 119 |
if (!defined($self->{option_results}->{resource_group}) || $self->{option_results}->{resource_group} eq '') { |
| 120 |
$self->{output}->add_option_msg(short_msg => "Need to specify --resource-group option"); |
| 121 |
$self->{output}->option_exit(); |
| 122 |
} |
| 123 |
|
| 124 |
$self->change_macros(macros => ['warning_status', 'critical_status']); |
| 125 |
} |
| 126 |
|
| 127 |
sub manage_selection { |
| 128 |
my ($self, %options) = @_; |
| 129 |
|
| 130 |
$self->{global} = { |
| 131 |
succeeded => 0, failed => 0, |
| 132 |
}; |
| 133 |
$self->{deployments} = {}; |
| 134 |
my $deployments = $options{custom}->azure_list_deployments(resource_group => $self->{option_results}->{resource_group}); |
| 135 |
foreach my $deployment (@{$deployments}) { |
| 136 |
my $duration = $options{custom}->convert_duration(time_string => $deployment->{properties}->{duration}); |
| 137 |
|
| 138 |
$self->{deployments}->{$deployment->{id}} = { |
| 139 |
display => $deployment->{name}, |
| 140 |
status => $deployment->{properties}->{provisioningState}, |
| 141 |
duration => $duration, |
| 142 |
last_modified => ($deployment->{properties}->{timestamp} =~ /^(.*)\..*$/) ? $1 : '', |
| 143 |
}; |
| 144 |
|
| 145 |
foreach my $status (keys %{$self->{global}}) { |
| 146 |
$self->{global}->{$status}++ if ($deployment->{properties}->{provisioningState} =~ /$status/i); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
if (scalar(keys %{$self->{deployments}}) <= 0) { |
| 151 |
$self->{output}->add_option_msg(short_msg => "No deployments found."); |
| 152 |
$self->{output}->option_exit(); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
1; |
| 157 |
|
| 158 |
__END__ |
| 159 |
|
| 160 |
=head1 MODE |
| 161 |
|
| 162 |
Check deployments status. |
| 163 |
|
| 164 |
Example: |
| 165 |
perl centreon_plugins.pl --plugin=cloud::azure::management::resource::plugin --custommode=azcli --mode=deployments-status |
| 166 |
--filter-counters='^total-failed$' --critical-total-failed='1' --verbose |
| 167 |
|
| 168 |
=over 8 |
| 169 |
|
| 170 |
=item B<--resource-group> |
| 171 |
|
| 172 |
Set resource group (required). |
| 173 |
|
| 174 |
=item B<--filter-counters> |
| 175 |
|
| 176 |
Only display some counters (regexp can be used). |
| 177 |
Example: --filter-counters='^total-succeeded$' |
| 178 |
|
| 179 |
=item B<--warning-status> |
| 180 |
|
| 181 |
Define the conditions to match for the status to be WARNING (default: ''). |
| 182 |
You can use the following variables: %{status}, %{display} |
| 183 |
|
| 184 |
=item B<--critical-status> |
| 185 |
|
| 186 |
Define the conditions to match for the status to be CRITICAL (default: '%{status} ne "Succeeded"'). |
| 187 |
You can use the following variables: %{status}, %{display} |
| 188 |
|
| 189 |
=item B<--warning-*> |
| 190 |
|
| 191 |
Warning threshold. |
| 192 |
Can be: 'total-succeeded', 'total-failed'. |
| 193 |
|
| 194 |
=item B<--critical-*> |
| 195 |
|
| 196 |
Critical threshold. |
| 197 |
Can be: 'total-succeeded', 'total-failed'. |
| 198 |
|
| 199 |
=back |
| 200 |
|
| 201 |
=cut |