Debian Codegraph @

src/cloud/azure/common/storageaccount/transactionslatency.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::transactionslatency;
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 set_counters {
39 my ($self, %options) = @_;
40
41 $self->{maps_counters_type} = [
42 { name => 'metric', type => 1, cb_prefix_output => 'prefix_metric_output', message_multiple => "All transactions metrics are ok", skipped_code => { -10 => 1 } },
43 ];
44
45 foreach my $aggregation ('minimum', 'maximum', 'average', 'total') {
46 foreach my $metric ('SuccessServerLatency', 'SuccessE2ELatency') {
47 my $metric_label = lc($metric);
48 my $entry = { label => $metric_label . '-' . $aggregation, set => {
49 key_values => [ { name => $metric_label . '_' . $aggregation }, { name => 'display' }, { name => 'stat' } ],
50 output_template => $metric . ': %.2f ms',
51 perfdatas => [
52 { label => $metric_label . '_' . $aggregation, value => $metric_label . '_' . $aggregation ,
53 template => '%.2f', unit => 'ms', label_extra_instance => 1, instance_use => 'display',
54 min => 0 },
55 ],
56 }
57 };
58 push @{$self->{maps_counters}->{metric}}, $entry;
59 }
60 }
61 }
62
63 sub new {
64 my ($class, %options) = @_;
65 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
66 bless $self, $class;
67
68 $options{options}->add_options(arguments => {
69 "resource:s@" => { name => 'resource' },
70 "resource-group:s" => { name => 'resource_group' },
71 "resource-namespace:s" => { name => 'resource_namespace' },
72 "storage-type:s" => { name => 'storage_type' },
73 "filter-metric:s" => { name => 'filter_metric' },
74 });
75
76 return $self;
77 }
78
79 sub check_options {
80 my ($self, %options) = @_;
81 $self->SUPER::check_options(%options);
82
83 if (!defined($self->{option_results}->{resource})) {
84 $self->{output}->add_option_msg(short_msg => "Need to specify either --resource <name> with --resource-group option or --resource <id>.");
85 $self->{output}->option_exit();
86 }
87
88 $self->{az_resource} = $self->{option_results}->{resource};
89 $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group}));
90 $self->{az_resource_type} = 'storageAccounts';
91 $self->{az_resource_namespace} = defined($self->{option_results}->{resource_namespace}) ? $self->{option_results}->{resource_namespace} : 'Microsoft.Storage';
92 $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 900;
93 $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT5M";
94 $self->{az_storage_type} = defined($self->{option_results}->{storage_type}) ? $self->{option_results}->{storage_type} : '';
95 $self->{az_aggregations} = ['Average'];
96 if (defined($self->{option_results}->{aggregation})) {
97 $self->{az_aggregations} = [];
98 foreach my $stat (@{$self->{option_results}->{aggregation}}) {
99 if ($stat ne '') {
100 push @{$self->{az_aggregations}}, ucfirst(lc($stat));
101 }
102 }
103 }
104
105 foreach my $metric ('SuccessServerLatency', 'SuccessE2ELatency') {
106 next if (defined($self->{option_results}->{filter_metric}) && $self->{option_results}->{filter_metric} ne ''
107 && $metric !~ /$self->{option_results}->{filter_metric}/);
108
109 push @{$self->{az_metrics}}, $metric;
110 }
111 }
112
113 sub manage_selection {
114 my ($self, %options) = @_;
115
116 my %metric_results;
117 foreach my $resource (@{$self->{az_resource}}) {
118 my $resource_group = $self->{az_resource_group};
119 my $resource_name = $resource;
120 my $namespace = $self->{az_resource_namespace};
121 my $storagetype_full = ($self->{az_storage_type} ne '' && lc($self->{az_storage_type}) ne 'account' ) ? '/' . lc($self->{az_storage_type}) . 'Services/default' : '';
122 my $storagetype_name = ($self->{az_storage_type} ne '') ? $self->{az_storage_type} : "Account";
123 if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/(.*)\/storageAccounts\/(.*)\/(.*)\/default$/ ||
124 $resource =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/(.*)\/storageAccounts\/(.*)$/) {
125 $resource_group = $1;
126 $namespace = $2;
127 $resource_name = $3;
128 $storagetype_full = '/' . $4 . '/default' if(defined($4));
129 $storagetype_name = $4 if(defined($4));
130 }
131 $storagetype_name =~ s/Services//g;
132
133 ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics(
134 resource => $resource_name . $storagetype_full,
135 resource_group => $resource_group,
136 resource_type => $self->{az_resource_type},
137 resource_namespace => $namespace,
138 metrics => $self->{az_metrics},
139 aggregations => $self->{az_aggregations},
140 timeframe => $self->{az_timeframe},
141 interval => $self->{az_interval},
142 );
143
144 foreach my $metric (@{$self->{az_metrics}}) {
145 my $metric_name = lc($metric);
146 $metric_name =~ s/ /_/g;
147 foreach my $aggregation (@{$self->{az_aggregations}}) {
148 next if (!defined($metric_results{$resource_name}->{$metric_name}->{lc($aggregation)}) && !defined($self->{option_results}->{zeroed}));
149
150 $self->{metric}->{$resource_name . "_" . lc($aggregation)}->{display} = $resource_name;
151 $self->{metric}->{$resource_name . "_" . lc($aggregation)}->{timeframe} = $self->{az_timeframe};
152 $self->{metric}->{$resource_name . "_" . lc($aggregation)}->{stat} = lc($aggregation);
153 $self->{metric}->{$resource_name . "_" . lc($aggregation)}->{storagetype} = ucfirst($storagetype_name);
154 $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;
155 }
156 }
157 }
158
159 if (scalar(keys %{$self->{metric}}) <= 0) {
160 $self->{output}->add_option_msg(short_msg => 'No metrics. Check your options or use --zeroed option to set 0 on undefined values');
161 $self->{output}->option_exit();
162 }
163 }
164
165 1;
166
167 __END__
168
169 =head1 MODE
170
171 Check storage account resources transaction latency metrics.
172
173 Example:
174
175 Using resource name:
176
177 perl centreon_plugins.pl --plugin=cloud::azure::storage::storageaccount::plugin --custommode=azcli --mode=transactions-latency
178 --resource=MYFILER --resource-group=MYHOSTGROUP --resource-namespace=Blob --aggregation='average'
179 --critical-successserverlatency-average='10' --verbose
180
181 Using resource ID:
182
183 perl centreon_plugins.pl --plugin=cloud::azure::storage::storageaccount::plugin --custommode=azcli --mode=transactions-latency
184 --resource='/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Storage/storageAccounts/xxx'
185 --aggregation='average' --critical-successserverlatency-average='10' --verbose
186
187 Default aggregation: 'average' / All aggregations are valid.
188
189 =over 8
190
191 =item B<--resource>
192
193 Set resource name or ID (required).
194
195 =item B<--resource-group>
196
197 Set resource group (required if resource's name is used).
198
199 =item B<--resource-namespace>
200
201 Specify resource namespace. Can be: 'Microsoft.Storage' or 'Microsoft.ClassicStorage'.
202 Default: 'Microsoft.Storage'.
203
204 =item B<--storage-type>
205
206 Set storage type (can be: 'Account', 'Blob', 'File', 'Table', 'Queue').
207
208 =item B<--filter-metric>
209
210 Filter metrics (can be: 'SuccessServerLatency', 'SuccessE2ELatency') (Can be a regexp).
211
212 =item B<--warning-$metric$-$aggregation$>
213
214 Warning thresholds ($metric$ can be: 'successserverlatency', 'successe2elatency',
215 $aggregation$ can be: 'minimum', 'maximum', 'average', 'total').
216
217 =item B<--critical-$metric$-$aggregation$>
218
219 Critical thresholds ($metric$ can be: 'successserverlatency', 'successe2elatency',
220 $aggregation$ can be: 'minimum', 'maximum', 'average', 'total').
221
222 =back
223
224 =cut