Debian Codegraph @

src/cloud/azure/common/storageaccount/transactionsthroughput.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::transactionsthroughput;
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 my $msg = "Resource '" . $options{instance_value}->{display} . "'";
32 $msg .= " (" . $options{instance_value}->{storagetype} . ")" if ($options{instance_value}->{storagetype} ne '');
33 $msg .= " " . $options{instance_value}->{stat} . " ";
34
35 return $msg;
36 }
37
38 sub custom_metric_calc {
39 my ($self, %options) = @_;
40
41 $self->{result_values}->{stat} = $options{new_datas}->{$self->{instance} . '_stat'};
42 $self->{result_values}->{metric_perf} = lc($options{extra_options}->{metric_perf});
43 $self->{result_values}->{metric_label} = lc($options{extra_options}->{metric_label});
44 $self->{result_values}->{metric_name} = $options{extra_options}->{metric_name};
45 $self->{result_values}->{timeframe} = $options{new_datas}->{$self->{instance} . '_timeframe'};
46 $self->{result_values}->{value} = $options{new_datas}->{$self->{instance} . '_' . $self->{result_values}->{metric_perf} . '_' . $self->{result_values}->{stat}};
47 $self->{result_values}->{value_per_sec} = $self->{result_values}->{value} / $self->{result_values}->{timeframe};
48 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
49 $self->{result_values}->{storagetype} = $options{new_datas}->{$self->{instance} . '_storagetype'};
50 return 0;
51 }
52
53 sub custom_metric_threshold {
54 my ($self, %options) = @_;
55
56 my $exit = $self->{perfdata}->threshold_check(value => defined($self->{instance_mode}->{option_results}->{per_sec}) ? $self->{result_values}->{value_per_sec} : $self->{result_values}->{value},
57 threshold => [ { label => 'critical-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}, exit_litteral => 'critical' },
58 { label => 'warning-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}, exit_litteral => 'warning' } ]);
59 return $exit;
60 }
61
62 sub custom_usage_perfdata {
63 my ($self, %options) = @_;
64
65 my $extra_label = '';
66 $extra_label = '_' . lc($self->{result_values}->{display}) if (!defined($options{extra_instance}) || $options{extra_instance} != 0);
67
68 $self->{output}->perfdata_add(label => $self->{result_values}->{metric_perf} . "_" . $self->{result_values}->{stat} . $extra_label,
69 unit => defined($self->{instance_mode}->{option_results}->{per_sec}) ? 'B/s' : 'B',
70 value => sprintf("%.2f", defined($self->{instance_mode}->{option_results}->{per_sec}) ? $self->{result_values}->{value_per_sec} : $self->{result_values}->{value}),
71 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}),
72 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{result_values}->{metric_label} . "-" . $self->{result_values}->{stat}),
73 min => 0
74 );
75 }
76
77 sub custom_usage_output {
78 my ($self, %options) = @_;
79 my $msg = "";
80
81 if (defined($self->{instance_mode}->{option_results}->{per_sec})) {
82 my ($value, $unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{value_per_sec});
83 $msg = $self->{result_values}->{metric_name} . ": " . $value . ' ' . $unit . "/s";
84 } else {
85 my ($value, $unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{value});
86 $msg = $self->{result_values}->{metric_name} . ": " . $value . ' ' . $unit;
87 }
88 return $msg;
89 }
90
91 sub set_counters {
92 my ($self, %options) = @_;
93
94 $self->{maps_counters_type} = [
95 { name => 'metric', type => 1, cb_prefix_output => 'prefix_metric_output', message_multiple => "All transactions metrics are ok", skipped_code => { -10 => 1 } },
96 ];
97
98 foreach my $aggregation ('minimum', 'maximum', 'average', 'total') {
99 foreach my $metric ('Ingress', 'Egress') {
100 my $metric_label = lc($metric);
101 my $entry = { label => $metric_label . '-' . $aggregation, set => {
102 key_values => [ { name => $metric_label . '_' . $aggregation }, { name => 'display' },
103 { name => 'stat' }, { name => 'timeframe' }, { name => 'storagetype' } ],
104 closure_custom_calc => $self->can('custom_metric_calc'),
105 closure_custom_calc_extra_options => { metric_perf => $metric_label,
106 metric_label => $metric_label, metric_name => $metric },
107 closure_custom_output => $self->can('custom_usage_output'),
108 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
109 closure_custom_threshold_check => $self->can('custom_metric_threshold'),
110 }
111 };
112 push @{$self->{maps_counters}->{metric}}, $entry;
113 }
114 }
115 }
116
117 sub new {
118 my ($class, %options) = @_;
119 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
120 bless $self, $class;
121
122 $options{options}->add_options(arguments => {
123 "resource:s@" => { name => 'resource' },
124 "resource-group:s" => { name => 'resource_group' },
125 "resource-namespace:s" => { name => 'resource_namespace' },
126 "storage-type:s" => { name => 'storage_type' },
127 "filter-metric:s" => { name => 'filter_metric' },
128 "per-sec" => { name => 'per_sec' },
129 });
130
131 return $self;
132 }
133
134 sub check_options {
135 my ($self, %options) = @_;
136 $self->SUPER::check_options(%options);
137
138 if (!defined($self->{option_results}->{resource})) {
139 $self->{output}->add_option_msg(short_msg => "Need to specify either --resource <name> with --resource-group option or --resource <id>.");
140 $self->{output}->option_exit();
141 }
142
143 $self->{az_resource} = $self->{option_results}->{resource};
144 $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group}));
145 $self->{az_resource_type} = 'storageAccounts';
146 $self->{az_resource_namespace} = defined($self->{option_results}->{resource_namespace}) ? $self->{option_results}->{resource_namespace} : 'Microsoft.Storage';
147 $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 900;
148 $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT5M";
149 $self->{az_storage_type} = defined($self->{option_results}->{storage_type}) ? $self->{option_results}->{storage_type} : '';
150 $self->{az_aggregations} = ['Total'];
151 if (defined($self->{option_results}->{aggregation})) {
152 $self->{az_aggregations} = [];
153 foreach my $stat (@{$self->{option_results}->{aggregation}}) {
154 if ($stat ne '') {
155 push @{$self->{az_aggregations}}, ucfirst(lc($stat));
156 }
157 }
158 }
159
160 foreach my $metric ('Ingress', 'Egress') {
161 next if (defined($self->{option_results}->{filter_metric}) && $self->{option_results}->{filter_metric} ne ''
162 && $metric !~ /$self->{option_results}->{filter_metric}/);
163
164 push @{$self->{az_metrics}}, $metric;
165 }
166 }
167
168 sub manage_selection {
169 my ($self, %options) = @_;
170
171 my %metric_results;
172 foreach my $resource (@{$self->{az_resource}}) {
173 my $resource_group = $self->{az_resource_group};
174 my $resource_name = $resource;
175 my $namespace = $self->{az_resource_namespace};
176 my $storagetype_full = ($self->{az_storage_type} ne '' && lc($self->{az_storage_type}) ne 'account' ) ? '/' . lc($self->{az_storage_type}) . 'Services/default' : '';
177 my $storagetype_name = ($self->{az_storage_type} ne '') ? $self->{az_storage_type} : "Account";
178 if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/(.*)\/storageAccounts\/(.*)\/(.*)\/default$/ ||
179 $resource =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/(.*)\/storageAccounts\/(.*)$/) {
180 $resource_group = $1;
181 $namespace = $2;
182 $resource_name = $3;
183 $storagetype_full = '/' . $4 . '/default' if(defined($4));
184 $storagetype_name = $4 if(defined($4));
185 }
186 $storagetype_name =~ s/Services//g;
187
188 ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics(
189 resource => $resource_name . $storagetype_full,
190 resource_group => $resource_group,
191 resource_type => $self->{az_resource_type},
192 resource_namespace => $namespace,
193 metrics => $self->{az_metrics},
194 aggregations => $self->{az_aggregations},
195 timeframe => $self->{az_timeframe},
196 interval => $self->{az_interval},
197 );
198
199 foreach my $metric (@{$self->{az_metrics}}) {
200 my $metric_name = lc($metric);
201 $metric_name =~ s/ /_/g;
202 foreach my $aggregation (@{$self->{az_aggregations}}) {
203 next if (!defined($metric_results{$resource_name}->{$metric_name}->{lc($aggregation)}) && !defined($self->{option_results}->{zeroed}));
204
205 $self->{metric}->{$resource_name . "_" . lc($aggregation)}->{display} = $resource_name;
206 $self->{metric}->{$resource_name . "_" . lc($aggregation)}->{timeframe} = $self->{az_timeframe};
207 $self->{metric}->{$resource_name . "_" . lc($aggregation)}->{stat} = lc($aggregation);
208 $self->{metric}->{$resource_name . "_" . lc($aggregation)}->{storagetype} = ucfirst($storagetype_name);
209 $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;
210 }
211 }
212 }
213
214 if (scalar(keys %{$self->{metric}}) <= 0) {
215 $self->{output}->add_option_msg(short_msg => 'No metrics. Check your options or use --zeroed option to set 0 on undefined values');
216 $self->{output}->option_exit();
217 }
218 }
219
220 1;
221
222 __END__
223
224 =head1 MODE
225
226 Check storage account resources transaction throughput metrics.
227
228 Example:
229
230 Using resource name:
231
232 perl centreon_plugins.pl --plugin=cloud::azure::storage::storageaccount::plugin --custommode=azcli --mode=transactions-throughput
233 --resource=MYFILER --resource-group=MYHOSTGROUP--resource-namespace=Blob --aggregation='total' --critical-egress-total='10' --verbose
234
235 Using resource ID:
236
237 perl centreon_plugins.pl --plugin=cloud::azure::storage::storageaccount::plugin --custommode=azcli --mode=transactions-throughput
238 --resource='/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Storage/storageAccounts/xxx'
239 --aggregation='total' --critical-egress-total='10' --verbose
240
241 Default aggregation: 'total' / All aggregations are valid.
242
243 =over 8
244
245 =item B<--resource>
246
247 Set resource name or ID (required).
248
249 =item B<--resource-group>
250
251 Set resource group (required if resource's name is used).
252
253 =item B<--resource-namespace>
254
255 Specify resource namespace. Can be: 'Microsoft.Storage' or 'Microsoft.ClassicStorage'.
256 Default: 'Microsoft.Storage'.
257
258 =item B<--storage-type>
259
260 Set storage type (can be: 'Account', 'Blob', 'File', 'Table', 'Queue').
261
262 =item B<--filter-metric>
263
264 Filter metrics (can be: 'Ingress', 'Egress') (Can be a regexp).
265
266 =item B<--warning-$metric$-$aggregation$>
267
268 Warning thresholds ($metric$ can be: 'ingress', 'egress',
269 $aggregation$ can be: 'minimum', 'maximum', 'average', 'total').
270
271 =item B<--critical-$metric$-$aggregation$>
272
273 Critical thresholds ($metric$ can be: 'ingress', 'egress',
274 $aggregation$ can be: 'minimum', 'maximum', 'average', 'total').
275
276 =item B<--per-sec>
277
278 Change the data to be unit/sec.
279
280 =back
281
282 =cut