Debian Codegraph @

src/cloud/azure/common/storageaccount/queuecapacity.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::common::storageaccount::queuecapacity;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27
28 sub prefix_metric_output {
29 my ($self, %options) = @_;
30
31 return "Resource '" . $options{instance_value}->{display} . "' " . $options{instance_value}->{stat} . " ";
32 }
33
34 sub set_counters {
35 my ($self, %options) = @_;
36
37 $self->{maps_counters_type} = [
38 { name => 'metric', type => 1, cb_prefix_output => 'prefix_metric_output', message_multiple => "All capacity metrics are ok", skipped_code => { -10 => 1 } },
39 ];
40
41 foreach my $aggregation ('average', 'total') {
42 foreach my $metric ('QueueCapacity') {
43 my $metric_label = lc($metric);
44 my $entry = { label => $metric_label . '-' . $aggregation, set => {
45 key_values => [ { name => $metric_label . '_' . $aggregation }, { name => 'display' }, { name => 'stat' } ],
46 output_template => $metric . ': %s %s',
47 output_change_bytes => 1,
48 perfdatas => [
49 { label => $metric_label . '_' . $aggregation, value => $metric_label . '_' . $aggregation ,
50 template => '%s', unit => 'B', label_extra_instance => 1, instance_use => 'display',
51 min => 0 },
52 ],
53 }
54 };
55 push @{$self->{maps_counters}->{metric}}, $entry;
56 }
57 }
58 }
59
60 sub new {
61 my ($class, %options) = @_;
62 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
63 bless $self, $class;
64
65 $options{options}->add_options(arguments =>
66 {
67 "resource:s@" => { name => 'resource' },
68 "resource-group:s" => { name => 'resource_group' },
69 "resource-namespace:s" => { name => 'resource_namespace' }
70 });
71
72 return $self;
73 }
74
75 sub check_options {
76 my ($self, %options) = @_;
77 $self->SUPER::check_options(%options);
78
79 if (!defined($self->{option_results}->{resource})) {
80 $self->{output}->add_option_msg(short_msg => "Need to specify either --resource <name> with --resource-group option or --resource <id>.");
81 $self->{output}->option_exit();
82 }
83
84 $self->{az_resource} = $self->{option_results}->{resource};
85 $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group}));
86 $self->{az_resource_type} = 'storageAccounts';
87 $self->{az_resource_namespace} = defined($self->{option_results}->{resource_namespace}) ? $self->{option_results}->{resource_namespace} : 'Microsoft.Storage';
88 $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 3600;
89 $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT1H";
90 $self->{az_aggregations} = ['Average'];
91 if (defined($self->{option_results}->{aggregation})) {
92 $self->{az_aggregations} = [];
93 foreach my $stat (@{$self->{option_results}->{aggregation}}) {
94 if ($stat ne '') {
95 push @{$self->{az_aggregations}}, ucfirst(lc($stat));
96 }
97 }
98 }
99
100 foreach my $metric ('QueueCapacity') {
101 push @{$self->{az_metrics}}, $metric;
102 }
103 }
104
105 sub manage_selection {
106 my ($self, %options) = @_;
107
108 my %metric_results;
109 foreach my $resource (@{$self->{az_resource}}) {
110 my $resource_group = $self->{az_resource_group};
111 my $resource_name = $resource;
112 my $queueservice_name = '/queueServices/default';
113 if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/(.*)\/storageAccounts\/(.*)$/) {
114 $resource_group = $1;
115 $self->{az_resource_namespace} = $2,
116 $resource_name = $3;
117 }
118
119 ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics(
120 resource => $resource_name . $queueservice_name,
121 resource_group => $resource_group,
122 resource_type => $self->{az_resource_type},
123 resource_namespace => $self->{az_resource_namespace},
124 metrics => $self->{az_metrics},
125 aggregations => $self->{az_aggregations},
126 timeframe => $self->{az_timeframe},
127 interval => $self->{az_interval},
128 );
129
130 foreach my $metric (@{$self->{az_metrics}}) {
131 my $metric_name = lc($metric);
132 $metric_name =~ s/ /_/g;
133 foreach my $aggregation (@{$self->{az_aggregations}}) {
134 next if (!defined($metric_results{$resource_name}->{$metric_name}->{lc($aggregation)}) && !defined($self->{option_results}->{zeroed}));
135
136 $self->{metric}->{$resource_name . "_" . lc($aggregation)}->{display} = $resource_name;
137 $self->{metric}->{$resource_name . "_" . lc($aggregation)}->{stat} = lc($aggregation);
138 $self->{metric}->{$resource_name . "_" . lc($aggregation)}->{$metric_name . "_" . lc($aggregation)} = defined($metric_results{$resource_name}->{$metric_name}->{lc($aggregation)}) ? $metric_results{$resource_name}->{$metric_name}->{lc($aggregation)} : 0;
139 }
140 }
141 }
142
143 if (scalar(keys %{$self->{metric}}) <= 0) {
144 $self->{output}->add_option_msg(short_msg => 'No metrics. Check your options or use --zeroed option to set 0 on undefined values');
145 $self->{output}->option_exit();
146 }
147 }
148
149 1;
150
151 __END__
152
153 =head1 MODE
154
155 Check storage account resources queue capacity metric.
156
157 Example:
158
159 Using resource name:
160
161 perl centreon_plugins.pl --plugin=cloud::azure::storage::storageaccount::plugin --custommode=azcli --mode=queue-capacity
162 --resource=MYFILER --resource-group=MYHOSTGROUP --aggregation='average' --critical-queuecapacity-average='10' --verbose
163
164 Using resource ID:
165
166 perl centreon_plugins.pl --plugin=cloud::azure::storage::storageaccount::plugin --custommode=azcli --mode=queue-capacity
167 --resource='/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Storage/storageAccounts/xxx/queueServices/default'
168 --aggregation='average' --critical-queuecapacity-average='10' --verbose
169
170 Default aggregation: 'average' / Total and average are valid.
171
172 =over 8
173
174 =item B<--resource>
175
176 Set resource name or ID (required).
177
178 =item B<--resource-group>
179
180 Set resource group (required if resource's name is used).
181
182 =item B<--resource-namespace>
183
184 Specify resource namespace. Can be: 'Microsoft.Storage' or 'Microsoft.ClassicStorage'.
185 Default: 'Microsoft.Storage'.
186
187 =item B<--warning-queuecapacity-*>
188
189 Warning thresholds (* can be: 'average', 'total').
190
191 =item B<--critical-queuecapacity-*>
192
193 Critical thresholds (* can be: 'average', 'total').
194
195 =back
196
197 =cut