| 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::costs::mode::orphanresources; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
|
| 28 |
sub custom_orphaned_output { |
| 29 |
my ($self, %options) = @_; |
| 30 |
|
| 31 |
my $msg = sprintf(" orphaned resources %s (total: %s)", $self->{result_values}->{count}, $self->{result_values}->{total}); |
| 32 |
|
| 33 |
return $msg; |
| 34 |
} |
| 35 |
|
| 36 |
sub prefix_disks_output { |
| 37 |
my ($self, %options) = @_; |
| 38 |
|
| 39 |
return 'Managed disks'; |
| 40 |
} |
| 41 |
|
| 42 |
sub prefix_nsgs_output { |
| 43 |
my ($self, %options) = @_; |
| 44 |
|
| 45 |
return 'NSGs'; |
| 46 |
} |
| 47 |
|
| 48 |
sub prefix_nics_output { |
| 49 |
my ($self, %options) = @_; |
| 50 |
|
| 51 |
return 'NICs'; |
| 52 |
} |
| 53 |
|
| 54 |
sub prefix_publicips_output { |
| 55 |
my ($self, %options) = @_; |
| 56 |
|
| 57 |
return 'Public IPs'; |
| 58 |
} |
| 59 |
|
| 60 |
sub prefix_routetables_output { |
| 61 |
my ($self, %options) = @_; |
| 62 |
|
| 63 |
return 'Route tables'; |
| 64 |
} |
| 65 |
|
| 66 |
sub prefix_snapshots_output { |
| 67 |
my ($self, %options) = @_; |
| 68 |
|
| 69 |
return 'Snapshots'; |
| 70 |
} |
| 71 |
|
| 72 |
sub new { |
| 73 |
my ($class, %options) = @_; |
| 74 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 75 |
bless $self, $class; |
| 76 |
|
| 77 |
$options{options}->add_options(arguments => { |
| 78 |
'exclude-name:s' => { name => 'exclude_name' }, |
| 79 |
'skip-managed-disks' => { name => 'skip_managed_disks' }, |
| 80 |
'skip-nics' => { name => 'skip_nics' }, |
| 81 |
'skip-nsgs' => { name => 'skip_nsgs' }, |
| 82 |
'skip-public-ips' => { name => 'skip_public_ips' }, |
| 83 |
'skip-route-tables' => { name => 'skip_route_tables' }, |
| 84 |
'skip-snapshots' => { name => 'skip_snapshots' } |
| 85 |
}); |
| 86 |
|
| 87 |
return $self; |
| 88 |
} |
| 89 |
|
| 90 |
sub check_options { |
| 91 |
my ($self, %options) = @_; |
| 92 |
$self->SUPER::check_options(%options); |
| 93 |
} |
| 94 |
|
| 95 |
sub set_counters { |
| 96 |
my ($self, %options) = @_; |
| 97 |
|
| 98 |
$self->{maps_counters_type} = [ |
| 99 |
{ name => 'orphaned_resources', type => 0 }, |
| 100 |
{ name => 'orphaned_disks', type => 0, cb_prefix_output => 'prefix_disks_output' }, |
| 101 |
{ name => 'orphaned_nics', type => 0, cb_prefix_output => 'prefix_nics_output' }, |
| 102 |
{ name => 'orphaned_nsgs', type => 0, cb_prefix_output => 'prefix_nsgs_output' }, |
| 103 |
{ name => 'orphaned_publicips', type => 0, cb_prefix_output => 'prefix_publicips_output' }, |
| 104 |
{ name => 'orphaned_routetables', type => 0, cb_prefix_output => 'prefix_routetables_output' }, |
| 105 |
{ name => 'orphaned_snapshots', type => 0, cb_prefix_output => 'prefix_snapshots_output' } |
| 106 |
]; |
| 107 |
|
| 108 |
# nics, nsgs, public-ips, route-tables, snapshots |
| 109 |
$self->{maps_counters}->{orphaned_resources} = [ |
| 110 |
{ label => 'orphaned-resources', nlabel => 'azure.resources.orphaned.count', set => { |
| 111 |
key_values => [ { name => 'count' }, { name => 'total'} ], |
| 112 |
output_template => 'Orphaned resources: %s', |
| 113 |
perfdatas => [ |
| 114 |
{ template => '%d', min => 0, max => 'total' } |
| 115 |
] |
| 116 |
} |
| 117 |
} |
| 118 |
]; |
| 119 |
$self->{maps_counters}->{orphaned_disks} = [ |
| 120 |
{ label => 'orphaned-managed-disks', display_ok => 0, nlabel => 'azure.manageddisks.orphaned.count', set => { |
| 121 |
key_values => [ { name => 'count' }, { name => 'total' } ], |
| 122 |
closure_custom_output => $self->can('custom_orphaned_output'), |
| 123 |
perfdatas => [ |
| 124 |
{ template => '%d', min => 0, max => 'total' } |
| 125 |
] |
| 126 |
} |
| 127 |
} |
| 128 |
]; |
| 129 |
$self->{maps_counters}->{orphaned_nics} = [ |
| 130 |
{ label => 'orphaned-nics', display_ok => 0, nlabel => 'azure.nics.orphaned.count', set => { |
| 131 |
key_values => [ { name => 'count' }, { name => 'total' } ], |
| 132 |
closure_custom_output => $self->can('custom_orphaned_output'), |
| 133 |
perfdatas => [ |
| 134 |
{ template => '%d', min => 0, max => 'total' } |
| 135 |
] |
| 136 |
} |
| 137 |
} |
| 138 |
]; |
| 139 |
$self->{maps_counters}->{orphaned_nsgs} = [ |
| 140 |
{ label => 'orphaned-nsgs', display_ok => 0, nlabel => 'azure.nsgs.orphaned.count', set => { |
| 141 |
key_values => [ { name => 'count' }, { name => 'total' } ], |
| 142 |
closure_custom_output => $self->can('custom_orphaned_output'), |
| 143 |
perfdatas => [ |
| 144 |
{ template => '%d', min => 0, max => 'total' } |
| 145 |
] |
| 146 |
} |
| 147 |
} |
| 148 |
]; |
| 149 |
$self->{maps_counters}->{orphaned_publicips} = [ |
| 150 |
{ label => 'orphaned-publicips', display_ok => 0, nlabel => 'azure.publicips.orphaned.count', set => { |
| 151 |
key_values => [ { name => 'count' }, { name => 'total' } ], |
| 152 |
closure_custom_output => $self->can('custom_orphaned_output'), |
| 153 |
perfdatas => [ |
| 154 |
{ template => '%d', min => 0, max => 'total' } |
| 155 |
] |
| 156 |
} |
| 157 |
} |
| 158 |
]; |
| 159 |
$self->{maps_counters}->{orphaned_routetables} = [ |
| 160 |
{ label => 'orphaned-routetables', display_ok => 0, nlabel => 'azure.routetables.orphaned.count', set => { |
| 161 |
key_values => [ { name => 'count' }, { name => 'total' } ], |
| 162 |
closure_custom_output => $self->can('custom_orphaned_output'), |
| 163 |
perfdatas => [ |
| 164 |
{ template => '%d', min => 0, max => 'total' } |
| 165 |
] |
| 166 |
} |
| 167 |
} |
| 168 |
]; |
| 169 |
$self->{maps_counters}->{orphaned_snapshots} = [ |
| 170 |
{ label => 'orphaned-snapshots', display_ok => 0, nlabel => 'azure.snapshots.orphaned.count', set => { |
| 171 |
key_values => [ { name => 'count' }, { name => 'total' } ], |
| 172 |
closure_custom_output => $self->can('custom_orphaned_output'), |
| 173 |
perfdatas => [ |
| 174 |
{ template => '%d', min => 0, max => 'total'} |
| 175 |
] |
| 176 |
} |
| 177 |
} |
| 178 |
]; |
| 179 |
} |
| 180 |
|
| 181 |
sub manage_selection { |
| 182 |
my ($self, %options) = @_; |
| 183 |
|
| 184 |
my $resultset; |
| 185 |
my @item_list; |
| 186 |
$self->{orphaned_resources}->{count} = 0; |
| 187 |
$self->{orphaned_resources}->{total} = 0; |
| 188 |
|
| 189 |
# orphan managed disks |
| 190 |
if (!defined($self->{option_results}->{skip_managed_disks})) { |
| 191 |
$self->{orphaned_disks}->{count} = 0; |
| 192 |
$self->{orphaned_disks}->{total} = 0; |
| 193 |
$resultset = $options{custom}->azure_list_compute_disks( |
| 194 |
resource_group => $self->{option_results}->{resource_group}, |
| 195 |
force_api_version => "2022-07-02" |
| 196 |
); |
| 197 |
foreach my $item (@{$resultset}) { |
| 198 |
next if (defined($self->{option_results}->{exclude_name}) && $self->{option_results}->{exclude_name} ne '' |
| 199 |
&& $item->{name} =~ /$self->{option_results}->{exclude_name}/); |
| 200 |
$self->{orphaned_disks}->{total}++; |
| 201 |
$self->{orphaned_resources}->{total}++; |
| 202 |
next if ($item->{properties}->{diskState} !~ /Unattached/); |
| 203 |
$self->{orphaned_disks}->{count}++; |
| 204 |
$self->{orphaned_resources}->{count}++; |
| 205 |
push @item_list, $item->{name}; |
| 206 |
} |
| 207 |
if (scalar @item_list != 0) { |
| 208 |
$self->{output}->output_add(long_msg => "Managed Disks orphaned list:" . "[" . join(", ", @item_list) . "]"); |
| 209 |
} |
| 210 |
@item_list = (); |
| 211 |
} |
| 212 |
|
| 213 |
# orphan NICs |
| 214 |
if (!defined($self->{option_results}->{skip_nics})) { |
| 215 |
$self->{orphaned_nics}->{count} = 0; |
| 216 |
$self->{orphaned_nics}->{total} = 0; |
| 217 |
$resultset = $options{custom}->azure_list_nics( |
| 218 |
resource_group => $self->{option_results}->{resource_group}, |
| 219 |
force_api_version => "2022-05-01" |
| 220 |
); |
| 221 |
foreach my $item (@{$resultset}) { |
| 222 |
next if (defined($self->{option_results}->{exclude_name}) && $self->{option_results}->{exclude_name} ne '' |
| 223 |
&& $item->{name} =~ /$self->{option_results}->{exclude_name}/); |
| 224 |
$self->{orphaned_nics}->{total}++; |
| 225 |
$self->{orphaned_resources}->{total}++; |
| 226 |
next if (scalar(keys %{$item->{properties}->{virtualMachine}}) != 0 || defined($item->{properties}->{privateEndpoint})); |
| 227 |
$self->{orphaned_nics}->{count}++; |
| 228 |
$self->{orphaned_resources}->{count}++; |
| 229 |
push @item_list, $item->{name}; |
| 230 |
} |
| 231 |
if (scalar @item_list != 0) { |
| 232 |
$self->{output}->output_add(long_msg => "NICs orphaned list:" . "[" . join(", ", @item_list) . "]"); |
| 233 |
} |
| 234 |
@item_list = (); |
| 235 |
} |
| 236 |
|
| 237 |
# orphan NSGs |
| 238 |
if (!defined($self->{option_results}->{skip_nsgs})) { |
| 239 |
$self->{orphaned_nsgs}->{count} = 0; |
| 240 |
$self->{orphaned_nsgs}->{total} = 0; |
| 241 |
|
| 242 |
$resultset = $options{custom}->azure_list_nsgs( |
| 243 |
resource_group => $self->{option_results}->{resource_group}, |
| 244 |
force_api_version => "2022-05-01" |
| 245 |
); |
| 246 |
foreach my $item (@{$resultset}) { |
| 247 |
next if (defined($self->{option_results}->{exclude_name}) && $self->{option_results}->{exclude_name} ne '' |
| 248 |
&& $item->{name} =~ /$self->{option_results}->{exclude_name}/); |
| 249 |
$self->{orphaned_nsgs}->{total}++; |
| 250 |
$self->{orphaned_resources}->{total}++; |
| 251 |
next if (defined($item->{properties}->{subnets}) && scalar($item->{properties}->{subnets}) != 0); |
| 252 |
next if (defined($item->{properties}->{networkInterface}) && scalar($item->{properties}->{networkInterface}) != 0); |
| 253 |
next if (defined($item->{properties}->{networkInterfaces}) && scalar($item->{properties}->{networkInterfaces}) != 0); |
| 254 |
$self->{orphaned_nsgs}->{count}++; |
| 255 |
$self->{orphaned_resources}->{count}++; |
| 256 |
push @item_list, $item->{name}; |
| 257 |
} |
| 258 |
if (scalar @item_list != 0) { |
| 259 |
$self->{output}->output_add(long_msg => "NSGs orphaned list:" . "[" . join(", ", @item_list) . "]"); |
| 260 |
} |
| 261 |
@item_list = (); |
| 262 |
} |
| 263 |
|
| 264 |
# orphan public IPs |
| 265 |
if (!defined($self->{option_results}->{skip_public_ips})) { |
| 266 |
$self->{orphaned_publicips}->{count} = 0; |
| 267 |
$self->{orphaned_publicips}->{total} = 0; |
| 268 |
$resultset = $options{custom}->azure_list_publicips( |
| 269 |
resource_group => $self->{option_results}->{resource_group}, |
| 270 |
force_api_version => "2022-01-01" |
| 271 |
); |
| 272 |
foreach my $item (@{$resultset}) { |
| 273 |
next if (defined($self->{option_results}->{exclude_name}) && $self->{option_results}->{exclude_name} ne '' |
| 274 |
&& $item->{name} =~ /$self->{option_results}->{exclude_name}/); |
| 275 |
$self->{orphaned_publicips}->{total}++; |
| 276 |
$self->{orphaned_resources}->{total}++; |
| 277 |
next if (defined($item->{properties}->{ipConfiguration}) && scalar($item->{properties}->{ipConfiguration}) != 0); |
| 278 |
$self->{orphaned_publicips}->{count}++; |
| 279 |
$self->{orphaned_resources}->{count}++; |
| 280 |
push @item_list, $item->{name}; |
| 281 |
} |
| 282 |
if (scalar @item_list != 0) { |
| 283 |
$self->{output}->output_add(long_msg => "Public IPs orphaned list:" . "[" . join(", ", @item_list) . "]"); |
| 284 |
} |
| 285 |
@item_list = (); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
# orphan route tables |
| 291 |
if (!defined($self->{option_results}->{skip_route_tables})) { |
| 292 |
$self->{orphaned_routetables}->{count} = 0; |
| 293 |
$self->{orphaned_routetables}->{total} = 0; |
| 294 |
$resultset = $options{custom}->azure_list_route_tables( |
| 295 |
resource_group => $self->{option_results}->{resource_group}, |
| 296 |
force_api_version => "2022-01-01" |
| 297 |
); |
| 298 |
foreach my $item (@{$resultset}) { |
| 299 |
next if (defined($self->{option_results}->{exclude_name}) && $self->{option_results}->{exclude_name} ne '' |
| 300 |
&& $item->{name} =~ /$self->{option_results}->{exclude_name}/); |
| 301 |
$self->{orphaned_routetables}->{total}++; |
| 302 |
$self->{orphaned_resources}->{total}++; |
| 303 |
next if (defined($item->{properties}->{subnets}) && scalar($item->{properties}->{subnets}) != 0); |
| 304 |
$self->{orphaned_routetables}->{count}++; |
| 305 |
$self->{orphaned_resources}->{count}++; |
| 306 |
push @item_list, $item->{name}; |
| 307 |
} |
| 308 |
if (scalar @item_list != 0) { |
| 309 |
$self->{output}->output_add(long_msg => "Route tables orphaned list:" . "[" . join(", ", @item_list) . "]"); |
| 310 |
} |
| 311 |
@item_list = (); |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
# orphan snapshots |
| 316 |
if (!defined($self->{option_results}->{skip_snapshots})) { |
| 317 |
$self->{orphaned_snapshots}->{count} = 0; |
| 318 |
$self->{orphaned_snapshots}->{total} = 0; |
| 319 |
$resultset = $options{custom}->azure_list_snapshots( |
| 320 |
resource_group => $self->{option_results}->{resource_group}, |
| 321 |
force_api_version => "2021-12-01" |
| 322 |
); |
| 323 |
foreach my $item (@{$resultset}) { |
| 324 |
next if (defined($self->{option_results}->{exclude_name}) && $self->{option_results}->{exclude_name} ne '' |
| 325 |
&& $item->{name} =~ /$self->{option_results}->{exclude_name}/); |
| 326 |
$self->{orphaned_snapshots}->{total}++; |
| 327 |
$self->{orphaned_resources}->{total}++; |
| 328 |
next if (defined($item->{properties}->{subnets}) && scalar($item->{properties}->{subnets}) != 0); |
| 329 |
$self->{orphaned_snapshots}->{count}++; |
| 330 |
$self->{orphaned_resources}->{count}++; |
| 331 |
push @item_list, $item->{name}; |
| 332 |
} |
| 333 |
if (scalar @item_list != 0) { |
| 334 |
$self->{output}->output_add(long_msg => "Snapshots orphaned list:" . "[" . join(", ", @item_list) . "]"); |
| 335 |
} |
| 336 |
@item_list = (); |
| 337 |
} |
| 338 |
|
| 339 |
} |
| 340 |
|
| 341 |
1; |
| 342 |
|
| 343 |
__END__ |
| 344 |
|
| 345 |
=head1 MODE |
| 346 |
|
| 347 |
Check orphaned resource within an Azure subscription. |
| 348 |
|
| 349 |
Example: |
| 350 |
perl centreon_plugins.pl --plugin=cloud::azure::management::costs::plugin --custommode=api --mode=orphan-resource |
| 351 |
{--resource-group='MYRESOURCEGROUP'] --exclude-name='MyDisk|DataDisk.*' [--skip-managed-disks] [--skip-nics] [--skip-nsgs] [--skip-public-ips] [--skip-route-tables] [--skip-snapshots] |
| 352 |
|
| 353 |
Adding --verbose will display the item names. |
| 354 |
|
| 355 |
=over 8 |
| 356 |
|
| 357 |
=item B<--resource-group> |
| 358 |
|
| 359 |
Set resource group. |
| 360 |
|
| 361 |
=item B<--exclude-name> |
| 362 |
|
| 363 |
Exclude resource from check (can be a regexp). |
| 364 |
|
| 365 |
=item B<--warning-*> |
| 366 |
|
| 367 |
Warning threshold on the number of orphaned resources. |
| 368 |
Substitue '*' by the resource type amongst this list: |
| 369 |
(orphaned-snapshots orphaned-routetables orphaned-managed-disks orphaned-nsgs orphaned-nics orphaned-resources orphaned-publicips) |
| 370 |
|
| 371 |
=îtem B<--critical-*> |
| 372 |
|
| 373 |
Critical threshold on the number of orphaned resources. |
| 374 |
Substitue '*' by the resource type amongst this list: |
| 375 |
(orphaned-snapshots orphaned-routetables orphaned-managed-disks orphaned-nsgs orphaned-nics orphaned-resources orphaned-publicips) |
| 376 |
|
| 377 |
=item B<--skip-*> |
| 378 |
|
| 379 |
Skip a specific kind of resource. Can be multiple. |
| 380 |
|
| 381 |
Accepted values: disks, nics, nsgs, public-ips, route-tables, snapshots |
| 382 |
|
| 383 |
=back |
| 384 |
|
| 385 |
=cut |