Debian Codegraph @

src/cloud/aws/elb/classic/mode/performances.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::aws::elb::classic::mode::performances;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27
28 my %metrics_mapping = (
29 'RequestCount' => { # Minimum, Maximum, and Average all return 1.
30 'output' => 'Requests',
31 'label' => 'requestcount',
32 'nlabel' => 'elb.requestcount.count',
33 },
34 'Latency' => { # Minimum is typically not useful
35 'output' => 'Latency',
36 'label' => 'latency',
37 'nlabel' => 'elb.latency.seconds',
38 },
39 );
40
41 my %map_type = (
42 "loadbalancer" => "LoadBalancerName",
43 "availabilityzone" => "AvailabilityZone",
44 );
45
46 sub prefix_metric_output {
47 my ($self, %options) = @_;
48
49 my $availability_zone = "";
50 if (defined($options{instance_value}->{availability_zone}) && $options{instance_value}->{availability_zone} ne '') {
51 $availability_zone = "[$options{instance_value}->{availability_zone}] ";
52 }
53
54 return ucfirst($self->{option_results}->{type}) . " '" . $options{instance_value}->{display} . "' " . $availability_zone;
55 }
56
57 sub prefix_statistics_output {
58 my ($self, %options) = @_;
59
60 return "Statistic '" . $options{instance_value}->{display} . "' Metrics ";
61 }
62
63 sub long_output {
64 my ($self, %options) = @_;
65
66 my $availability_zone = "";
67 if (defined($options{instance_value}->{availability_zone}) && $options{instance_value}->{availability_zone} ne '') {
68 $availability_zone = "[$options{instance_value}->{availability_zone}] ";
69 }
70
71 return "Checking " . ucfirst($self->{option_results}->{type}) . " '" . $options{instance_value}->{display} . "' " . $availability_zone;
72 }
73
74 sub set_counters {
75 my ($self, %options) = @_;
76
77 $self->{maps_counters_type} = [
78 { name => 'metrics', type => 3, cb_prefix_output => 'prefix_metric_output', cb_long_output => 'long_output',
79 message_multiple => 'All elb metrics are ok', indent_long_output => ' ',
80 group => [
81 { name => 'statistics', display_long => 1, cb_prefix_output => 'prefix_statistics_output',
82 message_multiple => 'All metrics are ok', type => 1, skipped_code => { -10 => 1 } },
83 ]
84 }
85 ];
86
87 foreach my $metric (keys %metrics_mapping) {
88 my $entry = {
89 label => $metrics_mapping{$metric}->{label},
90 nlabel => $metrics_mapping{$metric}->{nlabel},
91 set => {
92 key_values => [ { name => $metric }, { name => 'display' } ],
93 output_template => $metrics_mapping{$metric}->{output} . ': %.2f',
94 perfdatas => [
95 { value => $metric , template => '%.2f', label_extra_instance => 1 }
96 ],
97 }
98 };
99 push @{$self->{maps_counters}->{statistics}}, $entry;
100 }
101 }
102
103 sub new {
104 my ($class, %options) = @_;
105 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
106 bless $self, $class;
107
108 $options{options}->add_options(arguments => {
109 "type:s" => { name => 'type' },
110 "name:s@" => { name => 'name' },
111 "availability-zone:s" => { name => 'availability_zone' },
112 "filter-metric:s" => { name => 'filter_metric' },
113 "statistic:s@" => { name => 'statistic' },
114 });
115
116 return $self;
117 }
118
119 sub check_options {
120 my ($self, %options) = @_;
121 $self->SUPER::check_options(%options);
122 if (!defined($self->{option_results}->{type}) || $self->{option_results}->{type} eq '') {
123 $self->{output}->add_option_msg(short_msg => "Need to specify --type option.");
124 $self->{output}->option_exit();
125 }
126
127 if ($self->{option_results}->{type} ne 'loadbalancer' && $self->{option_results}->{type} ne 'availabilityzone') {
128 $self->{output}->add_option_msg(short_msg => "Instance type '" . $self->{option_results}->{type} . "' is not handled for this mode");
129 $self->{output}->option_exit();
130 }
131
132 if ($self->{option_results}->{type} eq 'availabilityzone' && defined($self->{option_results}->{availability_zone})) {
133 $self->{output}->add_option_msg(short_msg => "Can't specify --availability-zone option with availabilityzone instance's type");
134 $self->{output}->option_exit();
135 }
136
137 if (!defined($self->{option_results}->{name}) || $self->{option_results}->{name} eq '') {
138 $self->{output}->add_option_msg(short_msg => "Need to specify --name option.");
139 $self->{output}->option_exit();
140 }
141
142 foreach my $instance (@{$self->{option_results}->{name}}) {
143 if ($instance ne '') {
144 push @{$self->{aws_instance}}, $instance;
145 }
146 }
147
148 $self->{aws_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 600;
149 $self->{aws_period} = defined($self->{option_results}->{period}) ? $self->{option_results}->{period} : 60;
150
151 $self->{aws_statistics} = ['Sum', 'Average'];
152 if (defined($self->{option_results}->{statistic})) {
153 $self->{aws_statistics} = [];
154 foreach my $stat (@{$self->{option_results}->{statistic}}) {
155 if ($stat ne '') {
156 push @{$self->{aws_statistics}}, ucfirst(lc($stat));
157 }
158 }
159 }
160
161 foreach my $metric (keys %metrics_mapping) {
162 next if (defined($self->{option_results}->{filter_metric}) && $self->{option_results}->{filter_metric} ne ''
163 && $metric !~ /$self->{option_results}->{filter_metric}/);
164
165 push @{$self->{aws_metrics}}, $metric;
166 }
167 }
168
169 sub manage_selection {
170 my ($self, %options) = @_;
171
172 my %metric_results;
173 foreach my $instance (@{$self->{aws_instance}}) {
174 push @{$self->{aws_dimensions}}, { Name => $map_type{$self->{option_results}->{type}}, Value => $instance };
175 if (defined($self->{option_results}->{availability_zone}) && $self->{option_results}->{availability_zone} ne '') {
176 push @{$self->{aws_dimensions}}, { Name => 'AvailabilityZone', Value => $self->{option_results}->{availability_zone} };
177 }
178 $metric_results{$instance} = $options{custom}->cloudwatch_get_metrics(
179 namespace => 'AWS/ELB',
180 dimensions => $self->{aws_dimensions},
181 metrics => $self->{aws_metrics},
182 statistics => $self->{aws_statistics},
183 timeframe => $self->{aws_timeframe},
184 period => $self->{aws_period},
185 );
186
187 foreach my $metric (@{$self->{aws_metrics}}) {
188 foreach my $statistic (@{$self->{aws_statistics}}) {
189 next if (!defined($metric_results{$instance}->{$metric}->{lc($statistic)}) && !defined($self->{option_results}->{zeroed}));
190
191 $self->{metrics}->{$instance}->{display} = $instance;
192 $self->{metrics}->{$instance}->{availability_zone} = $self->{option_results}->{availability_zone};
193 $self->{metrics}->{$instance}->{statistics}->{lc($statistic)}->{display} = $statistic;
194 $self->{metrics}->{$instance}->{statistics}->{lc($statistic)}->{$metric} = defined($metric_results{$instance}->{$metric}->{lc($statistic)}) ? $metric_results{$instance}->{$metric}->{lc($statistic)} : 0;
195 }
196 }
197 }
198
199 if (scalar(keys %{$self->{metrics}}) <= 0) {
200 $self->{output}->add_option_msg(short_msg => 'No metrics. Check your options or use --zeroed option to set 0 on undefined values');
201 $self->{output}->option_exit();
202 }
203 }
204
205 1;
206
207 __END__
208
209 =head1 MODE
210
211 Check Classic ELB performances.
212
213 Example:
214 perl centreon_plugins.pl --plugin=cloud::aws::elb::classic::plugin --custommode=paws --mode=performances
215 --region='eu-west-1' --type='loadbalancer' --name='elb-www-fr' --critical-requestcount='10' --verbose
216
217 See 'https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-cloudwatch-metrics.html' for more informations.
218
219 Default statistic: 'sum', 'average' / Most useful statistics: RequestCount: 'sum', Latency: 'average'.
220
221 =over 8
222
223 =item B<--type>
224
225 Set the instance type (required) (can be: 'loadbalancer', 'availabilityzone').
226
227 =item B<--name>
228
229 Set the instance name (required) (can be defined multiple times).
230
231 =item B<--availability-zone>
232
233 Add Availability Zone dimension (only with --type='loadbalancer').
234
235 =item B<--filter-metric>
236
237 Filter metrics (can be: 'RequestCount', 'Latency')
238 (can be a regexp).
239
240 =item B<--warning-*> B<--critical-*>
241
242 Warning thresholds (can be: 'requestcount', 'latency').
243
244 =back
245
246 =cut