Debian Codegraph @

src/cloud/aws/elb/classic/mode/targetshealth.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::targetshealth;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27
28 my %metrics_mapping = (
29 'HealthyHostCount' => {
30 'output' => 'Healthy Hosts',
31 'label' => 'healthyhostcount',
32 'nlabel' => 'elb.healthyhostcount.count',
33 },
34 'UnHealthyHostCount' => {
35 'output' => 'Unhealthy Hosts',
36 'label' => 'unhealthyhostcount',
37 'nlabel' => 'elb.unhealthyhostcount.count',
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
123 if (!defined($self->{option_results}->{type}) || $self->{option_results}->{type} eq '') {
124 $self->{output}->add_option_msg(short_msg => "Need to specify --type option.");
125 $self->{output}->option_exit();
126 }
127
128 if ($self->{option_results}->{type} ne 'loadbalancer' && $self->{option_results}->{type} ne 'availabilityzone') {
129 $self->{output}->add_option_msg(short_msg => "Instance type '" . $self->{option_results}->{type} . "' is not handled for this mode");
130 $self->{output}->option_exit();
131 }
132
133 if ($self->{option_results}->{type} eq 'availabilityzone' && defined($self->{option_results}->{availability_zone})) {
134 $self->{output}->add_option_msg(short_msg => "Can't specify --availability-zone option with availabilityzone instance's type");
135 $self->{output}->option_exit();
136 }
137
138 if (!defined($self->{option_results}->{name}) || $self->{option_results}->{name} eq '') {
139 $self->{output}->add_option_msg(short_msg => "Need to specify --name option.");
140 $self->{output}->option_exit();
141 }
142
143 foreach my $instance (@{$self->{option_results}->{name}}) {
144 if ($instance ne '') {
145 push @{$self->{aws_instance}}, $instance;
146 }
147 }
148
149 $self->{aws_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 600;
150 $self->{aws_period} = defined($self->{option_results}->{period}) ? $self->{option_results}->{period} : 60;
151
152 $self->{aws_statistics} = ['Average'];
153 if (defined($self->{option_results}->{statistic})) {
154 $self->{aws_statistics} = [];
155 foreach my $stat (@{$self->{option_results}->{statistic}}) {
156 if ($stat ne '') {
157 push @{$self->{aws_statistics}}, ucfirst(lc($stat));
158 }
159 }
160 }
161
162 foreach my $metric (keys %metrics_mapping) {
163 next if (defined($self->{option_results}->{filter_metric}) && $self->{option_results}->{filter_metric} ne ''
164 && $metric !~ /$self->{option_results}->{filter_metric}/);
165
166 push @{$self->{aws_metrics}}, $metric;
167 }
168 }
169
170 sub manage_selection {
171 my ($self, %options) = @_;
172
173 my %metric_results;
174 foreach my $instance (@{$self->{aws_instance}}) {
175 push @{$self->{aws_dimensions}}, { Name => $map_type{$self->{option_results}->{type}}, Value => $instance };
176 if (defined($self->{option_results}->{availability_zone}) && $self->{option_results}->{availability_zone} ne '') {
177 push @{$self->{aws_dimensions}}, { Name => 'AvailabilityZone', Value => $self->{option_results}->{availability_zone} };
178 }
179 $metric_results{$instance} = $options{custom}->cloudwatch_get_metrics(
180 namespace => 'AWS/ELB',
181 dimensions => $self->{aws_dimensions},
182 metrics => $self->{aws_metrics},
183 statistics => $self->{aws_statistics},
184 timeframe => $self->{aws_timeframe},
185 period => $self->{aws_period},
186 );
187
188 foreach my $metric (@{$self->{aws_metrics}}) {
189 foreach my $statistic (@{$self->{aws_statistics}}) {
190 next if (!defined($metric_results{$instance}->{$metric}->{lc($statistic)}) && !defined($self->{option_results}->{zeroed}));
191
192 $self->{metrics}->{$instance}->{display} = $instance;
193 $self->{metrics}->{$instance}->{availability_zone} = $self->{option_results}->{availability_zone};
194 $self->{metrics}->{$instance}->{statistics}->{lc($statistic)}->{display} = $statistic;
195 $self->{metrics}->{$instance}->{statistics}->{lc($statistic)}->{$metric} = defined($metric_results{$instance}->{$metric}->{lc($statistic)}) ? $metric_results{$instance}->{$metric}->{lc($statistic)} : 0;
196 }
197 }
198 }
199
200 if (scalar(keys %{$self->{metrics}}) <= 0) {
201 $self->{output}->add_option_msg(short_msg => 'No metrics. Check your options or use --zeroed option to set 0 on undefined values');
202 $self->{output}->option_exit();
203 }
204 }
205
206 1;
207
208 __END__
209
210 =head1 MODE
211
212 Check Classic ELB instances health.
213
214 Example:
215 perl centreon_plugins.pl --plugin=cloud::aws::elb::classic::plugin --custommode=paws --mode=instancehealth --region='eu-west-1'
216 --type='loadbalancer' --name='elb-www-fr' --critical-healthyhostcount='10' --verbose
217
218 See 'https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-cloudwatch-metrics.html' for more informations.
219
220 Default statistic: 'average' / Most useful statistics: 'average', 'minimum', 'maximum'.
221
222 =over 8
223
224 =item B<--type>
225
226 Set the instance type (required) (can be: 'loadbalancer', 'availabilityzone').
227
228 =item B<--name>
229
230 Set the instance name (required) (can be defined multiple times).
231
232 =item B<--availability-zone>
233
234 Add Availability Zone dimension (only with --type='loadbalancer').
235
236 =item B<--filter-metric>
237
238 Filter metrics (can be: 'HealthyHostCount', 'UnHealthyHostCount')
239 (can be a regexp).
240
241 =item B<--warning-*> B<--critical-*>
242
243 Warning thresholds (can be: 'healthyhostcount', 'unhealthyhostcount').
244
245 =back
246
247 =cut