Debian Codegraph @

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