Debian Codegraph @

src/cloud/aws/elb/classic/mode/discovery.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::discovery;
22
23 use base qw(centreon::plugins::mode);
24
25 use strict;
26 use warnings;
27 use JSON::XS;
28
29 sub new {
30 my ($class, %options) = @_;
31 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
32 bless $self, $class;
33
34 $options{options}->add_options(arguments => {
35 "prettify" => { name => 'prettify' },
36 });
37
38 return $self;
39 }
40
41 sub check_options {
42 my ($self, %options) = @_;
43 $self->SUPER::init(%options);
44 }
45
46 sub run {
47 my ($self, %options) = @_;
48
49 my @disco_data;
50 my $disco_stats;
51
52 $disco_stats->{start_time} = time();
53
54 my $load_balancers = $options{custom}->discovery(
55 service => 'elb',
56 command => 'describe-load-balancers'
57 );
58
59 foreach my $load_balancer (@{$load_balancers->{LoadBalancerDescriptions}}) {
60 next if (!defined($load_balancer->{LoadBalancerName}));
61 my %elb;
62 $elb{type} = "classic";
63 $elb{name} = $load_balancer->{LoadBalancerName};
64 $elb{dns_name} = $load_balancer->{DNSName};
65 $elb{availability_zones} = $load_balancer->{AvailabilityZones};
66 $elb{vpc_id} = $load_balancer->{VPCId};
67 $elb{instances} = $load_balancer->{Instances};
68 $elb{security_groups} = $load_balancer->{SecurityGroups};
69 $elb{subnets} = $load_balancer->{Subnets};
70 push @disco_data, \%elb;
71 }
72
73 $disco_stats->{end_time} = time();
74 $disco_stats->{duration} = $disco_stats->{end_time} - $disco_stats->{start_time};
75 $disco_stats->{discovered_items} = @disco_data;
76 $disco_stats->{results} = \@disco_data;
77
78 my $encoded_data;
79 eval {
80 if (defined($self->{option_results}->{prettify})) {
81 $encoded_data = JSON::XS->new->utf8->pretty->encode($disco_stats);
82 } else {
83 $encoded_data = JSON::XS->new->utf8->encode($disco_stats);
84 }
85 };
86 if ($@) {
87 $encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}';
88 }
89
90 return @disco_data if (defined($options{discover}));
91
92 $self->{output}->output_add(short_msg => $encoded_data);
93 $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1);
94 $self->{output}->exit();
95 }
96
97 1;
98
99 __END__
100
101 =head1 MODE
102
103 Classic ELB discovery.
104
105 =over 8
106
107 =item B<--prettify>
108
109 Prettify JSON output.
110
111 =back
112
113 =cut