Debian Codegraph @

src/cloud/azure/management/costs/mode/hybridbenefits.pm

raw
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::hybridbenefits;
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(" resources without hybrid benefits %s (out of: %s)", $self->{result_values}->{count}, $self->{result_values}->{total});
32
33 return $msg;
34 }
35
36 sub prefix_vm_output {
37 my ($self, %options) = @_;
38
39 return 'Virtual machines';
40 }
41
42 sub prefix_sql_vm_output {
43 my ($self, %options) = @_;
44
45 return 'SQL Virtual machines';
46 }
47
48 sub prefix_sql_db_output {
49 my ($self, %options) = @_;
50
51 return 'SQL Databases';
52 }
53
54 sub prefix_elasticpool_output {
55 my ($self, %options) = @_;
56
57 return 'SQL Elastic Pools';
58 }
59
60 sub new {
61 my ($class, %options) = @_;
62 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
63 bless $self, $class;
64
65 $options{options}->add_options(arguments => {
66 'skip-vm' => { name => 'skip_vm' },
67 'skip-sql-vm' => { name => 'skip_sql_vm' },
68 'skip-sql-database' => { name => 'skip_sql_database' },
69 'skip-elastic-pool' => { name => 'skip_elastic_pool' },
70 'exclude-name:s' => { name => 'exclude_name' }
71 });
72
73 return $self;
74 }
75
76 sub set_counters {
77 my ($self, %options) = @_;
78
79 $self->{maps_counters_type} = [
80 { name => 'nohybridbenefits_resources', type => 0 },
81 { name => 'nohybridbenefits_vm', type => 0, cb_prefix_output => 'prefix_vm_output' },
82 { name => 'nohybridbenefits_sql_vm', type => 0, cb_prefix_output => 'prefix_sql_vm_output' },
83 { name => 'nohybridbenefits_sql_db', type => 0, cb_prefix_output => 'prefix_sql_db_output' },
84 { name => 'nohybridbenefits_elasticpool', type => 0, cb_prefix_output => 'prefix_elasticpool_output' }
85 ];
86
87 # vm, sql-vm, sql-database, elastic-pool
88 $self->{maps_counters}->{nohybridbenefits_resources} = [
89 { label => 'resources', nlabel => 'azure.resources.nohybridbenefits.count', set => {
90 key_values => [ { name => 'count' }, { name => 'total' } ],
91 output_template => 'Resources without hybrid benefits: %s',
92 perfdatas => [
93 { template => '%d', min => 0, max => 'total' }
94 ]
95 }
96 }
97 ];
98 $self->{maps_counters}->{nohybridbenefits_vm} = [
99 { label => 'vm', display_ok => 0, nlabel => 'azure.vm.nohybridbenefits.count', set => {
100 key_values => [ { name => 'count' }, { name => 'total' } ],
101 closure_custom_output => $self->can('custom_hybridbenefits_output'),
102 perfdatas => [
103 { template => '%d', min => 0, max => 'total' }
104 ]
105 }
106 }
107 ];
108 $self->{maps_counters}->{nohybridbenefits_sql_vm} = [
109 { label => 'sql-vm', display_ok => 0, nlabel => 'azure.sqlvm.nohybridbenefits.count', set => {
110 key_values => [ { name => 'count' }, { name => 'total' } ],
111 closure_custom_output => $self->can('custom_hybridbenefits_output'),
112 perfdatas => [
113 { template => '%d', min => 0, max => 'total' }
114 ]
115 }
116 }
117 ];
118 $self->{maps_counters}->{nohybridbenefits_sql_db} = [
119 { label => 'sql-database', display_ok => 0, nlabel => 'azure.sqldatabase.nohybridbenefits.count', set => {
120 key_values => [ { name => 'count' }, { name => 'total' } ],
121 closure_custom_output => $self->can('custom_hybridbenefits_output'),
122 perfdatas => [
123 { template => '%d', min => 0, max => 'total' }
124 ]
125 }
126 }
127 ];
128 $self->{maps_counters}->{nohybridbenefits_elasticpool} = [
129 { label => 'elastic-pool', display_ok => 0, nlabel => 'azure.elasticpool.nohybridbenefits.count', set => {
130 key_values => [ { name => 'count' }, { name => 'total' } ],
131 closure_custom_output => $self->can('custom_hybridbenefits_output'),
132 perfdatas => [
133 { template => '%d', min => 0, max => 'total' }
134 ]
135 }
136 }
137 ];
138 }
139
140 sub check_options {
141 my ($self, %options) = @_;
142 $self->SUPER::check_options(%options);
143 }
144
145 sub manage_selection {
146 my ($self, %options) = @_;
147
148 my $resultset;
149 my @item_list;
150 $self->{nohybridbenefits_resources}->{count} = 0;
151 $self->{nohybridbenefits_resources}->{total} = 0;
152
153 # VMs
154 if (!defined($self->{option_results}->{skip_vm})) {
155 $self->{nohybridbenefits_vm}->{count} = 0;
156 $self->{nohybridbenefits_vm}->{total} = 0;
157 $resultset = $options{custom}->azure_list_vms(
158 resource_group => $self->{option_results}->{resource_group},
159 force_api_version => "2022-03-01"
160 );
161
162 foreach my $item (@{ $resultset}) {
163 next if (defined($self->{option_results}->{exclude_name}) && $self->{option_results}->{exclude_name} ne ''
164 && $item->{name} =~ /$self->{option_results}->{exclude_name}/);
165 $self->{vm_hybrid_benefits}->{total}++;
166 $self->{nohybridbenefits_resources}->{total}++;
167 next if (!defined($item->{properties}->{licenseType}) || (defined($item->{properties}->{licenseType}) && $item->{properties}->{licenseType} !~ /None/));
168 $self->{nohybridbenefits_vm}->{count}++;
169 $self->{nohybridbenefits_resources}->{count}++;
170 push @item_list, $item->{name};
171 }
172 if (scalar @item_list != 0) {
173 $self->{output}->output_add(long_msg => "Virtual Machines withtout hybrid benefits:" . "[" . join(", ", @item_list) . "]");
174 }
175 @item_list = ();
176 }
177
178 # SQL VMs
179 if (!defined($self->{option_results}->{skip_sql_vm})) {
180 $self->{nohybridbenefits_sql_vm}->{count} = 0;
181 $self->{nohybridbenefits_sql_vm}->{total} = 0;
182 $resultset = $options{custom}->azure_list_sqlvms(
183 resource_group => $self->{option_results}->{resource_group},
184 force_api_version => "2022-02-01"
185 );
186 foreach my $item (@{ $resultset}) {
187 next if (defined($self->{option_results}->{exclude_name}) && $self->{option_results}->{exclude_name} ne ''
188 && $item->{name} =~ /$self->{option_results}->{exclude_name}/);
189 $self->{nohybridbenefits_sql_vm}->{total}++;
190 $self->{nohybridbenefits_resources}->{total}++;
191 next if ($item->{properties}->{sqlServerLicenseType } =~ /AHUB/ || $item->{properties}->{sqlImageSku} =~ /Express/);
192 $self->{nohybridbenefits_sql_vm}->{count}++;
193 $self->{nohybridbenefits_resources}->{count}++;
194 push @item_list, $item->{name};
195 }
196 if (scalar @item_list != 0) {
197 $self->{output}->output_add(long_msg => "SQL Virtual Machines withtout hybrid benefits:" . "[" . join(", ", @item_list) . "]");
198 }
199 @item_list = ();
200
201 }
202
203 if (!defined($self->{option_results}->{skip_sql_vm}) || !defined($self->{option_results}->{skip_elastic_pool})) {
204 my @item_list_sql;
205 my @item_list_elastic;
206 $resultset = $options{custom}->azure_list_sqlservers(
207 resource_group => $self->{option_results}->{resource_group},
208 force_api_version => "2021-11-01"
209 );
210
211 $self->{nohybridbenefits_elasticpool}->{count} = 0;
212 $self->{nohybridbenefits_elasticpool}->{total} = 0;
213 $self->{nohybridbenefits_sql_db}->{count} = 0;
214 $self->{nohybridbenefits_sql_db}->{total} = 0;
215
216 foreach my $item (@{$resultset}) {
217 my @sqlserver_id = split /\//, $item->{id};
218
219 # SQL databases
220 if (!defined($self->{option_results}->{skip_sql_database})) {
221 my $resultset_sql = $options{custom}->azure_list_sqldatabases(
222 resource_group => $sqlserver_id[4],
223 server => $item->{name},
224 force_api_version => "2021-11-01"
225 );
226
227 foreach my $item_sql (@{ $resultset_sql}) {
228 next if ($item_sql->{properties}->{currentSku}->{name} =~ /ElasticPool/);
229 next if (defined($item_sql->{properties}->{currentSku}->{tier}) && $item_sql->{properties}->{currentSku}->{tier} !~ /GeneralPurpose/);
230 next if (defined($self->{option_results}->{exclude_name}) && $self->{option_results}->{exclude_name} ne ''
231 && $item_sql->{name} =~ /$self->{option_results}->{exclude_name}/);
232 next if (defined($item_sql->{properties}->{licenseType}) && $item_sql->{properties}->{licenseType} eq "BasePrice");
233 $self->{nohybridbenefits_sql_db}->{total}++;
234 $self->{nohybridbenefits_resources}->{total}++;
235
236 if (defined($item_sql->{properties}->{licenseType})) {
237 $self->{nohybridbenefits_sql_db}->{count}++;
238 $self->{nohybridbenefits_resources}->{count}++;
239 push @item_list_sql, $item_sql->{name};
240 }
241 }
242 }
243
244 # SQL Elastic pools
245 if (!defined($self->{option_results}->{skip_elastic_pool})) {
246 my $resultset_elastic = $options{custom}->azure_list_sqlelasticpools(
247 resource_group => $sqlserver_id[4],
248 server => $item->{name},
249 force_api_version => "2021-11-01"
250 );
251
252 foreach my $item_ep (@{$resultset_elastic}) {
253 next if (defined($self->{option_results}->{exclude_name}) && $self->{option_results}->{exclude_name} ne ''
254 && $item_ep->{name} =~ /$self->{option_results}->{exclude_name}/);
255 $self->{nohybridbenefits_elasticpool}->{total}++;
256 $self->{nohybridbenefits_resources}->{total}++;
257 next if (defined($item_ep->{properties}->{licenseType}) && $item_ep->{properties}->{licenseType} =~ /BasePrice/);
258 $self->{nohybridbenefits_elasticpool}->{count}++;
259 $self->{nohybridbenefits_resources}->{count}++;
260 push @item_list_elastic, $item_ep->{name};
261 }
262 }
263 }
264 if (scalar @item_list_sql != 0) {
265 $self->{output}->output_add(long_msg => "SQL Databases withtout hybrid benefits:" . "[" . join(", ", @item_list_sql) . "]");
266 }
267 if (scalar @item_list_elastic != 0) {
268 $self->{output}->output_add(long_msg => "SQL Elastic pools withtout hybrid benefits:" . "[" . join(", ", @item_list_elastic) . "]");
269 }
270 }
271 }
272
273 1;
274
275 __END__
276
277 =head1 MODE
278
279 Check if hybrid benefits is enabled on eligible resources.
280
281 Example:
282 perl centreon_plugins.pl --plugin=cloud::azure::management::costs::plugin --custommode=api --mode=hybrid-benefits
283 {--resource-group='MYRESOURCEGROUP'] --exclude-name='MyDb|MyEpool.*' [--skip-vm] [--skip-sql-vm] [--skip-sql-database] [--skip-sql-elastic-pool] [--show-details --verbose]
284
285 Adding --verbose will display the item names.
286
287 =over 8
288
289 =item B<--resource-group>
290
291 Set resource group.
292
293 =item B<--exclude-name>
294
295 Exclude resource from check (can be a regexp).
296
297 =item B<--warning-*>
298
299 Warning threshold on the number of orphaned resources.
300 Substitue '*' by the resource type amongst this list:
301 ( elastic-pool sql-database vm sql-vm resources)
302
303 =îtem B<--critical-*>
304
305 Critical threshold on the number of orphaned resources.
306 Substitue '*' by the resource type amongst this list:
307 (elastic-pool sql-database vm sql-vm resources)
308
309 =item B<--skip-*>
310
311 Skip a specific kind of resource. Can be multiple.
312
313 Accepted values: vm, sql-vm, sql-database, elastic-pool
314
315 =back
316
317 =back
318
319 =cut